d04f46589df16991c87060fb78d395810563377d
[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 table.ajax.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 table.ajax.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 table.ajax.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 $formGroupClone.find('input').val('');
160 $formGroupClone.find('button').toggleClass('btn-success btn-add btn-danger btn-remove');
161 $formGroupClone.find('button').text('–');
162 $formGroupClone.insertAfter($formGroup);
163
164 };
165
166 var removeFormGroup = function (event) {
167 event.preventDefault();
168 var $formGroup = $(this).closest('.form-group');
169 $formGroup.remove();
170 };
171
172 var addInterfaceGroup = function (event) {
173 event.preventDefault();
174
175 var $formGroup = $(this).closest('.interface-group');
176 var $formGroupClone = $formGroup.clone();
177
178 $(this)
179 .toggleClass('btn-success btn-add btn-danger btn-remove')
180 .html('–');
181
182 $formGroupClone.find('input').val('');
183 $formGroupClone.insertAfter($formGroup);
184
185 };
186
187 var removeInterfaceGroup = function (event) {
188 event.preventDefault();
189 var $formGroup = $(this).closest('.interface-group');
190 $formGroup.remove();
191 };
192
193 function showTopology(type, instance_id) {
194 var url = '/instances/'+type+'/'+instance_id+'/topology';
195 window.location = url;
196 }
197
198 function showInstanceDetails(type, instance_id) {
199 var url_info = '/instances/'+type+'/'+instance_id;
200 var dialog = bootbox.dialog({
201 message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
202 closeButton: true
203 });
204 $.ajax({
205 url: url_info,
206 type: 'GET',
207 dataType: "json",
208 contentType: "application/json;charset=utf-8",
209 success: function (result) {
210
211 if (result['data'] !== undefined) {
212 editorJSON.setValue(JSON.stringify(result['data'], null, "\t"));
213 editorJSON.setOption("autoRefresh", true);
214 dialog.modal('hide');
215 $('#modal_show_instance').modal('show');
216 }
217 else {
218 dialog.modal('hide');
219 bootbox.alert("An error occurred while retrieving the information.");
220 }
221 },
222 error: function (result) {
223 dialog.modal('hide');
224 bootbox.alert("An error occurred while retrieving the information.");
225 }
226 });
227 }
228
229 var editorJSON;
230
231 $(document).ready(function () {
232 var json_editor_settings = {
233 mode: "javascript",
234 showCursorWhenSelecting: true,
235 autofocus: true,
236 lineNumbers: true,
237 lineWrapping: true,
238 foldGutter: true,
239 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
240 autoCloseBrackets: true,
241 matchBrackets: true,
242 extraKeys: {
243 "F11": function (cm) {
244 cm.setOption("fullScreen", !cm.getOption("fullScreen"));
245 },
246 "Esc": function (cm) {
247 if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
248 },
249 "Ctrl-Q": function (cm) {
250 cm.foldCode(cm.getCursor());
251 }
252 },
253 theme: "neat",
254 keyMap: "sublime"
255 };
256 var myJsonTextArea = document.getElementById("instance_view_json");
257 editorJSON = CodeMirror(function (elt) {
258 myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
259 }, json_editor_settings);
260
261
262 $(document).on('click', '.primitive-group .btn-add', addFormGroup);
263 $(document).on('click', '.primitive-group .btn-remove', removeFormGroup);
264
265 $(document).on('click', '.interface-group .btn-add', addInterfaceGroup);
266 $(document).on('click', '.interface-group .btn-remove', removeInterfaceGroup);
267
268
269 $("#formCreateNS").submit(function (event) {
270 event.preventDefault(); //prevent default action
271 var post_url = $(this).attr("action"); //get form action url
272 var request_method = $(this).attr("method"); //get form GET/POST method
273 var form_data = new FormData(this); //Encode form elements for submission
274 $.ajax({
275 url: post_url,
276 type: request_method,
277 data: form_data,
278 headers: {
279 "Accept": 'application/json'
280 },
281 contentType: false,
282 processData: false
283 }).done(function (response, textStatus, jqXHR) {
284 table.ajax.reload();
285 $('#modal_new_instance').modal('hide');
286 }).fail(function (result) {
287 var data = result.responseJSON;
288 var title = "Error " + (data.code ? data.code : 'unknown');
289 var message = data.detail ? data.detail : 'No detail available.';
290 bootbox.alert({
291 title: title,
292 message: message
293 });
294 });
295 });
296
297 $("#formCreatePDU").submit(function (event) {
298 event.preventDefault(); //prevent default action
299 var post_url = $(this).attr("action"); //get form action url
300 var request_method = $(this).attr("method"); //get form GET/POST method
301 var form_data = new FormData(this); //Encode form elements for submission
302 $.ajax({
303 url: post_url,
304 type: request_method,
305 data: form_data,
306 headers: {
307 "Accept": 'application/json'
308 },
309 contentType: false,
310 processData: false
311 }).done(function (response, textStatus, jqXHR) {
312 table.ajax.reload();
313 $('#modal_new_pdu').modal('hide');
314 }).fail(function (result) {
315 var data = result.responseJSON;
316 var title = "Error " + (data.code ? data.code : 'unknown');
317 var message = data.detail ? data.detail : 'No detail available.';
318 bootbox.alert({
319 title: title,
320 message: message
321 });
322 });
323 });
324
325 $("#formActionNS").submit(function (event) {
326 event.preventDefault(); //prevent default action
327 var post_url = $(this).attr("action"); //get form action url
328 var request_method = $(this).attr("method"); //get form GET/POST method
329 var form_data = new FormData(this); //Encode form elements for submission
330 console.log(post_url);
331 $.ajax({
332 url: post_url,
333 type: request_method,
334 data: form_data,
335 headers: {
336 "Accept": 'application/json'
337 },
338 contentType: false,
339 processData: false
340 }).done(function (response, textStatus, jqXHR) {
341 $('#modal_instance_new_action').modal('hide');
342 bootbox.alert({
343 title: "Action",
344 message: "Action received."
345 });
346 }).fail(function (result) {
347 var data = result.responseJSON;
348 var title = "Error " + (data.code ? data.code : 'unknown');
349 var message = data.detail ? data.detail : 'No detail available.';
350 bootbox.alert({
351 title: title,
352 message: message
353 });
354 });
355 });
356
357 $("#formAlarmNS").submit(function (event) {
358 event.preventDefault(); //prevent default action
359 var post_url = $(this).attr("action"); //get form action url
360 var request_method = $(this).attr("method"); //get form GET/POST method
361 var form_data = new FormData(this); //Encode form elements for submission
362 console.log(post_url);
363 $.ajax({
364 url: post_url,
365 type: request_method,
366 data: form_data,
367 headers: {
368 "Accept": 'application/json'
369 },
370 contentType: false,
371 processData: false
372 }).done(function (response, textStatus, jqXHR) {
373 $('#modal_instance_new_action').modal('hide');
374 bootbox.alert({
375 title: "Metric",
376 message: "Alarm created."
377 });
378 }).fail(function (result) {
379 var data = result.responseJSON;
380 var title = "Error " + (data.code ? data.code : 'unknown');
381 var message = data.detail ? data.detail : 'No detail available.';
382 bootbox.alert({
383 title: title,
384 message: message
385 });
386 });
387 });
388
389 $("#formExportMetricNS").submit(function (event) {
390 event.preventDefault(); //prevent default action
391 var post_url = $(this).attr("action"); //get form action url
392 var request_method = $(this).attr("method"); //get form GET/POST method
393 var form_data = new FormData(this); //Encode form elements for submission
394 console.log(post_url);
395 $.ajax({
396 url: post_url,
397 type: request_method,
398 data: form_data,
399 headers: {
400 "Accept": 'application/json'
401 },
402 contentType: false,
403 processData: false
404 }).done(function (response, textStatus, jqXHR) {
405 $('#modal_instance_new_action').modal('hide');
406 bootbox.alert({
407 title: "Metric",
408 message: "Metric exported."
409 });
410 }).fail(function (result) {
411 var data = result.responseJSON;
412 var title = "Error " + (data.code ? data.code : 'unknown');
413 var message = data.detail ? data.detail : 'No detail available.';
414 bootbox.alert({
415 title: title,
416 message: message
417 });
418 });
419 });
420
421 });