rebuild GUI structure(only changed modules' name)
[vnfsdk/refrepo.git] / extsys / src / main / webapp / extsys / vim / js / vimController.js
1 /*
2  * Copyright 2016-2017 ZTE Corporation.
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 var vm = avalon
17     .define({
18         $id: "vimController",
19         vimInfo: [],
20         //vimStatusTime:$.i18n.prop('com_zte_ums_eco_roc_vim_getting_info'),
21         // ifSearch : 0,
22         // server_rtn:{
23         //      info_block:false,
24         //      warning_block:false,
25         //      rtn_info:"",
26         //      $RTN_SUCCESS:"RTN_SUCCESS",
27         //      $RTN_FAILED:"RTN_FAILED",
28         //              wait : $.i18n.prop('com_zte_ums_eco_roc_vim_checking_status')
29         // },
30         executeWait: {clazz: 'alert-info', visible: true, text: $.i18n.prop('com_zte_ums_eco_roc_vim_checking_status')},
31         executeError: {clazz: 'alert-danger', visible: true, text: 'error'},
32         $Status: {
33             success: "active",
34             failed: "inactive",
35             displayActive: $.i18n.prop('com_zte_ums_eco_roc_vim_normal'),
36             displayInactive: $.i18n.prop('com_zte_ums_eco_roc_vim_abnormal')
37         },
38         isSave: true,
39         action: {ADD: 'add', UPDATE: 'update'},
40         $queryVimInfoUrl: '/openoapi/extsys/v1/vims',
41         $addVimInfoUrl: '/openoapi/extsys/v1/vims/',
42         $updateVimInfoUrl: '/openoapi/extsys/v1/vims/',
43         $delVimInfoUrl: '/openoapi/extsys/v1/vims/{vim_id}',
44         $initTable: function () {
45             $.ajax({
46                 "type": 'get',
47                 "url": vm.$queryVimInfoUrl,
48                 //"dataType": "json",
49                 "success": function (resp, statusText, jqXHR) {
50                     if (jqXHR.status == "200") {
51                         vm.vimInfo = resp;
52                     }
53                     else {
54                         vm.vimInfo = [];
55                         bootbox.alert($.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_query_failed"));
56                         return;
57                     }
58                 },
59                 error: function (XMLHttpRequest, textStatus, errorThrown) {
60                     bootbox.alert($.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_query_failed") + textStatus + ":" + errorThrown);
61                     return;
62                 },
63                 complete: function () {
64                     resUtil.tooltipVimStatus();
65                 }
66             });
67
68         },
69         $vimType: {
70             condName: $.i18n.prop("com_zte_ums_eco_roc_vim_type"),
71             component_type: 'select',
72             selectItems: [
73                 {
74                     cond_value: 'vmware',
75                     name: "vmware",
76                     value: true
77                 },
78                 {
79                     cond_value: 'openstack',
80                     name: "openstack",
81                     value: true
82                 }
83             ]
84         },
85         addVim: {
86             titleName: $.i18n.prop("com_zte_ums_eco_roc_vim_register_info"),
87             vimId: "",
88             vimName: "",
89             domain: '',
90             domainVisable:true,
91             vimNameModify:false,
92             userName: "",
93             tenant: "",
94             password: "",
95             url: "",
96             saveType: "add",
97             description: "",
98             vimType: "openstack",
99             vendor: "",
100             version: ""
101         },
102         $showVimTable: function (el, action) {
103             vm.isSave = false;
104             if (vm.action.ADD == action) {
105                 vm.addVim.vimId = "";
106                 vm.addVim.vimName = "";
107                 vm.addVim.vimNameModify=false;
108                 vm.addVim.userName = "";
109                 vm.addVim.password = "";
110                 vm.addVim.url = "";
111                 vm.addVim.domain = "";
112                 vm.addVim.domainVisable = true;
113                 vm.addVim.description = "";
114                 vm.addVim.tenant = "";
115                 vm.addVim.vendor = "";
116                 vm.addVim.saveType = "add";
117                 vm.addVim.vimType = "openstack";
118                 vm.addVim.titleName = $.i18n.prop("com_zte_ums_eco_roc_vim_register_info");
119
120             } else {
121                 vm.addVim.vimId = el.vimId;
122                 vm.addVim.vimName = el.name;
123                 vm.addVim.vimNameModify=true;
124                 vm.addVim.url = el.url;
125                 vm.addVim.description = el.description;
126                 vm.addVim.userName = el.userName;
127                 vm.addVim.password = el.password;
128                 vm.addVim.tenant = el.tenant;
129                 vm.addVim.domain = el.domain;
130                 vm.addVim.domainVisable=vm.$getdomainVisable(el.type);
131                 vm.addVim.saveType = "update";
132                 vm.addVim.titleName = $.i18n.prop('com_zte_ums_eco_roc_vim_modify_info');
133                 vm.addVim.vimType = el.type;
134                 vm.addVim.vendor = el.vendor;
135                 vm.addVim.version = el.version;
136             }
137             vm.executeError.visible = false;
138             vm.executeWait.visible = false;
139             $(".form-group").each(function () {
140                 $(this).removeClass('has-success');
141                 $(this).removeClass('has-error');
142                 $(this).find(".help-block[id]").remove();
143             });
144             $("#addVimDlg").modal("show");
145         },
146
147         $saveVimTable: function () {
148             vm.isSave = true;
149             success.hide();
150             error.hide();
151             if (form.valid() == false) {
152                 vm.isSave = false;
153                 return false;
154             }
155             vm.executeWait.visible = true;
156             vm.executeError.visible = false;
157             if (vm.addVim.saveType == "add") { 
158                 for( var i = 0; i < vm.vimInfo.length; i ++ ){
159                     if(vm.addVim.name == vm.vimInfo[i].name){
160                        resUtil.growl($.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_title") +  ' already exists',"info");
161                        $('#addVimDlg').modal('hide');
162                         return;
163                     }
164                 }                 
165                 vm.persistVim();
166             } else if (vm.addVim.saveType == "update") {
167                 vm.updateVim();
168             }
169         },
170         //add vim
171         persistVim: function () {
172             $.ajax({
173                 type: "Post",
174                 url: vm.$addVimInfoUrl,
175                 data: JSON.stringify({
176                     name: vm.addVim.vimName,
177                     url: vm.addVim.url,
178                     userName: vm.addVim.userName,
179                     password: vm.addVim.password,
180                     tenant: vm.addVim.tenant,
181                     domain: vm.addVim.domain,
182                     vendor: vm.addVim.vendor,
183                     version: vm.addVim.version,
184                     description: vm.addVim.description,
185                     type: vm.addVim.vimType,
186                 }),
187                 async: false,
188                 dataType: "json",
189                 contentType: 'application/json',
190                 success: function (data, statusText, jqXHR) {
191                     vm.executeWait.visible = false;
192                     vm.executeError.visible = false;
193                     if (jqXHR.status == "201") {
194                         vm.addVim.vimId = data.vimId;
195                         vm.addVim.name = data.name;
196                         vm.addVim.tenant = data.tenant;
197                         vm.addVim.type = data.type;
198                         var newVim = jQuery.extend({}, vm.addVim);
199                         vm.vimInfo.push(newVim);
200
201                         $('#addVimDlg').modal('hide');
202                         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");
203                     } else {
204                         vm.executeError.visible = true;
205                         vm.executeError.text = $.i18n.prop("com_zte_ums_eco_roc_vim_growl_msg_save_failed");
206                     }
207                 },
208                 error: function (XMLHttpRequest, textStatus, errorThrown) {
209                     vm.executeError.visible = true;
210                     vm.executeError.text = textStatus + ":" + errorThrown;
211                     vm.executeWait.visible = false;
212                     vm.isSave = false;
213                 }
214             });
215         },
216         //update vim
217         updateVim: function () {
218             $.ajax({
219                 type: "Put",
220                 url: vm.$updateVimInfoUrl + vm.addVim.vimId,
221                 contentType: 'application/json',
222                 data: JSON.stringify({
223                     name: vm.addVim.vimName,
224                     userName: vm.addVim.userName,
225                     password: vm.addVim.password,
226                     domain: vm.addVim.domain,
227                     version: vm.addVim.version,
228                     description: vm.addVim.description,
229                     url: vm.addVim.url,
230                     tenant: vm.addVim.tenant,
231                     type: vm.addVim.vimType,
232                     vendor: vm.addVim.vendor,
233                 }),
234                 dataType: "json",
235                 async: false,
236                 success: function (data, statusText, jqXHR) {
237                     vm.executeWait.visible = false;
238                     vm.executeError.visible = false;
239                     if (jqXHR.status == "200") {
240                         for (var i = 0; i < vm.vimInfo.length; i++) {
241                             if (vm.vimInfo[i].vimId == vm.addVim.vimId) {
242                                 vm.vimInfo[i].name = vm.addVim.vimName;
243                                 vm.vimInfo[i].userName = vm.addVim.userName;
244                                 vm.vimInfo[i].password = vm.addVim.password;
245                                 vm.vimInfo[i].url = vm.addVim.url;
246                                 vm.vimInfo[i].tenant = vm.addVim.tenant;
247                                 vm.vimInfo[i].domain = vm.addVim.domain;
248                                 vm.vimInfo[i].description = vm.addVim.description;
249                                 vm.vimInfo[i].type = vm.addVim.vimType;
250                                 vm.vimInfo[i].version=vm.addVim.version;
251                                 vm.vimInfo[i].vendor=vm.addVim.vendor;
252                             }
253                         }
254                         $('#addVimDlg').modal('hide');
255                         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");
256                     }
257                     else {
258                         vm.executeError.visible = true;
259                         vm.executeError.text = $.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_save_failed');
260                     }
261                 },
262                 error: function (XMLHttpRequest, textStatus, errorThrown) {
263                     vm.isSave = false;
264                     vm.executeError.visible = true;
265                     vm.executeError.text = textStatus + ":" + errorThrown;
266                     vm.executeWait.visible = false;
267                 }
268             });
269         },
270         delVim: function (el) {
271             bootbox.confirm($.i18n.prop('com_zte_ums_eco_roc_vim_confirm_delete_vim_record'), function (result) {
272                 if (result) {
273                     $.ajax({
274                         type: "DELETE",
275                         url: vm.$delVimInfoUrl.replace('{vim_id}', el.vimId),
276                         success: function (data, statusText, jqXHR) {
277                             if (jqXHR.status == "204") {
278                                 for (var i = 0; i < vm.vimInfo.length; i++) {
279                                     if (el.vimId == vm.vimInfo[i].vimId) {
280                                         vm.vimInfo.splice(i, 1);
281                                         break;
282                                     }
283                                 }
284                                 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");
285                             }
286                             else {
287                                 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");
288                             }
289                         },
290                         error: function (XMLHttpRequest, textStatus, errorThrown) {
291                             resUtil.growl($.i18n.prop('com_zte_ums_eco_roc_vim_growl_msg_title') + errorThrown, "danger");
292                         }
293                     });
294                 }
295             });
296         },
297         gotoChartPage: function (oid, name, tenant) {
298             window.location.href = "vimChart.html?" + oid + "&" + name + "&" + tenant;
299         },
300         $getdomainVisable:function(vimType){
301             if ("openstack"==vimType){
302                 return true;           
303              }else{
304                 return false;
305              }
306
307         },
308         vimTypeRendered:function(){
309             vm.addVim.domainVisable=vm.$getdomainVisable(vm.addVim.vimType);
310         }
311
312
313     });
314 avalon.scan();
315 vm.$initTable();
316
317
318
319