RIFT-14874: Allow deny of duplicate events in loggin
[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
33
34
35
36 /**
37 * Serialize DescriptorModel JSON into CONFD JSON. Also, cleans up the data as needed.
38 *
39 * @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(*=))}}}
40 */
41 const DescriptorModelSerializer = {
42 serialize(model) {
43 const type = model.uiState && model.uiState.type;
44 const serializer = this[type];
45 if (serializer) {
46 model = serializer.serialize(model);
47 this[':clean'](model);
48 return model;
49 }
50 return false;
51 },
52 ':clean'(model) {
53 // remove uiState from all elements accept nsd and vnfd
54 // remove empty / blank value fields
55 function clean(m) {
56 Object.keys(m).forEach(k => {
57 const isEmptyObject = typeof m[k] === 'object' && _.isEmpty(m[k]);
58 if (typeof m[k] === 'undefined' || isEmptyObject || m[k] === '') {
59 delete m[k];
60 }
61 const isMetaAllowed = /^nsd|vnfd$/.test(m.uiState && m.uiState.type);
62 if (k === 'uiState') {
63 if (isMetaAllowed) {
64 // remove any transient ui state properties
65 const uiState = _.pick(m.uiState, DescriptorModelFields.meta);
66 if (!_.isEmpty(uiState)) {
67 // uiState field must be a string
68 m['meta'] = JSON.stringify(uiState);
69 }
70 }
71 delete m[k];
72 }
73 if (typeof m[k] === 'object') {
74 clean(m[k]);
75 }
76 });
77 }
78 clean(model);
79 return model;
80 },
81 nsd: {
82 serialize(nsdModel) {
83 if(!nsdFields) nsdFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd').concat('uiState');
84 const confd = _.pick(nsdModel, nsdFields);
85
86 // vnfd is defined in the ETSI etsi_gs reference manual but RIFT does not use it
87 delete confd.vnfd;
88
89 // map the vnfd instances into the CONFD constituent-vnfd ref instances
90 confd['constituent-vnfd'] = confd['constituent-vnfd'].map((d, index) => {
91
92 const constituentVNFD = {
93 'member-vnf-index': d['member-vnf-index'],
94 'vnfd-id-ref': d['vnfd-id-ref']
95 };
96
97 if (d['vnf-configuration']) {
98 const vnfConfig = _.cloneDeep(d['vnf-configuration']);
99 const configType = vnfConfig['config-type'] || 'none';
100 // make sure we send the correct values based on config type
101 if (configType === 'none') {
102 constituentVNFD['vnf-configuration'] = {'config-type': 'none'};
103 const configPriority = utils.resolvePath(vnfConfig, 'input-params.config-priority');
104 const configPriorityValue = _.isNumber(configPriority) ? configPriority : d.uiState['member-vnf-index'];
105 utils.assignPathValue(constituentVNFD['vnf-configuration'], 'input-params.config-priority', configPriorityValue);
106 } else {
107 // remove any unused configuration options
108 ['netconf', 'rest', 'script', 'juju'].forEach(type => {
109 if (configType !== type) {
110 delete vnfConfig[type];
111 }
112 });
113 constituentVNFD['vnf-configuration'] = vnfConfig;
114 }
115 }
116
117 if (d.hasOwnProperty('start-by-default')) {
118 constituentVNFD['start-by-default'] = d['start-by-default'];
119 }
120
121 return constituentVNFD;
122
123 });
124
125 // serialize the VLD instances
126 confd.vld = confd.vld.map(d => {
127 return DescriptorModelSerializer.serialize(d);
128 });
129
130 return confd;
131
132 }
133 },
134 vld: {
135 serialize(vldModel) {
136 if(!vldFields) vldFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd.vld');
137 const confd = _.pick(vldModel, vldFields);
138 const property = 'vnfd-connection-point-ref';
139
140 // TODO: There is a bug in RIFT-REST that is not accepting empty
141 // strings for string properties.
142 // once that is fixed, remove this piece of code.
143 // fix-start
144 for (var key in confd) {
145 if (confd.hasOwnProperty(key) && confd[key] === '') {
146 delete confd[key];
147 }
148 }
149
150 const deepProperty = 'provider-network';
151 for (var key in confd[deepProperty]) {
152 if (confd[deepProperty].hasOwnProperty(key) && confd[deepProperty][key] === '') {
153 delete confd[deepProperty][key];
154 }
155 }
156 // fix-end
157
158
159 confd[property] = confd[property].map(d => DescriptorModelSerializer[property].serialize(d));
160 return confd;
161 }
162 },
163 'vnfd-connection-point-ref': {
164 serialize(ref) {
165 return _.pick(ref, ['member-vnf-index-ref', 'vnfd-id-ref', 'vnfd-connection-point-ref']);
166 }
167 },
168 'constituent-vnfd': {
169 serialize(cvnfdModel) {
170 if(!cvnfdFields) cvnfdFields = DescriptorModelMetaFactory.getModelFieldNamesForType('nsd.constituent-vnfd');
171 return _.pick(cvnfdModel, cvnfdFields);
172 }
173 },
174 vnfd: {
175 serialize(vnfdModel) {
176 if(!vnfdFields) vnfdFields = DescriptorModelMetaFactory.getModelFieldNamesForType('vnfd').concat('uiState');
177 const confd = _.pick(vnfdModel, vnfdFields);
178 confd.vdu = confd.vdu.map(d => DescriptorModelSerializer.serialize(d));
179 return confd;
180 }
181 },
182 vdu: {
183 serialize(vduModel) {
184 const confd = _.omit(vduModel, ['uiState']);
185 return confd;
186 }
187 }
188 };
189
190 export default DescriptorModelSerializer;