blob: 212fbbf59f4988450e9f5c065ce455100d67c808 [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
lombardofrf5776442018-06-26 10:37:40 +020021import json
lombardoffb37bca2018-05-03 16:20:04 +020022import logging
lombardofrf5776442018-06-26 10:37:40 +020023from lib.osm.osmclient.clientv2 import Client
lombardofr99f922f2018-07-17 17:27:36 +020024import authosm.utils as osmutils
lombardofrf5776442018-06-26 10:37:40 +020025
26logging.basicConfig(level=logging.DEBUG)
27log = logging.getLogger('instancehandler/view.py')
lombardoffb37bca2018-05-03 16:20:04 +020028
lombardoffb37bca2018-05-03 16:20:04 +020029@login_required
lombardofr99f922f2018-07-17 17:27:36 +020030def list(request, type=None):
31 user = osmutils.get_user(request)
32 project_id = user.project_id
lombardoffb37bca2018-05-03 16:20:04 +020033 client = Client()
34 if type == 'ns':
lombardofr99f922f2018-07-17 17:27:36 +020035 instance_list = client.ns_list(user.get_token())
lombardof647aa2e2018-05-26 17:11:13 +020036 elif type == 'vnf':
lombardofr99f922f2018-07-17 17:27:36 +020037 instance_list = client.vnf_list(user.get_token())
lombardoffb37bca2018-05-03 16:20:04 +020038
lombardofrf5776442018-06-26 10:37:40 +020039 result = {'instances': instance_list['data'] if instance_list and instance_list['error'] is False else [],
40 'type': type, 'project_id': project_id}
lombardofa03da5e2018-06-02 18:36:44 +020041
lombardofr0b4fb872018-07-24 17:10:48 +020042 if "OSM_ERROR" in request.session:
43 result['alert_error'] = request.session["OSM_ERROR"]
44 del request.session["OSM_ERROR"]
45
lombardofa03da5e2018-06-02 18:36:44 +020046 return __response_handler(request, result, 'instance_list.html')
lombardoffb37bca2018-05-03 16:20:04 +020047
48
49@login_required
lombardofr99f922f2018-07-17 17:27:36 +020050def create(request):
lombardoffb37bca2018-05-03 16:20:04 +020051 result = {}
lombardofr0b4fb872018-07-24 17:10:48 +020052 try:
lombardofdd73c0c2018-05-09 10:46:49 +020053
lombardofr0b4fb872018-07-24 17:10:48 +020054 ns_data = {
55 "nsName": request.POST.get('nsName', 'WithoutName'),
56 "nsDescription": request.POST.get('nsDescription', ''),
57 "nsdId": request.POST.get('nsdId', ''),
58 "vimAccountId": request.POST.get('vimAccountId', ''),
59 }
60 if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
61 ns_data["ssh-authorized-key"] = [request.POST.get('ssh_key')]
lombardofdd73c0c2018-05-09 10:46:49 +020062
lombardofr0b4fb872018-07-24 17:10:48 +020063 if 'config' in request.POST:
64 ns_config = yaml.load(request.POST.get('config'))
65 if isinstance(ns_config, dict):
66 if "vim-network-name" in ns_config:
67 ns_config["vld"] = ns_config.pop("vim-network-name")
68 if "vld" in ns_config:
69 print ns_config
70 for vld in ns_config["vld"]:
71 if vld.get("vim-network-name"):
72 if isinstance(vld["vim-network-name"], dict):
73 vim_network_name_dict = {}
74 for vim_account, vim_net in vld["vim-network-name"].items():
75 vim_network_name_dict[ns_data["vimAccountId"]] = vim_net
76 vld["vim-network-name"] = vim_network_name_dict
77 ns_data["vld"] = ns_config["vld"]
78 if "vnf" in ns_config:
79 for vnf in ns_config["vnf"]:
80 if vnf.get("vim_account"):
81 vnf["vimAccountId"] = ns_data["vimAccountId"]
82
83 ns_data["vnf"] = ns_config["vnf"]
84 except Exception as e:
85 request.session["OSM_ERROR"] = "Error creating the NS; Invalid parameters provided."
86 return __response_handler(request, {}, 'instances:list', to_redirect=True, type='ns', )
87
lombardoffb37bca2018-05-03 16:20:04 +020088 print ns_data
lombardofr99f922f2018-07-17 17:27:36 +020089 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +020090 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +020091 result = client.ns_create(user.get_token(), ns_data)
lombardofr0b4fb872018-07-24 17:10:48 +020092 return __response_handler(request, result, 'instances:list', to_redirect=True, type='ns',)
lombardofrf5776442018-06-26 10:37:40 +020093
lombardoffb37bca2018-05-03 16:20:04 +020094
lombardof74ed51a2018-05-11 01:07:01 +020095@login_required
lombardofr99f922f2018-07-17 17:27:36 +020096def ns_operations(request, instance_id=None, type=None):
97 user = osmutils.get_user(request)
98 project_id = user.project_id
lombardof74ed51a2018-05-11 01:07:01 +020099 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200100 op_list = client.ns_op_list(user.get_token(), instance_id)
lombardofrf5776442018-06-26 10:37:40 +0200101 return __response_handler(request,
102 {'operations': op_list['data'] if op_list and op_list['error'] is False else [],
103 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html')
104
lombardof74ed51a2018-05-11 01:07:01 +0200105
106@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200107def ns_operation(request, op_id, instance_id=None, type=None):
108 user = osmutils.get_user(request)
lombardof74ed51a2018-05-11 01:07:01 +0200109 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200110 result = client.ns_op(user.get_token(), op_id)
lombardofrf5776442018-06-26 10:37:40 +0200111 return __response_handler(request, result['data'])
112
lombardoffb37bca2018-05-03 16:20:04 +0200113
114@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200115def action(request, instance_id=None, type=None):
116 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +0200117 client = Client()
lombardoffb37bca2018-05-03 16:20:04 +0200118 # result = client.ns_action(instance_id, action_payload)
119 primitive_param_keys = request.POST.getlist('primitive_params_name')
120 primitive_param_value = request.POST.getlist('primitive_params_value')
121 action_payload = {
122 "vnf_member_index": request.POST.get('vnf_member_index'),
123 "primitive": request.POST.get('primitive'),
124 "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0}
125 }
126
lombardofr99f922f2018-07-17 17:27:36 +0200127 result = client.ns_action(user.get_token(), instance_id, action_payload)
lombardofrf5776442018-06-26 10:37:40 +0200128 print result
129 if result['error']:
130 return __response_handler(request, result['data'], url=None,
131 status=result['data']['status'] if 'status' in result['data'] else 500)
132
133 else:
134 return __response_handler(request, {}, url=None, status=200)
lombardoffb37bca2018-05-03 16:20:04 +0200135
136
137@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200138def delete(request, instance_id=None, type=None):
lombardof7e33ad62018-06-03 16:13:38 +0200139 force = bool(request.GET.get('force', False))
lombardoffb37bca2018-05-03 16:20:04 +0200140 result = {}
lombardofr99f922f2018-07-17 17:27:36 +0200141 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +0200142 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200143 result = client.ns_delete(user.get_token(), instance_id, force)
lombardoffb37bca2018-05-03 16:20:04 +0200144 print result
lombardofrc9488202018-07-24 14:38:16 +0200145 return __response_handler(request, result, 'instances:list', to_redirect=True, type='ns')
lombardofr99f922f2018-07-17 17:27:36 +0200146
147
148def show_topology(request, instance_id=None, type=None):
149 user = osmutils.get_user(request)
150 project_id = user.project_id
151 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
152 if 'application/json' in raw_content_types:
153 result = {'vertices': [
154 {"info": {"type": "vnf", "property": {"custom_label": ""},
155 "group": []}, "id": "ping"},
156 {"info": {"type": "vnf", "property": {"custom_label": ""},
157 "group": []}, "id": "pong"},
158 {"info": {"type": "vdu", "property": {"custom_label": ""},
159 "group": ['pong']}, "id": "pong/ubuntu"},
160 {"info": {"type": "vdu", "property": {"custom_label": ""},
161 "group": ['ping']}, "id": "ping/ubuntu"},
162 {"info": {"type": "cp", "property": {"custom_label": ""},
163 "group": ['ping']}, "id": "ping/cp0"},
164 {"info": {"type": "cp", "property": {"custom_label": ""},
165 "group": ['ping']}, "id": "ping/cp1"},
166 {"info": {"type": "cp", "property": {"custom_label": ""},
167 "group": ['pong']}, "id": "pong/cp0"},
168 {"info": {"type": "cp", "property": {"custom_label": ""},
169 "group": ['pong']}, "id": "pong/cp1"},
170 {"info": {"type": "ns_vl", "property": {"custom_label": ""},
171 "group": []}, "id": "mgmt_vl"},
172 ],
173 'edges': [
174 # {"source": "ping", "group": [], "target": "ping/cp0", "view": "Data"},
175 {"source": "pong/ubuntu", "group": ['pong'], "target": "pong/cp0", "view": "vnf"},
176 {"source": "ping/ubuntu", "group": ['ping'], "target": "ping/cp0", "view": "vnf"},
177 {"source": "pong/ubuntu", "group": ['pong'], "target": "pong/cp1", "view": "vnf"},
178 {"source": "ping/ubuntu", "group": ['ping'], "target": "ping/cp1", "view": "vnf"},
179 {"source": "pong", "group": [], "target": "mgmt_vl", "view": "ns"},
180 {"source": "ping", "group": [], "target": "mgmt_vl", "view": "ns"},
181 ], 'graph_parameters': [],
182 'model': {
183 "layer": {
184
185 "ns": {
186 "nodes": {
187 "vnf": {
188 "addable": {
189 "callback": "addNode"
190 },
191 "removable": {
192 "callback": "removeNode"
193 },
194 "expands": "vnf"
195 },
196 "ns_vl": {
197 "addable": {
198 "callback": "addNode"
199 },
200 "removable": {
201 "callback": "removeNode"
202 }
203 },
204
205 },
206 "allowed_edges": {
207 "ns_vl": {
208 "destination": {
209 "vnf": {
210 "callback": "addLink",
211 "direct_edge": False,
212 "removable": {
213 "callback": "removeLink"
214 }
215 }
216 }
217 },
218 "vnf": {
219 "destination": {
220 "ns_vl": {
221 "callback": "addLink",
222 "direct_edge": False,
223 "removable": {
224 "callback": "removeLink"
225 }
226 },
227
228 }
229 }
230
231 }
232 },
233 "vnf": {
234 "nodes": {
235 "vdu": {
236 "addable": {
237 "callback": "addNode"
238 },
239 "removable": {
240 "callback": "removeNode"
241 }
242 },
243 "cp": {
244 "addable": {
245 "callback": "addNode"
246 },
247 "removable": {
248 "callback": "removeNode"
249 }
250 },
251
252 },
253 "allowed_edges": {
254 "vdu": {
255 "destination": {
256 "cp": {
257 "callback": "addLink",
258 "direct_edge": False,
259 "removable": {
260 "callback": "removeLink"
261 }
262 }
263 }
264 },
265 "cp": {
266 "destination": {
267 "vdu": {
268 "callback": "addLink",
269 "direct_edge": False,
270 "removable": {
271 "callback": "removeLink"
272 }
273 }
274 }
275 }
276 }
277 },
278 "name": "OSM",
279 "version": 1,
280 "nodes": {
281 "vnf": {
282 "label": "vnf"
283 },
284 "ns_vl": {
285 "label": "vl"
286 },
287 "cp": {
288 "label": "cp"
289 },
290 "vdu": {
291 "label": "vdu"
292 }
293 },
294 "description": "osm"
295 }
296 }}
297 return __response_handler(request, result)
298 else:
299 result = {'type': type, 'project_id': project_id, 'instance_id': instance_id}
300 return __response_handler(request, result, 'instance_topology_view.html')
lombardoffb37bca2018-05-03 16:20:04 +0200301
302
303@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200304def show(request, instance_id=None, type=None):
lombardoffb37bca2018-05-03 16:20:04 +0200305 # result = {}
lombardofr99f922f2018-07-17 17:27:36 +0200306 user = osmutils.get_user(request)
307 project_id = user.project_id
lombardoffb37bca2018-05-03 16:20:04 +0200308 client = Client()
lombardof647aa2e2018-05-26 17:11:13 +0200309 if type == 'ns':
lombardofr99f922f2018-07-17 17:27:36 +0200310 result = client.ns_get(user.get_token(), instance_id)
lombardof647aa2e2018-05-26 17:11:13 +0200311 elif type == 'vnf':
lombardofr99f922f2018-07-17 17:27:36 +0200312 result = client.vnf_get(user.get_token(), instance_id)
lombardoffb37bca2018-05-03 16:20:04 +0200313 print result
314 return __response_handler(request, result)
315
lombardofrf5776442018-06-26 10:37:40 +0200316
lombardofr99624b52018-06-18 15:54:24 +0200317@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200318def export_metric(request, instance_id=None, type=None):
lombardofr99624b52018-06-18 15:54:24 +0200319 metric_data = request.POST.dict()
lombardofr99f922f2018-07-17 17:27:36 +0200320 user = osmutils.get_user(request)
321 project_id = user.project_id
lombardofr99624b52018-06-18 15:54:24 +0200322 client = Client()
323 keys = ["collection_period",
324 "vnf_member_index",
325 "metric_name",
326 "correlation_id",
327 "vdu_name",
328 "collection_unit"]
329 metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items()))
330
lombardofr99f922f2018-07-17 17:27:36 +0200331 result = client.ns_metric_export(user.get_token(), instance_id, metric_data)
lombardofr99624b52018-06-18 15:54:24 +0200332
lombardofrf5776442018-06-26 10:37:40 +0200333 if result['error']:
334 print result
335 return __response_handler(request, result['data'], url=None,
336 status=result['data']['status'] if 'status' in result['data'] else 500)
337 else:
338 return __response_handler(request, {}, url=None, status=200)
339
lombardofr99624b52018-06-18 15:54:24 +0200340
341@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200342def create_alarm(request, instance_id=None, type=None):
lombardofr99624b52018-06-18 15:54:24 +0200343 metric_data = request.POST.dict()
344 print metric_data
lombardofr99f922f2018-07-17 17:27:36 +0200345 user = osmutils.get_user(request)
346 project_id = user.project_id
lombardofr99624b52018-06-18 15:54:24 +0200347 client = Client()
348
lombardofr99624b52018-06-18 15:54:24 +0200349 keys = ["threshold_value",
350 "vnf_member_index",
351 "metric_name",
352 "vdu_name",
353 "alarm_name",
354 "correlation_id",
355 "statistic",
356 "operation",
357 "severity"]
358 metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items()))
359
lombardofr99f922f2018-07-17 17:27:36 +0200360 result = client.ns_alarm_create(user.get_token(), instance_id, metric_data)
lombardofrf5776442018-06-26 10:37:40 +0200361 if result['error']:
362 print result
363 return __response_handler(request, result['data'], url=None,
364 status=result['data']['status'] if 'status' in result['data'] else 500)
365 else:
366 return __response_handler(request, {}, url=None, status=200)
lombardofr99624b52018-06-18 15:54:24 +0200367
lombardoffb37bca2018-05-03 16:20:04 +0200368
369def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
370 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
371 if 'application/json' in raw_content_types or url is None:
lombardofrc9488202018-07-24 14:38:16 +0200372 return HttpResponse(json.dumps(data_res), content_type="application/json")
lombardoffb37bca2018-05-03 16:20:04 +0200373 elif to_redirect:
374 return redirect(url, *args, **kwargs)
375 else:
376 return render(request, url, data_res)