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;
48 this.bindActions(NetworkServiceActions
);
49 this.nsdConfiguration
= {
51 selectedCloudAccount
: {},
54 /*Collection of vnf state containting cloud account and datacenter info.
57 this.vnfdCloudAccounts
= {};
59 this.configAgentAccounts
= [];
61 this.isPreviewing
= false;
62 this.registerAsync(NetworkServiceSource
);
63 this.exportPublicMethods({
64 getMockData
: getMockData
.bind(this),
65 getMockSLA
: getMockSLA
.bind(this),
66 saveNetworkServiceRecord
: this.saveNetworkServiceRecord
,
69 updateInputParam
: this.updateInputParam
,
70 resetView
: this.resetView
,
71 nameUpdated
: this.nameUpdated
,
73 descriptorSelected
: this.descriptorSelected
.bind(this),
74 deselectDescriptor
: this.deselectDescriptor
,
75 previewDescriptor
: this.previewDescriptor
,
77 ipProfileFn
: this.ipProfileFn
,
79 usersFn
: this.usersFn
,
86 console
.log('reseting state');
89 'input-parameter-xpath': null,
90 'ns-placement-groups': null,
91 'vnf-placement-groups':null,
98 getCatalogSuccess(catalogs
) {
104 catalogs
.forEach(function(catalog
) {
105 switch (catalog
.type
) {
109 self
.descriptorSelected(catalog
.descriptors
[0])
111 console
.log('unable to select catalog')
122 nsd
[0].descriptors
.map(function(n
) {
126 nsd
, vnfd
, pnfd
, nsdDict
,
130 getLaunchCloudAccountSuccess(cloudAccounts
) {
132 newState
.cloudAccounts
= cloudAccounts
|| [];
133 if(cloudAccounts
&& cloudAccounts
.length
> 0) {
134 newState
.selectedCloudAccount
= cloudAccounts
[0];
135 if (cloudAccounts
[0]['account-type'] == 'openstack') {
136 newState
.displayPlacementGroups
= true;
138 newState
.displayPlacementGroups
= false;
141 newState
.selectedCloudAccount
= {};
144 this.setState(newState
);
146 getConfigAgentSuccess(configAgentAccounts
) {
148 configAgentAccounts
: configAgentAccounts
151 getDataCentersSuccess(dataCenters
) {
153 dataCenters
: dataCenters
155 if (this.selectedCloudAccount
['account-type'] == 'openmano') {
156 newState
.dataCenterID
= dataCenters
[this.selectedCloudAccount
.name
][0].uuid
158 this.setState(newState
)
160 getVDUSuccess(VNFD
) {
166 Alt
.actions
.global
.showScreenLoader
.defer();
170 console
.log('is Loading', this)
172 launchNSRSuccess(data
) {
173 console
.log('Launching Network Service')
174 let tokenizedHash
= window
.location
.hash
.split('/');
175 Alt
.actions
.global
.hideScreenLoader
.defer();
179 return window
.location
.hash
= 'launchpad/' + tokenizedHash
[2];
181 launchNSRError(error
) {
182 Alt
.actions
.global
.showError
.defer('Something went wrong while trying to instantiate. Check the error logs for more information');
183 Alt
.actions
.global
.hideScreenLoader
.defer();
188 getInstantiateSshKeySuccess
= (data
) => {
195 nameUpdated
= (e
) => {
200 deselectDescriptor
= () => {
201 console
.log('deselecting')
208 descriptorSelected
= (data
) => {
213 selectedNSDid
: NSD
.id
,
214 vld
: NSD
&& NSD
.vld
&& NSD
.vld
.map(function(v
) {
215 //Adding a type for UI state management
216 //This is deleted before launch
217 if(v
['ip-profile-ref']) {
218 v
.type
= 'ip-profile-ref';
220 if(v
['vim-network-name']) {
221 v
.type
= 'vim-network-name';
228 ipProfiles
: NSD
['ip-profiles']
230 newState
.selectedNSD
= data
;
231 if (NSD
['input-parameter-xpath']) {
232 newState
.hasConfigureNSD
= true;
233 newState
['input-parameters'] = NSD
['input-parameter-xpath'];
235 newState
.hasConfigureNSD
= false;
236 newState
['input-parameters'] = null;
238 if(NSD
['ns-placement-groups'] && NSD
['ns-placement-groups'].length
> 0 ) {
239 newState
['ns-placement-groups'] = NSD
['ns-placement-groups'];
241 if(NSD
['vnf-placement-groups'] && NSD
['vnf-placement-groups'].length
> 0 ) {
242 newState
['vnf-placement-groups'] = NSD
['vnf-placement-groups'];
244 NSD
["constituent-vnfd"].map((v
) => {
245 VNFIDs
.push(v
["vnfd-id-ref"]);
247 this.getInstance().getVDU(VNFIDs
);
248 this.setState(newState
);
250 previewDescriptor
= (data
) => {
259 updateInputParam
= (i
, value
) => {
260 let ip
= this['input-parameters'];
263 'input-parameters': ip
269 updateSelectedCloudAccount
: (cloudAccount
) => {
270 let nsd
= self
.nsd
[0];
272 selectedCloudAccount
: cloudAccount
274 if (cloudAccount
['account-type'] == 'openstack') {
275 newState
.displayPlacementGroups
= true;
277 newState
.displayPlacementGroups
= false;
279 if (cloudAccount
['account-type'] == 'openmano' && this.dataCenters
&& self
.dataCenters
[cloudAccount
['name']]) {
280 let datacenter
= self
.dataCenters
[cloudAccount
['name']][0];
281 newState
.dataCenterID
= datacenter
.uuid
;
283 self
.setState(newState
);
285 updateSelectedDataCenter
: (dataCenter
) => {
287 dataCenterID
: dataCenter
290 placementGroupUpdate
: (i
, k
, value
) => {
291 let pg
= self
['ns-placement-groups'];
294 'ns-placement-groups': pg
297 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
298 let pg
= self
['ns-placement-groups'];
299 let ha
= pg
[pgi
]['host-aggregate'][hai
];
302 'ns-placement-groups': pg
305 addHostAggregate
: (pgi
) => {
306 let pg
= self
['ns-placement-groups'];
307 let ha
= pg
[pgi
]['host-aggregate'];
310 'ns-placement-groups': pg
313 removeHostAggregate
: (pgi
, hai
) => {
314 let pg
= self
['ns-placement-groups'];
315 let ha
= pg
[pgi
]['host-aggregate'];
318 'ns-placement-groups': pg
321 getNSDByID
: (id
) => {
329 placementGroupUpdate
: (i
, k
, value
) => {
330 let pg
= self
['vnf-placement-groups'];
333 'vnf-placement-groups': pg
336 hostAggregateUpdate
: (pgi
, hai
, k
, value
) => {
337 let pg
= self
['vnf-placement-groups'];
338 let ha
= pg
[pgi
]['host-aggregate'][hai
];
341 'vnf-placement-groups': pg
344 addHostAggregate
: (pgi
) => {
345 let pg
= self
['vnf-placement-groups'];
346 let ha
= pg
[pgi
]['host-aggregate'];
349 'vnf-placement-groups': pg
352 removeHostAggregate
: (pgi
, hai
) => {
353 let pg
= self
['vnf-placement-groups'];
354 let ha
= pg
[pgi
]['host-aggregate'];
357 'vnf-placement-groups': pg
360 updateSelectedCloudAccount
: (id
, cloudAccount
) => {
361 let vnfCA
= self
.vnfdCloudAccounts
;
363 if(!vnfCA
.hasOwnProperty(id
)) {
366 vnfCA
[id
].account
= cloudAccount
;
368 if (cloudAccount
['account-type'] == 'openmano' && this.dataCenters
&& self
.dataCenters
[cloudAccount
['name']]) {
369 let datacenter
= self
.dataCenters
[cloudAccount
['name']][0];
370 vnfCA
[id
].datacenter
= datacenter
.uuid
;
372 if (vnfCA
[id
].datacenter
) {
373 delete vnfCA
[id
].datacenter
;
377 if(vnfCA
.hasOwnProperty(id
)) {
378 if(vnfCA
[id
].hasOwnProperty('config-agent-account')) {
379 delete vnfCA
[id
].account
;
386 vnfdCloudAccounts
: vnfCA
389 updateSelectedConfigAgent
: (id
) => {
391 let configAgentRef
= JSON
.parse(e
.target
.value
);
392 let vnfCA
= self
.vnfdCloudAccounts
;
394 if(!vnfCA
.hasOwnProperty(id
)) {
397 vnfCA
[id
]['config-agent-account'] = configAgentRef
;
399 if(vnfCA
[id
].hasOwnProperty('account')) {
400 delete vnfCA
[id
]['config-agent-account'];
406 vnfdCloudAccounts
: vnfCA
410 updateSelectedDataCenter
: (id
, dataCenter
) => {
411 let vnfCA
= self
.vnfdCloudAccounts
;
412 vnfCA
[id
].datacenter
= dataCenter
;
414 vnfdCloudAccounts
: vnfCA
424 let type
= e
.target
.value
;
426 if (vld
[i
].hasOwnProperty('type')) {
427 delete vld
[i
][vld
[i
].type
]
431 if(type
== 'ip-profile-ref') {
432 let IPProfile
= self
.ipProfiles
;
433 vld
[i
][type
] = IPProfile
[0] && IPProfile
[0].name
;
434 delete vld
[i
]['vim-network-name'];
436 delete vld
[i
]['dns-server'];
438 if(type
== 'unknown') {
439 delete vld
[i
]['ip-profile-ref'];
440 delete vld
[i
]['vim-network-name'];
442 self
.setState({vld
:vld
});
445 updateValue
: (i
, type
) => {
447 // Select Option returns JSON values.
448 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
450 vld
[i
][type
] = value
;
451 self
.setState({vld
:vld
});
456 ipProfileFn
= () => {
459 updateProfile
: (i
, key
) => {
461 // Select Option returns JSON values.
462 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
463 self
.ipProfiles
[i
]['ip-profile-params'][key
] = value
;
465 self
.setState({ipProfiles
:self
.ipProfiles
});
468 updateVersion
: (i
) => {
470 // Select Option returns JSON values.
471 let value
= e
.target
.value
;
472 self
.ipProfiles
[i
]['ip-profile-params']['ip-version'] = value
;
473 self
.setState({ipProfiles
:self
.ipProfiles
});
476 updateDNS
: (i
, dnsIndex
) => {
478 // Select Option returns JSON values.
479 let value
= e
.target
.nodeName
== "SELECT" ? JSON
.parse(e
.target
.value
) : e
.target
.value
;
480 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'][dnsIndex
] = value
;
481 self
.setState({ipProfiles
:self
.ipProfiles
});
484 updateDHCP
: (i
, property
) => {
486 let value
= e
.target
.value
;
487 //If value is meant to be boolean, convert it
488 if(value
== "true" || value
== "false") {
489 value
= JSON
.parse(value
);
491 if(!self
.ipProfiles
[i
]['ip-profile-params'].hasOwnProperty('dhcp-params')) {
492 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'] = {
498 //Removing DCHP property on disable to allow instantiation
500 delete self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'];
502 self
.ipProfiles
[i
]['ip-profile-params']['dhcp-params'][property
] = value
;
504 self
.setState({ipProfiles
:self
.ipProfiles
});
515 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server']) {
516 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].unshift('')
518 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'] = [''];
521 self
.setState({ipProfiles
:self
.ipProfiles
});
524 removeDNS
: (i
, k
) => {
527 self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].splice(k
, 1);
528 if(self
.ipProfiles
[i
]['ip-profile-params']['dns-server'].length
== 0) {
529 delete self
.ipProfiles
[i
]['ip-profile-params']['dns-server'];
531 self
.setState({ipProfiles
:self
.ipProfiles
});
539 updateNewKeyRefSelection
: (e
) => {
541 newRefSelection
: e
.target
.value
544 updateKeyRef
: (refIndex
, remove
) => {
547 let sshKeysRef
= self
.sshKeysRef
;
549 // if(!e.target.value) {
550 // return Alt.actions.global.showError.defer('Please select a key pair');
552 if(!isNaN(refIndex
)){
553 sshKeysRef
.splice(refIndex
, 1);
554 sshKeysRef
.push(e
.target
.value
);
556 sshKeysRef
.push(e
.target
.value
);
560 sshKeysRef
.splice(refIndex
, 1);
563 sshKeysRef
: sshKeysRef
,
564 newRefSelection
: null
574 console
.log('adding user')
580 let usersList
= self
.usersList
;
581 usersList
.push(newUser
);
586 remove: function(i
) {
588 self
.usersList
.splice(i
, 1);
590 usersList
: self
.usersList
594 update: function(i
, key
) {
596 let value
= e
.target
.value
;
597 self
.usersList
[i
][key
] = value
;
599 usersList
: self
.usersList
605 saveNetworkServiceRecord(name
, launch
) {
606 //input-parameter: [{uuid: < some_unique_name>, xpath: <same as you got from nsd>, value: <user_entered_value>}]
608 'input-parameter-xpath':[{
616 // Create a filtered NSD payload from the decorated one as RW.REST cannot handle extra parameters now
618 nsdPayload
= _
.cloneDeep(_
.find(this.state
.nsd
[0].descriptors
, {id
: this.state
.selectedNSDid
}));
620 if (nsdPayload
!= {}) {
621 nsdPayload
['meta'] && delete nsdPayload
['meta'];
622 nsdPayload
['constituent-vnfd'] && nsdPayload
['constituent-vnfd'].map((constituentVnfd
) => {
623 constituentVnfd
['vnf-name'] && delete constituentVnfd
['vnf-name'];
624 constituentVnfd
['name'] && delete constituentVnfd
['name'];
626 nsdPayload
['placement-groups'] && nsdPayload
['placement-groups'].map((placementGroup
) => {
627 placementGroup
['member-vnfd'] && placementGroup
['member-vnfd'].map((memberVnfd
) => {
628 memberVnfd
['name'] && delete memberVnfd
['name'];
631 nsdPayload
['ns-placement-groups'] && delete nsdPayload
['ns-placement-groups'];
632 nsdPayload
['vnf-placement-groups'] && delete nsdPayload
['vnf-placement-groups'];
633 nsdPayload
.vld
= this.state
.vld
;
634 nsdPayload
.vld
&& nsdPayload
.vld
.map(function(v
){
639 let vnfdCloudAccounts
= this.state
.vnfdCloudAccounts
;
644 "description": "a description for " + guuid
,
645 "admin-status": launch
? "ENABLED" : "DISABLED",
648 payload
["cloud-account"] = this.state
.selectedCloudAccount
.name
;
649 if (this.state
.selectedCloudAccount
['account-type'] == "openmano") {
650 payload
['om-datacenter'] = this.state
.dataCenterID
;
652 if (this.state
.hasConfigureNSD
) {
653 let ips
= this.state
['input-parameters'];
654 let ipsToSend
= ips
.filter(function(ip
) {
655 if (ip
.value
&& ip
.value
!= "") {
662 if (ipsToSend
.length
> 0) {
663 payload
['input-parameter'] = ipsToSend
;
666 // These placement groups need to be refactored. Too much boilerplate.
667 if (this.state
.displayPlacementGroups
) {
668 nsPg
= this.state
['ns-placement-groups'];
669 vnfPg
= this.state
['vnf-placement-groups'];
670 if(nsPg
&& (nsPg
.length
> 0)) {
671 payload
['nsd-placement-group-maps'] = nsPg
.map(function(n
, i
) {
672 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
674 'cloud-type': 'openstack'
676 if(n
['host-aggregate'].length
> 0) {
677 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
679 'metadata-key': h
.key
,
680 'metadata-value': h
.value
684 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
685 obj
['availability-zone'] = {name
: n
['availability-zone']};
687 if(n
['server-group'] && (n
['server-group'] != '')) {
688 obj
['server-group'] = {name
: n
['server-group']};
690 obj
['placement-group-ref'] = n
.name
;
693 }).filter(function(o
){
701 if(vnfPg
&& (vnfPg
.length
> 0)) {
702 payload
['vnfd-placement-group-maps'] = vnfPg
.map(function(n
, i
) {
703 if(n
['availability-zone'] || n
['server-group'] || (n
['host-aggregate'].length
> 0)) {
705 'cloud-type': 'openstack'
707 if(n
['host-aggregate'].length
> 0) {
708 obj
['host-aggregate'] = n
['host-aggregate'].map(function(h
, j
) {
710 'metadata-key': h
.key
,
711 'metadata-value': h
.value
715 if(n
['server-group'] && (n
['server-group'] != '')) {
716 obj
['server-group'] = {name
: n
['server-group']};
718 if(n
['availability-zone'] && (n
['availability-zone'] != '')) {
719 obj
['availability-zone'] = {name
: n
['availability-zone']};
721 obj
['placement-group-ref'] = n
.name
;
722 obj
['vnfd-id-ref'] = n
['vnfd-id-ref'];
725 }).filter(function(o
){
734 //Construct VNF cloud accounts
735 payload
['vnf-cloud-account-map'] = [];
736 for(let k
in vnfdCloudAccounts
) {
738 vnf
['member-vnf-index-ref'] = k
;
739 if(vnfdCloudAccounts
[k
].hasOwnProperty('account') && (vnfdCloudAccounts
[k
]['account'] && vnfdCloudAccounts
[k
]['account'].name
)) {
740 vnf
['cloud-account'] = vnfdCloudAccounts
[k
].account
.name
;
742 if(vnfdCloudAccounts
[k
].hasOwnProperty('config-agent-account') && vnfdCloudAccounts
[k
]['config-agent-account']) {
743 vnf
['config-agent-account'] = vnfdCloudAccounts
[k
]['config-agent-account'];
745 if(vnfdCloudAccounts
[k
].hasOwnProperty('datacenter')) {
746 vnf
['om-datacenter'] = vnfdCloudAccounts
[k
].datacenter
;
748 payload
['vnf-cloud-account-map'].push(vnf
);
751 payload
['ssh-authorized-key'] = this.state
.sshKeysRef
.map(function(k
) {
752 return {'key-pair-ref': JSON
.parse(k
).name
};
755 payload
['user'] = this.state
.usersList
;
764 function getMockSLA(id
) {
765 console
.log('Getting mock SLA Data for id: ' + id
);
767 sla_parameters
: slaData
771 function getMockData() {
772 console
.log('Getting mock Descriptor Data');
779 // export default Alt.createStore(LaunchNetworkServiceStore);
780 export default LaunchNetworkServiceStore
;