blob: a5118f639aba19338b8da9c7520002789e0c8cb0 [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()
lombardof647aa2e2018-05-26 17:11:13 +020030 elif type == 'vnf':
31 result = client.vnf_list()
lombardoffb37bca2018-05-03 16:20:04 +020032
lombardof647aa2e2018-05-26 17:11:13 +020033 return __response_handler(request, {'instances': result, 'type': type, 'project_id': project_id}, 'instance_list.html')
lombardoffb37bca2018-05-03 16:20:04 +020034
35
36@login_required
37def create(request, project_id=None):
38 result = {}
39 ns_data = {
40 "nsName": request.POST.get('nsName', 'WithoutName'),
41 "nsDescription": request.POST.get('nsDescription', ''),
42 "nsdId": request.POST.get('nsdId', ''),
43 "vimAccountId": request.POST.get('vimAccountId', ''),
lombardoffb37bca2018-05-03 16:20:04 +020044 }
lombardof1a6af282018-05-10 11:49:32 +020045 if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
lombardofdd73c0c2018-05-09 10:46:49 +020046 ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')]
47
48 if 'config' in request.POST:
49 ns_config = yaml.load(request.POST.get('config'))
lombardof1a6af282018-05-10 11:49:32 +020050 if isinstance(ns_config, dict):
51 if "vim-network-name" in ns_config:
52 ns_config["vld"] = ns_config.pop("vim-network-name")
53 if "vld" in ns_config:
54 for vld in ns_config["vld"]:
55 if vld.get("vim-network-name"):
56 if isinstance(vld["vim-network-name"], dict):
57 vim_network_name_dict = {}
58 for vim_account, vim_net in vld["vim-network-name"].items():
59 vim_network_name_dict[ns_data["vimAccountId"]] = vim_net
60 vld["vim-network-name"] = vim_network_name_dict
61 ns_data["vld"] = ns_config["vld"]
62 if "vnf" in ns_config:
63 for vnf in ns_config["vnf"]:
64 if vnf.get("vim_account"):
65 vnf["vimAccountId"] = ns_data["vimAccountId"]
lombardofdd73c0c2018-05-09 10:46:49 +020066
lombardof1a6af282018-05-10 11:49:32 +020067 ns_data["vnf"] = ns_config["vnf"]
lombardoffb37bca2018-05-03 16:20:04 +020068 print ns_data
69 client = Client()
70 result = client.ns_create(ns_data)
71 return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id)
72
lombardof74ed51a2018-05-11 01:07:01 +020073@login_required
74def ns_operations(request, project_id=None, instance_id=None, type=None):
75 client = Client()
76 result = client.ns_op_list(instance_id)
77 return __response_handler(request, {'operations': result, 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html')
78
79@login_required
80def ns_operation(request, op_id, project_id=None, instance_id=None, type=None):
81 client = Client()
82 result = client.ns_op(op_id)
83 return __response_handler(request, result)
lombardoffb37bca2018-05-03 16:20:04 +020084
85@login_required
86def action(request, project_id=None, instance_id=None, type=None):
lombardofdd73c0c2018-05-09 10:46:49 +020087
lombardoffb37bca2018-05-03 16:20:04 +020088 client = Client()
89
90 # result = client.ns_action(instance_id, action_payload)
91 primitive_param_keys = request.POST.getlist('primitive_params_name')
92 primitive_param_value = request.POST.getlist('primitive_params_value')
93 action_payload = {
94 "vnf_member_index": request.POST.get('vnf_member_index'),
95 "primitive": request.POST.get('primitive'),
96 "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0}
97 }
98
99 result = client.ns_action(instance_id, action_payload)
lombardof35e62b02018-05-25 16:15:12 +0200100 return __response_handler(request, result, None, to_redirect=False, status=result['status'] if 'status' in result else None )
lombardoffb37bca2018-05-03 16:20:04 +0200101
102
103@login_required
104def delete(request, project_id=None, instance_id=None, type=None):
105 result = {}
106 client = Client()
107 result = client.ns_delete(instance_id)
108 print result
109 return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id)
110
111
112@login_required
113def show(request, project_id=None, instance_id=None, type=None):
114 # result = {}
115 client = Client()
lombardof647aa2e2018-05-26 17:11:13 +0200116 if type == 'ns':
117 result = client.ns_get(instance_id)
118 elif type == 'vnf':
119 result = client.vnf_get(instance_id)
lombardoffb37bca2018-05-03 16:20:04 +0200120 print result
121 return __response_handler(request, result)
122
123
124def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
125 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
126 if 'application/json' in raw_content_types or url is None:
127 return JsonResponse(data_res, *args, **kwargs)
128 elif to_redirect:
129 return redirect(url, *args, **kwargs)
130 else:
131 return render(request, url, data_res)