7fa65240e5428745d9be708f70c2b973f73bb99f
[osm/LW-UI.git] / static / src / instancehandler / instance_list.js
1 /*
2 Copyright 2018 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
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 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 function performAction(instance_name, instance_id) {
18 var url = '/instances/ns/'+instance_id+'/action';
19 $("#formActionNS").attr("action", url);
20 $('#modal_instance_new_action').modal('show');
21 }
22
23 function exportMetricNs(instance_name, instance_id) {
24 var url = '/instances/ns/'+instance_id+'/monitoring/metric';
25 $("#formExportMetricNS").attr("action", url);
26 $('#modal_instance_export_metric').modal('show');
27 }
28
29 function newAlarmNs(instance_name, instance_id) {
30 var url = '/instances/ns/'+instance_id+'/monitoring/alarm';
31 $("#formAlarmNS").attr("action", url);
32 $('#modal_instance_new_alarm').modal('show');
33 }
34
35 function deleteNs(instance_name, instance_id, force) {
36 var url = '/instances/ns/'+instance_id+'/delete';
37 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
38 if (result) {
39 if (force)
40 url = url + '?force=true';
41 var dialog = bootbox.dialog({
42 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
43 closeButton: true
44 });
45 $.ajax({
46 url: url,
47 type: 'GET',
48 dataType: "json",
49 contentType: "application/json;charset=utf-8",
50 success: function (result) {
51 if (result['error'] == true){
52 dialog.modal('hide');
53 bootbox.alert("An error occurred.");
54 }
55 else {
56 dialog.modal('hide');
57 location.reload();
58 }
59 },
60 error: function (result) {
61 dialog.modal('hide');
62 var data = result.responseJSON;
63 var title = "Error " + (data && data.code ? data.code : 'unknown');
64 var message = data && data.detail ? data.detail : 'No detail available.';
65 bootbox.alert({
66 title: title,
67 message: message
68 });
69 }
70 });
71 }
72 })
73 }
74 function deleteNsi(instance_name, instance_id, force) {
75 var url = '/instances/nsi/'+instance_id+'/delete';
76 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
77 if (result) {
78 if (force)
79 url = url + '?force=true';
80 var dialog = bootbox.dialog({
81 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
82 closeButton: true
83 });
84 $.ajax({
85 url: url,
86 type: 'GET',
87 dataType: "json",
88 contentType: "application/json;charset=utf-8",
89 success: function (result) {
90 console.log(result)
91 if (result['error'] == true){
92 dialog.modal('hide');
93 var data = result.responseJSON;
94 var title = "Error " + (data && data.code ? data.code : 'unknown');
95 var message = data && data.detail ? data.detail : 'No detail available.';
96 bootbox.alert({
97 title: title,
98 message: message
99 });
100 }
101 else {
102 dialog.modal('hide');
103 location.reload();
104 }
105 },
106 error: function (result) {
107 dialog.modal('hide');
108 var data = result.responseJSON;
109 var title = "Error " + (data && data.code ? data.code : 'unknown');
110 var message = data && data.detail ? data.detail : 'No detail available.';
111 bootbox.alert({
112 title: title,
113 message: message
114 });
115 }
116 });
117 }
118 })
119 }
120
121 function deletePDU(instance_name, instance_id) {
122 var url = '/instances/pdu/'+instance_id+'/delete';
123 bootbox.confirm("Are you sure want to delete " + instance_name + "?", function (result) {
124 if (result) {
125 var dialog = bootbox.dialog({
126 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
127 closeButton: true
128 });
129 $.ajax({
130 url: url,
131 type: 'GET',
132 dataType: "json",
133 contentType: "application/json;charset=utf-8",
134 success: function (result) {
135 if (result['error'] == true){
136 dialog.modal('hide');
137 bootbox.alert("An error occurred.");
138 }
139 else {
140 dialog.modal('hide');
141 location.reload();
142 }
143 },
144 error: function (error) {
145 dialog.modal('hide');
146 bootbox.alert("An error occurred.");
147 }
148 });
149 }
150 })
151 }
152
153 var addFormGroup = function (event) {
154 event.preventDefault();
155
156 var $formGroup = $(this).closest('.form-group');
157 var $formGroupClone = $formGroup.clone();
158
159 $(this)
160 .toggleClass('btn-success btn-add btn-danger btn-remove')
161 .html('–');
162
163 $formGroupClone.find('input').val('');
164 $formGroupClone.insertAfter($formGroup);
165
166 };
167
168 var removeFormGroup = function (event) {
169 event.preventDefault();
170 var $formGroup = $(this).closest('.form-group');
171 $formGroup.remove();
172 };
173
174 var addInterfaceGroup = function (event) {
175 event.preventDefault();
176
177 var $formGroup = $(this).closest('.interface-group');
178 var $formGroupClone = $formGroup.clone();
179
180 $(this)
181 .toggleClass('btn-success btn-add btn-danger btn-remove')
182 .html('–');
183
184 $formGroupClone.find('input').val('');
185 $formGroupClone.insertAfter($formGroup);
186
187 };
188
189 var removeInterfaceGroup = function (event) {
190 event.preventDefault();
191 var $formGroup = $(this).closest('.interface-group');
192 $formGroup.remove();
193 };
194
195 function showTopology(type, instance_id) {
196 var url = '/instances/'+type+'/'+instance_id+'/topology';
197 window.location = url;
198 }
199
200 function showInstanceDetails(type, instance_id) {
201 var url_info = '/instances/'+type+'/'+instance_id;
202 var dialog = bootbox.dialog({
203 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
204 closeButton: true
205 });
206 $.ajax({
207 url: url_info,
208 type: 'GET',
209 dataType: "json",
210 contentType: "application/json;charset=utf-8",
211 success: function (result) {
212
213 if (result['data'] !== undefined) {
214 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
215 editorJSON.setOption("autoRefresh", true);
216 dialog.modal('hide');
217 $('#modal_show_instance').modal('show');
218 }
219 else {
220 dialog.modal('hide');
221 bootbox.alert("An error occurred while retrieving the information.");
222 }
223 },
224 error: function (result) {
225 dialog.modal('hide');
226 bootbox.alert("An error occurred while retrieving the information.");
227 }
228 });
229 }
230
231 var editorJSON;
232
233 $(document).ready(function () {
234 var json_editor_settings = {
235 mode: "javascript",
236 showCursorWhenSelecting: true,
237 autofocus: true,
238 lineNumbers: true,
239 lineWrapping: true,
240 foldGutter: true,
241 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
242 autoCloseBrackets: true,
243 matchBrackets: true,
244 extraKeys: {
245 "F11": function (cm) {
246 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
247 },
248 "Esc": function (cm) {
249 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
250 },
251 "Ctrl-Q": function (cm) {
252 cm.foldCode(cm.getCursor());
253 }
254 },
255 theme: "neat",
256 keyMap: "sublime"
257 };
258 var myJsonTextArea = document.getElementById("instance_view_json");
259 editorJSON = CodeMirror(function (elt) {
260 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
261 }, json_editor_settings);
262
263
264 $(document).on('click', '.primitive-group .btn-add', addFormGroup);
265 $(document).on('click', '.primitive-group .btn-remove', removeFormGroup);
266
267 $(document).on('click', '.interface-group .btn-add', addInterfaceGroup);
268 $(document).on('click', '.interface-group .btn-remove', removeInterfaceGroup);
269
270 $("#formCreatePDU").submit(function (event) {
271 event.preventDefault(); //prevent default action
272 var post_url = $(this).attr("action"); //get form action url
273 var request_method = $(this).attr("method"); //get form GET/POST method
274 var form_data = new FormData(this); //Encode form elements for submission
275 $.ajax({
276 url: post_url,
277 type: request_method,
278 data: form_data,
279 headers: {
280 "Accept": 'application/json'
281 },
282 contentType: false,
283 processData: false
284 }).done(function (response, textStatus, jqXHR) {
285 table.ajax.reload();
286 $('#modal_new_pdu').modal('hide');
287 }).fail(function (result) {
288 var data = result.responseJSON;
289 var title = "Error " + (data.code ? data.code : 'unknown');
290 var message = data.detail ? data.detail : 'No detail available.';
291 bootbox.alert({
292 title: title,
293 message: message
294 });
295 });
296 });
297
298 $("#formActionNS").submit(function (event) {
299 event.preventDefault(); //prevent default action
300 var post_url = $(this).attr("action"); //get form action url
301 var request_method = $(this).attr("method"); //get form GET/POST method
302 var form_data = new FormData(this); //Encode form elements for submission
303 console.log(post_url);
304 $.ajax({
305 url: post_url,
306 type: request_method,
307 data: form_data,
308 headers: {
309 "Accept": 'application/json'
310 },
311 contentType: false,
312 processData: false
313 }).done(function (response, textStatus, jqXHR) {
314 $('#modal_instance_new_action').modal('hide');
315 bootbox.alert({
316 title: "Action",
317 message: "Action received."
318 });
319 }).fail(function (result) {
320 var data = result.responseJSON;
321 var title = "Error " + (data.code ? data.code : 'unknown');
322 var message = data.detail ? data.detail : 'No detail available.';
323 bootbox.alert({
324 title: title,
325 message: message
326 });
327 });
328 });
329
330 $("#formAlarmNS").submit(function (event) {
331 event.preventDefault(); //prevent default action
332 var post_url = $(this).attr("action"); //get form action url
333 var request_method = $(this).attr("method"); //get form GET/POST method
334 var form_data = new FormData(this); //Encode form elements for submission
335 console.log(post_url);
336 $.ajax({
337 url: post_url,
338 type: request_method,
339 data: form_data,
340 headers: {
341 "Accept": 'application/json'
342 },
343 contentType: false,
344 processData: false
345 }).done(function (response, textStatus, jqXHR) {
346 $('#modal_instance_new_action').modal('hide');
347 bootbox.alert({
348 title: "Metric",
349 message: "Alarm created."
350 });
351 }).fail(function (result) {
352 var data = result.responseJSON;
353 var title = "Error " + (data.code ? data.code : 'unknown');
354 var message = data.detail ? data.detail : 'No detail available.';
355 bootbox.alert({
356 title: title,
357 message: message
358 });
359 });
360 });
361
362 $("#formExportMetricNS").submit(function (event) {
363 event.preventDefault(); //prevent default action
364 var post_url = $(this).attr("action"); //get form action url
365 var request_method = $(this).attr("method"); //get form GET/POST method
366 var form_data = new FormData(this); //Encode form elements for submission
367 console.log(post_url);
368 $.ajax({
369 url: post_url,
370 type: request_method,
371 data: form_data,
372 headers: {
373 "Accept": 'application/json'
374 },
375 contentType: false,
376 processData: false
377 }).done(function (response, textStatus, jqXHR) {
378 $('#modal_instance_new_action').modal('hide');
379 bootbox.alert({
380 title: "Metric",
381 message: "Metric exported."
382 });
383 }).fail(function (result) {
384 var data = result.responseJSON;
385 var title = "Error " + (data.code ? data.code : 'unknown');
386 var message = data.detail ? data.detail : 'No detail available.';
387 bootbox.alert({
388 title: title,
389 message: message
390 });
391 });
392 });
393
394 });