| lombardofr | 3218b2b | 2018-10-29 13:41:08 +0100 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 EveryUP Srl |
| 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 | import json |
| 18 | import logging |
| 19 | |
| 20 | import yaml |
| 21 | from sf_t3d.decorators import login_required |
| 22 | from django.http import HttpResponse |
| 23 | from django.shortcuts import render, redirect |
| 24 | |
| 25 | |
| 26 | from lib.util import Util |
| 27 | from lib.osm.osmclient.clientv2 import Client |
| lombardofr | 1e32006 | 2018-10-30 22:16:25 +0100 | [diff] [blame] | 28 | from lib.osm.osm_rdcl_parser import OsmParser |
| lombardofr | 3218b2b | 2018-10-29 13:41:08 +0100 | [diff] [blame] | 29 | import authosm.utils as osmutils |
| 30 | |
| 31 | logging.basicConfig(level=logging.DEBUG) |
| 32 | log = logging.getLogger('descriptorhandler/view.py') |
| 33 | |
| 34 | |
| 35 | @login_required |
| 36 | def show_descriptors(request, descriptor_type=None): |
| 37 | user = osmutils.get_user(request) |
| 38 | project_id = user.project_id |
| 39 | client = Client() |
| 40 | print descriptor_type |
| 41 | try: |
| 42 | if descriptor_type == 'nsd': |
| 43 | descriptors = client.nsd_list(user.get_token()) |
| 44 | elif descriptor_type == 'vnfd': |
| 45 | descriptors = client.vnfd_list(user.get_token()) |
| 46 | except Exception as e: |
| 47 | log.exception(e) |
| 48 | descriptors = [] |
| 49 | |
| 50 | url = 'osm/osm_project_descriptors.html' |
| 51 | return __response_handler(request, { |
| 52 | 'descriptors': descriptors['data'] if descriptors and descriptors['error'] is False else [], |
| 53 | 'project_id': project_id, |
| 54 | 'project_type': 'osm', |
| 55 | 'descriptor_type': descriptor_type |
| 56 | }, url) |
| 57 | |
| 58 | |
| 59 | @login_required |
| 60 | def delete_descriptor(request, descriptor_type=None, descriptor_id=None): |
| 61 | user = osmutils.get_user(request) |
| 62 | project_id = user.project_id |
| 63 | try: |
| 64 | client = Client() |
| 65 | if descriptor_type == 'nsd': |
| 66 | result = client.nsd_delete(user.get_token(), descriptor_id) |
| 67 | elif descriptor_type == 'vnfd': |
| 68 | result = client.vnfd_delete(user.get_token(), descriptor_id) |
| 69 | except Exception as e: |
| 70 | log.exception(e) |
| 71 | result = {'error': True, 'data': str(e)} |
| 72 | |
| 73 | url = 'osm/osm_project_descriptors.html' |
| 74 | descriptors = {} |
| 75 | try: |
| 76 | if descriptor_type == 'nsd': |
| 77 | descriptors = client.nsd_list(user.get_token()) |
| 78 | elif descriptor_type == 'vnfd': |
| 79 | descriptors = client.vnfd_list(user.get_token()) |
| 80 | except Exception as e: |
| 81 | log.exception(e) |
| 82 | |
| 83 | return __response_handler(request, { |
| 84 | 'descriptors': descriptors['data'] if descriptors and descriptors['error'] is False else [], |
| 85 | 'project_id': project_id, |
| 86 | 'project_type': 'osm', |
| 87 | 'descriptor_type': descriptor_type, |
| 88 | 'alert_message': { |
| 89 | 'success': False if result['error'] is True else True, |
| 90 | 'message': 'An error occurred while processing your request.' if result and result['error'] is True else "Record deleted successfully"} |
| 91 | }, url) |
| 92 | |
| 93 | |
| 94 | @login_required |
| 95 | def clone_descriptor(request, descriptor_type=None, descriptor_id=None): |
| 96 | user = osmutils.get_user(request) |
| 97 | project_id = user.project_id |
| 98 | |
| 99 | try: |
| 100 | client = Client() |
| 101 | if descriptor_type == 'nsd': |
| 102 | result = client.nsd_clone(user.get_token(), descriptor_id) |
| 103 | elif descriptor_type == 'vnfd': |
| 104 | result = client.vnfd_clone(user.get_token(), descriptor_id) |
| 105 | else: |
| 106 | log.debug('Update descriptor: Unknown data type') |
| 107 | result = {'error': True, 'data': 'Update descriptor: Unknown data type'} |
| 108 | except Exception as e: |
| 109 | log.exception(e) |
| 110 | result = {'error': True, 'data': str(e)} |
| 111 | if result['error'] == True: |
| 112 | return __response_handler(request, result['data'], url=None, |
| 113 | status=result['data']['status'] if 'status' in result['data'] else 500) |
| 114 | |
| 115 | else: |
| 116 | return __response_handler(request, {}, url=None, status=200) |
| 117 | |
| 118 | |
| 119 | @login_required |
| 120 | def new_descriptor(request, descriptor_type=None): |
| 121 | user = osmutils.get_user(request) |
| 122 | project_id = user.project_id |
| 123 | page = 'descriptor_new.html' |
| 124 | if request.method == 'GET': |
| 125 | request_id = request.GET.get('id', '') |
| 126 | |
| 127 | return __response_handler(request, { |
| 128 | 'project_id': project_id, |
| 129 | 'descriptor_type': descriptor_type, |
| 130 | 'descriptor_id': request_id, |
| 131 | }, page) |
| 132 | elif request.method == 'POST': |
| 133 | data_type = request.POST.get('type') |
| 134 | if data_type == "file": |
| 135 | file_uploaded = request.FILES['file'] |
| 136 | |
| 137 | try: |
| 138 | client = Client() |
| 139 | if descriptor_type == 'nsd': |
| 140 | result = client.nsd_onboard(user.get_token(), file_uploaded) |
| 141 | elif descriptor_type == 'vnfd': |
| 142 | result = client.vnfd_onboard(user.get_token(), file_uploaded) |
| 143 | else: |
| 144 | log.debug('Create descriptor: Unknown data type') |
| 145 | result = {'error': True, 'data': 'Create descriptor: Unknown data type'} |
| 146 | |
| 147 | except Exception as e: |
| 148 | log.exception(e) |
| 149 | result = {'error': True, 'data': str(e)} |
| 150 | else: |
| 151 | result = {'error': True, 'data': 'Create descriptor: Unknown data type'} |
| 152 | |
| 153 | if result['error']: |
| 154 | return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500) |
| 155 | else: |
| 156 | return __response_handler(request, {}, url=None, status=200) |
| 157 | |
| 158 | |
| 159 | @login_required |
| 160 | def edit_descriptor(request, descriptor_id=None, descriptor_type=None): |
| 161 | user = osmutils.get_user(request) |
| 162 | project_id = user.project_id |
| 163 | if request.method == 'POST': |
| 164 | new_data = request.POST.get('text'), |
| 165 | data_type = request.POST.get('type') |
| 166 | #print new_data |
| 167 | try: |
| 168 | client = Client() |
| 169 | if descriptor_type == 'nsd': |
| 170 | if data_type == 'yaml': |
| 171 | new_data = yaml.load(request.POST.get('text')) |
| 172 | elif data_type == 'json': |
| 173 | new_data = json.loads(request.POST.get('text')) |
| 174 | result = client.nsd_update(user.get_token(), descriptor_id, new_data) |
| 175 | elif descriptor_type == 'vnfd': |
| 176 | if data_type == 'yaml': |
| 177 | new_data = yaml.load(request.POST.get('text')) |
| 178 | elif data_type == 'json': |
| 179 | new_data = json.loads(request.POST.get('text')) |
| 180 | result = client.vnfd_update(user.get_token(), descriptor_id, new_data) |
| 181 | |
| 182 | else: |
| 183 | log.debug('Update descriptor: Unknown data type') |
| 184 | result = {'error': True, 'data': 'Update descriptor: Unknown data type'} |
| 185 | except Exception as e: |
| 186 | log.exception(e) |
| 187 | result = {'error': True, 'data': str(e)} |
| 188 | if result['error'] == True: |
| 189 | return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500) |
| 190 | |
| 191 | else: |
| 192 | return __response_handler(request, {}, url=None, status=200) |
| 193 | |
| 194 | elif request.method == 'GET': |
| 195 | |
| 196 | page = 'descriptor_view.html' |
| 197 | try: |
| 198 | client = Client() |
| 199 | if descriptor_type == 'nsd': |
| 200 | result = client.nsd_get(user.get_token(), descriptor_id) |
| 201 | elif descriptor_type == 'vnfd': |
| 202 | result = client.vnfd_get(user.get_token(), descriptor_id) |
| 203 | |
| 204 | except Exception as e: |
| 205 | log.exception(e) |
| 206 | result = {'error': True, 'data': str(e)} |
| 207 | |
| 208 | if isinstance(result, dict) and 'error' in result and result['error']: |
| 209 | return render(request, 'error.html') |
| 210 | |
| 211 | descriptor_string_json = json.dumps(result, indent=2) |
| 212 | descriptor_string_yaml = Util.json2yaml(result) |
| 213 | # print descriptor |
| 214 | return render(request, page, { |
| 215 | 'project_id': project_id, |
| 216 | 'descriptor_id': descriptor_id, |
| 217 | 'descriptor_type': descriptor_type, |
| 218 | 'descriptor_strings': {'descriptor_string_yaml': descriptor_string_yaml, |
| 219 | 'descriptor_string_json': descriptor_string_json}}) |
| 220 | |
| 221 | |
| 222 | @login_required |
| 223 | def get_package_files_list(request, descriptor_id, descriptor_type): |
| 224 | user = osmutils.get_user(request) |
| 225 | try: |
| 226 | client = Client() |
| 227 | if descriptor_type == 'nsd': |
| 228 | artifacts_res = client.nsd_artifacts(user.get_token(), descriptor_id) |
| 229 | elif descriptor_type == 'vnfd': |
| 230 | artifacts_res = client.vnf_packages_artifacts(user.get_token(), descriptor_id) |
| 231 | else: |
| 232 | return False |
| 233 | |
| 234 | files_list = yaml.load(artifacts_res['data'] if artifacts_res and artifacts_res['error'] is False else []) |
| 235 | result = {'files': files_list} |
| 236 | except Exception as e: |
| 237 | log.exception(e) |
| 238 | url = 'error.html' |
| 239 | result = {'error_msg': 'Unknown error.'} |
| 240 | return __response_handler(request, result) |
| 241 | |
| 242 | |
| 243 | @login_required |
| 244 | def download_pkg(request, descriptor_id, descriptor_type): |
| 245 | user = osmutils.get_user(request) |
| 246 | file_name = "osm_export.tar.gz" |
| 247 | tar_pkg = None |
| 248 | try: |
| 249 | client = Client() |
| 250 | if descriptor_type == 'nsd': |
| 251 | tar_pkg = client.get_nsd_pkg(user.get_token(), descriptor_id) |
| 252 | elif descriptor_type == 'vnfd': |
| 253 | tar_pkg = client.get_vnfd_pkg(user.get_token(), descriptor_id) |
| 254 | |
| 255 | except Exception as e: |
| 256 | log.exception(e) |
| 257 | |
| 258 | response = HttpResponse(content_type="application/tgz") |
| 259 | response["Content-Disposition"] = "attachment; filename="+ file_name |
| 260 | response.write(tar_pkg.getvalue()) |
| 261 | return response |
| 262 | |
| 263 | @login_required |
| 264 | def open_composer(request): |
| 265 | user = osmutils.get_user(request) |
| 266 | project_id = user.project_id |
| lombardofr | 1e32006 | 2018-10-30 22:16:25 +0100 | [diff] [blame] | 267 | descriptor_id = request.GET.get('id') |
| 268 | descriptor_type = request.GET.get('type') |
| 269 | client = Client() |
| 270 | if descriptor_id: |
| 271 | try: |
| 272 | if descriptor_type == 'nsd': |
| 273 | descriptor_result = client.nsd_get(user.get_token(), descriptor_id) |
| 274 | elif descriptor_type == 'vnfd': |
| 275 | descriptor_result = client.vnfd_get(user.get_token(), descriptor_id) |
| 276 | |
| 277 | except Exception as e: |
| 278 | descriptor_result = {'error': True, 'data': str(e)} |
| 279 | |
| 280 | if isinstance(descriptor_result, dict) and 'error' in descriptor_result and descriptor_result['error']: |
| 281 | return render(request, 'error.html') |
| 282 | |
| 283 | test = OsmParser() |
| 284 | # print nsr_object |
| 285 | if descriptor_type == 'nsd': |
| 286 | result = test.nsd_to_graph(descriptor_result) |
| 287 | elif descriptor_type == 'vnfd': |
| 288 | result = test.vnfd_to_graph(descriptor_result) |
| 289 | return __response_handler(request, result,'composer.html') |
| 290 | |
| lombardofr | 3218b2b | 2018-10-29 13:41:08 +0100 | [diff] [blame] | 291 | result = {'project_id': project_id, |
| 292 | 'vertices': [ |
| 293 | {"info": {"type": "vnf", "property": {"custom_label": ""}, |
| 294 | "group": []}, "id": "vm"}, |
| 295 | {"info": {"type": "vnf", "property": {"custom_label": ""}, |
| 296 | "group": []}, "id": "vlan"}, |
| 297 | {"info": {"type": "vnf", "property": {"custom_label": ""}, |
| 298 | "group": []}, "id": "firewall"}, |
| 299 | {"info": {"type": "vnf", "property": {"custom_label": ""}, |
| 300 | "group": []}, "id": "ping"}, |
| 301 | |
| 302 | {"info": {"type": "ns_vl", "property": {"custom_label": ""}, |
| 303 | "group": []}, "id": "vl1"}, |
| 304 | {"info": {"type": "ns_vl", "property": {"custom_label": ""}, |
| 305 | "group": []}, "id": "vl2"}, |
| 306 | {"info": {"type": "ns_vl", "property": {"custom_label": ""}, |
| 307 | "group": []}, "id": "vl3"}, |
| 308 | ], |
| 309 | 'edges': [ |
| 310 | {"source": "vm", "group": [], "target": "vl3", "view": "ns"}, |
| 311 | {"source": "vlan", "group": [], "target": "vl3", "view": "ns"}, |
| 312 | {"source": "vlan", "group": [], "target": "vl1", "view": "ns"}, |
| 313 | {"source": "firewall", "group": [], "target": "vl1", "view": "ns"}, |
| 314 | {"source": "firewall", "group": [], "target": "vl2", "view": "ns"}, |
| 315 | {"source": "ping", "group": [], "target": "vl2", "view": "ns"}, |
| 316 | ], |
| 317 | 'model': { |
| 318 | "layer": { |
| 319 | |
| 320 | "ns": { |
| 321 | "nodes": { |
| 322 | "vnf": { |
| 323 | "addable": { |
| 324 | "callback": "addNode" |
| 325 | }, |
| 326 | "removable": { |
| 327 | "callback": "removeNode" |
| 328 | }, |
| 329 | "expands": "vnf" |
| 330 | }, |
| 331 | "ns_vl": { |
| 332 | "addable": { |
| 333 | "callback": "addNode" |
| 334 | }, |
| 335 | "removable": { |
| 336 | "callback": "removeNode" |
| 337 | } |
| 338 | }, |
| 339 | |
| 340 | }, |
| 341 | "allowed_edges": { |
| 342 | "ns_vl": { |
| 343 | "destination": { |
| 344 | "vnf": { |
| 345 | "callback": "addLink", |
| 346 | "direct_edge": False, |
| 347 | "removable": { |
| 348 | "callback": "removeLink" |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | }, |
| 353 | "vnf": { |
| 354 | "destination": { |
| 355 | "ns_vl": { |
| 356 | "callback": "addLink", |
| 357 | "direct_edge": False, |
| 358 | "removable": { |
| 359 | "callback": "removeLink" |
| 360 | } |
| 361 | }, |
| 362 | |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | } |
| 367 | }, |
| 368 | "vnf": { |
| 369 | "nodes": { |
| 370 | "vdu": { |
| 371 | "addable": { |
| 372 | "callback": "addNode" |
| 373 | }, |
| 374 | "removable": { |
| 375 | "callback": "removeNode" |
| 376 | } |
| 377 | }, |
| 378 | "cp": { |
| 379 | "addable": { |
| 380 | "callback": "addNode" |
| 381 | }, |
| 382 | "removable": { |
| 383 | "callback": "removeNode" |
| 384 | } |
| 385 | }, |
| 386 | |
| 387 | }, |
| 388 | "allowed_edges": { |
| 389 | "vdu": { |
| 390 | "destination": { |
| 391 | "cp": { |
| 392 | "callback": "addLink", |
| 393 | "direct_edge": False, |
| 394 | "removable": { |
| 395 | "callback": "removeLink" |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | }, |
| 400 | "cp": { |
| 401 | "destination": { |
| 402 | "vdu": { |
| 403 | "callback": "addLink", |
| 404 | "direct_edge": False, |
| 405 | "removable": { |
| 406 | "callback": "removeLink" |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | }, |
| 413 | "name": "OSM", |
| 414 | "version": 1, |
| 415 | "nodes": { |
| 416 | "vnf": { |
| 417 | "label": "vnf" |
| 418 | }, |
| 419 | "ns_vl": { |
| 420 | "label": "vl" |
| 421 | }, |
| 422 | "cp": { |
| 423 | "label": "cp" |
| 424 | }, |
| 425 | "vdu": { |
| 426 | "label": "vdu" |
| 427 | } |
| 428 | }, |
| 429 | "description": "osm", |
| 430 | "callback": { |
| 431 | "addNode": { |
| 432 | "file": "osm_controller.js", |
| 433 | "class": "OsmController" |
| 434 | }, |
| 435 | "removeNode": { |
| 436 | "file": "osm_controller.js", |
| 437 | "class": "OsmController" |
| 438 | }, |
| 439 | "addLink": { |
| 440 | "file": "osm_controller.js", |
| 441 | "class": "OsmController" |
| 442 | }, |
| 443 | "removeLink": { |
| 444 | "file": "osm_controller.js", |
| 445 | "class": "OsmController" |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | } |
| 450 | }} |
| 451 | return __response_handler(request, result, 'composer.html') |
| 452 | |
| 453 | |
| 454 | def get_available_nodes(request): |
| 455 | |
| 456 | params = request.GET.dict() |
| 457 | nodes = { |
| 458 | 'ns': [{"types": [{"name": "Generic", "id": "vnf"}, |
| 459 | {"name": "ping", "id": "vnf"}, |
| 460 | {"name": "pong", "id": "vnf"}, |
| 461 | {"name": "hackfest1-vm", "id": "vnf"}], "category_name": "Vnf"}, |
| 462 | {"types": [{"name": "VL", "id": "ns_vl"}], "category_name": "VirtualLink"}], |
| 463 | 'vnf': [{"types": [{"name": "VDU", "id": "vdu"}], "category_name": "Vdu"}, |
| 464 | {"types": [{"name": "CP", "id": "cp"}], "category_name": "CP"}] |
| 465 | } |
| 466 | |
| 467 | return __response_handler(request, nodes[params['layer']]) |
| 468 | |
| 469 | |
| 470 | @login_required |
| 471 | def custom_action(request, descriptor_id=None, descriptor_type=None, action_name=None): |
| 472 | if request.method == 'GET': |
| 473 | return globals()[action_name](request, descriptor_id, descriptor_type) |
| 474 | |
| 475 | |
| 476 | def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): |
| 477 | raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') |
| 478 | if 'application/json' in raw_content_types or url is None: |
| 479 | return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs) |
| 480 | elif to_redirect: |
| 481 | return redirect(url, *args, **kwargs) |
| 482 | else: |
| 483 | return render(request, url, data_res) |