4 * Copyright 2016 RIFT.IO Inc
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Created by onvelocity on 10/20/15.
23 import _
from 'lodash'
24 import utils
from './../utils'
25 import DescriptorModelFields
from './DescriptorModelFields'
26 import DescriptorModelMetaFactory
from './DescriptorModelMetaFactory'
30 let vnfdFields
= null;
31 let cvnfdFields
= null;
32 let configParameterMapFields
= null;
38 * Serialize DescriptorModel JSON into CONFD JSON. Also, cleans up the data as needed.
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(*=))}}}
42 const DescriptorModelSerializer
= {
44 const type
= model
.uiState
&& model
.uiState
.type
;
45 const serializer
= this[type
];
47 model
= serializer
.serialize(model
);
48 this[':clean'](model
);
54 // remove uiState from all elements accept nsd and vnfd
55 // remove empty / blank value fields
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
] === '') {
62 const isMetaAllowed
= /^nsd|vnfd$/.test(m
.uiState
&& m
.uiState
.type
);
63 if (k
=== 'uiState') {
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
);
74 if (typeof m
[k
] === 'object') {
84 if(!nsdFields
) nsdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd').concat('uiState');
85 const confd
= _
.pick(nsdModel
, nsdFields
);
87 // vnfd is defined in the ETSI etsi_gs reference manual but RIFT does not use it
90 // map the vnfd instances into the CONFD constituent-vnfd ref instances
91 confd
['constituent-vnfd'] = confd
['constituent-vnfd'].map((d
, index
) => {
93 const constituentVNFD
= {
94 'member-vnf-index': d
['member-vnf-index'],
95 'vnfd-id-ref': d
['vnfd-id-ref']
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
);
108 // remove any unused configuration options
109 ['netconf', 'rest', 'script', 'juju'].forEach(type
=> {
110 if (configType
!== type
) {
111 delete vnfConfig
[type
];
114 constituentVNFD
['vnf-configuration'] = vnfConfig
;
118 if (d
.hasOwnProperty('start-by-default')) {
119 constituentVNFD
['start-by-default'] = d
['start-by-default'];
122 return constituentVNFD
;
125 for (var key
in confd
) {
126 checkForChoiceAndRemove(key
, confd
, nsdModel
);
128 // serialize the VLD instances
129 confd
.vld
= confd
.vld
.map(d
=> {
130 return DescriptorModelSerializer
.serialize(d
);
133 return cleanEmptyTopKeys(confd
);
138 serialize(vldModel
) {
139 if(!vldFields
) vldFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.vld');
140 const confd
= _
.pick(vldModel
, vldFields
);
141 const property
= 'vnfd-connection-point-ref';
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.
147 for (var key
in confd
) {
148 if (confd
.hasOwnProperty(key
) && confd
[key
] === '') {
151 //removes choice properties from top level object and copies immediate children onto it.
152 checkForChoiceAndRemove(key
, confd
, vldModel
);
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
];
164 confd
[property
] = confd
[property
].map(d
=> DescriptorModelSerializer
[property
].serialize(d
));
165 return cleanEmptyTopKeys(confd
);
168 'vnfd-connection-point-ref': {
170 return _
.pick(ref
, ['member-vnf-index-ref', 'vnfd-id-ref', 'vnfd-connection-point-ref']);
173 'internal-connection-point': {
175 return _
.pick(ref
, ['id-ref']);
178 'constituent-vnfd': {
179 serialize(cvnfdModel
) {
180 if(!cvnfdFields
) cvnfdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.constituent-vnfd');
181 return _
.pick(cvnfdModel
, cvnfdFields
);
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
);
193 serialize(vduModel
) {
194 const copy
= _
.cloneDeep(vduModel
);
195 for (let k
in copy
) {
196 checkForChoiceAndRemove(k
, copy
, vduModel
)
198 const confd
= _
.omit(copy
, ['uiState']);
199 return cleanEmptyTopKeys(confd
);
202 'config-parameter-map': {
203 serialize(configParameterMap
) {
205 if(!configParameterMapFields
) configParameterMapFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.config-parameter-map');
206 return _
.pick(configParameterMap
, configParameterMapFields
);
212 function checkForChoiceAndRemove(k
, confd
, model
) {
213 let state
= model
.uiState
;
215 let choice
= state
.choice
[k
]
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
]
227 for (let key
in confd
[k
]) {
228 if(choice
&& (choice
.selected
.indexOf(key
) > -1)) {
229 confd
[key
] = confd
[k
][key
]
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
] === '') {
250 export default DescriptorModelSerializer
;