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
;
125 // serialize the VLD instances
126 confd
.vld
= confd
.vld
.map(d
=> {
127 return DescriptorModelSerializer
.serialize(d
);
135 serialize(vldModel
) {
136 if(!vldFields
) vldFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.vld');
137 const confd
= _
.pick(vldModel
, vldFields
);
138 const property
= 'vnfd-connection-point-ref';
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.
144 for (var key
in confd
) {
145 if (confd
.hasOwnProperty(key
) && confd
[key
] === '') {
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
];
159 confd
[property
] = confd
[property
].map(d
=> DescriptorModelSerializer
[property
].serialize(d
));
163 'vnfd-connection-point-ref': {
165 return _
.pick(ref
, ['member-vnf-index-ref', 'vnfd-id-ref', 'vnfd-connection-point-ref']);
168 'internal-connection-point': {
170 return _
.pick(ref
, ['id-ref']);
173 'constituent-vnfd': {
174 serialize(cvnfdModel
) {
175 if(!cvnfdFields
) cvnfdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('nsd.constituent-vnfd');
176 return _
.pick(cvnfdModel
, cvnfdFields
);
180 serialize(vnfdModel
) {
181 if(!vnfdFields
) vnfdFields
= DescriptorModelMetaFactory
.getModelFieldNamesForType('vnfd').concat('uiState');
182 const confd
= _
.pick(vnfdModel
, vnfdFields
);
183 confd
.vdu
= confd
.vdu
.map(d
=> DescriptorModelSerializer
.serialize(d
));
188 serialize(vduModel
) {
189 const confd
= _
.omit(vduModel
, ['uiState']);
195 export default DescriptorModelSerializer
;