RIFT-15154: Config parameter map
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / model / DescriptorModelMetaFactory.js
1 /**
2 * Created by onvelocity on 1/27/16.
3 *
4 * This class provides methods to get the metadata about descriptor models.
5 */
6
7 'use strict';
8
9 import _ from 'lodash'
10 import utils from './../utils'
11 import DescriptorModelMetaProperty from './DescriptorModelMetaProperty'
12 import CommonUtils from 'utils/utils';
13 const assign = Object.assign;
14
15 const exportInnerTypesMap = {
16 'constituent-vnfd': 'nsd.constituent-vnfd',
17 'config-parameter-map': 'nsd.config-parameter-map',
18 'vdu': 'vnfd.vdu'
19 };
20
21 function getPathForType(type) {
22 if (exportInnerTypesMap[type]) {
23 return exportInnerTypesMap[type];
24 }
25 return type;
26 }
27
28 let modelMetaByPropertyNameMap = [];
29
30 let cachedDescriptorModelMetaRequest = null;
31
32 export default {
33 init() {
34 if (!cachedDescriptorModelMetaRequest) {
35 cachedDescriptorModelMetaRequest = new Promise(function(resolve, reject) {
36 CommonUtils.getDescriptorModelMeta().then(function(data) {
37 let DescriptorModelMetaJSON = data;
38 modelMetaByPropertyNameMap = Object.keys(DescriptorModelMetaJSON).reduce((map, key) => {
39 function mapProperties(parentMap, parentObj) {
40 parentMap[':meta'] = parentObj;
41 const properties = parentObj && parentObj.properties ? parentObj.properties : [];
42 properties.forEach(p => {
43 parentMap[p.name] = mapProperties({}, assign(p, {':qualified-type': parentObj[':qualified-type'] + '.' + p.name}));
44 return map;
45 }, parentMap);
46 return parentMap;
47 }
48 map[key] = mapProperties({}, assign(DescriptorModelMetaJSON[key], {':qualified-type': key}));
49 return map;
50 }, {});
51
52 (() => {
53 // initialize the UI centric properties that CONFD could care less about
54 utils.assignPathValue(modelMetaByPropertyNameMap, 'nsd.meta.:meta.preserve-line-breaks', true);
55 utils.assignPathValue(modelMetaByPropertyNameMap, 'vnfd.meta.:meta.preserve-line-breaks', true);
56 utils.assignPathValue(modelMetaByPropertyNameMap, 'vnfd.vdu.cloud-init.:meta.preserve-line-breaks', true);
57 utils.assignPathValue(modelMetaByPropertyNameMap, 'nsd.constituent-vnfd.vnf-configuration.config-template.:meta.preserve-line-breaks', true);
58 })();
59 resolve();
60 }, function(error) {
61 cachedDescriptorModelMetaRequest = null;
62 })
63 })
64 }
65
66 return cachedDescriptorModelMetaRequest;
67 },
68 createModelInstanceForType(typeOrPath) {
69 const modelMeta = this.getModelMetaForType(typeOrPath);
70 return DescriptorModelMetaProperty.createModelInstance(modelMeta);
71 },
72 getModelMetaForType(typeOrPath, filterProperties = () => true) {
73 // resolve paths like 'nsd' or 'vnfd.vdu' or 'nsd.constituent-vnfd'
74 const found = utils.resolvePath(modelMetaByPropertyNameMap, getPathForType(typeOrPath));
75 if (found) {
76 const uiState = _.cloneDeep(found[':meta']);
77 uiState.properties = uiState.properties.filter(filterProperties);
78 return uiState;
79 }
80 console.warn('no model uiState found for type', typeOrPath);
81 },
82 getModelFieldNamesForType(typeOrPath) {
83 // resolve paths like 'nsd' or 'vnfd.vdu' or 'nsd.constituent-vnfd'
84 const found = utils.resolvePath(modelMetaByPropertyNameMap, getPathForType(typeOrPath));
85 if (found) {
86 let result = [];
87 found[':meta'].properties.map((p) => {
88 // if(false) {
89 if(p.type == 'choice') {
90 result.push(p.name)
91 return p.properties.map(function(q){
92 result.push(q.properties[0].name);
93 })
94
95 } else {
96 return result.push(p.name);
97 }
98 })
99 return result;
100 }
101 console.warn('no model uiState found for type', typeOrPath);
102 }
103 }