9ace8ee61bab8088241330576358975211d50e13
[osm/LW-UI.git] / instancehandler / views.py
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
18 from django.contrib.auth.decorators import login_required
19 from django.http import HttpResponse, JsonResponse
20 import yaml
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()
30
31 return __response_handler(request, {'instances': result, 'type': 'ns', 'project_id': project_id}, 'instance_list.html')
32
33
34 @login_required
35 def 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', ''),
42 }
43 if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
44 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'))
48 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"]
64
65 ns_data["vnf"] = ns_config["vnf"]
66 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
71 @login_required
72 def 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
78 def 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)
82
83 @login_required
84 def action(request, project_id=None, instance_id=None, type=None):
85
86 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)
98 return __response_handler(request, result, None, to_redirect=False, status=result['status'] if 'status' in result else None )
99
100
101 @login_required
102 def 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
111 def 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
119 def __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)