RIFT-15154: Config parameter map
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / model / DescriptorModelSerializer.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 /**
20 * Created by onvelocity on 10/20/15.
21 */
22
23 import _ from 'lodash'
24 import utils from './../utils'
25 import DescriptorModelFields from './DescriptorModelFields'
26 import DescriptorModelMetaFactory from './DescriptorModelMetaFactory'
27
28 let nsdFields = null;
29 let vldFields = null;
30 let vnfdFields = null;
31 let cvnfdFields = null;
32 let configParameterMapFields = null;
33
34
35
36
37 /**
38 * Serialize DescriptorModel JSON into CONFD JSON. Also, cleans up the data as needed.
39 *
40 * @type {{serialize: (function(*=)), ':clean': (function(*=)), nsd: {serialize: (function(*=))}, vld: {serialize: (function(*=))}, vnfd-connection-point-ref: {serialize: (function(*=))}, constituent-vnfd: {serialize: (function(*=))}, vnfd: {serialize: (function(*=))}, vdu: {serialize: (function(*=))}}}
41 */
42 const DescriptorModelSerializer = {
43 serialize(model) {
44 const type = model.uiState && model.uiState.type;
45 const serializer = this[type];
46 if (serializer) {
47 model = serializer.serialize(model);
48 this[':clean'](model);
49 return model;
50 }
51 return false;
52 },
53 ':clean'(model) {
54 // remove uiState from all elements accept nsd and vnfd
55 // remove empty / blank value fields
56 function clean(m) {
57 Object.keys(m).forEach(k => {
58 const isEmptyObject = typeof m[k] === 'object' && _.isEmpty(m[k]);
59 if (typeof m[k] === 'undefined' || isEmptyObject || m[k] === '') {
60 delete m[k];
61 }
62 const isMetaAllowed = /^nsd|vnfd$/.test(m.uiState && m.uiState.type);
63 if (k === 'uiState') {
64 if (isMetaAllowed) {
65 // remove any transient ui state properties
66 const uiState = _.pick(m.uiState, DescriptorModelFields.meta);
67 if (!_.isEmpty(uiState)) {
68 // uiState field must be a string
69 m['meta'] = JSON.stringify(uiState);
70 }
71 }
72 delete m[k];
73 }
74 if (typeof m[k] === 'object') {
75 clean(m[k]);
76 }
77 });
78 }
79 clean(model);
80 return model;
81 },
82 nsd: {
83 serialize(nsdModel) {
84 if(!nsdFields) nsdFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd').concat('uiState');
85 const confd = _.pick(nsdModel, nsdFields);
86
87 // vnfd is defined in the ETSI etsi_gs reference manual but RIFT does not use it
88 delete confd.vnfd;
89
90 // map the vnfd instances into the CONFD constituent-vnfd ref instances
91 confd['constituent-vnfd'] = confd['constituent-vnfd'].map((d, index) => {
92
93 const constituentVNFD = {
94 'member-vnf-index': d['member-vnf-index'],
95 'vnfd-id-ref': d['vnfd-id-ref']
96 };
97
98 if (d['vnf-configuration']) {
99 const vnfConfig = _.cloneDeep(d['vnf-configuration']);
100 const configType = vnfConfig['config-type'] || 'none';
101 // make sure we send the correct values based on config type
102 if (configType === 'none') {
103 constituentVNFD['vnf-configuration'] = {'config-type': 'none'};
104 const configPriority = utils.resolvePath(vnfConfig, 'input-params.config-priority');
105 const configPriorityValue = _.isNumber(configPriority) ? configPriority : d.uiState['member-vnf-index'];
106 utils.assignPathValue(constituentVNFD['vnf-configuration'], 'input-params.config-priority', configPriorityValue);
107 } else {
108 // remove any unused configuration options
109 ['netconf', 'rest', 'script', 'juju'].forEach(type => {
110 if (configType !== type) {
111 delete vnfConfig[type];
112 }
113 });
114 constituentVNFD['vnf-configuration'] = vnfConfig;
115 }
116 }
117
118 if (d.hasOwnProperty('start-by-default')) {
119 constituentVNFD['start-by-default'] = d['start-by-default'];
120 }
121
122 return constituentVNFD;
123
124 });
125 for (var key in confd) {
126 checkForChoiceAndRemove(key, confd, nsdModel);
127 }
128 // serialize the VLD instances
129 confd.vld = confd.vld.map(d => {
130 return DescriptorModelSerializer.serialize(d);
131 });
132
133 return cleanEmptyTopKeys(confd);
134
135 }
136 },
137 vld: {
138 serialize(vldModel) {
139 if(!vldFields) vldFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd.vld');
140 const confd = _.pick(vldModel, vldFields);
141 const property = 'vnfd-connection-point-ref';
142
143 // TODO: There is a bug in RIFT-REST that is not accepting empty
144 // strings for string properties.
145 // once that is fixed, remove this piece of code.
146 // fix-start
147 for (var key in confd) {
148 if (confd.hasOwnProperty(key) && confd[key] === '') {
149 delete confd[key];
150 } else {
151 //removes choice properties from top level object and copies immediate children onto it.
152 checkForChoiceAndRemove(key, confd, vldModel);
153 }
154 }
155
156
157 const deepProperty = 'provider-network';
158 for (var key in confd[deepProperty]) {
159 if (confd[deepProperty].hasOwnProperty(key) && confd[deepProperty][key] === '') {
160 delete confd[deepProperty][key];
161 }
162 }
163 // fix-end
164 confd[property] = confd[property].map(d => DescriptorModelSerializer[property].serialize(d));
165 return cleanEmptyTopKeys(confd);
166 }
167 },
168 'vnfd-connection-point-ref': {
169 serialize(ref) {
170 return _.pick(ref, ['member-vnf-index-ref', 'vnfd-id-ref', 'vnfd-connection-point-ref']);
171 }
172 },
173 'internal-connection-point': {
174 serialize(ref) {
175 return _.pick(ref, ['id-ref']);
176 }
177 },
178 'constituent-vnfd': {
179 serialize(cvnfdModel) {
180 if(!cvnfdFields) cvnfdFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd.constituent-vnfd');
181 return _.pick(cvnfdModel, cvnfdFields);
182 }
183 },
184 vnfd: {
185 serialize(vnfdModel) {
186 if(!vnfdFields) vnfdFields = DescriptorModelMetaFactory.getModelFieldNamesForType('vnfd').concat('uiState');
187 const confd = _.pick(vnfdModel, vnfdFields);
188 confd.vdu = confd.vdu.map(d => DescriptorModelSerializer.serialize(d));
189 return cleanEmptyTopKeys(confd);
190 }
191 },
192 vdu: {
193 serialize(vduModel) {
194 const copy = _.cloneDeep(vduModel);
195 for (let k in copy) {
196 checkForChoiceAndRemove(k, copy, vduModel)
197 }
198 const confd = _.omit(copy, ['uiState']);
199 return cleanEmptyTopKeys(confd);
200 }
201 },
202 'config-parameter-map': {
203 serialize(configParameterMap) {
204 //vnfapMapFields
205 if(!configParameterMapFields) configParameterMapFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd.config-parameter-map');
206 return _.pick(configParameterMap, configParameterMapFields);
207 }
208 }
209 };
210
211
212 function checkForChoiceAndRemove(k, confd, model) {
213 let state = model.uiState;
214 if (state.choice) {
215 let choice = state.choice[k]
216 if(choice) {
217 if (choice.constructor.name == "Array") {
218 for(let i = 0; i < choice.length; i++) {
219 for (let key in confd[k][i]) {
220 if(choice[i] && (choice[i].selected.indexOf(key) > -1)) {
221 confd[k][i][key] = confd[k][i][key]
222 }
223 confd[key];
224 };
225 }
226 } else {
227 for (let key in confd[k]) {
228 if(choice && (choice.selected.indexOf(key) > -1)) {
229 confd[key] = confd[k][key]
230 }
231 };
232 delete confd[k];
233 }
234
235 }
236 }
237 return confd;
238 }
239
240 function cleanEmptyTopKeys(m){
241 Object.keys(m).forEach(k => {
242 const isEmptyObject = typeof m[k] === 'object' && _.isEmpty(m[k]);
243 if (typeof m[k] === 'undefined' || isEmptyObject || m[k] === '') {
244 delete m[k];
245 }
246 });
247 return m;
248 }
249
250 export default DescriptorModelSerializer;