blob: 45120e2842893a0f132816fd1d32fa0cd3025835 [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
lombardofr4908f382018-09-10 11:36:06 +020018#from 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
lombardofr4908f382018-09-10 11:36:06 +020025from sf_t3d.decorators import login_required
lombardofrf5776442018-06-26 10:37:40 +020026
27logging.basicConfig(level=logging.DEBUG)
28log = logging.getLogger('instancehandler/view.py')
lombardoffb37bca2018-05-03 16:20:04 +020029
lombardofr4908f382018-09-10 11:36:06 +020030
lombardoffb37bca2018-05-03 16:20:04 +020031@login_required
lombardofr99f922f2018-07-17 17:27:36 +020032def list(request, type=None):
lombardofr4908f382018-09-10 11:36:06 +020033
lombardofr99f922f2018-07-17 17:27:36 +020034 user = osmutils.get_user(request)
35 project_id = user.project_id
lombardoffb37bca2018-05-03 16:20:04 +020036 client = Client()
lombardofr4908f382018-09-10 11:36:06 +020037 result = {'type': type, 'project_id': project_id}
38 if "OSM_ERROR" in request.session:
39 result['alert_error'] = request.session["OSM_ERROR"]
40 del request.session["OSM_ERROR"]
41 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
42 if 'application/json' not in raw_content_types:
43 return __response_handler(request, result, 'instance_list.html')
44 instance_list = None
lombardoffb37bca2018-05-03 16:20:04 +020045 if type == 'ns':
lombardofr99f922f2018-07-17 17:27:36 +020046 instance_list = client.ns_list(user.get_token())
lombardof647aa2e2018-05-26 17:11:13 +020047 elif type == 'vnf':
lombardofr99f922f2018-07-17 17:27:36 +020048 instance_list = client.vnf_list(user.get_token())
lombardoffb37bca2018-05-03 16:20:04 +020049
lombardofr4908f382018-09-10 11:36:06 +020050 result['instances'] = instance_list['data'] if instance_list and instance_list['error'] is False else []
lombardofr0b4fb872018-07-24 17:10:48 +020051
lombardofa03da5e2018-06-02 18:36:44 +020052 return __response_handler(request, result, 'instance_list.html')
lombardoffb37bca2018-05-03 16:20:04 +020053
54
55@login_required
lombardofr99f922f2018-07-17 17:27:36 +020056def create(request):
lombardoffb37bca2018-05-03 16:20:04 +020057 result = {}
lombardofr0b4fb872018-07-24 17:10:48 +020058 try:
lombardofdd73c0c2018-05-09 10:46:49 +020059
lombardofr0b4fb872018-07-24 17:10:48 +020060 ns_data = {
61 "nsName": request.POST.get('nsName', 'WithoutName'),
62 "nsDescription": request.POST.get('nsDescription', ''),
63 "nsdId": request.POST.get('nsdId', ''),
64 "vimAccountId": request.POST.get('vimAccountId', ''),
65 }
66 if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
lombardofrd3e9f5c2018-10-02 11:51:03 +020067 ns_data["ssh_keys"] = request.POST.get('ssh_key')
lombardofdd73c0c2018-05-09 10:46:49 +020068
lombardofr0b4fb872018-07-24 17:10:48 +020069 if 'config' in request.POST:
70 ns_config = yaml.load(request.POST.get('config'))
71 if isinstance(ns_config, dict):
72 if "vim-network-name" in ns_config:
73 ns_config["vld"] = ns_config.pop("vim-network-name")
74 if "vld" in ns_config:
75 print ns_config
76 for vld in ns_config["vld"]:
77 if vld.get("vim-network-name"):
78 if isinstance(vld["vim-network-name"], dict):
79 vim_network_name_dict = {}
80 for vim_account, vim_net in vld["vim-network-name"].items():
81 vim_network_name_dict[ns_data["vimAccountId"]] = vim_net
82 vld["vim-network-name"] = vim_network_name_dict
83 ns_data["vld"] = ns_config["vld"]
84 if "vnf" in ns_config:
85 for vnf in ns_config["vnf"]:
86 if vnf.get("vim_account"):
87 vnf["vimAccountId"] = ns_data["vimAccountId"]
88
89 ns_data["vnf"] = ns_config["vnf"]
90 except Exception as e:
91 request.session["OSM_ERROR"] = "Error creating the NS; Invalid parameters provided."
92 return __response_handler(request, {}, 'instances:list', to_redirect=True, type='ns', )
93
lombardoffb37bca2018-05-03 16:20:04 +020094 print ns_data
lombardofr99f922f2018-07-17 17:27:36 +020095 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +020096 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +020097 result = client.ns_create(user.get_token(), ns_data)
lombardofr0b4fb872018-07-24 17:10:48 +020098 return __response_handler(request, result, 'instances:list', to_redirect=True, type='ns',)
lombardofrf5776442018-06-26 10:37:40 +020099
lombardoffb37bca2018-05-03 16:20:04 +0200100
lombardof74ed51a2018-05-11 01:07:01 +0200101@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200102def ns_operations(request, instance_id=None, type=None):
103 user = osmutils.get_user(request)
104 project_id = user.project_id
lombardofr4908f382018-09-10 11:36:06 +0200105
106 result = {'type': 'ns', 'project_id': project_id, 'instance_id': instance_id}
107 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
108 if 'application/json' not in raw_content_types:
109 return __response_handler(request, result, 'instance_operations_list.html')
lombardof74ed51a2018-05-11 01:07:01 +0200110 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200111 op_list = client.ns_op_list(user.get_token(), instance_id)
lombardofr4908f382018-09-10 11:36:06 +0200112 result['operations'] = op_list['data'] if op_list and op_list['error'] is False else []
lombardofrf5776442018-06-26 10:37:40 +0200113
lombardofr4908f382018-09-10 11:36:06 +0200114 return __response_handler(request, result, 'instance_operations_list.html')
lombardof74ed51a2018-05-11 01:07:01 +0200115
116@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200117def ns_operation(request, op_id, instance_id=None, type=None):
118 user = osmutils.get_user(request)
lombardof74ed51a2018-05-11 01:07:01 +0200119 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200120 result = client.ns_op(user.get_token(), op_id)
lombardofrf5776442018-06-26 10:37:40 +0200121 return __response_handler(request, result['data'])
122
lombardoffb37bca2018-05-03 16:20:04 +0200123
124@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200125def action(request, instance_id=None, type=None):
126 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +0200127 client = Client()
lombardoffb37bca2018-05-03 16:20:04 +0200128 # result = client.ns_action(instance_id, action_payload)
129 primitive_param_keys = request.POST.getlist('primitive_params_name')
130 primitive_param_value = request.POST.getlist('primitive_params_value')
131 action_payload = {
132 "vnf_member_index": request.POST.get('vnf_member_index'),
133 "primitive": request.POST.get('primitive'),
134 "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0}
135 }
136
lombardofr99f922f2018-07-17 17:27:36 +0200137 result = client.ns_action(user.get_token(), instance_id, action_payload)
lombardofrf5776442018-06-26 10:37:40 +0200138 print result
139 if result['error']:
140 return __response_handler(request, result['data'], url=None,
141 status=result['data']['status'] if 'status' in result['data'] else 500)
142
143 else:
144 return __response_handler(request, {}, url=None, status=200)
lombardoffb37bca2018-05-03 16:20:04 +0200145
146
147@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200148def delete(request, instance_id=None, type=None):
lombardof7e33ad62018-06-03 16:13:38 +0200149 force = bool(request.GET.get('force', False))
lombardoffb37bca2018-05-03 16:20:04 +0200150 result = {}
lombardofr99f922f2018-07-17 17:27:36 +0200151 user = osmutils.get_user(request)
lombardoffb37bca2018-05-03 16:20:04 +0200152 client = Client()
lombardofr99f922f2018-07-17 17:27:36 +0200153 result = client.ns_delete(user.get_token(), instance_id, force)
lombardoffb37bca2018-05-03 16:20:04 +0200154 print result
lombardofrc9488202018-07-24 14:38:16 +0200155 return __response_handler(request, result, 'instances:list', to_redirect=True, type='ns')
lombardofr99f922f2018-07-17 17:27:36 +0200156
157
158def show_topology(request, instance_id=None, type=None):
159 user = osmutils.get_user(request)
160 project_id = user.project_id
161 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
162 if 'application/json' in raw_content_types:
163 result = {'vertices': [
164 {"info": {"type": "vnf", "property": {"custom_label": ""},
165 "group": []}, "id": "ping"},
166 {"info": {"type": "vnf", "property": {"custom_label": ""},
167 "group": []}, "id": "pong"},
168 {"info": {"type": "vdu", "property": {"custom_label": ""},
169 "group": ['pong']}, "id": "pong/ubuntu"},
170 {"info": {"type": "vdu", "property": {"custom_label": ""},
171 "group": ['ping']}, "id": "ping/ubuntu"},
172 {"info": {"type": "cp", "property": {"custom_label": ""},
173 "group": ['ping']}, "id": "ping/cp0"},
174 {"info": {"type": "cp", "property": {"custom_label": ""},
175 "group": ['ping']}, "id": "ping/cp1"},
176 {"info": {"type": "cp", "property": {"custom_label": ""},
177 "group": ['pong']}, "id": "pong/cp0"},
178 {"info": {"type": "cp", "property": {"custom_label": ""},
179 "group": ['pong']}, "id": "pong/cp1"},
180 {"info": {"type": "ns_vl", "property": {"custom_label": ""},
181 "group": []}, "id": "mgmt_vl"},
182 ],
183 'edges': [
184 # {"source": "ping", "group": [], "target": "ping/cp0", "view": "Data"},
185 {"source": "pong/ubuntu", "group": ['pong'], "target": "pong/cp0", "view": "vnf"},
186 {"source": "ping/ubuntu", "group": ['ping'], "target": "ping/cp0", "view": "vnf"},
187 {"source": "pong/ubuntu", "group": ['pong'], "target": "pong/cp1", "view": "vnf"},
188 {"source": "ping/ubuntu", "group": ['ping'], "target": "ping/cp1", "view": "vnf"},
189 {"source": "pong", "group": [], "target": "mgmt_vl", "view": "ns"},
190 {"source": "ping", "group": [], "target": "mgmt_vl", "view": "ns"},
191 ], 'graph_parameters': [],
192 'model': {
193 "layer": {
194
195 "ns": {
196 "nodes": {
197 "vnf": {
198 "addable": {
199 "callback": "addNode"
200 },
201 "removable": {
202 "callback": "removeNode"
203 },
204 "expands": "vnf"
205 },
206 "ns_vl": {
207 "addable": {
208 "callback": "addNode"
209 },
210 "removable": {
211 "callback": "removeNode"
212 }
213 },
214
215 },
216 "allowed_edges": {
217 "ns_vl": {
218 "destination": {
219 "vnf": {
220 "callback": "addLink",
221 "direct_edge": False,
222 "removable": {
223 "callback": "removeLink"
224 }
225 }
226 }
227 },
228 "vnf": {
229 "destination": {
230 "ns_vl": {
231 "callback": "addLink",
232 "direct_edge": False,
233 "removable": {
234 "callback": "removeLink"
235 }
236 },
237
238 }
239 }
240
241 }
242 },
243 "vnf": {
244 "nodes": {
245 "vdu": {
246 "addable": {
247 "callback": "addNode"
248 },
249 "removable": {
250 "callback": "removeNode"
251 }
252 },
253 "cp": {
254 "addable": {
255 "callback": "addNode"
256 },
257 "removable": {
258 "callback": "removeNode"
259 }
260 },
261
262 },
263 "allowed_edges": {
264 "vdu": {
265 "destination": {
266 "cp": {
267 "callback": "addLink",
268 "direct_edge": False,
269 "removable": {
270 "callback": "removeLink"
271 }
272 }
273 }
274 },
275 "cp": {
276 "destination": {
277 "vdu": {
278 "callback": "addLink",
279 "direct_edge": False,
280 "removable": {
281 "callback": "removeLink"
282 }
283 }
284 }
285 }
286 }
287 },
288 "name": "OSM",
289 "version": 1,
290 "nodes": {
291 "vnf": {
292 "label": "vnf"
293 },
294 "ns_vl": {
295 "label": "vl"
296 },
297 "cp": {
298 "label": "cp"
299 },
300 "vdu": {
301 "label": "vdu"
302 }
303 },
304 "description": "osm"
305 }
306 }}
307 return __response_handler(request, result)
308 else:
309 result = {'type': type, 'project_id': project_id, 'instance_id': instance_id}
310 return __response_handler(request, result, 'instance_topology_view.html')
lombardoffb37bca2018-05-03 16:20:04 +0200311
312
313@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200314def show(request, instance_id=None, type=None):
lombardoffb37bca2018-05-03 16:20:04 +0200315 # result = {}
lombardofr99f922f2018-07-17 17:27:36 +0200316 user = osmutils.get_user(request)
317 project_id = user.project_id
lombardoffb37bca2018-05-03 16:20:04 +0200318 client = Client()
lombardof647aa2e2018-05-26 17:11:13 +0200319 if type == 'ns':
lombardofr99f922f2018-07-17 17:27:36 +0200320 result = client.ns_get(user.get_token(), instance_id)
lombardof647aa2e2018-05-26 17:11:13 +0200321 elif type == 'vnf':
lombardofr99f922f2018-07-17 17:27:36 +0200322 result = client.vnf_get(user.get_token(), instance_id)
lombardoffb37bca2018-05-03 16:20:04 +0200323 print result
324 return __response_handler(request, result)
325
lombardofrf5776442018-06-26 10:37:40 +0200326
lombardofr99624b52018-06-18 15:54:24 +0200327@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200328def export_metric(request, instance_id=None, type=None):
lombardofr99624b52018-06-18 15:54:24 +0200329 metric_data = request.POST.dict()
lombardofr99f922f2018-07-17 17:27:36 +0200330 user = osmutils.get_user(request)
331 project_id = user.project_id
lombardofr99624b52018-06-18 15:54:24 +0200332 client = Client()
333 keys = ["collection_period",
334 "vnf_member_index",
335 "metric_name",
336 "correlation_id",
337 "vdu_name",
338 "collection_unit"]
339 metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items()))
340
lombardofr99f922f2018-07-17 17:27:36 +0200341 result = client.ns_metric_export(user.get_token(), instance_id, metric_data)
lombardofr99624b52018-06-18 15:54:24 +0200342
lombardofrf5776442018-06-26 10:37:40 +0200343 if result['error']:
344 print result
345 return __response_handler(request, result['data'], url=None,
346 status=result['data']['status'] if 'status' in result['data'] else 500)
347 else:
348 return __response_handler(request, {}, url=None, status=200)
349
lombardofr99624b52018-06-18 15:54:24 +0200350
351@login_required
lombardofr99f922f2018-07-17 17:27:36 +0200352def create_alarm(request, instance_id=None, type=None):
lombardofr99624b52018-06-18 15:54:24 +0200353 metric_data = request.POST.dict()
354 print metric_data
lombardofr99f922f2018-07-17 17:27:36 +0200355 user = osmutils.get_user(request)
356 project_id = user.project_id
lombardofr99624b52018-06-18 15:54:24 +0200357 client = Client()
358
lombardofr99624b52018-06-18 15:54:24 +0200359 keys = ["threshold_value",
360 "vnf_member_index",
361 "metric_name",
362 "vdu_name",
363 "alarm_name",
364 "correlation_id",
365 "statistic",
366 "operation",
367 "severity"]
368 metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items()))
369
lombardofr99f922f2018-07-17 17:27:36 +0200370 result = client.ns_alarm_create(user.get_token(), instance_id, metric_data)
lombardofrf5776442018-06-26 10:37:40 +0200371 if result['error']:
372 print result
373 return __response_handler(request, result['data'], url=None,
374 status=result['data']['status'] if 'status' in result['data'] else 500)
375 else:
376 return __response_handler(request, {}, url=None, status=200)
lombardofr99624b52018-06-18 15:54:24 +0200377
lombardoffb37bca2018-05-03 16:20:04 +0200378
379def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
380 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
381 if 'application/json' in raw_content_types or url is None:
lombardofrc9488202018-07-24 14:38:16 +0200382 return HttpResponse(json.dumps(data_res), content_type="application/json")
lombardoffb37bca2018-05-03 16:20:04 +0200383 elif to_redirect:
384 return redirect(url, *args, **kwargs)
385 else:
386 return render(request, url, data_res)