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 _cloneDeep
from 'lodash/cloneDeep';
24 import _find
from 'lodash/find';
27 class LaunchNetworkServiceStore
{
37 this.sshKeysList
= [];
39 this.sla_parameters
= [];
41 this.selectedNSD
= {};
42 this.selectedCloudAccount
= {};
43 this.dataCenters
= [];
44 this.cloudAccounts
= [];
45 this.isLoading
= false;
46 this.hasConfigureNSD
= false;
47 this['input-parameters'] = [];
48 this.displayPlacementGroups
= false;
50 this.bindActions(NetworkServiceActions
);
51 this.nsdConfiguration
= {
53 selectedCloudAccount
: {},
56 /*Collection of vnf state containting cloud account and datacenter info.
59 this.vnfdCloudAccounts
= {};
61 this.configAgentAccounts
= [];
63 this.isPreviewing
= false;
64 this.isOpenMano
= false;
65 this.registerAsync(NetworkServiceSource
);
66 this.exportPublicMethods({
67 getMockData
: getMockData
.bind(this),
68 getMockSLA
: getMockSLA
.bind(this),
69 saveNetworkServiceRecord
: this.saveNetworkServiceRecord
,
72 updateInputParam
: this.updateInputParam
,
73 resetView
: this.resetView
,
74 nameUpdated
: this.nameUpdated
,
76 descriptorSelected
: this.descriptorSelected
.bind(this),
77 deselectDescriptor
: this.deselectDescriptor
,
78 previewDescriptor
: this.previewDescriptor
,
80 ipProfileFn
: this.ipProfileFn
,
82 usersFn
: this.usersFn
,
89 console
.log('reseting state');
92 'input-parameter-xpath': null,
93 'ns-placement-groups': null,
94 'vnf-placement-groups':null,
101 getCatalogSuccess(catalogs
) {
107 catalogs
.forEach(function(catalog
) {
108 switch (catalog
.type
) {
112 self
.descriptorSelected(catalog
.descriptors
[0])
114 console
.log('unable to select catalog')
125 nsd
[0].descriptors
.map(function(n
) {
129 nsd
, vnfd
, pnfd
, nsdDict
,
133 getLaunchCloudAccountSuccess(cloudAccounts
) {
135 newState
.cloudAccounts
= cloudAccounts
.filter(function(v
) {
137 return v
['connection-status'].status
== 'success';
139 if(cloudAccounts
.length
!= newState
.cloudAccounts
.length
) {
140 Alt
.actions
.global
.showNotification
.defer({type
: 'warning', msg
: 'One or more VIM accounts have failed to connect'});
142 if(cloudAccounts
&& cloudAccounts
.length
> 0) {
143 newState
.selectedCloudAccount
= newState
.cloudAccounts
[0];
144 if (cloudAccounts
[0]['account-type'] == 'openstack') {
145 newState
.displayPlacementGroups
= true;
147 newState
.displayPlacementGroups
= false;
150 newState
.selectedCloudAccount
= {};
153 this.setState(newState
);
155 getConfigAgentSuccess(configAgentAccounts
) {
157 configAgentAccounts
: configAgentAccounts
160 getDataCentersSuccess(data
) {
161 let dataCenters
= data
;
164 dataCenters
: dataCenters
|| []
166 if (this.ro
&& this.ro
['account-type'] == 'openmano') {
167 newState
.dataCenterID
= dataCenters
[this.ro
.name
][0].uuid
169 this.setState(newState
)
171 getVDUSuccess(VNFD
) {
177 Alt
.actions
.global
.showScreenLoader
.defer();
181 console
.log('is Loading', this)
183 launchNSRSuccess(data
) {
184 console
.log('Launching Network Service')
185 let tokenizedHash
= window
.location
.hash
.split('/');
186 Alt
.actions
.global
.hideScreenLoader
.defer();
190 return window
.location
.hash
= 'launchpad/' + tokenizedHash
[2];
192 launchNSRError(data
) {
193 var msg
= 'Something went wrong while trying to instantiate. Check the error logs for more information';
200 Alt
.actions
.global
.showNotification
.defer(msg
);
201 Alt
.actions
.global
.hideScreenLoader
.defer();
206 getInstantiateSshKeySuccess
= (data
) => {
212 getResourceOrchestratorSuccess
= (data
) => {
213 Alt
.actions
.global
.hideScreenLoader
.defer();
218 getResourceOrchestratorError
= (data
) => {
219 console
.log('getResourceOrchestrator Error: ', data
)
222 nameUpdated
= (e
) => {
227 deselectDescriptor
= () => {
228 console
.log('deselecting')
235 descriptorSelected
= (data
) => {
240 selectedNSDid
: NSD
.id
,
241 vld
: NSD
&& NSD
.vld
&& NSD
.vld
.map(function(v
) {
242 //Adding a type for UI state management
243 //This is deleted before launch
244 if(v
['ip-profile-ref']) {
245 v
.type
= 'ip-profile-ref';
247 if(v
['vim-network-name']) {
248 v
.type
= 'vim-network-name';
255 ipProfiles
: NSD
['ip-profiles']
257 newState
.selectedNSD
= data
;
258 newState
['input-parameters'] = [];
259 if (NSD
['input-parameter-xpath']) {
260 newState
.hasConfigureNSD
= true;
261 NSD
['input-parameter-xpath'].map(function(p
) {
262 newState
.hasConfigureNSD
= true;
263 newState
['input-parameters'].push(_cloneDeep(p
));
266 newState
.hasConfigureNSD
= false;
267 newState
['input-parameters'] = null;
269 if(NSD
['ns-placement-groups'] && NSD
['ns-placement-groups'].length
> 0 ) {
270 newState
['ns-placement-groups'] = NSD
['ns-placement-groups'];
272 if(NSD
['vnf-placement-groups'] && NSD
['vnf-placement-groups'].length
> 0 ) {
273 newState
['vnf-placement-groups'] = NSD
['vnf-placement-groups'];
275 NSD
["constituent-vnfd"].map((v
) => {
276 VNFIDs
.push(v
["vnfd-id-ref"]);
278 this.getInstance().getVDU(VNFIDs
);
279 this.setState(newState
);
281 previewDescriptor
= (data
) => {
290 updateInputParam
= (i
, value
) => {
291 let ip
= this['input-parameters'];
294 'input-parameters': ip
300 updateSelectedCloudAccount
: (cloudAccount
) => {
301 let nsd
= self
.nsd
[0];
303 selectedCloudAccount
: JSON
.parse(cloudAccount
.target
.value
)
305 if (cloudAccount
['account-type'] == 'openstack') {
306 newState
.displayPlacementGroups
= true;
308 newState
.displayPlacementGroups
= false;
310 self
.setState(newState
);
312 updateSelectedDataCenter
: (dataCenter
) => {
314 dataCenterID
: JSON
.parse(dataCenter
.target
.value
)
317 placementGroupUpdate
: (i
, k
, value
) => {
318 let pg
= self
['ns-placement-groups'];
321 'ns-placement-groups': pg
324 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
325 let pg
= self
['ns-placement-groups'];
326 let ha
= pg
[pgi
]['host-aggregate'][hai
];
329 'ns-placement-groups': pg
332 addHostAggregate
: (pgi
) => {
333 let pg
= self
['ns-placement-groups'];
334 let ha
= pg
[pgi
]['host-aggregate'];
337 'ns-placement-groups': pg
340 removeHostAggregate
: (pgi
, hai
) => {
341 let pg
= self
['ns-placement-groups'];
342 let ha
= pg
[pgi
]['host-aggregate'];
345 'ns-placement-groups': pg
348 getNSDByID
: (id
) => {
356 placementGroupUpdate
: (i
, k
, value
) => {
357 let pg
= self
['vnf-placement-groups'];
360 'vnf-placement-groups': pg
363 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
364 let pg
= self
['vnf-placement-groups'];
365 let ha
= pg
[pgi
]['host-aggregate'][hai
];
368 'vnf-placement-groups': pg
371 addHostAggregate
: (pgi
) => {
372 let pg
= self
['vnf-placement-groups'];
373 let ha
= pg
[pgi
]['host-aggregate'];
376 'vnf-placement-groups': pg
379 removeHostAggregate
: (pgi
, hai
) => {
380 let pg
= self
['vnf-placement-groups'];
381 let ha
= pg
[pgi
]['host-aggregate'];
384 'vnf-placement-groups': pg
387 updateSelectedCloudAccount
: (id
, cloudAccount
) => {
388 let vnfCA
= self
.vnfdCloudAccounts
;
390 if(!vnfCA
.hasOwnProperty(id
)) {
393 vnfCA
[id
].account
= JSON
.parse(cloudAccount
.target
.value
);
395 if (cloudAccount
['account-type'] == 'openmano' && this.dataCenters
&& self
.dataCenters
[cloudAccount
['name']]) {
396 let datacenter
= self
.dataCenters
[cloudAccount
['name']][0];
397 vnfCA
[id
].datacenter
= datacenter
.uuid
;
399 if (vnfCA
[id
].datacenter
) {
400 delete vnfCA
[id
].datacenter
;
404 if(vnfCA
.hasOwnProperty(id
)) {
405 if(vnfCA
[id
].hasOwnProperty('config-agent-account')) {
406 delete vnfCA
[id
].account
;
413 vnfdCloudAccounts
: vnfCA
416 updateSelectedConfigAgent
: (id
) => {
418 let configAgentRef
= JSON
.parse(e
.target
.value
);
419 let vnfCA
= self
.vnfdCloudAccounts
;
421 if(!vnfCA
.hasOwnProperty(id
)) {
424 vnfCA
[id
]['config-agent-account'] = configAgentRef
;
426 if(vnfCA
[id
].hasOwnProperty('account')) {
427 delete vnfCA
[id
]['config-agent-account'];
433 vnfdCloudAccounts
: vnfCA
437 updateSelectedDataCenter
: (id
, dataCenter
) => {
438 let vnfCA
= self
.vnfdCloudAccounts
;
442 vnfCA
[id
].datacenter
= JSON
.parse(dataCenter
.target
.value
);
444 vnfdCloudAccounts
: vnfCA
454 let type
= e
.target
.value
;
456 if (vld
[i
].hasOwnProperty('type')) {
457 delete vld
[i
][vld
[i
].type
]
461 if(type
== 'ip-profile-ref') {
462 let IPProfile
= self
.ipProfiles
;
463 vld
[i
][type
] = IPProfile
[0] && IPProfile
[0].name
;
464 delete vld
[i
]['vim-network-name'];
466 delete vld
[i
]['dns-server'];
469 delete vld
[i
]['ip-profile-ref'];
470 delete vld
[i
]['vim-network-name'];
472 self
.setState({vld
:vld
});
475 updateValue
: (i
, type
) => {
477 // Select Option returns JSON values.
478 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
480 vld
[i
][type
] = value
;
481 self
.setState({vld
:vld
});
486 ipProfileFn
= () => {
489 updateProfile
: (i
, key
) => {
491 // Select Option returns JSON values.
492 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
493 self
.ipProfiles
[i
]['ip-profile-params'][key
] = value
;
496 // Don't send this key
497 delete self
.ipProfiles
[i
]['ip-profile-params'][key
];
500 self
.setState({ipProfiles
:self
.ipProfiles
});
503 updateVersion
: (i
) => {
505 // Select Option returns JSON values.
506 let value
= e
.target
.value
;
507 self
.ipProfiles
[i
]['ip-profile-params']['ip-version'] = value
;
508 self
.setState({ipProfiles
:self
.ipProfiles
});
511 updateDNS
: (i
, dnsIndex
) => {
513 // Select Option returns JSON values.
514 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
515 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][dnsIndex
] = value
;
516 self
.setState({ipProfiles
:self
.ipProfiles
});
519 updateDHCP
: (i
, property
) => {
521 let value
= e
.target
.value
;
522 //If value is meant to be boolean, convert it
523 if(value
== "true" || value
== "false") {
524 value
= JSON
.parse(value
);
526 if(!self
.ipProfiles
[i
]['ip-profile-params'].hasOwnProperty('dhcp-params')) {
527 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
533 //Removing DCHP property on disable to allow instantiation
535 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
539 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'][property
] = value
;
541 self
.setState({ipProfiles
:self
.ipProfiles
});
552 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server']) {
553 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].unshift({})
555 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'] = [{}];
558 self
.setState({ipProfiles
:self
.ipProfiles
});
561 removeDNS
: (i
, k
) => {
564 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].splice(k
, 1);
565 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].length
== 0) {
566 delete self
.ipProfiles
[i
]['ip-profile-params']['dns-server'];
568 self
.setState({ipProfiles
:self
.ipProfiles
});
571 updateDNS
: (i
, k
) => {
574 let value
= e
.target
.value
;
575 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][k
].address
= value
;
576 self
.setState({ipProfiles
:self
.ipProfiles
});
584 updateNewKeyRefSelection
: (e
) => {
586 newRefSelection
: e
.target
.value
589 updateKeyRef
: (refIndex
, remove
) => {
592 let sshKeysRef
= self
.sshKeysRef
;
594 // if(!e.target.value) {
595 // return Alt.actions.global.showError.defer('Please select a key pair');
597 if(!isNaN(refIndex
)){
598 sshKeysRef
.splice(refIndex
, 1);
599 sshKeysRef
.push(e
.target
.value
);
601 sshKeysRef
.push(e
.target
.value
);
605 sshKeysRef
.splice(refIndex
, 1);
608 sshKeysRef
: sshKeysRef
,
609 newRefSelection
: null
618 add: function(sshKeysList
) {
623 'ssh-authorized-key': [sshKeysList
[0].name
]
625 let usersList
= self
.usersList
;
626 usersList
.push(newUser
);
632 remove: function(i
) {
634 self
.usersList
.splice(i
, 1);
636 usersList
: self
.usersList
640 update: function(i
, key
) {
642 let value
= e
.target
.value
;
643 self
.usersList
[i
][key
] = value
;
645 usersList
: self
.usersList
649 updateSSHkeyRef: function(i
, j
, remove
){
651 let usersList
= _cloneDeep(self
.usersList
)
652 let keys
= usersList
[i
]['ssh-authorized-key'];
654 let keyRef
= JSON
.parse(e
.target
.value
).name
;
662 usersList
[i
]['ssh-authorized-key'] = keys
;
670 saveNetworkServiceRecord(name
, launch
) {
671 //input-parameter: [{uuid: < some_unique_name>, xpath: <same as you got from nsd>, value: <user_entered_value>}]
673 'input-parameter-xpath':[{
681 // Create a filtered NSD payload from the decorated one as RW.REST cannot handle extra parameters now
683 nsdPayload
= _cloneDeep(_find(this.state
.nsd
[0].descriptors
, {id
: this.state
.selectedNSDid
}));
685 if (nsdPayload
!= {}) {
686 nsdPayload
['meta'] && delete nsdPayload
['meta'];
687 nsdPayload
['constituent-vnfd'] && nsdPayload
['constituent-vnfd'].map((constituentVnfd
) => {
688 constituentVnfd
['vnf-name'] && delete constituentVnfd
['vnf-name'];
689 constituentVnfd
['name'] && delete constituentVnfd
['name'];
691 nsdPayload
['placement-groups'] && nsdPayload
['placement-groups'].map((placementGroup
) => {
692 placementGroup
['member-vnfd'] && placementGroup
['member-vnfd'].map((memberVnfd
) => {
693 memberVnfd
['name'] && delete memberVnfd
['name'];
696 nsdPayload
['ns-placement-groups'] && delete nsdPayload
['ns-placement-groups'];
697 nsdPayload
['vnf-placement-groups'] && delete nsdPayload
['vnf-placement-groups'];
698 nsdPayload
.vld
= this.state
.vld
;
699 nsdPayload
.vld
&& nsdPayload
.vld
.map(function(v
){
704 let vnfdCloudAccounts
= this.state
.vnfdCloudAccounts
;
709 "description": "a description for " + guuid
,
710 "admin-status": launch
? "ENABLED" : "DISABLED",
714 if (this.state
.ro
&& this.state
.ro
['account-type'] == 'openmano') {
715 payload
['om-datacenter'] = this.state
.dataCenterID
;
717 if(!this.state
.selectedCloudAccount
) {
718 Alt
.actions
.global
.showNotification
.defer("No VIM Account Selected");
721 payload
["cloud-account"] = this.state
.selectedCloudAccount
.name
;
723 //Clean Input Parameters
724 if (this.state
.hasConfigureNSD
) {
725 let ips
= _cloneDeep(this.state
['input-parameters']);
727 let ipsToSend
= ips
.filter(function(ip
) {
728 if (ip
.value
&& ip
.value
!= "") {
731 delete ip
['default-value'];
737 if (ipsToSend
.length
> 0) {
738 payload
['input-parameter'] = ipsToSend
;
741 // These placement groups need to be refactored. Too much boilerplate.
742 if (this.state
.displayPlacementGroups
) {
743 nsPg
= this.state
['ns-placement-groups'];
744 vnfPg
= this.state
['vnf-placement-groups'];
745 if(nsPg
&& (nsPg
.length
> 0)) {
746 payload
['nsd-placement-group-maps'] = nsPg
.map(function(n
, i
) {
747 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
749 'cloud-type': 'openstack'
751 if(n
['host-aggregate'].length
> 0) {
752 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
754 'metadata-key': h
.key
,
755 'metadata-value': h
.value
759 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
760 obj
['availability-zone'] = {name
: n
['availability-zone']};
762 if(n
['server-group'] && (n
['server-group'] != '')) {
763 obj
['server-group'] = {name
: n
['server-group']};
765 obj
['placement-group-ref'] = n
.name
;
768 }).filter(function(o
){
776 if(vnfPg
&& (vnfPg
.length
> 0)) {
777 payload
['vnfd-placement-group-maps'] = vnfPg
.map(function(n
, i
) {
778 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
780 'cloud-type': 'openstack'
782 if(n
['host-aggregate'].length
> 0) {
783 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
785 'metadata-key': h
.key
,
786 'metadata-value': h
.value
790 if(n
['server-group'] && (n
['server-group'] != '')) {
791 obj
['server-group'] = {name
: n
['server-group']};
793 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
794 obj
['availability-zone'] = {name
: n
['availability-zone']};
796 obj
['placement-group-ref'] = n
.name
;
797 obj
['vnfd-id-ref'] = n
['vnfd-id-ref'];
800 }).filter(function(o
){
809 //Construct VNF cloud accounts
810 payload
['vnf-cloud-account-map'] = [];
811 for(let k
in vnfdCloudAccounts
) {
813 vnf
['member-vnf-index-ref'] = k
;
814 if(vnfdCloudAccounts
[k
].hasOwnProperty('account') && (vnfdCloudAccounts
[k
]['account'] && vnfdCloudAccounts
[k
]['account'].name
)) {
815 vnf
['cloud-account'] = vnfdCloudAccounts
[k
].account
.name
;
817 if(vnfdCloudAccounts
[k
].hasOwnProperty('config-agent-account') && vnfdCloudAccounts
[k
]['config-agent-account']) {
818 vnf
['config-agent-account'] = vnfdCloudAccounts
[k
]['config-agent-account'];
820 if(vnfdCloudAccounts
[k
].hasOwnProperty('datacenter')) {
821 vnf
['om-datacenter'] = vnfdCloudAccounts
[k
].datacenter
;
823 if(vnf
['om-datacenter'] || vnf
['cloud-account'] || vnf
['config-agent-account']) {
824 payload
['vnf-cloud-account-map'].push(vnf
);
828 payload
['ssh-authorized-key'] = this.state
.sshKeysRef
.map(function(k
) {
829 return {'key-pair-ref': JSON
.parse(k
).name
};
832 payload
['user'] = addKeyPairRefToUsers(this.state
.usersList
);
833 // console.log(payload)
841 function addKeyPairRefToUsers(list
) {
842 return list
.map(function(u
) {
845 'user-info': u
['user-info'],
846 'ssh-authorized-key': u
['ssh-authorized-key'].map(function(k
) {
855 function getMockSLA(id
) {
856 console
.log('Getting mock SLA Data for id: ' + id
);
858 sla_parameters
: slaData
862 function getMockData() {
863 console
.log('Getting mock Descriptor Data');
870 export default LaunchNetworkServiceStore
;