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.
18 import NetworkServiceActions
from './launchNetworkServiceActions.js';
19 import NetworkServiceSource
from './launchNetworkServiceSource.js';
20 import GUID
from 'utils/guid.js';
21 import AppHeaderActions
from 'widgets/header/headerActions.js';
22 import Alt
from '../alt';
23 import _
from 'lodash';
26 class LaunchNetworkServiceStore
{
36 this.sshKeysList
= [];
38 this.sla_parameters
= [];
40 this.selectedNSD
= {};
41 this.selectedCloudAccount
= {};
42 this.dataCenters
= [];
43 this.cloudAccounts
= [];
44 this.isLoading
= false;
45 this.hasConfigureNSD
= false;
46 this['input-parameters'] = [];
47 this.displayPlacementGroups
= false;
49 this.bindActions(NetworkServiceActions
);
50 this.nsdConfiguration
= {
52 selectedCloudAccount
: {},
55 /*Collection of vnf state containting cloud account and datacenter info.
58 this.vnfdCloudAccounts
= {};
60 this.configAgentAccounts
= [];
62 this.isPreviewing
= false;
63 this.isOpenMano
= false;
64 this.registerAsync(NetworkServiceSource
);
65 this.exportPublicMethods({
66 getMockData
: getMockData
.bind(this),
67 getMockSLA
: getMockSLA
.bind(this),
68 saveNetworkServiceRecord
: this.saveNetworkServiceRecord
,
71 updateInputParam
: this.updateInputParam
,
72 resetView
: this.resetView
,
73 nameUpdated
: this.nameUpdated
,
75 descriptorSelected
: this.descriptorSelected
.bind(this),
76 deselectDescriptor
: this.deselectDescriptor
,
77 previewDescriptor
: this.previewDescriptor
,
79 ipProfileFn
: this.ipProfileFn
,
81 usersFn
: this.usersFn
,
88 console
.log('reseting state');
91 'input-parameter-xpath': null,
92 'ns-placement-groups': null,
93 'vnf-placement-groups':null,
100 getCatalogSuccess(catalogs
) {
106 catalogs
.forEach(function(catalog
) {
107 switch (catalog
.type
) {
111 self
.descriptorSelected(catalog
.descriptors
[0])
113 console
.log('unable to select catalog')
124 nsd
[0].descriptors
.map(function(n
) {
128 nsd
, vnfd
, pnfd
, nsdDict
,
132 getLaunchCloudAccountSuccess(cloudAccounts
) {
134 newState
.cloudAccounts
= cloudAccounts
.filter(function(v
) {
136 return v
['connection-status'].status
== 'success';
138 if(cloudAccounts
.length
!= newState
.cloudAccounts
.length
) {
139 Alt
.actions
.global
.showNotification
.defer({type
: 'warning', msg
: 'One or more VIM accounts have failed to connect'});
141 if(cloudAccounts
&& cloudAccounts
.length
> 0) {
142 newState
.selectedCloudAccount
= cloudAccounts
[0];
143 if (cloudAccounts
[0]['account-type'] == 'openstack') {
144 newState
.displayPlacementGroups
= true;
146 newState
.displayPlacementGroups
= false;
149 newState
.selectedCloudAccount
= {};
152 this.setState(newState
);
154 getConfigAgentSuccess(configAgentAccounts
) {
156 configAgentAccounts
: configAgentAccounts
159 getDataCentersSuccess(data
) {
160 let dataCenters
= data
;
163 dataCenters
: dataCenters
|| []
165 if (this.ro
&& this.ro
['account-type'] == 'openmano') {
166 newState
.dataCenterID
= dataCenters
[this.ro
.name
][0].uuid
168 this.setState(newState
)
170 getVDUSuccess(VNFD
) {
176 Alt
.actions
.global
.showScreenLoader
.defer();
180 console
.log('is Loading', this)
182 launchNSRSuccess(data
) {
183 console
.log('Launching Network Service')
184 let tokenizedHash
= window
.location
.hash
.split('/');
185 Alt
.actions
.global
.hideScreenLoader
.defer();
189 return window
.location
.hash
= 'launchpad/' + tokenizedHash
[2];
191 launchNSRError(error
) {
192 var msg
= 'Something went wrong while trying to instantiate. Check the error logs for more information';
196 Alt
.actions
.global
.showNotification
.defer(msg
);
197 Alt
.actions
.global
.hideScreenLoader
.defer();
202 getInstantiateSshKeySuccess
= (data
) => {
208 getResourceOrchestratorSuccess
= (data
) => {
209 Alt
.actions
.global
.hideScreenLoader
.defer();
214 getResourceOrchestratorError
= (data
) => {
215 console
.log('getResourceOrchestrator Error: ', data
)
218 nameUpdated
= (e
) => {
223 deselectDescriptor
= () => {
224 console
.log('deselecting')
231 descriptorSelected
= (data
) => {
236 selectedNSDid
: NSD
.id
,
237 vld
: NSD
&& NSD
.vld
&& NSD
.vld
.map(function(v
) {
238 //Adding a type for UI state management
239 //This is deleted before launch
240 if(v
['ip-profile-ref']) {
241 v
.type
= 'ip-profile-ref';
243 if(v
['vim-network-name']) {
244 v
.type
= 'vim-network-name';
251 ipProfiles
: NSD
['ip-profiles']
253 newState
.selectedNSD
= data
;
254 if (NSD
['input-parameter-xpath']) {
255 newState
.hasConfigureNSD
= true;
256 newState
['input-parameters'] = NSD
['input-parameter-xpath'];
258 newState
.hasConfigureNSD
= false;
259 newState
['input-parameters'] = null;
261 if(NSD
['ns-placement-groups'] && NSD
['ns-placement-groups'].length
> 0 ) {
262 newState
['ns-placement-groups'] = NSD
['ns-placement-groups'];
264 if(NSD
['vnf-placement-groups'] && NSD
['vnf-placement-groups'].length
> 0 ) {
265 newState
['vnf-placement-groups'] = NSD
['vnf-placement-groups'];
267 NSD
["constituent-vnfd"].map((v
) => {
268 VNFIDs
.push(v
["vnfd-id-ref"]);
270 this.getInstance().getVDU(VNFIDs
);
271 this.setState(newState
);
273 previewDescriptor
= (data
) => {
282 updateInputParam
= (i
, value
) => {
283 let ip
= this['input-parameters'];
286 'input-parameters': ip
292 updateSelectedCloudAccount
: (cloudAccount
) => {
293 let nsd
= self
.nsd
[0];
295 selectedCloudAccount
: JSON
.parse(cloudAccount
.target
.value
)
297 if (cloudAccount
['account-type'] == 'openstack') {
298 newState
.displayPlacementGroups
= true;
300 newState
.displayPlacementGroups
= false;
302 self
.setState(newState
);
304 updateSelectedDataCenter
: (dataCenter
) => {
306 dataCenterID
: JSON
.parse(dataCenter
.target
.value
)
309 placementGroupUpdate
: (i
, k
, value
) => {
310 let pg
= self
['ns-placement-groups'];
313 'ns-placement-groups': pg
316 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
317 let pg
= self
['ns-placement-groups'];
318 let ha
= pg
[pgi
]['host-aggregate'][hai
];
321 'ns-placement-groups': pg
324 addHostAggregate
: (pgi
) => {
325 let pg
= self
['ns-placement-groups'];
326 let ha
= pg
[pgi
]['host-aggregate'];
329 'ns-placement-groups': pg
332 removeHostAggregate
: (pgi
, hai
) => {
333 let pg
= self
['ns-placement-groups'];
334 let ha
= pg
[pgi
]['host-aggregate'];
337 'ns-placement-groups': pg
340 getNSDByID
: (id
) => {
348 placementGroupUpdate
: (i
, k
, value
) => {
349 let pg
= self
['vnf-placement-groups'];
352 'vnf-placement-groups': pg
355 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
356 let pg
= self
['vnf-placement-groups'];
357 let ha
= pg
[pgi
]['host-aggregate'][hai
];
360 'vnf-placement-groups': pg
363 addHostAggregate
: (pgi
) => {
364 let pg
= self
['vnf-placement-groups'];
365 let ha
= pg
[pgi
]['host-aggregate'];
368 'vnf-placement-groups': pg
371 removeHostAggregate
: (pgi
, hai
) => {
372 let pg
= self
['vnf-placement-groups'];
373 let ha
= pg
[pgi
]['host-aggregate'];
376 'vnf-placement-groups': pg
379 updateSelectedCloudAccount
: (id
, cloudAccount
) => {
380 let vnfCA
= self
.vnfdCloudAccounts
;
382 if(!vnfCA
.hasOwnProperty(id
)) {
385 vnfCA
[id
].account
= JSON
.parse(cloudAccount
.target
.value
);
387 if (cloudAccount
['account-type'] == 'openmano' && this.dataCenters
&& self
.dataCenters
[cloudAccount
['name']]) {
388 let datacenter
= self
.dataCenters
[cloudAccount
['name']][0];
389 vnfCA
[id
].datacenter
= datacenter
.uuid
;
391 if (vnfCA
[id
].datacenter
) {
392 delete vnfCA
[id
].datacenter
;
396 if(vnfCA
.hasOwnProperty(id
)) {
397 if(vnfCA
[id
].hasOwnProperty('config-agent-account')) {
398 delete vnfCA
[id
].account
;
405 vnfdCloudAccounts
: vnfCA
408 updateSelectedConfigAgent
: (id
) => {
410 let configAgentRef
= JSON
.parse(e
.target
.value
);
411 let vnfCA
= self
.vnfdCloudAccounts
;
413 if(!vnfCA
.hasOwnProperty(id
)) {
416 vnfCA
[id
]['config-agent-account'] = configAgentRef
;
418 if(vnfCA
[id
].hasOwnProperty('account')) {
419 delete vnfCA
[id
]['config-agent-account'];
425 vnfdCloudAccounts
: vnfCA
429 updateSelectedDataCenter
: (id
, dataCenter
) => {
430 let vnfCA
= self
.vnfdCloudAccounts
;
434 vnfCA
[id
].datacenter
= JSON
.parse(dataCenter
.target
.value
);
436 vnfdCloudAccounts
: vnfCA
446 let type
= e
.target
.value
;
448 if (vld
[i
].hasOwnProperty('type')) {
449 delete vld
[i
][vld
[i
].type
]
453 if(type
== 'ip-profile-ref') {
454 let IPProfile
= self
.ipProfiles
;
455 vld
[i
][type
] = IPProfile
[0] && IPProfile
[0].name
;
456 delete vld
[i
]['vim-network-name'];
458 delete vld
[i
]['dns-server'];
461 delete vld
[i
]['ip-profile-ref'];
462 delete vld
[i
]['vim-network-name'];
464 self
.setState({vld
:vld
});
467 updateValue
: (i
, type
) => {
469 // Select Option returns JSON values.
470 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
472 vld
[i
][type
] = value
;
473 self
.setState({vld
:vld
});
478 ipProfileFn
= () => {
481 updateProfile
: (i
, key
) => {
483 // Select Option returns JSON values.
484 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
485 self
.ipProfiles
[i
]['ip-profile-params'][key
] = value
;
487 self
.setState({ipProfiles
:self
.ipProfiles
});
490 updateVersion
: (i
) => {
492 // Select Option returns JSON values.
493 let value
= e
.target
.value
;
494 self
.ipProfiles
[i
]['ip-profile-params']['ip-version'] = value
;
495 self
.setState({ipProfiles
:self
.ipProfiles
});
498 updateDNS
: (i
, dnsIndex
) => {
500 // Select Option returns JSON values.
501 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
502 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][dnsIndex
] = value
;
503 self
.setState({ipProfiles
:self
.ipProfiles
});
506 updateDHCP
: (i
, property
) => {
508 let value
= e
.target
.value
;
509 //If value is meant to be boolean, convert it
510 if(value
== "true" || value
== "false") {
511 value
= JSON
.parse(value
);
513 if(!self
.ipProfiles
[i
]['ip-profile-params'].hasOwnProperty('dhcp-params')) {
514 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
520 //Removing DCHP property on disable to allow instantiation
522 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
526 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'][property
] = value
;
528 self
.setState({ipProfiles
:self
.ipProfiles
});
539 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server']) {
540 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].unshift({})
542 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'] = [{}];
545 self
.setState({ipProfiles
:self
.ipProfiles
});
548 removeDNS
: (i
, k
) => {
551 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].splice(k
, 1);
552 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].length
== 0) {
553 delete self
.ipProfiles
[i
]['ip-profile-params']['dns-server'];
555 self
.setState({ipProfiles
:self
.ipProfiles
});
558 updateDNS
: (i
, k
) => {
561 let value
= e
.target
.value
;
562 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][k
].address
= value
;
563 self
.setState({ipProfiles
:self
.ipProfiles
});
571 updateNewKeyRefSelection
: (e
) => {
573 newRefSelection
: e
.target
.value
576 updateKeyRef
: (refIndex
, remove
) => {
579 let sshKeysRef
= self
.sshKeysRef
;
581 // if(!e.target.value) {
582 // return Alt.actions.global.showError.defer('Please select a key pair');
584 if(!isNaN(refIndex
)){
585 sshKeysRef
.splice(refIndex
, 1);
586 sshKeysRef
.push(e
.target
.value
);
588 sshKeysRef
.push(e
.target
.value
);
592 sshKeysRef
.splice(refIndex
, 1);
595 sshKeysRef
: sshKeysRef
,
596 newRefSelection
: null
605 add: function(sshKeysList
) {
610 'ssh-authorized-key': [sshKeysList
[0].name
]
612 let usersList
= self
.usersList
;
613 usersList
.push(newUser
);
619 remove: function(i
) {
621 self
.usersList
.splice(i
, 1);
623 usersList
: self
.usersList
627 update: function(i
, key
) {
629 let value
= e
.target
.value
;
630 self
.usersList
[i
][key
] = value
;
632 usersList
: self
.usersList
636 updateSSHkeyRef: function(i
, j
, remove
){
638 let usersList
= _
.cloneDeep(self
.usersList
)
639 let keys
= usersList
[i
]['ssh-authorized-key'];
641 let keyRef
= JSON
.parse(e
.target
.value
).name
;
649 usersList
[i
]['ssh-authorized-key'] = keys
;
657 saveNetworkServiceRecord(name
, launch
) {
658 //input-parameter: [{uuid: < some_unique_name>, xpath: <same as you got from nsd>, value: <user_entered_value>}]
660 'input-parameter-xpath':[{
668 // Create a filtered NSD payload from the decorated one as RW.REST cannot handle extra parameters now
670 nsdPayload
= _
.cloneDeep(_
.find(this.state
.nsd
[0].descriptors
, {id
: this.state
.selectedNSDid
}));
672 if (nsdPayload
!= {}) {
673 nsdPayload
['meta'] && delete nsdPayload
['meta'];
674 nsdPayload
['constituent-vnfd'] && nsdPayload
['constituent-vnfd'].map((constituentVnfd
) => {
675 constituentVnfd
['vnf-name'] && delete constituentVnfd
['vnf-name'];
676 constituentVnfd
['name'] && delete constituentVnfd
['name'];
678 nsdPayload
['placement-groups'] && nsdPayload
['placement-groups'].map((placementGroup
) => {
679 placementGroup
['member-vnfd'] && placementGroup
['member-vnfd'].map((memberVnfd
) => {
680 memberVnfd
['name'] && delete memberVnfd
['name'];
683 nsdPayload
['ns-placement-groups'] && delete nsdPayload
['ns-placement-groups'];
684 nsdPayload
['vnf-placement-groups'] && delete nsdPayload
['vnf-placement-groups'];
685 nsdPayload
.vld
= this.state
.vld
;
686 nsdPayload
.vld
&& nsdPayload
.vld
.map(function(v
){
691 let vnfdCloudAccounts
= this.state
.vnfdCloudAccounts
;
696 "description": "a description for " + guuid
,
697 "admin-status": launch
? "ENABLED" : "DISABLED",
701 if (this.state
.ro
&& this.state
.ro
['account-type'] == 'openmano') {
702 payload
['om-datacenter'] = this.state
.dataCenterID
;
704 payload
["cloud-account"] = this.state
.selectedCloudAccount
.name
;
706 if (this.state
.hasConfigureNSD
) {
707 let ips
= this.state
['input-parameters'];
708 let ipsToSend
= ips
.filter(function(ip
) {
709 if (ip
.value
&& ip
.value
!= "") {
716 if (ipsToSend
.length
> 0) {
717 payload
['input-parameter'] = ipsToSend
;
720 // These placement groups need to be refactored. Too much boilerplate.
721 if (this.state
.displayPlacementGroups
) {
722 nsPg
= this.state
['ns-placement-groups'];
723 vnfPg
= this.state
['vnf-placement-groups'];
724 if(nsPg
&& (nsPg
.length
> 0)) {
725 payload
['nsd-placement-group-maps'] = nsPg
.map(function(n
, i
) {
726 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
728 'cloud-type': 'openstack'
730 if(n
['host-aggregate'].length
> 0) {
731 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
733 'metadata-key': h
.key
,
734 'metadata-value': h
.value
738 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
739 obj
['availability-zone'] = {name
: n
['availability-zone']};
741 if(n
['server-group'] && (n
['server-group'] != '')) {
742 obj
['server-group'] = {name
: n
['server-group']};
744 obj
['placement-group-ref'] = n
.name
;
747 }).filter(function(o
){
755 if(vnfPg
&& (vnfPg
.length
> 0)) {
756 payload
['vnfd-placement-group-maps'] = vnfPg
.map(function(n
, i
) {
757 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
759 'cloud-type': 'openstack'
761 if(n
['host-aggregate'].length
> 0) {
762 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
764 'metadata-key': h
.key
,
765 'metadata-value': h
.value
769 if(n
['server-group'] && (n
['server-group'] != '')) {
770 obj
['server-group'] = {name
: n
['server-group']};
772 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
773 obj
['availability-zone'] = {name
: n
['availability-zone']};
775 obj
['placement-group-ref'] = n
.name
;
776 obj
['vnfd-id-ref'] = n
['vnfd-id-ref'];
779 }).filter(function(o
){
788 //Construct VNF cloud accounts
789 payload
['vnf-cloud-account-map'] = [];
790 for(let k
in vnfdCloudAccounts
) {
792 vnf
['member-vnf-index-ref'] = k
;
793 if(vnfdCloudAccounts
[k
].hasOwnProperty('account') && (vnfdCloudAccounts
[k
]['account'] && vnfdCloudAccounts
[k
]['account'].name
)) {
794 vnf
['cloud-account'] = vnfdCloudAccounts
[k
].account
.name
;
796 if(vnfdCloudAccounts
[k
].hasOwnProperty('config-agent-account') && vnfdCloudAccounts
[k
]['config-agent-account']) {
797 vnf
['config-agent-account'] = vnfdCloudAccounts
[k
]['config-agent-account'];
799 if(vnfdCloudAccounts
[k
].hasOwnProperty('datacenter')) {
800 vnf
['om-datacenter'] = vnfdCloudAccounts
[k
].datacenter
;
802 if(vnf
['om-datacenter'] || vnf
['cloud-account'] || vnf
['config-agent-account']) {
803 payload
['vnf-cloud-account-map'].push(vnf
);
807 payload
['ssh-authorized-key'] = this.state
.sshKeysRef
.map(function(k
) {
808 return {'key-pair-ref': JSON
.parse(k
).name
};
811 payload
['user'] = addKeyPairRefToUsers(this.state
.usersList
);
812 // console.log(payload)
820 function addKeyPairRefToUsers(list
) {
821 return list
.map(function(u
) {
824 'user-info': u
['user-info'],
825 'ssh-authorized-key': u
['ssh-authorized-key'].map(function(k
) {
834 function getMockSLA(id
) {
835 console
.log('Getting mock SLA Data for id: ' + id
);
837 sla_parameters
: slaData
841 function getMockData() {
842 console
.log('Getting mock Descriptor Data');
849 // export default Alt.createStore(LaunchNetworkServiceStore);
850 export default LaunchNetworkServiceStore
;