blob: 9ace8ee61bab8088241330576358975211d50e13 [file] [log] [blame]
lombardoffb37bca2018-05-03 16:20:04 +02001#
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
17from django.shortcuts import render, redirect
lombardofdd73c0c2018-05-09 10:46:49 +020018from django.contrib.auth.decorators import login_required
lombardoffb37bca2018-05-03 16:20:04 +020019from django.http import HttpResponse, JsonResponse
lombardofdd73c0c2018-05-09 10:46:49 +020020import yaml
lombardoffb37bca2018-05-03 16:20:04 +020021import logging
22from lib.osm.osmclient.client import Client
23
24
25@login_required
26def list(request, project_id=None, type=None):
27 client = Client()
28 if type == 'ns':
29 result = client.ns_list()
30
31 return __response_handler(request, {'instances': result, 'type': 'ns', 'project_id': project_id}, 'instance_list.html')
32
33
34@login_required
35def create(request, project_id=None):
36 result = {}
37 ns_data = {
38 "nsName": request.POST.get('nsName', 'WithoutName'),
39 "nsDescription": request.POST.get('nsDescription', ''),
40 "nsdId": request.POST.get('nsdId', ''),
41 "vimAccountId": request.POST.get('vimAccountId', ''),
lombardoffb37bca2018-05-03 16:20:04 +020042 }
lombardof1a6af282018-05-10 11:49:32 +020043 if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
lombardofdd73c0c2018-05-09 10:46:49 +020044 ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')]
45
46 if 'config' in request.POST:
47 ns_config = yaml.load(request.POST.get('config'))
lombardof1a6af282018-05-10 11:49:32 +020048 if isinstance(ns_config, dict):
49 if "vim-network-name" in ns_config:
50 ns_config["vld"] = ns_config.pop("vim-network-name")
51 if "vld" in ns_config:
52 for vld in ns_config["vld"]:
53 if vld.get("vim-network-name"):
54 if isinstance(vld["vim-network-name"], dict):
55 vim_network_name_dict = {}
56 for vim_account, vim_net in vld["vim-network-name"].items():
57 vim_network_name_dict[ns_data["vimAccountId"]] = vim_net
58 vld["vim-network-name"] = vim_network_name_dict
59 ns_data["vld"] = ns_config["vld"]
60 if "vnf" in ns_config:
61 for vnf in ns_config["vnf"]:
62 if vnf.get("vim_account"):
63 vnf["vimAccountId"] = ns_data["vimAccountId"]
lombardofdd73c0c2018-05-09 10:46:49 +020064
lombardof1a6af282018-05-10 11:49:32 +020065 ns_data["vnf"] = ns_config["vnf"]
lombardoffb37bca2018-05-03 16:20:04 +020066 print ns_data
67 client = Client()
68 result = client.ns_create(ns_data)
69 return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id)
70
lombardof74ed51a2018-05-11 01:07:01 +020071@login_required
72def ns_operations(request, project_id=None, instance_id=None, type=None):
73 client = Client()
74 result = client.ns_op_list(instance_id)
75 return __response_handler(request, {'operations': result, 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html')
76
77@login_required
78def ns_operation(request, op_id, project_id=None, instance_id=None, type=None):
79 client = Client()
80 result = client.ns_op(op_id)
81 return __response_handler(request, result)
lombardoffb37bca2018-05-03 16:20:04 +020082
83@login_required
84def action(request, project_id=None, instance_id=None, type=None):
lombardofdd73c0c2018-05-09 10:46:49 +020085
lombardoffb37bca2018-05-03 16:20:04 +020086 client = Client()
87
88 # result = client.ns_action(instance_id, action_payload)
89 primitive_param_keys = request.POST.getlist('primitive_params_name')
90 primitive_param_value = request.POST.getlist('primitive_params_value')
91 action_payload = {
92 "vnf_member_index": request.POST.get('vnf_member_index'),
93 "primitive": request.POST.get('primitive'),
94 "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0}
95 }
96
97 result = client.ns_action(instance_id, action_payload)
lombardof35e62b02018-05-25 16:15:12 +020098 return __response_handler(request, result, None, to_redirect=False, status=result['status'] if 'status' in result else None )
lombardoffb37bca2018-05-03 16:20:04 +020099
100
101@login_required
102def delete(request, project_id=None, instance_id=None, type=None):
103 result = {}
104 client = Client()
105 result = client.ns_delete(instance_id)
106 print result
107 return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id)
108
109
110@login_required
111def show(request, project_id=None, instance_id=None, type=None):
112 # result = {}
113 client = Client()
114 result = client.ns_get(instance_id)
115 print result
116 return __response_handler(request, result)
117
118
119def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
120 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
121 if 'application/json' in raw_content_types or url is None:
122 return JsonResponse(data_res, *args, **kwargs)
123 elif to_redirect:
124 return redirect(url, *args, **kwargs)
125 else:
126 return render(request, url, data_res)