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