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