update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[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 var Utils = require('utils/utils.js');
22 var rw = require('utils/rw.js');
23 var altImage = rw.getSearchParams(window.location).alt_image;
24 var _ = require('lodash');
25
26 let Params = {
27 //Config Agent
28 ConfigAgent: {
29
30 }
31 }
32
33
34 let AccountMeta = {
35 'resource-orchestrator': {
36 defaultType: 'openmano',
37 types: [
38 {
39 name: "OpenMANO",
40 'ro-account-type': 'openmano'
41 }],
42 params: {
43 'openmano' : [{
44 label: "Host",
45 ref: 'host'
46 }, {
47 label: "Port",
48 ref: 'port'
49 }, {
50 label: "Tenant ID",
51 ref: 'tenant-id'
52 }]
53 }
54 },
55 'config-agent': {
56
57 defaultType: 'juju',
58 params: {
59 "juju": [{
60 label: "IP Address",
61 ref: 'ip-address'
62 }, {
63 label: "Port",
64 ref: 'port',
65 optional: true
66 }, {
67 label: "Username",
68 ref: 'user',
69 optional: true
70 }, {
71 label: "Secret",
72 ref: 'secret',
73 optional: true
74 }]
75 },
76 types: [{
77 "name": "JUJU",
78 "account-type": "juju",
79 }]
80 },
81 'sdn': {
82 defaultType: 'odl',
83 params: {
84 "odl": [{
85 label: "Username",
86 ref: 'username'
87 }, {
88 label: "Password",
89 ref: 'password'
90 }, {
91 label: "URL",
92 ref: 'url'
93 }],
94
95 "openstack": [{
96 label: "Key",
97 ref: 'key'
98 },{
99 label: "Secret",
100 ref: 'secret'
101 },{
102 label: "Authentication URL",
103 ref: 'auth_url'
104 },{
105 label: "Tenant",
106 ref: 'tenant'
107 },{
108 label: "User domain",
109 ref: 'user-domain',
110 optional: true
111 },{
112 label: "Project domain",
113 ref: 'project-domain',
114 optional: true
115 },{
116 label: "Region",
117 ref: 'region',
118 optional: true
119 }
120 // ,{
121 // label: "admin",
122 // ref: 'admin',
123 // default: false,
124 // optional: true
125 // }
126 // ,{
127 // label: "Management Network",
128 // ref: 'mgmt-network'
129 // }
130 // ,{
131 // label: "Plugin Name",
132 // ref: 'plugin-name',
133 // optional: true
134 // },{
135 // label: "Security Groups",
136 // ref: 'security-groups',
137 // type: 'list',
138 // optional: true
139 // },{
140 // label: "Dynamic Flavor Support ",
141 // ref: 'dynamic-flavor-support',
142 // type: 'boolean',
143 // optional: true
144 // }
145 //, {
146 // label: "Floating IP Pool",
147 // ref: 'floating-ip-pool',
148 // optional: true
149 // }
150 // ,{
151 // label: "Certificate Validation",
152 // ref: 'cert-validate',
153 // type: 'boolean',
154 // optional: true
155 // }
156 ]
157
158 },
159 types: [{
160 "name": "ODL",
161 "account-type": "odl",
162 },{
163 "name": "OpenStack",
164 "account-type": "openstack",
165 }]
166 },
167 'cloud': {
168 defaultType: 'openstack',
169 params: {
170 "aws": [{
171 label: "Key",
172 ref: 'key'
173 }, {
174 label: "Secret",
175 ref: 'secret'
176 }, {
177 label: "Availability Zone",
178 ref: 'availability-zone'
179 }, {
180 label: "Default Subnet ID",
181 ref: 'default-subnet-id'
182 }, {
183 label: "Region",
184 ref: 'region'
185 }, {
186 label: "VPC ID",
187 ref: 'vpcid'
188 }, {
189 label: "SSH Key",
190 ref: 'ssh-key'
191 }],
192 "cloudsim_proxy": [{
193 label: "Host",
194 ref: "host"
195 }],
196 "openstack": [{
197 label: "Key",
198 ref: 'key'
199 }, {
200 label: "Secret",
201 ref: 'secret'
202 }, {
203 label: "Authentication URL",
204 ref: 'auth_url'
205 }, {
206 label: "Tenant",
207 ref: 'tenant'
208 }, {
209 label: 'Default Management Network',
210 ref: 'mgmt-network',
211 optional: true
212 }, {
213 label: 'Default Floating IP Pool Network Name',
214 ref: 'floating-ip-pool',
215 optional: true
216 }, {
217 label: "User Domain",
218 ref: 'user-domain',
219 optional: true
220 }, {
221 label: "Project Domain",
222 ref: 'project-domain',
223 optional: true
224 }, {
225 label: "Region",
226 ref: 'region',
227 optional: true
228 }],
229 "openvim": [{
230 label: "Host",
231 ref: 'host'
232 }, {
233 label: "Tenant Name",
234 ref: 'tenant-name'
235 }, {
236 label: 'Management Network',
237 ref: 'mgmt-network'
238 }, {
239 label: "Port",
240 ref: 'port',
241 optional: true
242 }],
243 "prop_cloud1": [{
244 label: "Host",
245 ref: "host"
246 }, {
247 label: "Username",
248 ref: "username"
249 }, {
250 label: "Password",
251 ref: "password"
252 }, {
253 label: "Management Network",
254 ref: "mgmt-network"
255 }, {
256 label: "Public IP pool",
257 ref: "public-ip-pool"
258 }, {
259 label: "WAN Interface",
260 ref: "wan-interface"
261 }, {
262 label: "Firewall",
263 ref: "firewall",
264 optional: true
265 }]
266 },
267 nestedParams: {
268 "openvim": {
269 "container-name": "image-management",
270 "label": "Image Management",
271 "params": [{
272 label: "Username",
273 ref: 'username'
274 }, {
275 label: "Password",
276 ref: 'password'
277 }, {
278 label: 'Image Directory Path',
279 ref: 'image-directory-path',
280 optional: true
281 }]
282 }
283 },
284 types: [{
285 "name": "OpenStack",
286 "account-type": "openstack"
287 }, {
288 "name": "Cloudsim",
289 "account-type": "cloudsim_proxy"
290 }, {
291 "name": "AWS",
292 "account-type": "aws"
293 }, {
294 "name": "Open VIM",
295 "account-type": "openvim"
296 }, {
297 "name": "Brocade",
298 "account-type": "prop_cloud1"
299 }]
300 },
301 resources: {
302 },
303 image: {
304 "aws": require("../../images/aws.png"),
305 "openvim": require("../../images/openmano.png"),
306 "openstack": require("../../images/openstack.png"),
307 "cloudsim_proxy": require("../../images/riftio.png"),
308 "odl": require("../../images/OpenDaylight_logo.png"),
309 "juju": require("../../images/juju.svg"),
310 "prop_cloud1": require("../../images/brocade.png"),
311 "openmano": require("../../images/openmano.png")
312
313 },
314 labelByType: {
315 "aws": "AWS",
316 "openvim": "Open VIM",
317 "openstack": "OpenStack",
318 "cloudsim_proxy": "Cloudsim",
319 "prop_cloud1": "Brocade",
320 "openmano": "OpenStack"
321 }
322 }
323
324 export default class AccountStore {
325 constructor() {
326 // const savedData = JSON.parse(window.sessionStorage.getItem('account'));
327 const savedData = null;
328 this.saveAccountToSessionStorage(null, true)
329 this.cloud = [];
330 this['config-agent'] = [];
331 this['resource-orchestrator'] = [];
332 this.sdn = [];
333 this.savedData = savedData;
334 this.account = null;
335 // this.account = savedData.account;
336 // this.accountType = savedData.accountType;
337 this.types = this.accountType ? AccountMeta[this.accountType].types : [];
338 this.refreshingAll = false;
339 this.sdnOptions = [];
340 this.AccountMeta = AccountMeta;
341 this.showVIM = true;
342 this.vduInstanceTimeout = '';
343 this.bindActions(AccountActions(this.alt));
344 this.registerAsync(AccountSource);
345 this.exportPublicMethods({
346 closeSocket:this.closeSocket,
347 setAccountTemplate: this.setAccountTemplate,
348 handleParamChange: this.handleParamChange,
349 handleNameChange: this.handleNameChange,
350 handleAccountTypeChange: this.handleAccountTypeChange,
351 updateAccount: this.updateAccount,
352 viewAccount: this.viewAccount,
353 handleNestedParamChange: this.handleNestedParamChange,
354 getImage: this.getImage,
355 saveAccountToSessionStorage: this.saveAccountToSessionStorage,
356 updateVduTimeout: this.updateVduTimeout,
357 getTransientAccountForUser: this.getTransientAccountForUser
358 })
359 }
360 refreshAllAccountsSuccess = () => {
361 this.setState({
362 refreshingAll: false
363 });
364 }
365 refreshAllAccountsLoading = () => {
366 this.setState({
367 refreshingAll: true
368 });
369 }
370 refreshAllAccountsError = () => {
371
372 }
373 refreshCloudAccountFail = () => {
374 console.log(this);
375 }
376 refreshCloudAccountSuccess = () => {
377
378 }
379 deleteAccountSuccess = (response) => {
380 this.saveAccountToSessionStorage(null, true);
381 this.setState({
382 currentAccount: false,
383 account: {}
384 });
385 }
386 openAccountSocketSuccess = (connection) => {
387 let self = this;
388 let ws = window.multiplexer.channel(connection);
389 if (!connection) return;
390 this.setState({
391 socket: ws.ws,
392 channelId: connection
393 });
394 ws.onmessage = (socket) => {
395 try {
396 var data = JSON.parse(socket.data);
397 Utils.checkAuthentication(data.statusCode, function() {
398 self.closeSocket();
399 });
400 let SdnOptions = [{
401 label: 'Select an SDN Account',
402 value: false
403 }];
404 SdnOptions = SdnOptions.concat(this.generateOptionsByName(data.sdn.data))
405 var newState = {
406 cloud: data.cloud.data,
407 'config-agent': data['config-agent'].data,
408 sdn: data.sdn.data,
409 'resource-orchestrator': data['resource-orchestrator'].data,
410 sdnOptions: SdnOptions
411 };
412
413 //If account is selected, updated connection status only
414 if(self.currentAccount && (self.account || self.savedData)) {
415 let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name);
416 newState.account = self.changedData ? self.account : self.savedData || self.account;
417
418 newState.account['connection-status'] = Account && Account['connection-status'];
419 newState.savedData = null;
420 }
421 self.setState(newState);
422 } catch(error) {
423 console.log('Hit at exception in openAccountSocketSuccess', error)
424 }
425
426 }
427 ws.onclose = () => {
428 self.closeSocket();
429 }
430 }
431 closeSocket = () => {
432 if (this.socket) {
433 window.multiplexer.channel(this.channelId).close();
434 }
435 this.setState({
436 socket: null
437 })
438 }
439 setAccountTemplate = (AccountType, type, savedData) => {
440 console.log('Setting Account Template')
441 let account = {
442 name: '',
443 'account-type': type || AccountMeta[AccountType].defaultType,
444 params: AccountMeta[AccountType].params[AccountMeta[AccountType].defaultType],
445 nestedParams: AccountMeta[AccountType].nestedParams ? AccountMeta[AccountType].nestedParams[AccountMeta[AccountType].defaultType]:null,
446 'connection-status': {
447 status: ''
448 }
449 };
450
451 account[type || AccountMeta[AccountType].defaultType] = {}
452 this.setState({
453 account: savedData || account,
454 accountType: AccountType,
455 types: AccountMeta[AccountType].types,
456 currentAccount: null,
457 changedData: true,
458 savedData: null
459 })
460 }
461 getAccountFromStream(data, name) {
462 let result = null;
463 data && _.isArray(data) && data.map(function(a) {
464 if(a.name == name) {
465 result = a;
466 }
467 });
468 return result;
469 }
470 viewAccount = ({type, name}, savedData) => {
471 console.log('Viewing account')
472 var data = null;
473 var accounts = null;
474 let vduInstanceTimeout = '';
475 if(this && this[type] && this[type].length) {
476 accounts = this[type];
477 data = this.getAccountFromStream(accounts, name);
478 const isRo = data.hasOwnProperty('ro-account-type');
479 if(data) {
480 let accountParams = {
481 params: AccountMeta[type].params[
482 isRo ? data['ro-account-type'] : data['account-type']
483 ]
484 };
485
486 let accountNestedParams = {
487 nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[
488 isRo ? data['ro-account-type'] : data['account-type']
489 ]:null
490 };
491 if (data.hasOwnProperty('vdu-instance-timeout')) {
492 vduInstanceTimeout = data['vdu-instance-timeout'];
493 }
494 this.setState({
495 currentAccount: {type, name},
496 account: (savedData ? savedData : Object.assign(data, accountParams, accountNestedParams)) || null,
497 accountType: type,
498 types: AccountMeta[type].types,
499 vduInstanceTimeout: vduInstanceTimeout,
500 savedData: null
501 })
502 }
503 } else {
504 this.setState({
505 currentAccount: {type, name},
506 account: null,
507 accountType: type,
508 types: AccountMeta[type].types,
509 })
510 }
511 }
512 generateOptionsByName(data) {
513 let results = [];
514 if (data && _.isArray(data)) {
515 data.map(function(d) {
516 results.push({
517 label: d.name,
518 value: d.name
519 })
520 });
521 }
522 return results;
523 }
524 updateAccount = (account) => {
525 this.saveAccountToSessionStorage(account);
526 this.setState({account:account,
527 changedData: true})
528 }
529 handleNameChange = (event) => {
530 var account = this.account;
531 account.name = event.target.value;
532 this.saveAccountToSessionStorage(account)
533 this.setState(
534 {
535 account:account,
536 changedData: true
537 }
538 );
539 }
540 updateVduTimeout = (event) => {
541 var vduInstanceTimeout = event.target.value;
542 this.setState(
543 {
544 vduInstanceTimeout:vduInstanceTimeout
545 }
546 );
547 }
548 handleAccountTypeChange = (node, isRo, event) => {
549 var temp = {};
550 temp.name = this.account.name;
551 if (isRo) {
552 temp['ro-account-type'] = event.target.value;
553 } else {
554 temp['account-type'] = event.target.value;
555 }
556 temp.params= AccountMeta[this.accountType].params[event.target.value];
557 temp.nestedParams = (AccountMeta[this.accountType] && AccountMeta[this.accountType].nestedParams )?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
558 temp[event.target.value] = {};
559 this.saveAccountToSessionStorage(temp)
560 this.setState({
561 account: temp,
562 changedData: true
563 });
564 }
565 handleParamChange(node, isRo, event) {
566 return function(event) {
567 var account = this.state.account;
568 if (isRo) {
569 account[account['ro-account-type']][node.ref] = event.target.value;
570
571 } else {
572 account[account['account-type']][node.ref] = event.target.value;
573
574 }
575 account.params[node.ref] = event.target.value;
576 this.updateAccount(account);
577 }.bind(this);
578 }
579 handleNestedParamChange(containerName, node, event) {
580 return function(event) {
581 var account = this.state.account;
582 account[account['account-type']][containerName] = account[account['account-type']][containerName] || {};
583 account[account['account-type']][containerName][node.ref] = event.target.value;
584 account[containerName] = account[containerName] || {};
585 account[containerName].params = account[containerName].params || {};
586 account[containerName].params[node.ref] = event.target.value;
587 this.updateAccount(account);
588 }.bind(this);
589 }
590 getImage = (type) => {
591 return AccountMeta.image[type];
592 }
593 createAccountSuccess = () => {
594 this.setState({account: {}})
595 this.saveAccountToSessionStorage(null, true)
596 }
597 handleCancelAccount = () => {
598 this.setState({account: {}, currentAccount: null, savedData: null, accountType: null, types: []})
599 this.saveAccountToSessionStorage(null, true)
600 }
601 saveAccountToSessionStorage = (account, clear) => {
602 const userProfile = this.userProfile;
603 if(userProfile) {
604 if(clear) {
605
606 window.sessionStorage.removeItem(userProfile.userId + '@' + userProfile.domain + ':account');
607 // this.setState({
608 // account:null,
609 // accountType: null
610 // })
611 } else {
612 const state = account || this.account;
613 window.sessionStorage.setItem(userProfile.userId + '@' + userProfile.domain + ':account', JSON.stringify(state));
614 }
615 }
616
617 }
618 getTransientAccountForUser = (userProfile) => {
619 let userProfileTransientAccount = window.sessionStorage.getItem(userProfile.userId + '@' + userProfile.domain + ':account') || null;
620 var transientAccount = null;
621 if (userProfileTransientAccount) {
622 transientAccount = JSON.parse(userProfileTransientAccount);
623 };
624 this.saveAccountToSessionStorage(null, true);
625 if(!this.userProfile) {
626 this.setState({
627 savedData: transientAccount,
628 userProfile: userProfile
629 })
630 }
631 }
632 }
633