Merging master to master_vca_intg
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / model / DescriptorModelMetaProperty.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 /**
19 * Created by onvelocity on 1/27/16.
20 *
21 * This class provides utility methods for interrogating an instance of model uiState object.
22 */
23
24 'use strict';
25
26 import _ from 'lodash'
27 import guid from './../guid'
28 import changeCase from 'change-case'
29 import InstanceCounter from './../InstanceCounter'
30 import DescriptorModelFields from './DescriptorModelFields'
31 import DescriptorTemplateFactory from './DescriptorTemplateFactory'
32 import utils from '../utils'
33
34 export default {
35 isBoolean(property = {}) {
36 return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'boolean')
37 },
38 isLeaf(property = {}) {
39 return /leaf|choice/.test(property.type);
40 },
41 isList(property = {}) {
42 return /list|leaf_list/.test(property.type);
43 },
44 isLeafList(property = {}) {
45 return property.type === 'leaf_list';
46 },
47 isLeafRef(property = {}) {
48 const type = property['data-type'] || {};
49 return type.hasOwnProperty('leafref');
50 },
51 isArray(property = {}) {
52 // give '1' or '0..N' or '0..1' or '0..5' determine if represents an array
53 // '0..1' is not an array
54 // '0..2' is an array
55 // '0..N' is an array
56 const cardinality = String(property.cardinality).toUpperCase();
57 const pos = cardinality.lastIndexOf('.') + 1;
58 const val = cardinality.substr(pos);
59 return val === 'N' || parseInt(val, 10) > 1;
60 },
61 isEnumeration(property = {}) {
62 const type = property['data-type'] || {};
63 return type.hasOwnProperty('enumeration');
64 },
65 isRequired(property = {}) {
66 return /^1/.test(property.cardinality);
67 },
68 isObject(property = {}) {
69 return !/^(leaf|leaf_list)$/.test(property.type);
70 },
71 isSimpleList(property = {}) {
72 return _.includes(DescriptorModelFields.simpleList, property.name);
73 },
74 isPrimativeDataType(property = {}) {
75 const Property = this;
76 return /string|int/.test(property['data-type']) || Property.isEnumeration(property) || Property.isGuid(property);
77 },
78 defaultValue(property = {}) {
79 if (property.defaultValue) {
80 return property.defaultValue;
81 }
82 if (this.isObject(property)) {
83 return {};
84 }
85 return '';
86 },
87 getContainerMethod(property, container, methodName) {
88 const name = changeCase.camel(methodName + '-' + property.name);
89 if (typeof container[name] === 'function') {
90 return container[name].bind(container);
91 }
92 },
93 getContainerCreateMethod(property, container) {
94 const name = changeCase.camel('create-' + property.name);
95 if (typeof container[name] === 'function') {
96 return container[name].bind(container);
97 }
98 },
99 containerHasCreateMethod(container, property = {}) {
100 const find = changeCase.camel('create-' + property.name);
101 return typeof container[find] === 'function';
102 },
103 getEnumeration(property = {}, value) {
104 const enumeration = property['data-type'].enumeration.enum;
105 if (typeof enumeration === 'string') {
106 return [{name: enumeration, value: enumeration, isSelected: String(value) === enumeration}];
107 }
108 return Object.keys(enumeration).map(enumName => {
109 let enumValue = enumName;
110 // warn we only support named enums and systematically ignore enum values
111 //const enumObj = enumeration[enumName];
112 //if (enumObj) {
113 // enumValue = enumObj.value || enumName;
114 //}
115 return {name: enumName, value: enumValue, isSelected: String(enumValue) === String(value)};
116 });
117 },
118 getLeafRef(property = {}, path, value, fullFieldKey, transientCatalogs, container) {
119 const leafRefPath = property['data-type']['leafref']['path'];
120
121 const transientCatalogHash = {};
122
123 transientCatalogs.map((catalog) => {
124 transientCatalogHash[catalog.type + '-catalog'] = {};
125 transientCatalogHash[catalog.type + '-catalog'][catalog.type] = catalog['descriptors'];
126 });
127
128 let leafRefPathValues = utils.resolveLeafRefPath(transientCatalogHash, leafRefPath, fullFieldKey, path, container);
129
130 let leafRefObjects = [];
131
132 leafRefPathValues && leafRefPathValues.map((leafRefPathValue) => {
133 leafRefObjects.push({
134 name: leafRefPathValue,
135 value: leafRefPathValue,
136 isSelected: String(leafRefPathValue) === String(value)
137 });
138 });
139
140 return leafRefObjects;
141 },
142
143 getConfigParamRef(property = {}, path, value, fullFieldKey, transientCatalogs, container, vnfdId) {
144 // const leafRefPath = property['data-type']['leafref']['path'];
145 const leafRefPath = "/vnfd:vnfd-catalog/vnfd:vnfd[vnfd:id = " + vnfdId + "]/vnfd:config-parameter/vnfd:config-parameter-source/vnfd:name"
146 const transientCatalogHash = {};
147
148 transientCatalogs.map((catalog) => {
149 transientCatalogHash[catalog.type + '-catalog'] = {};
150 transientCatalogHash[catalog.type + '-catalog'][catalog.type] = catalog['descriptors'];
151 });
152
153 let leafRefPathValues = utils.resolveLeafRefPath(transientCatalogHash, leafRefPath, fullFieldKey, path, container);
154
155 let leafRefObjects = [];
156
157 leafRefPathValues && leafRefPathValues.map((leafRefPathValue) => {
158 leafRefObjects.push({
159 name: leafRefPathValue,
160 value: leafRefPathValue,
161 isSelected: String(leafRefPathValue) === String(value)
162 });
163 });
164
165 return leafRefObjects;
166 },
167 isGuid(property = {}) {
168 const type = property['data-type'];
169 if (typeof type === 'object' && type.leafref && type.leafref.path) {
170 return /\bid$/.test(type.leafref.path);
171 }
172 return /uuid/.test(property['data-type']);
173 },
174 createModelInstance(property) {
175 const Property = this;
176 const defaultValue = Property.defaultValue.bind(this);
177 function createModel(uiState, parentMeta) {
178 const model = {};
179 if (Property.isLeaf(uiState)) {
180 if (uiState.name === 'name') {
181 return changeCase.param(parentMeta.name) + '-' + InstanceCounter.count(parentMeta[':qualified-type']);
182 }
183 if (_.isArray(parentMeta.key) && _.includes(parentMeta.key, uiState.name)) {
184 if (/uuid/.test(uiState['data-type'])) {
185 return guid();
186 }
187 if (uiState['data-type'] === 'string') {
188 const prefix = uiState.name.replace('id', '');
189 return (prefix ? changeCase.param(prefix) + '-' : '') + guid(5);
190 }
191 if (/int/.test(uiState['data-type'])) {
192 return InstanceCounter.count(uiState[':qualified-type']);
193 }
194 }
195 return defaultValue(uiState);
196 } else if (Property.isList(uiState)) {
197 return [];
198 } else {
199 uiState.properties.forEach(p => {
200 model[p.name] = createModel(p, uiState);
201 });
202 }
203 return model;
204 }
205 if (property) {
206 if (Property.isPrimativeDataType(property)) {
207 return defaultValue(property);
208 }
209 if (property.type === 'leaf') {
210 return defaultValue(property);
211 }
212 if (/list/.test(property.type)) {
213 property.type = 'container';
214 }
215 const modelInstance = createModel(property, property);
216 modelInstance.uiState = {type: property.name};
217 const modelFragment = DescriptorTemplateFactory.createModelForType(property[':qualified-type'] || property.name) || {};
218 Object.assign(modelInstance, modelFragment);
219 return modelInstance;
220 }
221 }
222 }