3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Created by onvelocity on 1/27/16.
21 * This class provides utility methods for interrogating an instance of model uiState object.
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'
35 isLeaf(property
= {}) {
36 return /leaf|choice/.test(property
.type
);
38 isList(property
= {}) {
39 return /list|leaf_list/.test(property
.type
);
41 isLeafList(property
= {}) {
42 return property
.type
=== 'leaf_list';
44 isLeafRef(property
= {}) {
45 const type
= property
['data-type'] || {};
46 return type
.hasOwnProperty('leafref');
48 isArray(property
= {}) {
49 // give '1' or '0..N' or '0..1' or '0..5' determine if represents an array
50 // '0..1' is not an array
53 const cardinality
= String(property
.cardinality
).toUpperCase();
54 const pos
= cardinality
.lastIndexOf('.') + 1;
55 const val
= cardinality
.substr(pos
);
56 return val
=== 'N' || parseInt(val
, 10) > 1;
58 isEnumeration(property
= {}) {
59 const type
= property
['data-type'] || {};
60 return type
.hasOwnProperty('enumeration');
62 isRequired(property
= {}) {
63 return /^1/.test(property
.cardinality
);
65 isObject(property
= {}) {
66 return !/^(leaf|leaf_list)$/.test(property
.type
);
68 isSimpleList(property
= {}) {
69 return _
.includes(DescriptorModelFields
.simpleList
, property
.name
);
71 isPrimativeDataType(property
= {}) {
72 const Property
= this;
73 return /string|int/.test(property
['data-type']) || Property
.isEnumeration(property
) || Property
.isGuid(property
);
75 defaultValue(property
= {}) {
76 if (property
.defaultValue
) {
77 return property
.defaultValue
;
79 if (this.isObject(property
)) {
84 getContainerMethod(property
, container
, methodName
) {
85 const name
= changeCase
.camel(methodName
+ '-' + property
.name
);
86 if (typeof container
[name
] === 'function') {
87 return container
[name
].bind(container
);
90 getContainerCreateMethod(property
, container
) {
91 const name
= changeCase
.camel('create-' + property
.name
);
92 if (typeof container
[name
] === 'function') {
93 return container
[name
].bind(container
);
96 containerHasCreateMethod(container
, property
= {}) {
97 const find
= changeCase
.camel('create-' + property
.name
);
98 return typeof container
[find
] === 'function';
100 getEnumeration(property
= {}, value
) {
101 const enumeration
= property
['data-type'].enumeration
.enum;
102 if (typeof enumeration
=== 'string') {
103 return [{name
: enumeration
, value
: enumeration
, isSelected
: String(value
) === enumeration
}];
105 return Object
.keys(enumeration
).map(enumName
=> {
106 let enumValue
= enumName
;
107 // warn we only support named enums and systematically ignore enum values
108 //const enumObj = enumeration[enumName];
110 // enumValue = enumObj.value || enumName;
112 return {name
: enumName
, value
: enumValue
, isSelected
: String(enumValue
) === String(value
)};
115 getLeafRef(property
= {}, path
, value
, fullFieldKey
, transientCatalogs
, container
) {
116 const leafRefPath
= property
['data-type']['leafref']['path'];
118 const transientCatalogHash
= {};
120 transientCatalogs
.map((catalog
) => {
121 transientCatalogHash
[catalog
.type
+ '-catalog'] = {};
122 transientCatalogHash
[catalog
.type
+ '-catalog'][catalog
.type
] = catalog
['descriptors'];
125 let leafRefPathValues
= utils
.resolveLeafRefPath(transientCatalogHash
, leafRefPath
, fullFieldKey
, path
, container
);
127 let leafRefObjects
= [];
129 leafRefPathValues
&& leafRefPathValues
.map((leafRefPathValue
) => {
130 leafRefObjects
.push({
131 name
: leafRefPathValue
,
132 value
: leafRefPathValue
,
133 isSelected
: String(leafRefPathValue
) === String(value
)
137 return leafRefObjects
;
140 getConfigParamRef(property
= {}, path
, value
, fullFieldKey
, transientCatalogs
, container
, vnfdId
) {
141 // const leafRefPath = property['data-type']['leafref']['path'];
142 const leafRefPath
= "/vnfd:vnfd-catalog/vnfd:vnfd[vnfd:id = " + vnfdId
+ "]/vnfd:config-parameter/vnfd:config-parameter-source/vnfd:name"
143 const transientCatalogHash
= {};
145 transientCatalogs
.map((catalog
) => {
146 transientCatalogHash
[catalog
.type
+ '-catalog'] = {};
147 transientCatalogHash
[catalog
.type
+ '-catalog'][catalog
.type
] = catalog
['descriptors'];
150 let leafRefPathValues
= utils
.resolveLeafRefPath(transientCatalogHash
, leafRefPath
, fullFieldKey
, path
, container
);
152 let leafRefObjects
= [];
154 leafRefPathValues
&& leafRefPathValues
.map((leafRefPathValue
) => {
155 leafRefObjects
.push({
156 name
: leafRefPathValue
,
157 value
: leafRefPathValue
,
158 isSelected
: String(leafRefPathValue
) === String(value
)
162 return leafRefObjects
;
164 isGuid(property
= {}) {
165 const type
= property
['data-type'];
166 if (typeof type
=== 'object' && type
.leafref
&& type
.leafref
.path
) {
167 return /\bid$/.test(type
.leafref
.path
);
169 return /uuid/.test(property
['data-type']);
171 createModelInstance(property
) {
172 const Property
= this;
173 const defaultValue
= Property
.defaultValue
.bind(this);
174 function createModel(uiState
, parentMeta
) {
176 if (Property
.isLeaf(uiState
)) {
177 if (uiState
.name
=== 'name') {
178 return changeCase
.param(parentMeta
.name
) + '-' + InstanceCounter
.count(parentMeta
[':qualified-type']);
180 if (_
.isArray(parentMeta
.key
) && _
.includes(parentMeta
.key
, uiState
.name
)) {
181 if (/uuid/.test(uiState
['data-type'])) {
184 if (uiState
['data-type'] === 'string') {
185 const prefix
= uiState
.name
.replace('id', '');
186 return (prefix
? changeCase
.param(prefix
) + '-' : '') + guid(5);
188 if (/int/.test(uiState
['data-type'])) {
189 return InstanceCounter
.count(uiState
[':qualified-type']);
192 return defaultValue(uiState
);
193 } else if (Property
.isList(uiState
)) {
196 uiState
.properties
.forEach(p
=> {
197 model
[p
.name
] = createModel(p
, uiState
);
203 if (Property
.isPrimativeDataType(property
)) {
204 return defaultValue(property
);
206 if (property
.type
=== 'leaf') {
207 return defaultValue(property
);
209 if (/list/.test(property
.type
)) {
210 property
.type
= 'container';
212 const modelInstance
= createModel(property
, property
);
213 modelInstance
.uiState
= {type
: property
.name
};
214 const modelFragment
= DescriptorTemplateFactory
.createModelForType(property
[':qualified-type'] || property
.name
) || {};
215 Object
.assign(modelInstance
, modelFragment
);
216 return modelInstance
;