Merging master to master_vca_intg
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / model / descriptors / NetworkService.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 /**
19 * Created by onvelocity on 11/23/15.
20 */
21
22 'use strict';
23
24 import _ from 'lodash'
25 import ColorGroups from '../../ColorGroups'
26 import DescriptorModel from '../DescriptorModel'
27 import ForwardingGraph from './ForwardingGraph'
28 import VirtualNetworkFunctionAccessPointMap from './VirtualNetworkFunctionAccessPointMap'
29 import VirtualLink from './VirtualLink'
30 import ConstituentVnfd from './ConstituentVnfd'
31 import PhysicalNetworkFunction from './PhysicalNetworkFunction'
32 import DescriptorModelFactory from '../DescriptorModelFactory'
33 import DescriptorModelMetaFactory from '../DescriptorModelMetaFactory'
34
35 const indexAsInteger = d => 1 + (parseInt(d.model['member-vnf-index'], 10) || 0);
36
37 const suffixAsInteger = function (field) {
38 return (d) => 1 + (parseInt(String(d.model[field]).split('').reverse().join(''), 10) || 0);
39 };
40
41 const toBiggestValue = (newIndex, curIndex) => Math.max(newIndex, curIndex);
42
43 export default class NetworkService extends DescriptorModel {
44
45 static get type() {
46 return 'nsd';
47 }
48
49 static get className() {
50 return 'NetworkService';
51 }
52
53 constructor(model, parent) {
54 super(model, parent);
55 this.type = 'nsd';
56 this.className = 'NetworkService';
57 this._connectors = [];
58 }
59
60 get constituentVnfd() {
61 if (!this.model['constituent-vnfd']) {
62 this.model['constituent-vnfd'] = [];
63 }
64 return this.model['constituent-vnfd'].map(d => DescriptorModelFactory.newConstituentVnfd(d, this));
65 }
66
67 set constituentVnfd(obj) {
68 const updateNextVnfdIndex = (cvnfd) => {
69 const items = this.constituentVnfd;
70 const length = items.length;
71 // the default value is set to an instance count but we want it to be a sequence no
72 cvnfd.model['member-vnf-index'] = 0;
73 cvnfd.model['member-vnf-index'] = items.map(indexAsInteger).reduce(toBiggestValue, length);
74 };
75 this.updateModelList('constituent-vnfd', obj, ConstituentVnfd, updateNextVnfdIndex);
76 }
77
78 createConstituentVnfd(model) {
79 model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.constituent-vnfd');
80 return this.constituentVnfd = DescriptorModelFactory.newConstituentVnfd(model, this);
81 }
82
83 removeConstituentVnfd(cvnfd) {
84 cvnfd = this.findChildByUid(cvnfd);
85 this.vld.forEach(vld => vld.removeVnfdConnectionPointRefForVnfdIndex(cvnfd.vnfdIndex));
86 this.vnffgd.forEach(fg => fg.removeVnfdConnectionPointRefForVnfdIndex(cvnfd.vnfdIndex));
87 return this.removeModelListItem('constituentVnfd', cvnfd);
88 }
89
90 get pnfd() {
91 if (!this.model.pnfd) {
92 this.model.pnfd = [];
93 }
94 return this.model.pnfd.map(d => DescriptorModelFactory.newPhysicalNetworkFunction(d, this));
95 }
96
97 set pnfd(obj) {
98 this.updateModelList('pnfd', obj, PhysicalNetworkFunction);
99 }
100
101 createPnfd(model) {
102 model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.pnfd');
103 return this.pnfd = DescriptorModelFactory.newPhysicalNetworkFunction(model, this);
104 }
105
106 removePnfd(pnfd) {
107 return this.removeModelListItem('pnfd', pnfd);
108 }
109
110
111 get vld() {
112 if (!this.model.vld) {
113 this.model.vld = [];
114 }
115 return this.model.vld.map(d => DescriptorModelFactory.newVirtualLink(d, this));
116 }
117
118 set vld(obj) {
119 this.updateModelList('vld', obj, VirtualLink);
120 }
121
122 createVld() {
123 const model = DescriptorModelMetaFactory.createModelInstanceForType('nsd.vld');
124 return this.vld = DescriptorModelFactory.newVirtualLink(model, this);
125 }
126
127 removeVLD(vld) {
128 return this.removeModelListItem('vld', vld);
129 }
130
131
132 // <<<<<<< Updated upstream
133 // get configParameterMap() {
134 // if (!this.model['config-parameter-map']) {
135 // this.model['config-parameter-map'] = [];
136 // }
137 // return this.model['config-parameter-map'].map(d => DescriptorModelFactory.newVirtualNetworkFunctionAccessPointMap(d, this)).map((fg, i) => {
138 // return fg;
139 // });
140 // }
141
142 // set configParameterMap(obj) {
143 // const onVirtualNetworkFunctionAccessPointMap = (fg) => {
144
145 // };
146 // this.updateModelList('config-parameter-map', obj, VirtualNetworkFunctionAccessPointMap, onVirtualNetworkFunctionAccessPointMap);
147 // }
148
149 // createConfigParameterMap(model) {
150 // model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.config-parameter-map');
151 // return this.configParameterMap = DescriptorModelFactory.newVirtualNetworkFunctionAccessPointMap(model, this);
152 // }
153 // =======
154 get configParameterMap() {
155 if (!this.model['config-parameter-map']) {
156 this.model['config-parameter-map'] = [];
157 }
158 return this.model['config-parameter-map'].map(d => DescriptorModelFactory.newVirtualNetworkFunctionAccessPointMap(d, this))
159 }
160
161 set configParameterMap(obj) {
162 this.updateModelList('config-parameter-map', obj, VirtualNetworkFunctionAccessPointMap);
163 }
164
165 createConfigParameterMap() {
166 const model = DescriptorModelMetaFactory.createModelInstanceForType('nsd.config-parameter-map');
167 return this.configParameterMap = DescriptorModelFactory.newVirtualNetworkFunctionAccessPointMap(model, this);
168 }
169 // >>>>>>> Stashed changes
170
171 get vnffgd() {
172 if (!this.model.vnffgd) {
173 this.model.vnffgd = [];
174 }
175 return this.model.vnffgd.map(d => DescriptorModelFactory.newForwardingGraph(d, this)).map((fg, i) => {
176 fg.uiState.colors = ColorGroups.getColorPair(i);
177 return fg;
178 });
179 }
180
181 set vnffgd(obj) {
182 const onAddForwardingGraph = (fg) => {
183 const index = this.vnffgd.map(suffixAsInteger('short-name')).reduce(toBiggestValue, this.vnffgd.length);
184 fg.model['short-name'] = 'FG-' + index;
185 };
186 this.updateModelList('vnffgd', obj, ForwardingGraph, onAddForwardingGraph);
187 }
188
189 createVnffgd(model) {
190 model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.vnffgd');
191 return this.vnffgd = DescriptorModelFactory.newForwardingGraph(model, this);
192 }
193
194 removeVnffgd(fg) {
195 return this.removeModelListItem('vnffgd', fg);
196 }
197
198 get forwardingGraphs() {
199 return this.vnffgd;
200 }
201
202
203
204 // NOTE temporarily disable NSD connection points
205 // https://trello.com/c/crVgg2A1/88-do-not-render-nsd-connection-in-the-composer
206 //get connectionPoint() {
207 // if (this.model && this.model['connection-point']) {
208 // return this.model['connection-point'];
209 // }
210 // return [];
211 //}
212 //
213 //get connectors() {
214 // const parent = this;
215 // if (!this._connectors.length) {
216 // this._connectors = this.connectionPoint.map(cp => {
217 // return DescriptorModelFactory.newConnectionPoint(parent, cp);
218 // });
219 // }
220 // return this._connectors;
221 //}
222
223 removeAnyConnectionsForConnector(cpc) {
224 // todo need to also remove connection points from related ForwardingGraph paths
225 this.vld.forEach(vldc => vldc.removeVnfdConnectionPointRefKey(cpc.key));
226 }
227
228 createConstituentVnfdForVnfd(vnfdRef) {
229 const cvnfd = this.createConstituentVnfd();
230 cvnfd.vnfdRef = vnfdRef;
231 cvnfd.vnfdId = vnfdRef.id;
232 return cvnfd;
233 }
234
235 }