rebuild GUI structure(only changed modules' name)
[vnfsdk/refrepo.git] / extsys / src / main / webapp / extsys / vnfm / js / vnfmController.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
17 var vm = avalon
18     .define({
19         $id: "vnfmController",
20         vnfmInfo: [],
21         //mocSelectItems : [],
22         vimSelectItems: [],
23         server_rtn: {
24             info_block: false,
25             warning_block: false,
26             rtn_info: "",
27             $RTN_SUCCESS: "RTN_SUCCESS",
28             $RTN_FAILED: "RTN_FAILED"
29         },
30         $Status: {
31             success: "active",
32             failed: "inactive"
33         },
34         $restUrl: {
35             queryVnfmInfoUrl: '/openoapi/extsys/v1/vnfms',
36             addVnfmInfoUrl: '/openoapi/extsys/v1/vnfms',
37             updateVnfmInfoUrl: '/openoapi/extsys/v1/vnfms/',
38             delVnfmInfoUrl: '/openoapi/extsys/v1/vnfms/',
39             queryMocUrl: '',
40             queryVimUrl: '/openoapi/extsys/v1/vims'
41         },
42         $htmlText: {
43             saveSuccess: $.i18n.prop("nfv-vnfm-iui-message-save-success"),
44             saveFail: $.i18n.prop("nfv-vnfm-iui-message-save-fail"),
45             alreadyExist: $.i18n.prop("nfv-vnfm-iui-message-vnfm-already-exists"),
46             updateSuccess: $.i18n.prop("nfv-vnfm-iui-message-update-success"),
47             updateFail: $.i18n.prop("nfv-vnfm-iui-message-update-fail")
48         },
49         $initTable: function () {
50             $.ajax({
51                 "type": 'GET',
52                 "url": vm.$restUrl.queryVnfmInfoUrl,
53                 //"dataType": "json",
54                 "success": function (resp) {
55                     for (var i = 0; i < resp.length; i++) {
56                         resp[i].status = vm.$Status.success;
57                     }
58                     vm.vnfmInfo = resp;
59                 },
60                 error: function (XMLHttpRequest, textStatus, errorThrown) {
61                     bootbox.alert($.i18n.prop("nfv-vnfm-iui-message-query-fail") + ":" + textStatus + ":" + errorThrown);
62                     return;
63                 },
64                 complete: function () {
65                     vnfmUtil.tooltipVnfmStatus();
66                 }
67             });
68         },
69         // $initMoc : function() {
70         //      /*var url = vm.$restUrl.queryMocUrl;
71         //     commonUtil.get(url, null, function(resp) {
72         //         if (resp) {
73         //             vm.addVnfm.moc = resp.data;
74         //         }
75         //     });*/
76         //     var resp = [
77         //      { id : "nfv.vnfm.eco", name : "VNFM(ECO)"},
78         //      { id : "nfv.vnfm.tacker", name : "VNFM(Tacker)"},
79         //      { id : "nfv.vnfm.cmcc", name : "VNFM(CMCC)"},
80         //      { id : "nfv.vnfm.etsi", name : "VNFM(ETSI)"}
81         //     ]
82         //     vm.mocSelectItems = resp;
83         // },
84         $initVim: function () {
85             $.ajax({
86                 type: 'get',
87                 url: vm.$restUrl.queryVimUrl,
88                 dataType: 'json',
89                 success: function (resp) {
90                     if (resp) {
91                         vm.vimSelectItems = resp;
92                     }
93                     vm.vimSelectItems.push({"vimId": "", "name": ""});
94                 }
95             });
96         },
97         addVnfm: {
98             title: $.i18n.prop("nfv-vnfm-iui-text-register"),
99             vnfmId: "",
100             name: "",
101             type: "",
102             //moc : "",
103             nameReadonly : false,
104             vimId: "",
105             //vimVisiable : false,
106             vendor: "",
107             version: "",
108             description: "",
109             certificateUrl: "",
110             url: "",
111             urlTip: $.i18n.prop("nfv-vnfm-iui-text-url-tip"),
112             userName: "",
113             password: "",
114             saveType: "add",
115             status: ""
116         },
117         $showVnfmTable: function () {
118             vm.addVnfm.title = $.i18n.prop("nfv-vnfm-iui-text-register"),
119                 vm.addVnfm.vnfmId = "";
120             vm.addVnfm.name = "";
121             vm.addVnfm.type = "";
122             //vm.addVnfm.moc = "";
123             vm.addVnfm.nameReadonly = false;
124             vm.addVnfm.vimId = "";
125             //vm.addVnfm.vimVisiable = false;
126             vm.addVnfm.vendor = "";
127             vm.addVnfm.version = "";
128             vm.addVnfm.description = "";
129             vm.addVnfm.certificateUrl = "";
130             vm.addVnfm.url = "";
131             vm.addVnfm.userName = "";
132             vm.addVnfm.password = "";
133             vm.addVnfm.saveType = "add";
134             vm.server_rtn.warning_block = false;
135             vm.server_rtn.info_block = false;
136             //vm.$initMoc();
137             vm.$initVim();
138             //vm.$mocChange();
139
140             $(".form-group").each(function () {
141                 $(this).removeClass('has-success');
142                 $(this).removeClass('has-error');
143                 $(this).find(".help-block[id]").remove();
144             });
145             $("#addVnfmDlg").modal("show");
146         },
147         // $getMocName : function(mocId) {
148         //              var items = vm.mocSelectItems;
149         //           for(var i=0;i<items.length;i++) {
150         //              if(items[i].id == mocId) {
151         //                      return items[i].name;
152         //              }
153         //      }
154         //      return "";
155         // },
156         $saveVnfm: function () {
157             var form = $('#vnfm_form');
158             if (form.valid() == false) {
159                 return false;
160             }
161             vm.server_rtn.info_block = true;
162             vm.server_rtn.warning_block = false;
163             vm.addVnfm.status = vm.$Status.success;
164
165             var param = {
166                 name: vm.addVnfm.name,
167                 //status : vm.addVnfm.status,
168                 //moc : $("#moc").val(),
169                 //vimId : vm.$getVimId($("#moc").val()),
170                 vimId: $("#vimId").val(),
171                 vendor: vm.addVnfm.vendor,
172                 version: vm.addVnfm.version,
173                 type: vm.addVnfm.type,
174                 description: vm.addVnfm.description,
175                 certificateUrl: vm.addVnfm.certificateUrl,
176                 url: vm.addVnfm.url,
177                 userName: vm.addVnfm.userName,
178                 password: vm.addVnfm.password
179             }
180             //save VIM info
181             if (vm.addVnfm.saveType == "add") {  
182                 //      for( var i = 0; i < vm.vnfmInfo.length; i ++ ){
183                 //     if(vm.addVnfm.name == vm.vnfmInfo[i].name){                       
184                 //        vm.server_rtn.warning_block = true;
185                 //        vm.server_rtn.rtn_info = vm.$htmlText.alreadyExist;
186                 //        commonUtil.showMessage(vm.$htmlText.alreadyExist, "failed");
187                 //        return;
188                 //     }
189                 // }     
190                 $.ajax({
191                     type: "POST",
192                     url: vm.$restUrl.addVnfmInfoUrl,
193                     data: JSON.stringify(param),
194                     dataType: "json",
195                     contentType: "application/json",
196                     success: function (data) {
197                         vm.server_rtn.info_block = false;
198                         vm.server_rtn.warning_block = false;
199                         if (data) {
200                             vm.vnfmInfo = [];
201                             vm.$initTable();
202
203                             $('#addVnfmDlg').modal('hide');
204                             commonUtil.showMessage(vm.$htmlText.saveSuccess, "success");
205                         } else {
206                             vm.server_rtn.warning_block = true;
207                             vm.server_rtn.rtn_info = vm.$htmlText.saveFail;
208                             commonUtil.showMessage(vm.$htmlText.saveFail, "failed");
209                         }
210                     },
211                     error: function (XMLHttpRequest, textStatus, errorThrown) {
212                         vm.server_rtn.warning_block = true;
213                         vm.server_rtn.rtn_info = textStatus + ":" + errorThrown;
214                         vm.server_rtn.info_block = false;
215                     }
216                 });
217             } else {
218                 $.ajax({
219                     type: "PUT",
220                     url: vm.$restUrl.updateVnfmInfoUrl + vm.addVnfm.vnfmId,
221                     data: JSON.stringify(param),
222                     dataType: "json",
223                     contentType: "application/json",
224                     success: function (data) {
225                         vm.server_rtn.info_block = false;
226                         vm.server_rtn.warning_block = false;
227                         if (data) {
228                             for (var i = 0; i < vm.vnfmInfo.length; i++) {
229                                 if (vm.vnfmInfo[i].vnfmId == vm.addVnfm.vnfmId) {
230                                     vm.vnfmInfo[i].name = vm.addVnfm.name;
231                                     vm.vnfmInfo[i].vimId = $("#vimId").val();
232                                     vm.vnfmInfo[i].vendor = vm.addVnfm.vendor;
233                                     vm.vnfmInfo[i].version = vm.addVnfm.version;
234                                     vm.vnfmInfo[i].certificateUrl = vm.addVnfm.certificateUrl;
235                                     vm.vnfmInfo[i].description = vm.addVnfm.description;
236                                     vm.vnfmInfo[i].url = vm.addVnfm.url;
237                                     vm.vnfmInfo[i].userName = vm.addVnfm.userName;
238                                     vm.vnfmInfo[i].password = vm.addVnfm.password;
239                                 }
240                             }
241                             $('#addVnfmDlg').modal('hide');
242                             commonUtil.showMessage(vm.$htmlText.updateSuccess, "success");
243                         } else {
244                             vm.server_rtn.warning_block = true;
245                             vm.server_rtn.rtn_info = vm.$htmlText.updateFail;
246                             commonUtil.showMessage(vm.$htmlText.updateFail, "failed");
247                         }
248                     },
249                     error: function (XMLHttpRequest, textStatus, errorThrown) {
250                         vm.server_rtn.warning_block = true;
251                         vm.server_rtn.rtn_info = textStatus + ":" + errorThrown;
252                         vm.server_rtn.info_block = false;
253                     }
254                 });
255             }
256         },
257         //              mocRendered : function(action) {
258         //      if(vm.addVnfm.saveType === "update" && vm.addVnfm.moc) {
259         //              var items = vm.mocSelectItems;
260         //              for(var i=0;i<items.length;i++) {
261         //              if(items[i].id == vm.addVnfm.moc) {
262         //                      $("#moc")[0].selectedIndex = i;
263         //                      vm.$mocChange();
264         //                      break;
265         //              }
266         //      }
267         //      } else {
268         //              $("#moc")[0].selectedIndex = 0;
269         //      }
270         // },
271         vimRendered: function (action) {
272             var items = vm.vimSelectItems;
273             if (vm.addVnfm.saveType === "update") {
274                 for (var i = 0; i < items.length; i++) {
275                     if (items[i].vimId == vm.addVnfm.vimId) {
276                         $("#vimId")[0].selectedIndex = i;
277                         break;
278                     }
279                 }
280             } else {
281                 $("#vimId")[0].selectedIndex = items.length - 1;
282             }
283         },
284         // $mocChange : function() {
285         //      var mocId = $('#moc').val();
286         //      if(mocId == "nfv.vnfm.tacker") {
287         //              vm.addVnfm.vimVisiable = true;
288         //      } else {
289         //              vm.addVnfm.vimVisiable = false;
290         //      }
291         // },
292         // $getVimId : function(mocId) {
293         //      if(vm.addVnfm.vimVisiable) {
294         //              return $("#vimId").val();
295         //      } else {
296         //              return "";
297         //      }
298         // }
299     });
300 avalon.scan();
301 vm.$initTable();
302 //vm.$initMoc();