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