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