Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / accounts / src / account / accountStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18 import AccountActions from './accountActions.js';
19 import AccountSource from './accountSource.js';
20
21
22 var rw = require('utils/rw.js');
23 var altImage = rw.getSearchParams(window.location).alt_image;
24
25 let Params = {
26 //Config Agent
27 ConfigAgent: {
28
29 }
30 }
31
32
33
34 let AccountMeta = {
35 'config-agent': {
36
37 defaultType: 'juju',
38 params: {
39 "juju": [{
40 label: "IP Address",
41 ref: 'ip-address'
42 }, {
43 label: "Port",
44 ref: 'port',
45 optional: true
46 }, {
47 label: "Username",
48 ref: 'user',
49 optional: true
50 }, {
51 label: "Secret",
52 ref: 'secret',
53 optional: true
54 }]
55 },
56 types: [{
57 "name": "JUJU",
58 "account-type": "juju",
59 }]
60 },
61 'sdn': {
62 defaultType: 'odl',
63 params: {
64 "odl": [{
65 label: "Username",
66 ref: 'username'
67 }, {
68 label: "Password",
69 ref: 'password'
70 }, {
71 label: "URL",
72 ref: 'url'
73 }]
74 },
75 types: [{
76 "name": "ODL",
77 "account-type": "odl",
78 }]
79 },
80 'cloud': {
81 defaultType: 'openstack',
82 params: {
83 "aws": [{
84 label: "Key",
85 ref: 'key'
86 }, {
87 label: "Secret",
88 ref: 'secret'
89 }, {
90 label: "Availability Zone",
91 ref: 'availability-zone'
92 }, {
93 label: "Default Subnet ID",
94 ref: 'default-subnet-id'
95 }, {
96 label: "Region",
97 ref: 'region'
98 }, {
99 label: "VPC ID",
100 ref: 'vpcid'
101 }, {
102 label: "SSH Key",
103 ref: 'ssh-key'
104 }],
105 "cloudsim_proxy": [{
106 label: "Host",
107 ref: "host"
108 }],
109 "openstack": [{
110 label: "Key",
111 ref: 'key'
112 }, {
113 label: "Secret",
114 ref: 'secret'
115 }, {
116 label: "Authentication URL",
117 ref: 'auth_url'
118 }, {
119 label: "Tenant",
120 ref: 'tenant'
121 }, {
122 label: 'Management Network',
123 ref: 'mgmt-network'
124 }, {
125 label: 'Floating IP Pool',
126 ref: 'floating-ip-pool',
127 optional: true
128 }],
129 "openmano": [{
130 label: "Host",
131 ref: 'host'
132 }, {
133 label: "Port",
134 ref: 'port'
135 }, {
136 label: "Tenant ID",
137 ref: 'tenant-id'
138 }],
139 "openvim": [{
140 label: "Host",
141 ref: 'host'
142 }, {
143 label: "Tenant Name",
144 ref: 'tenant-name'
145 }, {
146 label: 'Management Network',
147 ref: 'mgmt-network'
148 }, {
149 label: "Port",
150 ref: 'port',
151 optional: true
152 }]
153 },
154 nestedParams: {
155 "openvim": {
156 "container-name": "image-management",
157 "label": "Image Management",
158 "params": [{
159 label: "Username",
160 ref: 'username'
161 }, {
162 label: "Password",
163 ref: 'password'
164 }, {
165 label: 'Image Directory Path',
166 ref: 'image-directory-path',
167 optional: true
168 }]
169 }
170 },
171 types: [{
172 "name": "OpenStack",
173 "account-type": "openstack"
174 }, {
175 "name": "Cloudsim",
176 "account-type": "cloudsim_proxy"
177 }, {
178 "name": "Open Mano",
179 "account-type": "openmano"
180 }, {
181 "name": "AWS",
182 "account-type": "aws"
183 }, {
184 "name": "Open VIM",
185 "account-type": "openvim"
186 }]
187 },
188 resources: {
189 },
190 image: {
191 "aws": require("../../images/aws.png"),
192 "openmano": altImage || require("../../images/openmano.png"),
193 "openvim": require("../../images/openmano.png"),
194 "openstack": require("../../images/openstack.png"),
195 "cloudsim_proxy": require("../../images/riftio.png"),
196 "odl": require("../../images/OpenDaylight_logo.png"),
197 "juju": require("../../images/juju.svg")
198
199 },
200 labelByType: {
201 "aws": "AWS",
202 "openmano": "OpenStack",
203 "openvim": "Open VIM",
204 "openstack": "OpenStack",
205 "cloudsim_proxy": "Cloudsim"
206 }
207 }
208
209 export default class AccountStore {
210 constructor() {
211 this.cloud = [];
212 this['config-agent'] = [];
213 this.sdn = [];
214 this.account = null;
215 this.types = [];
216 this.refreshingAll = false;
217 this.sdnOptions = [];
218 this.AccountMeta = AccountMeta;
219 this.bindActions(AccountActions(this.alt));
220 this.registerAsync(AccountSource);
221 this.exportPublicMethods({
222 closeSocket:this.closeSocket,
223 setAccountTemplate: this.setAccountTemplate,
224 handleParamChange: this.handleParamChange,
225 handleNameChange: this.handleNameChange,
226 handleAccountTypeChange: this.handleAccountTypeChange,
227 updateAccount: this.updateAccount,
228 viewAccount: this.viewAccount,
229 handleNestedParamChange: this.handleNestedParamChange,
230 getImage: this.getImage
231 })
232 }
233 refreshAllAccountsSuccess = () => {
234 this.setState({
235 refreshingAll: false
236 });
237 }
238 refreshAllAccountsLoading = () => {
239 this.setState({
240 refreshingAll: true
241 });
242 }
243 refreshAllAccountsError = () => {
244
245 }
246 refreshCloudAccountFail = () => {
247 console.log(this);
248 }
249 refreshCloudAccountSuccess = () => {
250
251 }
252 deleteAccountSuccess = (response) => {
253 this.setState({
254 currentAccount: false,
255 account: {}
256 });
257 }
258 openAccountSocketSuccess = (connection) => {
259 let self = this;
260 let ws = window.multiplexer.channel(connection);
261
262 if (!connection) return;
263 this.setState({
264 socket: ws.ws,
265 channelId: connection
266 });
267 ws.onmessage = (socket) => {
268 try {
269 var data = JSON.parse(socket.data);
270 let SdnOptions = [{
271 label: 'Select an SDN Account',
272 value: false
273 }];
274 SdnOptions = SdnOptions.concat(this.generateOptionsByName(data.sdn.data))
275 var newState = {
276 cloud: data.cloud.data,
277 'config-agent': data['config-agent'].data,
278 sdn: data.sdn.data,
279 sdnOptions: SdnOptions
280 };
281
282 //If account is selected, updated connection status only
283 if(self.currentAccount) {
284 let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name);
285 newState.account = self.account;
286 newState.account['connection-status'] = Account['connection-status']
287 }
288 self.setState(newState)
289 } catch(error) {
290 console.log('Hit at exception in openAccountSocketSuccess', error)
291 }
292
293 }
294 ws.onclose = () => {
295 self.closeSocket();
296 }
297 }
298 closeSocket = () => {
299 if (this.socket) {
300 window.multiplexer.channel(this.channelId).close();
301 }
302 this.setState({
303 socket: null
304 })
305 }
306 setAccountTemplate = (AccountType, type) => {
307 let account = {
308 name: '',
309 'account-type': type || AccountMeta[AccountType].defaultType,
310 params: AccountMeta[AccountType].params[AccountMeta[AccountType].defaultType],
311 nestedParams: AccountMeta[AccountType].nestedParams ? AccountMeta[AccountType].nestedParams[AccountMeta[AccountType].defaultType]:null,
312 'connection-status': {
313 status: ''
314 }
315 };
316
317 account[type || AccountMeta[AccountType].defaultType] = {}
318 this.setState({
319 account: account,
320 accountType: AccountType,
321 types: AccountMeta[AccountType].types,
322 currentAccount: null
323 })
324 }
325 getAccountFromStream(data, name) {
326 let result = null;
327 data.map(function(a) {
328 if(a.name == name) {
329 result = a;
330 }
331 });
332 return result;
333 }
334 viewAccount = ({type, name}) => {
335 var data = null;
336 var accounts = null;
337 if(this && this[type].length) {
338 accounts = this[type];
339 data = this.getAccountFromStream(accounts, name);
340 if(data) {
341 let accountParams = {
342 params: AccountMeta[type].params[data['account-type']]
343 };
344
345 let accountNestedParams = {
346 nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[data['account-type']]:null
347 };
348
349 this.setState({
350 currentAccount: {type, name},
351 account: Object.assign(data, accountParams, accountNestedParams),
352 accountType: type
353 })
354 }
355 }
356 }
357 generateOptionsByName(data) {
358 let results = [];
359 if (data.constructor.name == "Array") {
360 data.map(function(d) {
361 results.push({
362 label: d.name,
363 value: d.name
364 })
365 });
366 }
367 return results;
368 }
369 updateAccount = (account) => {
370 this.setState({account:account})
371 }
372 handleNameChange = (event) => {
373 var account = this.account;
374 account.name = event.target.value;
375 this.setState(
376 {
377 account:account
378 }
379 );
380 }
381 handleAccountTypeChange = (node, event) => {
382 var temp = {};
383 temp.name = this.account.name;
384 temp['account-type'] = event.target.value;
385 temp.params= AccountMeta[this.accountType].params[event.target.value];
386 temp.nestedParams = AccountMeta[this.accountType]?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
387 temp[event.target.value] = {};
388 this.setState({
389 account: temp
390 });
391 }
392 handleParamChange(node, event) {
393 return function(event) {
394 var account = this.state.account;
395 account[account['account-type']][node.ref] = event.target.value;
396 account.params[node.ref] = event.target.value;
397 this.updateAccount(account);
398 }.bind(this);
399 }
400 handleNestedParamChange(containerName, node, event) {
401 return function(event) {
402 var account = this.state.account;
403 account[account['account-type']][containerName] = account[account['account-type']][containerName] || {};
404 account[account['account-type']][containerName][node.ref] = event.target.value;
405 account[containerName] = account[containerName] || {};
406 account[containerName].params = account[containerName].params || {};
407 account[containerName].params[node.ref] = event.target.value;
408 this.updateAccount(account);
409 }.bind(this);
410 }
411 getImage = (type) => {
412 return AccountMeta.image[type];
413 }
414 }
415
416
417 /**
418 *Cloud
419 *
420 * {"name":"eng2","account-type":"openstack","openstack":{"key":"lmaultsb","secret":"mypasswd","auth_url":"http://engstack.eng.riftio.com:5000/v3/","tenant":"lmaultsb","mgmt-network":"private"}}
421 Name
422 Path
423
424
425 SDN
426
427
428 *
429 *
430 */