| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 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 | from django.shortcuts import render, redirect |
| lombardof | dd73c0c | 2018-05-09 10:46:49 +0200 | [diff] [blame] | 18 | from django.contrib.auth.decorators import login_required |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 19 | from django.http import HttpResponse, JsonResponse |
| lombardof | dd73c0c | 2018-05-09 10:46:49 +0200 | [diff] [blame] | 20 | import yaml |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 21 | import logging |
| 22 | from lib.osm.osmclient.client import Client |
| 23 | |
| 24 | |
| 25 | @login_required |
| 26 | def list(request, project_id=None, type=None): |
| 27 | client = Client() |
| 28 | if type == 'ns': |
| 29 | result = client.ns_list() |
| lombardof | 647aa2e | 2018-05-26 17:11:13 +0200 | [diff] [blame] | 30 | elif type == 'vnf': |
| 31 | result = client.vnf_list() |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 32 | |
| lombardof | a03da5e | 2018-06-02 18:36:44 +0200 | [diff] [blame] | 33 | result = {'instances': result, 'type': type, 'project_id': project_id} |
| 34 | |
| 35 | return __response_handler(request, result, 'instance_list.html') |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 36 | |
| 37 | |
| 38 | @login_required |
| 39 | def create(request, project_id=None): |
| 40 | result = {} |
| 41 | ns_data = { |
| 42 | "nsName": request.POST.get('nsName', 'WithoutName'), |
| 43 | "nsDescription": request.POST.get('nsDescription', ''), |
| 44 | "nsdId": request.POST.get('nsdId', ''), |
| 45 | "vimAccountId": request.POST.get('vimAccountId', ''), |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 46 | } |
| lombardof | 1a6af28 | 2018-05-10 11:49:32 +0200 | [diff] [blame] | 47 | if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '': |
| lombardof | dd73c0c | 2018-05-09 10:46:49 +0200 | [diff] [blame] | 48 | ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')] |
| 49 | |
| 50 | if 'config' in request.POST: |
| 51 | ns_config = yaml.load(request.POST.get('config')) |
| lombardof | 1a6af28 | 2018-05-10 11:49:32 +0200 | [diff] [blame] | 52 | if isinstance(ns_config, dict): |
| 53 | if "vim-network-name" in ns_config: |
| 54 | ns_config["vld"] = ns_config.pop("vim-network-name") |
| 55 | if "vld" in ns_config: |
| 56 | for vld in ns_config["vld"]: |
| 57 | if vld.get("vim-network-name"): |
| 58 | if isinstance(vld["vim-network-name"], dict): |
| 59 | vim_network_name_dict = {} |
| 60 | for vim_account, vim_net in vld["vim-network-name"].items(): |
| 61 | vim_network_name_dict[ns_data["vimAccountId"]] = vim_net |
| 62 | vld["vim-network-name"] = vim_network_name_dict |
| 63 | ns_data["vld"] = ns_config["vld"] |
| 64 | if "vnf" in ns_config: |
| 65 | for vnf in ns_config["vnf"]: |
| 66 | if vnf.get("vim_account"): |
| 67 | vnf["vimAccountId"] = ns_data["vimAccountId"] |
| lombardof | dd73c0c | 2018-05-09 10:46:49 +0200 | [diff] [blame] | 68 | |
| lombardof | 1a6af28 | 2018-05-10 11:49:32 +0200 | [diff] [blame] | 69 | ns_data["vnf"] = ns_config["vnf"] |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 70 | print ns_data |
| 71 | client = Client() |
| 72 | result = client.ns_create(ns_data) |
| 73 | return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id) |
| 74 | |
| lombardof | 74ed51a | 2018-05-11 01:07:01 +0200 | [diff] [blame] | 75 | @login_required |
| 76 | def ns_operations(request, project_id=None, instance_id=None, type=None): |
| 77 | client = Client() |
| 78 | result = client.ns_op_list(instance_id) |
| 79 | return __response_handler(request, {'operations': result, 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html') |
| 80 | |
| 81 | @login_required |
| 82 | def ns_operation(request, op_id, project_id=None, instance_id=None, type=None): |
| 83 | client = Client() |
| 84 | result = client.ns_op(op_id) |
| 85 | return __response_handler(request, result) |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 86 | |
| 87 | @login_required |
| 88 | def action(request, project_id=None, instance_id=None, type=None): |
| lombardof | dd73c0c | 2018-05-09 10:46:49 +0200 | [diff] [blame] | 89 | |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 90 | client = Client() |
| 91 | |
| 92 | # result = client.ns_action(instance_id, action_payload) |
| 93 | primitive_param_keys = request.POST.getlist('primitive_params_name') |
| 94 | primitive_param_value = request.POST.getlist('primitive_params_value') |
| 95 | action_payload = { |
| 96 | "vnf_member_index": request.POST.get('vnf_member_index'), |
| 97 | "primitive": request.POST.get('primitive'), |
| 98 | "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0} |
| 99 | } |
| 100 | |
| 101 | result = client.ns_action(instance_id, action_payload) |
| lombardof | 35e62b0 | 2018-05-25 16:15:12 +0200 | [diff] [blame] | 102 | return __response_handler(request, result, None, to_redirect=False, status=result['status'] if 'status' in result else None ) |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 103 | |
| 104 | |
| 105 | @login_required |
| 106 | def delete(request, project_id=None, instance_id=None, type=None): |
| lombardof | 7e33ad6 | 2018-06-03 16:13:38 +0200 | [diff] [blame] | 107 | force = bool(request.GET.get('force', False)) |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 108 | result = {} |
| 109 | client = Client() |
| lombardof | 7e33ad6 | 2018-06-03 16:13:38 +0200 | [diff] [blame] | 110 | result = client.ns_delete(instance_id, force) |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 111 | print result |
| 112 | return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id) |
| 113 | |
| 114 | |
| 115 | @login_required |
| 116 | def show(request, project_id=None, instance_id=None, type=None): |
| 117 | # result = {} |
| 118 | client = Client() |
| lombardof | 647aa2e | 2018-05-26 17:11:13 +0200 | [diff] [blame] | 119 | if type == 'ns': |
| 120 | result = client.ns_get(instance_id) |
| 121 | elif type == 'vnf': |
| 122 | result = client.vnf_get(instance_id) |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 123 | print result |
| 124 | return __response_handler(request, result) |
| 125 | |
| 126 | |
| 127 | def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): |
| 128 | raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') |
| 129 | if 'application/json' in raw_content_types or url is None: |
| 130 | return JsonResponse(data_res, *args, **kwargs) |
| 131 | elif to_redirect: |
| 132 | return redirect(url, *args, **kwargs) |
| 133 | else: |
| 134 | return render(request, url, data_res) |