update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / accounts / src / account / accountStore.js
index def033a..265a74f 100644 (file)
@@ -21,6 +21,7 @@ import AccountSource from './accountSource.js';
 var Utils = require('utils/utils.js');
 var rw = require('utils/rw.js');
 var altImage = rw.getSearchParams(window.location).alt_image;
+var _ = require('lodash');
 
 let Params = {
     //Config Agent
@@ -30,8 +31,27 @@ let Params = {
 }
 
 
-
 let AccountMeta = {
+    'resource-orchestrator': {
+        defaultType: 'openmano',
+        types: [
+        {
+            name: "OpenMANO",
+            'ro-account-type': 'openmano'
+        }],
+        params: {
+            'openmano' : [{
+                label: "Host",
+                ref: 'host'
+            }, {
+                label: "Port",
+                ref: 'port'
+            }, {
+                label: "Tenant ID",
+                ref: 'tenant-id'
+            }]
+        }
+    },
     'config-agent': {
 
         defaultType: 'juju',
@@ -70,11 +90,78 @@ let AccountMeta = {
             }, {
                 label: "URL",
                 ref: 'url'
-            }]
+            }],
+
+            "openstack": [{
+                label: "Key",
+                ref: 'key'
+            },{
+                label: "Secret",
+                ref: 'secret'
+            },{
+                label: "Authentication URL",
+                ref: 'auth_url'
+            },{
+                label: "Tenant",
+                ref: 'tenant'
+            },{
+                label: "User domain",
+                ref: 'user-domain',
+                optional: true
+            },{
+                label: "Project domain",
+                ref: 'project-domain',
+                optional: true
+            },{
+                label: "Region",
+                ref: 'region',
+                optional: true
+            }
+            // ,{
+            //     label: "admin",
+            //     ref: 'admin',
+            //     default: false,
+            //     optional: true
+            // }
+            // ,{
+            //    label: "Management Network",
+            //    ref: 'mgmt-network'
+            // }
+            // ,{
+            //     label: "Plugin Name",
+            //     ref: 'plugin-name',
+            //     optional: true
+            // },{
+            //     label: "Security Groups",
+            //     ref: 'security-groups',
+            //     type: 'list',
+            //     optional: true
+            // },{
+            //     label: "Dynamic Flavor Support ",
+            //     ref: 'dynamic-flavor-support',
+            //     type: 'boolean',
+            //     optional: true
+            // }
+            //, {
+            //    label: "Floating IP Pool",
+            //    ref: 'floating-ip-pool',
+            //    optional: true
+            // }
+            // ,{
+            //     label: "Certificate Validation",
+            //     ref: 'cert-validate',
+            //     type: 'boolean',
+            //     optional: true
+            // }
+            ]
+
         },
         types: [{
             "name": "ODL",
             "account-type": "odl",
+        },{
+            "name": "OpenStack",
+            "account-type": "openstack",
         }]
     },
     'cloud': {
@@ -119,10 +206,11 @@ let AccountMeta = {
                 label: "Tenant",
                 ref: 'tenant'
             }, {
-                label: 'Management Network',
-                ref: 'mgmt-network'
+                label: 'Default Management Network',
+                ref: 'mgmt-network',
+                optional: true
             }, {
-                label: 'Floating IP Pool Network Name',
+                label: 'Default Floating IP Pool Network Name',
                 ref: 'floating-ip-pool',
                 optional: true
             }, {
@@ -151,7 +239,30 @@ let AccountMeta = {
                 label: "Port",
                 ref: 'port',
                 optional: true
-            }]
+            }],
+            "prop_cloud1": [{
+                label: "Host",
+                ref: "host"
+            }, {
+                label: "Username",
+                ref: "username"
+            }, {
+                label: "Password",
+                ref: "password"
+            }, {
+                label: "Management Network",
+                ref: "mgmt-network"
+            }, {
+                label: "Public IP pool",
+                ref: "public-ip-pool"
+            }, {
+                label: "WAN Interface",
+                ref: "wan-interface"
+            }, {
+                label: "Firewall",
+                ref: "firewall",
+                optional: true
+             }]
         },
         nestedParams: {
             "openvim": {
@@ -182,7 +293,10 @@ let AccountMeta = {
         }, {
             "name": "Open VIM",
             "account-type": "openvim"
-        }]
+        }, {
+            "name": "Brocade",
+            "account-type": "prop_cloud1"
+         }]
     },
     resources: {
     },
