2e21bbc06ca27f818cafa471f29957a9349ed269
[osm/riftware.git] /
1 /*
2  * STANDARD_RIFT_IO_COPYRIGHT
3  */
4 import NetworkServiceActions from './launchNetworkServiceActions.js';
5 import NetworkServiceSource from './launchNetworkServiceSource.js';
6 import GUID from 'utils/guid.js';
7 import AppHeaderActions from 'widgets/header/headerActions.js';
8 import Alt from '../alt';
9
10
11 class LaunchNetworkServiceStore {
12     constructor() {
13         this.nsd = [];
14         this.vnfd = [];
15         this.pnfd = [];
16         this.name = "";
17         this.sla_parameters = [];
18         this.selectedNSDid;
19         this.selectedCloudAccount = {};
20         this.dataCenters = {};
21         this.cloudAccounts = [];
22         this.isLoading = false;
23         this.isStandAlone = false;
24         this.hasConfigureNSD = false;
25         this['input-parameters'] = [];
26         this.registerAsync(NetworkServiceSource);
27         this.exportPublicMethods({
28             getMockData: getMockData.bind(this),
29             getMockSLA: getMockSLA.bind(this),
30             saveNetworkServiceRecord: this.saveNetworkServiceRecord,
31             updateSelectedCloudAccount: this.updateSelectedCloudAccount,
32             updateSelectedDataCenter: this.updateSelectedDataCenter,
33             updateInputParam: this.updateInputParam,
34             resetView: this.resetView,
35             nameUpdated: this.nameUpdated
36         });
37         this.bindActions(NetworkServiceActions);
38         this.descriptorSelected = this.descriptorSelected.bind(this);
39         this.nameUpdated = this.nameUpdated.bind(this);
40     }
41     nameUpdated = (name) => {
42         this.setState({
43             name: name
44         })
45     }
46     updateSelectedCloudAccount = (cloudAccount) => {
47         var newState = {
48             selectedCloudAccount: cloudAccount
49         };
50         if (cloudAccount['account-type'] == 'openmano') {
51             let datacenter = this.dataCenters[cloudAccount['name']][0];
52             // newState.selectedDataCenter = datacenter;
53             newState.dataCenterID = datacenter.uuid;
54
55         }
56         this.setState(newState);
57     }
58     updateSelectedDataCenter = (dataCenter) => {
59         console.log('updateed', dataCenter)
60         this.setState({
61             dataCenterID: dataCenter
62         });
63     }
64     resetView = () => {
65         console.log('reseting state')
66         this.setState({
67             name: ''
68         })
69     }
70     descriptorSelected(data) {
71         let NSD = data[0];
72         let VNFIDs = [];
73
74         let newState = {
75             selectedNSDid: NSD.id
76         };
77         //['input-parameter-xpath']
78         if (NSD['input-parameter-xpath']) {
79             newState.hasConfigureNSD = true;
80             newState['input-parameters'] = NSD['input-parameter-xpath'];
81         } else {
82             newState.hasConfigureNSD = false;
83             newState['input-parameters'] = null;
84         }
85         NSD["constituent-vnfd"].map((v) => {
86             VNFIDs.push(v["vnfd-id-ref"]);
87         })
88         this.getInstance().getVDU(VNFIDs);
89         this.setState(newState);
90     }
91
92     //Action Handlers
93     getCatalogSuccess(catalogs) {
94         let self = this;
95         let nsd = [];
96         let vnfd = [];
97         let pnfd = [];
98         catalogs.forEach(function(catalog) {
99             switch (catalog.type) {
100                 case "nsd":
101                     nsd.push(catalog);
102                     try {
103                         self.descriptorSelected(catalog.descriptors)
104                     } catch (e) {}
105                     break;
106                 case "vnfd":
107                     vnfd.push(catalog);
108                     break;
109                 case "pnfd":
110                     pnfd.push(catalog);
111                     break;
112             }
113         });
114         this.setState({
115             nsd, vnfd, pnfd
116         });
117     }
118     getCloudAccountSuccess(cloudAccounts) {
119         let newState = {};
120         newState.cloudAccounts = cloudAccounts || [];
121         if(cloudAccounts && cloudAccounts.length > 0) {
122             newState.selectedCloudAccount = cloudAccounts[0]
123         } else {
124             newState.selectedCloudAccount = {};
125         }
126         this.setState(newState);
127     }
128     getDataCentersSuccess(dataCenters) {
129         let newState = {
130             dataCenters: dataCenters
131         };
132         if (this.selectedCloudAccount['account-type'] == 'openmano') {
133             newState.dataCenterID = dataCenters[this.selectedCloudAccount.name][0].uuid
134         }
135         this.setState(newState)
136     }
137     getLaunchpadConfigSuccess = (config) => {
138         let isStandAlone = ((!config) || config["operational-mode"] == "STANDALONE");
139         this.setState({
140             isStandAlone: isStandAlone
141         });
142     }
143     getVDUSuccess(VNFD) {
144         this.setState({
145             sla_parameters: VNFD
146         })
147     }
148     saveNetworkServiceRecord(name, launch) {
149         //input-parameter: [{uuid: < some_unique_name>, xpath: <same as you got from nsd>, value: <user_entered_value>}]
150         /*
151         'input-parameter-xpath':[{
152                 'xpath': 'someXpath'
153             }],
154          */
155         let guuid = GUID();
156         let payload = {
157             id: guuid,
158             "nsd-ref": this.state.selectedNSDid,
159             "name": name,
160             "short-name": name,
161             "description": "a description for " + guuid,
162             "admin-status": launch ? "ENABLED" : "DISABLED"
163         }
164         if (this.state.isStandAlone) {
165             payload["cloud-account"] = this.state.selectedCloudAccount.name;
166         }
167         if (this.state.selectedCloudAccount['account-type'] == "openmano") {
168             payload['om-datacenter'] = this.state.dataCenterID;
169         }
170         if (this.state.hasConfigureNSD) {
171             let ips = this.state['input-parameters'];
172             let ipsToSend = ips.filter(function(ip) {
173                 if (ip.value && ip.value != "") {
174                     ip.uuid = GUID();
175                     delete ip.name;
176                     return true;
177                 }
178                 return false;
179             });
180             if (ipsToSend.length > 0) {
181                 payload['input-parameter'] = ipsToSend;
182             }
183         }
184         this.launchNSR({
185             'nsr': [payload]
186         });
187     }
188     launchNSRLoading() {
189         this.setState({
190             isLoading: true
191         });
192         console.log('is Loading', this)
193     }
194     launchNSRSuccess(data) {
195         let tokenizedHash = window.location.hash.split('/');
196         this.setState({
197             isLoading: false
198         });
199         return window.location.hash = 'launchpad/' + tokenizedHash[2];
200     }
201     launchNSRError(error) {
202          AppHeaderActions.validateError.defer('Something went wrong while trying to instantiate. Check the error logs for more information');
203         this.setState({
204             isLoading: false
205         });
206     }
207     updateInputParam = (i, value) => {
208         let ip = this['input-parameters'];
209         ip[i].value = value;
210         this.setState({
211             'input-parameters': ip
212         })
213     }
214 }
215
216 function getMockSLA(id) {
217     console.log('Getting mock SLA Data for id: ' + id);
218     this.setState({
219         sla_parameters: slaData
220     });
221 }
222
223 function getMockData() {
224     console.log('Getting mock Descriptor Data');
225     this.setState({
226         nsd: data.nsd,
227         vnfd: data.vnfd,
228         pnfd: data.pnfd
229     });
230 }
231 export default Alt.createStore(LaunchNetworkServiceStore);