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;
37 * Serialize DescriptorModel JSON into CONFD JSON. Also, cleans up the data as needed.
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(*=))}}}
41 const DescriptorModelSerializer
= {
43 const type
= model
.uiState
&& model
.uiState
.type
;
44 const serializer
= this[type
];
46 model
= serializer
.serialize(model
);
47 this[':clean'](model
);
53 // remove uiState from all elements accept nsd and vnfd
54 // remove empty / blank value fields
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
] === '') {
61 const isMetaAllowed
= /^nsd|vnfd$/.test(m
.uiState
&& m
.uiState
.type
);
62 if (k
=== 'uiState') {
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
);
73 if (typeof m
[k
] === 'object') {
83 if(!nsdFields
) nsdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd').concat('uiState');
84 const confd
= _
.pick(nsdModel
, nsdFields
);
86 // vnfd is defined in the ETSI etsi_gs reference manual but RIFT does not use it
89 // map the vnfd instances into the CONFD constituent-vnfd ref instances
90 confd
['constituent-vnfd'] = confd
['constituent-vnfd'].map((d
, index
) => {
92 const constituentVNFD
= {
93 'member-vnf-index': d
['member-vnf-index'],
94 'vnfd-id-ref': d
['vnfd-id-ref']
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
);
107 // remove any unused configuration options
108 ['netconf', 'rest', 'script', 'juju'].forEach(type
=> {
109 if (configType
!== type
) {
110 delete vnfConfig
[type
];
113 constituentVNFD
['vnf-configuration'] = vnfConfig
;
117 if (d
.hasOwnProperty('start-by-default')) {
118 constituentVNFD
['start-by-default'] = d
['start-by-default'];
121 return constituentVNFD
;
124 for (var key
in confd
) {
125 checkForChoiceAndRemove(key
, confd
, nsdModel
);
127 // serialize the VLD instances
128 confd
.vld
= confd
.vld
.map(d
=> {
129 return DescriptorModelSerializer
.serialize(d
);
132 return cleanEmptyTopKeys(confd
);
137 serialize(vldModel
) {
138 if(!vldFields
) vldFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.vld');
139 const confd
= _
.pick(vldModel
, vldFields
);
140 const property
= 'vnfd-connection-point-ref';
142 // TODO: There is a bug in RIFT-REST that is not accepting empty
143 // strings for string properties.
144 // once that is fixed, remove this piece of code.
146 for (var key
in confd
) {
147 if (confd
.hasOwnProperty(key
) && confd
[key
] === '') {
150 //removes choice properties from top level object and copies immediate children onto it.
151 checkForChoiceAndRemove(key
, confd
, vldModel
);
156 const deepProperty
= 'provider-network';
157 for (var key
in confd
[deepProperty
]) {
158 if (confd
[deepProperty
].hasOwnProperty(key
) && confd
[deepProperty
][key
] === '') {
159 delete confd
[deepProperty
][key
];
163 confd
[property
] = confd
[property
].map(d
=> DescriptorModelSerializer
[property
].serialize(d
));
164 return cleanEmptyTopKeys(confd
);
167 'vnfd-connection-point-ref': {
169 return _
.pick(ref
, ['member-vnf-index-ref', 'vnfd-id-ref', 'vnfd-connection-point-ref']);
172 'internal-connection-point': {
174 return _
.pick(ref
, ['id-ref']);
177 'constituent-vnfd': {
178 serialize(cvnfdModel
) {
179 if(!cvnfdFields
) cvnfdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.constituent-vnfd');
180 return _
.pick(cvnfdModel
, cvnfdFields
);
184 serialize(vnfdModel
) {
185 if(!vnfdFields
) vnfdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('vnfd').concat('uiState');
186 const confd
= _
.pick(vnfdModel
, vnfdFields
);
187 confd
.vdu
= confd
.vdu
.map(d
=> DescriptorModelSerializer
.serialize(d
));
188 return cleanEmptyTopKeys(confd
);
192 serialize(vduModel
) {
193 const copy
= _
.cloneDeep(vduModel
);
194 for (let k
in copy
) {
195 checkForChoiceAndRemove(k
, copy
, vduModel
)
197 const confd
= _
.omit(copy
, ['uiState']);
198 return cleanEmptyTopKeys(confd
);
204 function checkForChoiceAndRemove(k
, confd
, model
) {
205 let state
= model
.uiState
;
207 let choice
= state
.choice
[k
]
209 if (choice
.constructor.name
== "Array") {
210 for(let i
= 0; i
< choice
.length
; i
++) {
211 for (let key
in confd
[k
][i
]) {
212 if(choice
[i
] && (choice
[i
].selected
.indexOf(key
) > -1)) {
213 confd
[k
][i
][key
] = confd
[k
][i
][key
]
219 for (let key
in confd
[k
]) {
220 if(choice
&& (choice
.selected
.indexOf(key
) > -1)) {
221 confd
[key
] = confd
[k
][key
]
232 function cleanEmptyTopKeys(m
){
233 Object
.keys(m
).forEach(k
=> {
234 const isEmptyObject
= typeof m
[k
] === 'object' && _
.isEmpty(m
[k
]);
235 if (typeof m
[k
] === 'undefined' || isEmptyObject
|| m
[k
] === '') {
242 export default DescriptorModelSerializer
;