@@ -192,28 +306,40 @@ let AccountMeta = {
         "openstack": require("../../images/openstack.png"),
         "cloudsim_proxy": require("../../images/riftio.png"),
         "odl": require("../../images/OpenDaylight_logo.png"),
-        "juju": require("../../images/juju.svg")
+        "juju": require("../../images/juju.svg"),
+        "prop_cloud1": require("../../images/brocade.png"),
+        "openmano": require("../../images/openmano.png")
 
     },
     labelByType: {
         "aws": "AWS",
         "openvim": "Open VIM",
         "openstack": "OpenStack",
-        "cloudsim_proxy": "Cloudsim"
+        "cloudsim_proxy": "Cloudsim",
+        "prop_cloud1": "Brocade",
+        "openmano": "OpenStack"
     }
 }
 
 export default class AccountStore {
     constructor() {
+        // const savedData = JSON.parse(window.sessionStorage.getItem('account'));
+        const savedData = null;
+        this.saveAccountToSessionStorage(null, true)
         this.cloud = [];
         this['config-agent'] = [];
+        this['resource-orchestrator'] = [];
         this.sdn = [];
+        this.savedData = savedData;
         this.account = null;
-        this.types = [];
+        // this.account = savedData.account;
+        // this.accountType = savedData.accountType;
+        this.types = this.accountType ? AccountMeta[this.accountType].types : [];
         this.refreshingAll = false;
         this.sdnOptions = [];
         this.AccountMeta = AccountMeta;
         this.showVIM = true;
+        this.vduInstanceTimeout = '';
         this.bindActions(AccountActions(this.alt));
         this.registerAsync(AccountSource);
         this.exportPublicMethods({
@@ -225,7 +351,10 @@ export default class AccountStore {
             updateAccount: this.updateAccount,
             viewAccount: this.viewAccount,
             handleNestedParamChange: this.handleNestedParamChange,
-            getImage: this.getImage
+            getImage: this.getImage,
+            saveAccountToSessionStorage: this.saveAccountToSessionStorage,
+            updateVduTimeout: this.updateVduTimeout,
+            getTransientAccountForUser: this.getTransientAccountForUser
         })
     }
     refreshAllAccountsSuccess = () => {
@@ -246,16 +375,9 @@ export default class AccountStore {
     }
     refreshCloudAccountSuccess = () => {
 
-    }
-    getResourceOrchestratorSuccess = (data) => {
-        this.alt.actions.global.hideScreenLoader.defer();
-        if(data['rw-launchpad:resource-orchestrator'] && (data['rw-launchpad:resource-orchestrator']['account-type'] == 'openmano')) {
-            this.setState({
-                showVIM: false
-            })
-        }
     }
     deleteAccountSuccess = (response) => {
+        this.saveAccountToSessionStorage(null, true);
         this.setState({
             currentAccount: false,
             account: {}
@@ -284,16 +406,19 @@ export default class AccountStore {
                     cloud: data.cloud.data,
                     'config-agent': data['config-agent'].data,
                     sdn: data.sdn.data,
+                    'resource-orchestrator': data['resource-orchestrator'].data,
                     sdnOptions: SdnOptions
                 };
 
                 //If account is selected, updated connection status only
-                if(self.currentAccount) {
+                if(self.currentAccount && (self.account || self.savedData)) {
                     let Account = self.getAccountFromStream(data[self.currentAccount.type].data, self.currentAccount.name);
-                    newState.account = self.account;
-                    newState.account['connection-status'] = Account['connection-status']
+                    newState.account = self.changedData ? self.account : self.savedData || self.account;
+
+                    newState.account['connection-status'] = Account && Account['connection-status'];
+                    newState.savedData = null;
                 }
-                self.setState(newState)
+                self.setState(newState);
             } catch(error) {
                 console.log('Hit at exception in openAccountSocketSuccess', error)
             }
@@ -311,7 +436,8 @@ export default class AccountStore {
             socket: null
         })
     }
-    setAccountTemplate = (AccountType, type) => {
+    setAccountTemplate = (AccountType, type, savedData) => {
+        console.log('Setting Account Template')
         let account = {
             name: '',
             'account-type': type || AccountMeta[AccountType].defaultType,
@@ -324,47 +450,68 @@ export default class AccountStore {
 
         account[type || AccountMeta[AccountType].defaultType] = {}
         this.setState({
-            account: account,
+            account: savedData || account,
             accountType: AccountType,
             types: AccountMeta[AccountType].types,
-            currentAccount: null
+            currentAccount: null,
+            changedData: true,
+            savedData: null
         })
     }
     getAccountFromStream(data, name) {
         let result = null;
-        data.map(function(a) {
+        data && _.isArray(data) && data.map(function(a) {
             if(a.name == name) {
                 result = a;
             }
         });
         return result;
     }
-    viewAccount = ({type, name}) => {
+    viewAccount = ({type, name}, savedData) => {
+        console.log('Viewing account')
         var data = null;
         var accounts = null;
-        if(this && this[type].length) {
+        let vduInstanceTimeout = '';
+        if(this && this[type] && this[type].length) {
             accounts = this[type];
             data = this.getAccountFromStream(accounts, name);
+            const isRo = data.hasOwnProperty('ro-account-type');
             if(data) {
                 let accountParams = {
-                    params: AccountMeta[type].params[data['account-type']]
+                    params: AccountMeta[type].params[
+                        isRo ? data['ro-account-type'] : data['account-type']
+                    ]
                 };
 
                 let accountNestedParams = {
-                    nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[data['account-type']]:null
+                    nestedParams: AccountMeta[type].nestedParams?AccountMeta[type].nestedParams[
+                        isRo ? data['ro-account-type'] : data['account-type']
+                    ]:null
                 };
-
+                if (data.hasOwnProperty('vdu-instance-timeout')) {
+                    vduInstanceTimeout = data['vdu-instance-timeout'];
+                }
                 this.setState({
                     currentAccount: {type, name},
-                    account: Object.assign(data, accountParams, accountNestedParams),
-                    accountType: type
+                    account: (savedData ? savedData : Object.assign(data, accountParams, accountNestedParams)) || null,
+                    accountType: type,
+                    types: AccountMeta[type].types,
+                    vduInstanceTimeout: vduInstanceTimeout,
+                    savedData: null
                 })
             }
+        } else {
+            this.setState({
+                    currentAccount: {type, name},
+                    account: null,
+                    accountType: type,
+                    types: AccountMeta[type].types,
+                })
         }
     }
     generateOptionsByName(data) {
         let results = [];
-        if (data && data.constructor.name == "Array") {
+        if (data && _.isArray(data)) {
           data.map(function(d) {
               results.push({
                   label: d.name,
@@ -375,32 +522,56 @@ export default class AccountStore {
         return results;
     }
     updateAccount = (account) => {
-        this.setState({account:account})
+        this.saveAccountToSessionStorage(account);
+        this.setState({account:account,
+            changedData: true})
     }
     handleNameChange = (event) => {
         var account = this.account;
         account.name = event.target.value;
+        this.saveAccountToSessionStorage(account)
         this.setState(
              {
-                account:account
+                account:account,
+                changedData: true
              }
         );
     }
-    handleAccountTypeChange = (node, event) => {
+    updateVduTimeout = (event) => {
+        var vduInstanceTimeout = event.target.value;
+        this.setState(
+             {
+                vduInstanceTimeout:vduInstanceTimeout
+             }
+        );
+    }
+    handleAccountTypeChange = (node, isRo, event) => {
         var temp = {};
         temp.name = this.account.name;
-        temp['account-type'] = event.target.value;
+        if (isRo) {
+            temp['ro-account-type'] = event.target.value;
+        } else {
+            temp['account-type'] = event.target.value;
+        }
         temp.params= AccountMeta[this.accountType].params[event.target.value];
-        temp.nestedParams = AccountMeta[this.accountType]?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
+        temp.nestedParams = (AccountMeta[this.accountType] && AccountMeta[this.accountType].nestedParams )?AccountMeta[this.accountType].nestedParams[event.target.value]:null;
         temp[event.target.value] = {};
+        this.saveAccountToSessionStorage(temp)
         this.setState({
-            account: temp
+            account: temp,
+            changedData: true
         });
     }
-    handleParamChange(node, event) {
+    handleParamChange(node, isRo, event) {
         return function(event) {
             var account = this.state.account;
-            account[account['account-type']][node.ref] = event.target.value;
+            if (isRo) {
+                account[account['ro-account-type']][node.ref] = event.target.value;
+
+            } else {
+                account[account['account-type']][node.ref] = event.target.value;
+
+            }
             account.params[node.ref] = event.target.value;
             this.updateAccount(account);
         }.bind(this);
@@ -419,5 +590,44 @@ export default class AccountStore {
     getImage = (type) => {
         return AccountMeta.image[type];
     }
+    createAccountSuccess = () => {
+        this.setState({account: {}})
+        this.saveAccountToSessionStorage(null, true)
+    }
+    handleCancelAccount = () => {
+        this.setState({account: {}, currentAccount: null, savedData: null, accountType: null, types: []})
+        this.saveAccountToSessionStorage(null, true)
+    }
+    saveAccountToSessionStorage = (account, clear) => {
+        const userProfile = this.userProfile;
+        if(userProfile) {
+             if(clear) {
+
+                window.sessionStorage.removeItem(userProfile.userId + '@' + userProfile.domain + ':account');
+                // this.setState({
+                //     account:null,
+                //     accountType: null
+                // })
+            } else {
+                const state = account || this.account;
+                window.sessionStorage.setItem(userProfile.userId + '@' + userProfile.domain + ':account', JSON.stringify(state));
+            }
+        }
+
+    }
+    getTransientAccountForUser = (userProfile) => {
+        let userProfileTransientAccount = window.sessionStorage.getItem(userProfile.userId + '@' + userProfile.domain + ':account') || null;
+        var transientAccount = null;
+        if (userProfileTransientAccount) {
+            transientAccount = JSON.parse(userProfileTransientAccount);
+        };
+        this.saveAccountToSessionStorage(null, true);
+        if(!this.userProfile) {
+            this.setState({
+                savedData:  transientAccount,
+                userProfile: userProfile
+            })
+        }
+    }
 }