summaryrefslogtreecommitdiffstats
path: root/portal/src/main/webapp/extsys/vim/js/vimController.js
blob: 486936c35d526a1f08dd7ef6d2681d1cfd0cba0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2016-2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var vm = avalon
    .define({
        $id: "vimController",
        vimInfo: [],
        executeWait: {clazz: 'alert-info', visible: true, text: $.i18n.prop('com_zte_ums_eco_roc_vim_checking_status')},
        executeError: {clazz: 'alert-danger', visible: true, text: 'error'},
        currentElement: {},
        currentIndex: -1,
        saveType: "add",
        modalTitle: $.i18n.prop('com_zte_ums_eco_roc_vim_register'),
        $blankElement: {
            "cloud-owner": "ZTE",
            "status": "active",
            "cloud-region-id": "",
            "cloud-type": "openstack",
            "cloud-region-version": "v1.0",
            "owner-defined-type": "",
            "cloud-zone": "",
            "complex-name": "",
            "cloud-extra-info": "",
            "auth-info-items": [
                {
                    "username": "",
                    "password": "",
                    "auth-url": "",
                    "ssl-cacert": "",
                    "ssl-insecure": "",
                    "cloud-domain": ""
                }
            ]
        },
        $Status: {
            success: "active",
            failed: "inactive",
            displayActive: $.i18n.prop('com_zte_ums_eco_roc_vim_normal'),
            displayInactive: $.i18n.prop('com_zte_ums_eco_roc_vim_abnormal')
        },
        isSave: true,
        action: {ADD: 'add', UPDATE: 'update'},
        $queryVimInfoUrl: 'mock-data/vim.json',//'/onapapi/aai/esr/v1/vims',
        $addVimInfoUrl: '/onapapi/aai/esr/v1/vims/',
        $updateVimInfoUrl: '/onapapi/aai/esr/v1/vims/',
        $delVimInfoUrl: '/onapapi/aai/esr/v1/vims/{vim_id}',
        $initTable: function () {
            $.ajax({
                "type": 'get',
                "url": vm.$queryVimInfoUrl,
                "success": function (resp, statusText, jqXHR) {
                    if (jqXHR.status == "200") {
                        vm.vimInfo = resp;
                    }
                    else {
                        vm.vimInfo = [];
                        bootbox.alert($.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_query_failed"));
                        return;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    bootbox.alert($.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_query_failed") + textStatus + ":" + errorThrown);
                    return;
                },
                complete: function () {
                    resUtil.tooltipVimStatus();
                }
            });

        },
        $showVimTable: function (index, action) {
            vm.isSave = false;
            vm.currentIndex = index;
            vm.saveType = action;
            if (vm.action.ADD == action) {
                vm.modalTitle = $.i18n.prop('com_zte_ums_eco_roc_vim_register');
                vm.fillElement(vm.$blankElement, vm.currentElement);
            } else {
                vm.modalTitle = $.i18n.prop('com_zte_ums_eco_roc_vim_modify_info');
                vm.fillElement(vm.vimInfo[index], vm.currentElement);
            }
            vm.$showModal();
        },
        $showModal: function () {
            $(".form-group").removeClass('has-success').removeClass('has-error').find(".help-block[id]").remove();
            $("#addVimDlg").modal("show");
        },
        $hideModal: function () {
            $(".form-group").removeClass('has-success').removeClass('has-error').find(".help-block[id]").remove();
            $("#addVimDlg").modal("hide");
        },
        $saveVimTable: function (index) {
            vm.isSave = true;
            if (form.valid() == false) {
                vm.isSave = false;
                return false;
            }
            var res = false;
            if (vm.saveType == "add") {
                res = vm.persistVim();
            } else if (vm.saveType == "update") {
                res = vm.updateVim();
            }
            if(res){
                vm.$hideModal();
            }
        },
        //add vim
        persistVim: function () {
            var newElement = vm.getVIMSave();
            vm.vimInfo.push(newElement);
            return true;
            /*$.ajax({
                type: "Post",
                url: vm.$addVimInfoUrl,
                data: JSON.stringify(vm.currentElement.$model),
                async: false,
                dataType: "json",
                contentType: 'application/json',
                success: function (data, statusText, jqXHR) {
                    vm.executeWait.visible = false;
                    vm.executeError.visible = false;
                    if (jqXHR.status == "201") {
                        vm.addVim.vimId = data.vimId;
                        vm.addVim.name = data.name;
                        vm.addVim.tenant = data.tenant;
                        vm.addVim.type = data.type;
                        var newVim = jQuery.extend({}, vm.addVim);
                        vm.vimInfo.push(newVim);

                        $('#addVimDlg').modal('hide');
                        resUtil.growl($.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_title") + $.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_save_success"), "success");
                    } else {
                        vm.executeError.visible = true;
                        vm.executeError.text = $.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_save_failed");
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    vm.executeError.visible = true;
                    vm.executeError.text = textStatus + ":" + errorThrown;
                    vm.executeWait.visible = false;
                    vm.isSave = false;
                }
            });*/
        },
        //update vim
        updateVim: function () {
            vm.fillElement(vm.getVIMSave(), vm.vimInfo[vm.currentIndex]);
            return true;
           /* $.ajax({
                type: "Put",
                url: vm.$updateVimInfoUrl + vm.addVim.vimId,
                contentType: 'application/json',
                data: JSON.stringify(vm.currentElement.$model),
                dataType: "json",
                async: false,
                success: function (data, statusText, jqXHR) {
                    vm.executeWait.visible = false;
                    vm.executeError.visible = false;
                    if (jqXHR.status == "200") {
                        for (var i = 0; i < vm.vimInfo.length; i++) {
                            if (vm.vimInfo[i].vimId == vm.addVim.vimId) {
                                vm.vimInfo[i].name = vm.addVim.vimName;
                                vm.vimInfo[i].userName = vm.addVim.userName;
                                vm.vimInfo[i].password = vm.addVim.password;
                                vm.vimInfo[i].url = vm.addVim.url;
                                vm.vimInfo[i].tenant = vm.addVim.tenant;
                                vm.vimInfo[i].domain = vm.addVim.domain;
                                vm.vimInfo[i].description = vm.addVim.description;
                                vm.vimInfo[i].type = vm.addVim.vimType;
                                vm.vimInfo[i].version=vm.addVim.version;
                                vm.vimInfo[i].vendor=vm.addVim.vendor;
                            }
                        }
                        $('#addVimDlg').modal('hide');
                        resUtil.growl($.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_title') + $.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_save_success'), "success");
                    }
                    else {
                        vm.executeError.visible = true;
                        vm.executeError.text = $.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_save_failed');
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    vm.isSave = false;
                    vm.executeError.visible = true;
                    vm.executeError.text = textStatus + ":" + errorThrown;
                    vm.executeWait.visible = false;
                }
            });*/
        },
        delVim: function (index) {
            bootbox.confirm($.i18n.prop('com_zte_ums_eco_roc_vim_confirm_delete_vim_record'), function (result) {
                if (result) {
                    var id = vm.vimInfo[index]["id"];
                    vm.vimInfo.splice(index, 1);
                   /* $.ajax({
                        type: "DELETE",
                        url: vm.$delVimInfoUrl.replace('{vim_id}', el.vimId),
                        success: function (data, statusText, jqXHR) {
                            if (jqXHR.status == "204") {
                                for (var i = 0; i < vm.vimInfo.length; i++) {
                                    if (el.vimId == vm.vimInfo[i].vimId) {
                                        vm.vimInfo.splice(i, 1);
                                        break;
                                    }
                                }
                                resUtil.growl($.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_title') + $.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_remove_success'), "success");
                            }
                            else {
                                resUtil.growl($.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_title') + $.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_remove_failed'), "warning");
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            resUtil.growl($.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_title') + errorThrown, "danger");
                        }
                    });*/
                }
            });
        },
        fillElement: function (sourceElement, targetElement) {
            targetElement["cloud-owner"] = sourceElement["cloud-owner"];
            targetElement["status"] = sourceElement["status"];
            targetElement["cloud-region-id"] = sourceElement["cloud-region-id"];
            targetElement["cloud-type"] = sourceElement["cloud-type"];
            targetElement["cloud-region-version"] = sourceElement["cloud-region-version"];
            targetElement["owner-defined-type"] = sourceElement["owner-defined-type"];
            targetElement["cloud-zone"] = sourceElement["cloud-zone"];
            targetElement["complex-name"] = sourceElement["complex-name"];
            targetElement["cloud-extra-info"] = sourceElement["cloud-extra-info"];
            if(!targetElement["auth-info-items"]){
                targetElement["auth-info-items"] = [{}];
            }
            targetElement["auth-info-items"][0]["username"] = sourceElement["auth-info-items"][0]["username"];
            targetElement["auth-info-items"][0]["password"] = sourceElement["auth-info-items"][0]["password"];
            targetElement["auth-info-items"][0]["auth-url"] = sourceElement["auth-info-items"][0]["auth-url"];
            targetElement["auth-info-items"][0]["ssl-cacert"] = sourceElement["auth-info-items"][0]["ssl-cacert"];
            targetElement["auth-info-items"][0]["ssl-insecure"] = sourceElement["auth-info-items"][0]["ssl-insecure"];
            targetElement["auth-info-items"][0]["cloud-domain"] = sourceElement["auth-info-items"][0]["cloud-domain"];
        },
        getVIMSave: function () {
            var vimSave = $.extend(true, {}, vm.currentElement.$model);
            vimSave["auth-info-items"] = $.extend(true, {}, vm.currentElement["auth-info-items"].$model);
            return vimSave;
        },
        gotoChartPage: function (oid, name, tenant) {
            window.location.href = "vimChart.html?" + oid + "&" + name + "&" + tenant;
        }
    });
vm.currentElement = $.extend(true, {}, vm.$blankElement);
avalon.scan();
vm.$initTable();