RIFT-15726 - optimize download size -> lodash usage in UI
[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 ColorGroups from '../../ColorGroups'
25 import DescriptorModel from '../DescriptorModel'
26 import ForwardingGraph from './ForwardingGraph'
27 import VirtualLink from './VirtualLink'
28 import ConstituentVnfd from './ConstituentVnfd'
29 import PhysicalNetworkFunction from './PhysicalNetworkFunction'
30 import DescriptorModelFactory from '../DescriptorModelFactory'
31 import DescriptorModelMetaFactory from '../DescriptorModelMetaFactory'
32
33 const indexAsInteger = d => 1 + (parseInt(d.model['member-vnf-index'], 10) || 0);
34
35 const suffixAsInteger = function (field) {
36 return (d) => 1 + (parseInt(String(d.model[field]).split('').reverse().join(''), 10) || 0);
37 };
38
39 const toBiggestValue = (newIndex, curIndex) => Math.max(newIndex, curIndex);
40
41 export default class NetworkService extends DescriptorModel {
42
43 static get type() {
44 return 'nsd';
45 }
46
47 static get className() {
48 return 'NetworkService';
49 }
50
51 constructor(model, parent) {
52 super(model, parent);
53 this.type = 'nsd';
54 this.className = 'NetworkService';
55 this._connectors = [];
56 }
57
58 get constituentVnfd() {
59 if (!this.model['constituent-vnfd']) {
60 this.model['constituent-vnfd'] = [];
61 }
62 return this.model['constituent-vnfd'].map(d => DescriptorModelFactory.newConstituentVnfd(d, this));
63 }
64
65 set constituentVnfd(obj) {
66 const updateNextVnfdIndex = (cvnfd) => {
67 const items = this.constituentVnfd;
68 const length = items.length;
69 // the default value is set to an instance count but we want it to be a sequence no
70 cvnfd.model['member-vnf-index'] = 0;
71 cvnfd.model['member-vnf-index'] = items.map(indexAsInteger).reduce(toBiggestValue, length);
72 };
73 this.updateModelList('constituent-vnfd', obj, ConstituentVnfd, updateNextVnfdIndex);
74 }
75
76 createConstituentVnfd(model) {
77 model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.constituent-vnfd');
78 return this.constituentVnfd = DescriptorModelFactory.newConstituentVnfd(model, this);
79 }
80
81 removeConstituentVnfd(cvnfd) {
82 cvnfd = this.findChildByUid(cvnfd);
83 this.vld.forEach(vld => vld.removeVnfdConnectionPointRefForVnfdIndex(cvnfd.vnfdIndex));
84 this.vnffgd.forEach(fg => fg.removeVnfdConnectionPointRefForVnfdIndex(cvnfd.vnfdIndex));
85 return this.removeModelListItem('constituentVnfd', cvnfd);
86 }
87
88 get pnfd() {
89 if (!this.model.pnfd) {
90 this.model.pnfd = [];
91 }
92 return this.model.pnfd.map(d => DescriptorModelFactory.newPhysicalNetworkFunction(d, this));
93 }
94
95 set pnfd(obj) {
96 this.updateModelList('pnfd', obj, PhysicalNetworkFunction);
97 }
98
99 createPnfd(model) {
100 model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.pnfd');
101 return this.pnfd = DescriptorModelFactory.newPhysicalNetworkFunction(model, this);
102 }
103
104 removePnfd(pnfd) {
105 return this.removeModelListItem('pnfd', pnfd);
106 }
107
108
109 get vld() {
110 if (!this.model.vld) {
111 this.model.vld = [];
112 }
113 return this.model.vld.map(d => DescriptorModelFactory.newVirtualLink(d, this));
114 }
115
116 set vld(obj) {
117 this.updateModelList('vld', obj, VirtualLink);
118 }
119
120 createVld() {
121 const model = DescriptorModelMetaFactory.createModelInstanceForType('nsd.vld');
122 return this.vld = DescriptorModelFactory.newVirtualLink(model, this);
123 }
124
125 removeVLD(vld) {
126 return this.removeModelListItem('vld', vld);
127 }
128
129
130 get vnffgd() {
131 if (!this.model.vnffgd) {
132 this.model.vnffgd = [];
133 }
134 return this.model.vnffgd.map(d => DescriptorModelFactory.newForwardingGraph(d, this)).map((fg, i) => {
135 fg.uiState.colors = ColorGroups.getColorPair(i);
136 return fg;
137 });
138 }
139
140 set vnffgd(obj) {
141 const onAddForwardingGraph = (fg) => {
142 const index = this.vnffgd.map(suffixAsInteger('short-name')).reduce(toBiggestValue, this.vnffgd.length);
143 fg.model['short-name'] = 'FG-' + index;
144 };
145 this.updateModelList('vnffgd', obj, ForwardingGraph, onAddForwardingGraph);
146 }
147
148 createVnffgd(model) {
149 model = model || DescriptorModelMetaFactory.createModelInstanceForType('nsd.vnffgd');
150 return this.vnffgd = DescriptorModelFactory.newForwardingGraph(model, this);
151 }
152
153 removeVnffgd(fg) {
154 return this.removeModelListItem('vnffgd', fg);
155 }
156
157 get forwardingGraphs() {
158 return this.vnffgd;
159 }
160
161
162 // NOTE temporarily disable NSD connection points
163 // https://trello.com/c/crVgg2A1/88-do-not-render-nsd-connection-in-the-composer
164 //get connectionPoint() {
165 // if (this.model && this.model['connection-point']) {
166 // return this.model['connection-point'];
167 // }
168 // return [];
169 //}
170 //
171 //get connectors() {
172 // const parent = this;
173 // if (!this._connectors.length) {
174 // this._connectors = this.connectionPoint.map(cp => {
175 // return DescriptorModelFactory.newConnectionPoint(parent, cp);
176 // });
177 // }
178 // return this._connectors;
179 //}
180
181 removeAnyConnectionsForConnector(cpc) {
182 // todo need to also remove connection points from related ForwardingGraph paths
183 this.vld.forEach(vldc => vldc.removeVnfdConnectionPointRefKey(cpc.key));
184 }
185
186 createConstituentVnfdForVnfd(vnfdRef) {
187 const cvnfd = this.createConstituentVnfd();
188 cvnfd.vnfdRef = vnfdRef;
189 cvnfd.vnfdId = vnfdRef.id;
190 return cvnfd;
191 }
192
193 }