3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Created by onvelocity on 11/23/15.
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'
34 const indexAsInteger
= d
=> 1 + (parseInt(d
.model
['member-vnf-index'], 10) || 0);
36 const suffixAsInteger = function (field
) {
37 return (d
) => 1 + (parseInt(String(d
.model
[field
]).split('').reverse().join(''), 10) || 0);
40 const toBiggestValue
= (newIndex
, curIndex
) => Math
.max(newIndex
, curIndex
);
42 export default class NetworkService
extends DescriptorModel
{
48 static get className() {
49 return 'NetworkService';
52 constructor(model
, parent
) {
55 this.className
= 'NetworkService';
56 this._connectors
= [];
59 get constituentVnfd() {
60 if (!this.model
['constituent-vnfd']) {
61 this.model
['constituent-vnfd'] = [];
63 return this.model
['constituent-vnfd'].map(d
=> DescriptorModelFactory
.newConstituentVnfd(d
, this));
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
);
74 this.updateModelList('constituent-vnfd', obj
, ConstituentVnfd
, updateNextVnfdIndex
);
77 createConstituentVnfd(model
) {
78 model
= model
|| DescriptorModelMetaFactory
.createModelInstanceForType('nsd.constituent-vnfd');
79 return this.constituentVnfd
= DescriptorModelFactory
.newConstituentVnfd(model
, this);
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
);
90 if (!this.model
.pnfd
) {
93 return this.model
.pnfd
.map(d
=> DescriptorModelFactory
.newPhysicalNetworkFunction(d
, this));
97 this.updateModelList('pnfd', obj
, PhysicalNetworkFunction
);
101 model
= model
|| DescriptorModelMetaFactory
.createModelInstanceForType('nsd.pnfd');
102 return this.pnfd
= DescriptorModelFactory
.newPhysicalNetworkFunction(model
, this);
106 return this.removeModelListItem('pnfd', pnfd
);
111 if (!this.model
.vld
) {
114 return this.model
.vld
.map(d
=> DescriptorModelFactory
.newVirtualLink(d
, this));
118 this.updateModelList('vld', obj
, VirtualLink
);
122 const model
= DescriptorModelMetaFactory
.createModelInstanceForType('nsd.vld');
123 return this.vld
= DescriptorModelFactory
.newVirtualLink(model
, this);
127 return this.removeModelListItem('vld', vld
);
132 if (!this.model
.vnffgd
) {
133 this.model
.vnffgd
= [];
135 return this.model
.vnffgd
.map(d
=> DescriptorModelFactory
.newForwardingGraph(d
, this)).map((fg
, i
) => {
136 fg
.uiState
.colors
= ColorGroups
.getColorPair(i
);
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
;
146 this.updateModelList('vnffgd', obj
, ForwardingGraph
, onAddForwardingGraph
);
149 createVnffgd(model
) {
150 model
= model
|| DescriptorModelMetaFactory
.createModelInstanceForType('nsd.vnffgd');
151 return this.vnffgd
= DescriptorModelFactory
.newForwardingGraph(model
, this);
155 return this.removeModelListItem('vnffgd', fg
);
158 get forwardingGraphs() {
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'];
173 // const parent = this;
174 // if (!this._connectors.length) {
175 // this._connectors = this.connectionPoint.map(cp => {
176 // return DescriptorModelFactory.newConnectionPoint(parent, cp);
179 // return this._connectors;
182 removeAnyConnectionsForConnector(cpc
) {
183 // todo need to also remove connection points from related ForwardingGraph paths
184 this.vld
.forEach(vldc
=> vldc
.removeVnfdConnectionPointRefKey(cpc
.key
));
187 createConstituentVnfdForVnfd(vnfdRef
) {
188 const cvnfd
= this.createConstituentVnfd();
189 cvnfd
.vnfdRef
= vnfdRef
;
190 cvnfd
.vnfdId
= vnfdRef
.id
;