| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 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 | if (typeof dreamer === 'undefined') { |
| 18 | var dreamer = {}; |
| 19 | } |
| 20 | var level = {} |
| 21 | |
| 22 | dreamer.GraphRequests = (function(global) { |
| 23 | 'use strict'; |
| 24 | |
| 25 | var DEBUG = true; |
| 26 | |
| 27 | GraphRequests.prototype.constructor = GraphRequests; |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | */ |
| 32 | function GraphRequests(args) { |
| 33 | |
| 34 | |
| 35 | } |
| 36 | |
| 37 | GraphRequests.prototype.addNode = function(args, choice, success, error) { |
| 38 | var data = new FormData(); |
| 39 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 40 | |
| 41 | data = args_to_formdata(args, data); |
| 42 | |
| 43 | //FIXME questo metodo dovrebbere essere generico |
| 44 | if(args.existing_element) |
| 45 | data.append('existing_element', args.existing_element ? args.existing_element : false) |
| 46 | //if (choice) |
| 47 | // data.append('choice', choice); |
| 48 | $.ajax({ |
| 49 | url: "addelement", |
| 50 | type: 'POST', |
| 51 | data: data, |
| 52 | cache: false, |
| 53 | contentType: false, |
| 54 | processData: false, |
| 55 | success: function(result) { |
| 56 | if (success) |
| 57 | success(); |
| 58 | }, |
| 59 | error: function(result) { |
| 60 | if (error) |
| 61 | error(result); |
| 62 | log("some error: " + result); |
| 63 | } |
| 64 | }); |
| 65 | }; |
| 66 | |
| 67 | GraphRequests.prototype.removeNode = function(args, choice, success, error) { |
| 68 | var data = new FormData(); |
| 69 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 70 | |
| 71 | data = args_to_formdata(args, data); |
| 72 | |
| 73 | $.ajax({ |
| 74 | url: "removeelement", |
| 75 | type: 'POST', |
| 76 | data: data, |
| 77 | cache: false, |
| 78 | contentType: false, |
| 79 | processData: false, |
| 80 | success: function(result) { |
| 81 | if (success) |
| 82 | success(); |
| 83 | }, |
| 84 | error: function(result) { |
| 85 | if (error) |
| 86 | error(result); |
| 87 | } |
| 88 | }); |
| 89 | }; |
| 90 | |
| 91 | GraphRequests.prototype.getNodeOverview = function(args, success, error) { |
| 92 | |
| 93 | var params = jQuery.param(args) |
| 94 | console.log("params", params) |
| 95 | $.ajax({ |
| 96 | url: "overviewelement?"+params, |
| 97 | type: 'GET', |
| 98 | success: function(result) { |
| 99 | if (success) |
| 100 | success(result); |
| 101 | }, |
| 102 | error: function(result) { |
| 103 | if (error) |
| 104 | error(result); |
| 105 | } |
| 106 | }); |
| 107 | }; |
| 108 | |
| 109 | GraphRequests.prototype.addLink = function(args, choice, success, error) { |
| 110 | var data = new FormData(); |
| 111 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 112 | data = args_to_formdata(args, data); |
| 113 | |
| 114 | //data.append('destination', JSON.stringify(destination)); |
| 115 | if (choice) |
| 116 | data.append('choice', choice); |
| 117 | //if(link.desc_id) |
| 118 | // data.append('element_desc_id', link.desc_id || ''); |
| 119 | $.ajax({ |
| 120 | url: "addlink", |
| 121 | type: 'POST', |
| 122 | data: data, |
| 123 | cache: false, |
| 124 | contentType: false, |
| 125 | processData: false, |
| 126 | success: function(result) { |
| 127 | if (success) |
| 128 | success(); |
| 129 | }, |
| 130 | error: function(result) { |
| 131 | if (error) |
| 132 | error(result); |
| 133 | log("some error: " + result); |
| 134 | } |
| 135 | }); |
| 136 | }; |
| 137 | |
| 138 | GraphRequests.prototype.removeLink = function(args, success, error) { |
| 139 | var data = new FormData(); |
| 140 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 141 | data = args_to_formdata(args, data); |
| 142 | |
| 143 | $.ajax({ |
| 144 | url: "removelink", |
| 145 | type: 'POST', |
| 146 | data: data, |
| 147 | cache: false, |
| 148 | contentType: false, |
| 149 | processData: false, |
| 150 | success: function(result) { |
| 151 | if (success) |
| 152 | success(); |
| 153 | }, |
| 154 | error: function(result) { |
| 155 | if (error) |
| 156 | error(result); |
| 157 | log("some error: " + result); |
| 158 | } |
| 159 | }); |
| 160 | }; |
| 161 | |
| 162 | // |
| 163 | GraphRequests.prototype.getAvailableNodes = function(args, success, error){ |
| 164 | var data = new FormData(); |
| 165 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 166 | $.ajax({ |
| 167 | url: "availablenodes?layer="+args.layer, |
| 168 | type: 'GET', |
| 169 | success: function(result) { |
| 170 | if (success) |
| 171 | success(result); |
| 172 | }, |
| 173 | error: function(result) { |
| 174 | if (error) |
| 175 | error(result); |
| 176 | log("some error: " + result); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | GraphRequests.prototype.savePositions = function(positions, success, error) { |
| 182 | var data = new FormData(); |
| 183 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 184 | data.append('positions', JSON.stringify(positions)); |
| 185 | $.ajax({ |
| 186 | url: "positions", |
| 187 | type: 'POST', |
| 188 | data: data, |
| 189 | cache: false, |
| 190 | contentType: false, |
| 191 | processData: false, |
| 192 | success: function(result) { |
| 193 | if (success) |
| 194 | success(); |
| 195 | }, |
| 196 | error: function(result) { |
| 197 | if (error) |
| 198 | error(result); |
| 199 | log("some error: " + result); |
| 200 | } |
| 201 | }); |
| 202 | }; |
| 203 | |
| 204 | /* START ETSI methods */ |
| 205 | GraphRequests.prototype.addVnffg = function(args, success, error) { |
| 206 | var data = new FormData(); |
| 207 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 208 | /* data.append('group_id', args.info.group[0]); |
| 209 | data.append('element_id', args.id); |
| 210 | data.append('element_type', args.info.type);*/ |
| 211 | data = args_to_formdata(args, data); |
| 212 | $.ajax({ |
| 213 | url: "addelement", |
| 214 | type: 'POST', |
| 215 | data: data, |
| 216 | cache: false, |
| 217 | contentType: false, |
| 218 | processData: false, |
| 219 | success: function(result) { |
| 220 | if (success) |
| 221 | success(result); |
| 222 | }, |
| 223 | error: function(result) { |
| 224 | if (error) |
| 225 | error(result); |
| 226 | log("some error: " + result); |
| 227 | } |
| 228 | }); |
| 229 | }; |
| 230 | |
| 231 | GraphRequests.prototype.addNodeToVnffg = function(args, success, error) { |
| 232 | var data = new FormData(); |
| 233 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 234 | /* data.append('group_id', args.info.group[0]); |
| 235 | data.append('element_id', args.id); |
| 236 | data.append('element_type', args.info.type); |
| 237 | data.append('vnffg_id', args.vnffgId);*/ |
| 238 | data = args_to_formdata(args, data); |
| 239 | |
| 240 | $.ajax({ |
| 241 | url: "addnodetovnffg", |
| 242 | type: 'POST', |
| 243 | data: data, |
| 244 | cache: false, |
| 245 | contentType: false, |
| 246 | processData: false, |
| 247 | success: function(result) { |
| 248 | if (success) |
| 249 | success(result); |
| 250 | }, |
| 251 | error: function(result) { |
| 252 | if (error) |
| 253 | error(result); |
| 254 | log("some error: " + result); |
| 255 | } |
| 256 | }); |
| 257 | }; |
| 258 | |
| 259 | GraphRequests.prototype.getUnusedVnf = function(nsd_id, success, error) { |
| 260 | var data = new FormData(); |
| 261 | data.append('csrfmiddlewaretoken', this.getCookie('csrftoken')); |
| 262 | $.ajax({ |
| 263 | url: "unusedvnf/" + nsd_id, |
| 264 | type: 'GET', |
| 265 | success: function(result) { |
| 266 | if (success) |
| 267 | success(result); |
| 268 | }, |
| 269 | error: function(result) { |
| 270 | if (error) |
| 271 | error(result); |
| 272 | log("some error: " + result); |
| 273 | } |
| 274 | }); |
| 275 | |
| 276 | }; |
| 277 | /* END ETSI methods */ |
| 278 | |
| 279 | GraphRequests.prototype.getCookie = function(name) { |
| 280 | var cookieValue = null; |
| 281 | if (document.cookie && document.cookie !== '') { |
| 282 | var cookies = document.cookie.split(';'); |
| 283 | for (var i = 0; i < cookies.length; i++) { |
| 284 | var cookie = jQuery.trim(cookies[i]); |
| 285 | // Does this cookie string begin with the name we want? |
| 286 | if (cookie.substring(0, name.length + 1) === (name + '=')) { |
| 287 | cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | return cookieValue; |
| 293 | }; |
| 294 | |
| 295 | function args_to_formdata(args, form_data){ |
| 296 | for ( var key in args ) { |
| 297 | form_data.append(key, args[key]); |
| 298 | } |
| 299 | return form_data; |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | /** |
| 304 | * Log utility |
| 305 | */ |
| 306 | function log(text) { |
| 307 | if (DEBUG) |
| 308 | console.log("::GraphRequests::", text); |
| 309 | } |
| 310 | |
| 311 | return GraphRequests; |
| 312 | |
| 313 | |
| 314 | }(this)); |
| 315 | |
| 316 | if (typeof module === 'object') { |
| 317 | module.exports = dreamer.GraphRequests; |
| 318 | } |