e015dedc0ead6a573505de9592132e698c6f62fc
[osm/LW-UI.git] / sdnctrlhandler / 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
20 import json
21 import logging
22 import authosm.utils as osmutils
23 from lib.osm.osmclient.clientv2 import Client
24
25 logging.basicConfig(level=logging.DEBUG)
26 log = logging.getLogger('sdnctrlhandler/view.py')
27
28
29 @login_required
30 def list(request):
31 user = osmutils.get_user(request)
32 project_id = user.project_id
33 client = Client()
34 result = client.sdn_list(user.get_token())
35
36 result = {
37 'project_id': project_id,
38 'sdns': result['data'] if result and result['error'] is False else []
39 }
40 return __response_handler(request, result, 'sdn_list.html')
41
42
43 @login_required
44 def create(request):
45 user = osmutils.get_user(request)
46 project_id = user.project_id
47 result = {'project_id': project_id}
48 if request.method == 'GET':
49 return __response_handler(request, result, 'sdn_create.html')
50 else:
51 new_sdn_dict = request.POST.dict()
52 client = Client()
53 keys = ["name",
54 "type",
55 "version",
56 "dpid",
57 "ip",
58 "port",
59 "user",
60 "password"]
61 sdn_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, new_sdn_dict.items()))
62 sdn_data['port'] = int(sdn_data['port'])
63
64 result = client.sdn_create(user.get_token(), sdn_data)
65
66 return __response_handler(request, result, 'sdns:list', to_redirect=True, )
67
68
69 @login_required
70 def delete(request, sdn_id=None):
71 user = osmutils.get_user(request)
72 project_id = user.project_id
73 try:
74 client = Client()
75 del_res = client.sdn_delete(user.get_token(), sdn_id)
76 except Exception as e:
77 log.exception(e)
78 return __response_handler(request, del_res, 'sdns:list', to_redirect=True, )
79
80
81 @login_required
82 def show(request, sdn_id=None):
83 user = osmutils.get_user(request)
84 project_id = user.project_id
85 client = Client()
86 result = client.sdn_get(user.get_token(), sdn_id)
87 if isinstance(result, dict) and 'error' in result and result['error']:
88 return render(request, 'error.html')
89 return __response_handler(request, {
90 "sdn": result['data'],
91 "project_id": project_id})
92
93
94 def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
95 raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
96 if 'application/json' in raw_content_types or url is None:
97 return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
98 elif to_redirect:
99 return redirect(url, *args, **kwargs)
100 else:
101 return render(request, url, data_res)