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
|| [];
135 if(cloudAccounts
&& cloudAccounts
.length
> 0) {
136 newState
.selectedCloudAccount
= cloudAccounts
[0];
137 if (cloudAccounts
[0]['account-type'] == 'openstack') {
138 newState
.displayPlacementGroups
= true;
140 newState
.displayPlacementGroups
= false;
143 newState
.selectedCloudAccount
= {};
146 this.setState(newState
);
148 getConfigAgentSuccess(configAgentAccounts
) {
150 configAgentAccounts
: configAgentAccounts
153 getDataCentersSuccess(data
) {
154 let dataCenters
= data
;
157 dataCenters
: dataCenters
|| []
159 if (this.state
.ro
&& this.state
.ro
['account-type'] == 'openmano') {
160 newState
.dataCenterID
= dataCenters
[this.ro
.name
][0].uuid
162 this.setState(newState
)
164 getVDUSuccess(VNFD
) {
170 Alt
.actions
.global
.showScreenLoader
.defer();
174 console
.log('is Loading', this)
176 launchNSRSuccess(data
) {
177 console
.log('Launching Network Service')
178 let tokenizedHash
= window
.location
.hash
.split('/');
179 Alt
.actions
.global
.hideScreenLoader
.defer();
183 return window
.location
.hash
= 'launchpad/' + tokenizedHash
[2];
185 launchNSRError(error
) {
186 var msg
= 'Something went wrong while trying to instantiate. Check the error logs for more information';
190 Alt
.actions
.global
.showNotification
.defer(msg
);
191 Alt
.actions
.global
.hideScreenLoader
.defer();
196 getInstantiateSshKeySuccess
= (data
) => {
202 getResourceOrchestratorSuccess
= (data
) => {
203 Alt
.actions
.global
.hideScreenLoader
.defer();
208 getResourceOrchestratorError
= (data
) => {
209 console
.log('getResourceOrchestrator Error: ', data
)
212 nameUpdated
= (e
) => {
217 deselectDescriptor
= () => {
218 console
.log('deselecting')
225 descriptorSelected
= (data
) => {
230 selectedNSDid
: NSD
.id
,
231 vld
: NSD
&& NSD
.vld
&& NSD
.vld
.map(function(v
) {
232 //Adding a type for UI state management
233 //This is deleted before launch
234 if(v
['ip-profile-ref']) {
235 v
.type
= 'ip-profile-ref';
237 if(v
['vim-network-name']) {
238 v
.type
= 'vim-network-name';
245 ipProfiles
: NSD
['ip-profiles']
247 newState
.selectedNSD
= data
;
248 if (NSD
['input-parameter-xpath']) {
249 newState
.hasConfigureNSD
= true;
250 newState
['input-parameters'] = NSD
['input-parameter-xpath'];
252 newState
.hasConfigureNSD
= false;
253 newState
['input-parameters'] = null;
255 if(NSD
['ns-placement-groups'] && NSD
['ns-placement-groups'].length
> 0 ) {
256 newState
['ns-placement-groups'] = NSD
['ns-placement-groups'];
258 if(NSD
['vnf-placement-groups'] && NSD
['vnf-placement-groups'].length
> 0 ) {
259 newState
['vnf-placement-groups'] = NSD
['vnf-placement-groups'];
261 NSD
["constituent-vnfd"].map((v
) => {
262 VNFIDs
.push(v
["vnfd-id-ref"]);
264 this.getInstance().getVDU(VNFIDs
);
265 this.setState(newState
);
267 previewDescriptor
= (data
) => {
276 updateInputParam
= (i
, value
) => {
277 let ip
= this['input-parameters'];
280 'input-parameters': ip
286 updateSelectedCloudAccount
: (cloudAccount
) => {
287 let nsd
= self
.nsd
[0];
289 selectedCloudAccount
: JSON
.parse(cloudAccount
.target
.value
)
291 if (cloudAccount
['account-type'] == 'openstack') {
292 newState
.displayPlacementGroups
= true;
294 newState
.displayPlacementGroups
= false;
296 self
.setState(newState
);
298 updateSelectedDataCenter
: (dataCenter
) => {
300 dataCenterID
: JSON
.parse(dataCenter
.target
.value
)
303 placementGroupUpdate
: (i
, k
, value
) => {
304 let pg
= self
['ns-placement-groups'];
307 'ns-placement-groups': pg
310 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
311 let pg
= self
['ns-placement-groups'];
312 let ha
= pg
[pgi
]['host-aggregate'][hai
];
315 'ns-placement-groups': pg
318 addHostAggregate
: (pgi
) => {
319 let pg
= self
['ns-placement-groups'];
320 let ha
= pg
[pgi
]['host-aggregate'];
323 'ns-placement-groups': pg
326 removeHostAggregate
: (pgi
, hai
) => {
327 let pg
= self
['ns-placement-groups'];
328 let ha
= pg
[pgi
]['host-aggregate'];
331 'ns-placement-groups': pg
334 getNSDByID
: (id
) => {
342 placementGroupUpdate
: (i
, k
, value
) => {
343 let pg
= self
['vnf-placement-groups'];
346 'vnf-placement-groups': pg
349 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
350 let pg
= self
['vnf-placement-groups'];
351 let ha
= pg
[pgi
]['host-aggregate'][hai
];
354 'vnf-placement-groups': pg
357 addHostAggregate
: (pgi
) => {
358 let pg
= self
['vnf-placement-groups'];
359 let ha
= pg
[pgi
]['host-aggregate'];
362 'vnf-placement-groups': pg
365 removeHostAggregate
: (pgi
, hai
) => {
366 let pg
= self
['vnf-placement-groups'];
367 let ha
= pg
[pgi
]['host-aggregate'];
370 'vnf-placement-groups': pg
373 updateSelectedCloudAccount
: (id
, cloudAccount
) => {
374 let vnfCA
= self
.vnfdCloudAccounts
;
376 if(!vnfCA
.hasOwnProperty(id
)) {
379 vnfCA
[id
].account
= JSON
.parse(cloudAccount
.target
.value
);
381 if (cloudAccount
['account-type'] == 'openmano' && this.dataCenters
&& self
.dataCenters
[cloudAccount
['name']]) {
382 let datacenter
= self
.dataCenters
[cloudAccount
['name']][0];
383 vnfCA
[id
].datacenter
= datacenter
.uuid
;
385 if (vnfCA
[id
].datacenter
) {
386 delete vnfCA
[id
].datacenter
;
390 if(vnfCA
.hasOwnProperty(id
)) {
391 if(vnfCA
[id
].hasOwnProperty('config-agent-account')) {
392 delete vnfCA
[id
].account
;
399 vnfdCloudAccounts
: vnfCA
402 updateSelectedConfigAgent
: (id
) => {
404 let configAgentRef
= JSON
.parse(e
.target
.value
);
405 let vnfCA
= self
.vnfdCloudAccounts
;
407 if(!vnfCA
.hasOwnProperty(id
)) {
410 vnfCA
[id
]['config-agent-account'] = configAgentRef
;
412 if(vnfCA
[id
].hasOwnProperty('account')) {
413 delete vnfCA
[id
]['config-agent-account'];
419 vnfdCloudAccounts
: vnfCA
423 updateSelectedDataCenter
: (id
, dataCenter
) => {
424 let vnfCA
= self
.vnfdCloudAccounts
;
428 vnfCA
[id
].datacenter
= JSON
.parse(dataCenter
.target
.value
);
430 vnfdCloudAccounts
: vnfCA
440 let type
= e
.target
.value
;
442 if (vld
[i
].hasOwnProperty('type')) {
443 delete vld
[i
][vld
[i
].type
]
447 if(type
== 'ip-profile-ref') {
448 let IPProfile
= self
.ipProfiles
;
449 vld
[i
][type
] = IPProfile
[0] && IPProfile
[0].name
;
450 delete vld
[i
]['vim-network-name'];
452 delete vld
[i
]['dns-server'];
455 delete vld
[i
]['ip-profile-ref'];
456 delete vld
[i
]['vim-network-name'];
458 self
.setState({vld
:vld
});
461 updateValue
: (i
, type
) => {
463 // Select Option returns JSON values.
464 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
466 vld
[i
][type
] = value
;
467 self
.setState({vld
:vld
});
472 ipProfileFn
= () => {
475 updateProfile
: (i
, key
) => {
477 // Select Option returns JSON values.
478 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
479 self
.ipProfiles
[i
]['ip-profile-params'][key
] = value
;
481 self
.setState({ipProfiles
:self
.ipProfiles
});
484 updateVersion
: (i
) => {
486 // Select Option returns JSON values.
487 let value
= e
.target
.value
;
488 self
.ipProfiles
[i
]['ip-profile-params']['ip-version'] = value
;
489 self
.setState({ipProfiles
:self
.ipProfiles
});
492 updateDNS
: (i
, dnsIndex
) => {
494 // Select Option returns JSON values.
495 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
496 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][dnsIndex
] = value
;
497 self
.setState({ipProfiles
:self
.ipProfiles
});
500 updateDHCP
: (i
, property
) => {
502 let value
= e
.target
.value
;
503 //If value is meant to be boolean, convert it
504 if(value
== "true" || value
== "false") {
505 value
= JSON
.parse(value
);
507 if(!self
.ipProfiles
[i
]['ip-profile-params'].hasOwnProperty('dhcp-params')) {
508 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
514 //Removing DCHP property on disable to allow instantiation
516 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
520 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'][property
] = value
;
522 self
.setState({ipProfiles
:self
.ipProfiles
});
533 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server']) {
534 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].unshift({})
536 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'] = [{}];
539 self
.setState({ipProfiles
:self
.ipProfiles
});
542 removeDNS
: (i
, k
) => {
545 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].splice(k
, 1);
546 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].length
== 0) {
547 delete self
.ipProfiles
[i
]['ip-profile-params']['dns-server'];
549 self
.setState({ipProfiles
:self
.ipProfiles
});
552 updateDNS
: (i
, k
) => {
555 let value
= e
.target
.value
;
556 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][k
].address
= value
;
557 self
.setState({ipProfiles
:self
.ipProfiles
});
565 updateNewKeyRefSelection
: (e
) => {
567 newRefSelection
: e
.target
.value
570 updateKeyRef
: (refIndex
, remove
) => {
573 let sshKeysRef
= self
.sshKeysRef
;
575 // if(!e.target.value) {
576 // return Alt.actions.global.showError.defer('Please select a key pair');
578 if(!isNaN(refIndex
)){
579 sshKeysRef
.splice(refIndex
, 1);
580 sshKeysRef
.push(e
.target
.value
);
582 sshKeysRef
.push(e
.target
.value
);
586 sshKeysRef
.splice(refIndex
, 1);
589 sshKeysRef
: sshKeysRef
,
590 newRefSelection
: null
599 add: function(sshKeysList
) {
604 'ssh-authorized-key': [sshKeysList
[0].name
]
606 let usersList
= self
.usersList
;
607 usersList
.push(newUser
);
613 remove: function(i
) {
615 self
.usersList
.splice(i
, 1);
617 usersList
: self
.usersList
621 update: function(i
, key
) {
623 let value
= e
.target
.value
;
624 self
.usersList
[i
][key
] = value
;
626 usersList
: self
.usersList
630 updateSSHkeyRef: function(i
, j
, remove
){
632 let usersList
= _
.cloneDeep(self
.usersList
)
633 let keys
= usersList
[i
]['ssh-authorized-key'];
635 let keyRef
= JSON
.parse(e
.target
.value
).name
;
643 usersList
[i
]['ssh-authorized-key'] = keys
;
651 saveNetworkServiceRecord(name
, launch
) {
652 //input-parameter: [{uuid: < some_unique_name>, xpath: <same as you got from nsd>, value: <user_entered_value>}]
654 'input-parameter-xpath':[{
662 // Create a filtered NSD payload from the decorated one as RW.REST cannot handle extra parameters now
664 nsdPayload
= _
.cloneDeep(_
.find(this.state
.nsd
[0].descriptors
, {id
: this.state
.selectedNSDid
}));
666 if (nsdPayload
!= {}) {
667 nsdPayload
['meta'] && delete nsdPayload
['meta'];
668 nsdPayload
['constituent-vnfd'] && nsdPayload
['constituent-vnfd'].map((constituentVnfd
) => {
669 constituentVnfd
['vnf-name'] && delete constituentVnfd
['vnf-name'];
670 constituentVnfd
['name'] && delete constituentVnfd
['name'];
672 nsdPayload
['placement-groups'] && nsdPayload
['placement-groups'].map((placementGroup
) => {
673 placementGroup
['member-vnfd'] && placementGroup
['member-vnfd'].map((memberVnfd
) => {
674 memberVnfd
['name'] && delete memberVnfd
['name'];
677 nsdPayload
['ns-placement-groups'] && delete nsdPayload
['ns-placement-groups'];
678 nsdPayload
['vnf-placement-groups'] && delete nsdPayload
['vnf-placement-groups'];
679 nsdPayload
.vld
= this.state
.vld
;
680 nsdPayload
.vld
&& nsdPayload
.vld
.map(function(v
){
685 let vnfdCloudAccounts
= this.state
.vnfdCloudAccounts
;
690 "description": "a description for " + guuid
,
691 "admin-status": launch
? "ENABLED" : "DISABLED",
695 if (this.state
.ro
&& this.state
.ro
['account-type'] == 'openmano') {
696 payload
['om-datacenter'] = this.state
.dataCenterID
;
698 payload
["cloud-account"] = this.state
.selectedCloudAccount
.name
;
700 if (this.state
.hasConfigureNSD
) {
701 let ips
= this.state
['input-parameters'];
702 let ipsToSend
= ips
.filter(function(ip
) {
703 if (ip
.value
&& ip
.value
!= "") {
710 if (ipsToSend
.length
> 0) {
711 payload
['input-parameter'] = ipsToSend
;
714 // These placement groups need to be refactored. Too much boilerplate.
715 if (this.state
.displayPlacementGroups
) {
716 nsPg
= this.state
['ns-placement-groups'];
717 vnfPg
= this.state
['vnf-placement-groups'];
718 if(nsPg
&& (nsPg
.length
> 0)) {
719 payload
['nsd-placement-group-maps'] = nsPg
.map(function(n
, i
) {
720 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
722 'cloud-type': 'openstack'
724 if(n
['host-aggregate'].length
> 0) {
725 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
727 'metadata-key': h
.key
,
728 'metadata-value': h
.value
732 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
733 obj
['availability-zone'] = {name
: n
['availability-zone']};
735 if(n
['server-group'] && (n
['server-group'] != '')) {
736 obj
['server-group'] = {name
: n
['server-group']};
738 obj
['placement-group-ref'] = n
.name
;
741 }).filter(function(o
){
749 if(vnfPg
&& (vnfPg
.length
> 0)) {
750 payload
['vnfd-placement-group-maps'] = vnfPg
.map(function(n
, i
) {
751 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
753 'cloud-type': 'openstack'
755 if(n
['host-aggregate'].length
> 0) {
756 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
758 'metadata-key': h
.key
,
759 'metadata-value': h
.value
763 if(n
['server-group'] && (n
['server-group'] != '')) {
764 obj
['server-group'] = {name
: n
['server-group']};
766 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
767 obj
['availability-zone'] = {name
: n
['availability-zone']};
769 obj
['placement-group-ref'] = n
.name
;
770 obj
['vnfd-id-ref'] = n
['vnfd-id-ref'];
773 }).filter(function(o
){
782 //Construct VNF cloud accounts
783 payload
['vnf-cloud-account-map'] = [];
784 for(let k
in vnfdCloudAccounts
) {
786 vnf
['member-vnf-index-ref'] = k
;
787 if(vnfdCloudAccounts
[k
].hasOwnProperty('account') && (vnfdCloudAccounts
[k
]['account'] && vnfdCloudAccounts
[k
]['account'].name
)) {
788 vnf
['cloud-account'] = vnfdCloudAccounts
[k
].account
.name
;
790 if(vnfdCloudAccounts
[k
].hasOwnProperty('config-agent-account') && vnfdCloudAccounts
[k
]['config-agent-account']) {
791 vnf
['config-agent-account'] = vnfdCloudAccounts
[k
]['config-agent-account'];
793 if(vnfdCloudAccounts
[k
].hasOwnProperty('datacenter')) {
794 vnf
['om-datacenter'] = vnfdCloudAccounts
[k
].datacenter
;
796 if(vnf
['om-datacenter'] || vnf
['cloud-account'] || vnf
['config-agent-account']) {
797 payload
['vnf-cloud-account-map'].push(vnf
);
801 payload
['ssh-authorized-key'] = this.state
.sshKeysRef
.map(function(k
) {
802 return {'key-pair-ref': JSON
.parse(k
).name
};
805 payload
['user'] = addKeyPairRefToUsers(this.state
.usersList
);
806 // console.log(payload)
814 function addKeyPairRefToUsers(list
) {
815 return list
.map(function(u
) {
818 'user-info': u
['user-info'],
819 'ssh-authorized-key': u
['ssh-authorized-key'].map(function(k
) {
828 function getMockSLA(id
) {
829 console
.log('Getting mock SLA Data for id: ' + id
);
831 sla_parameters
: slaData
835 function getMockData() {
836 console
.log('Getting mock Descriptor Data');
843 // export default Alt.createStore(LaunchNetworkServiceStore);
844 export default LaunchNetworkServiceStore
;