From 1cdb594fd48486eb069c29cf7d41ecb8cb121df4 Mon Sep 17 00:00:00 2001 From: lombardofr Date: Tue, 26 Jun 2018 10:37:40 +0200 Subject: [PATCH] working on client v2: projects and instances Change-Id: Ie9f69afc81eed91de71d9e596d7485908f2e741e Signed-off-by: lombardofr --- authosm/views.py | 5 +- .../modal/instance_export_metric.html | 75 +++ .../template/modal/instance_new_alarm.html | 83 +++ instancehandler/views.py | 79 ++- lib/osm/osmclient/client.py | 3 +- lib/osm/osmclient/clientv2.py | 512 +++++++++++++++++- lib/util.py | 2 +- .../osm/descriptor/descriptor_new.html | 2 +- .../osm/descriptor/descriptor_view.html | 2 +- .../template/project/project_descriptors.html | 4 +- projecthandler/views.py | 256 +++++---- 11 files changed, 869 insertions(+), 154 deletions(-) create mode 100644 instancehandler/template/modal/instance_export_metric.html create mode 100644 instancehandler/template/modal/instance_new_alarm.html diff --git a/authosm/views.py b/authosm/views.py index a662ed0..5ce8765 100644 --- a/authosm/views.py +++ b/authosm/views.py @@ -36,11 +36,12 @@ def user_login(request): if user and user.is_active: if user.is_authenticated(): login(request, user) - print next_page + request.session['token'] = user.session + print request.session['token'] if next_page == "" or next_page is None: return HttpResponseRedirect('/home') else: return HttpResponseRedirect(next_page) else: error_message = 'Login failed!' - return render(request, 'login.html', {'error_message': error_message, 'collapsed_sidebar': False}) + return render(request, 'login.html', {'error_message':error_message, 'collapsed_sidebar': False}) diff --git a/instancehandler/template/modal/instance_export_metric.html b/instancehandler/template/modal/instance_export_metric.html new file mode 100644 index 0000000..cd3d1b5 --- /dev/null +++ b/instancehandler/template/modal/instance_export_metric.html @@ -0,0 +1,75 @@ + diff --git a/instancehandler/template/modal/instance_new_alarm.html b/instancehandler/template/modal/instance_new_alarm.html new file mode 100644 index 0000000..fe5fbfe --- /dev/null +++ b/instancehandler/template/modal/instance_new_alarm.html @@ -0,0 +1,83 @@ + diff --git a/instancehandler/views.py b/instancehandler/views.py index d835de6..39837d7 100644 --- a/instancehandler/views.py +++ b/instancehandler/views.py @@ -18,19 +18,24 @@ from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.http import HttpResponse, JsonResponse import yaml +import json import logging -from lib.osm.osmclient.client import Client +from lib.osm.osmclient.clientv2 import Client + +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger('instancehandler/view.py') @login_required def list(request, project_id=None, type=None): client = Client() if type == 'ns': - result = client.ns_list() + instance_list = client.ns_list(request.session['token']) elif type == 'vnf': - result = client.vnf_list() + instance_list = client.vnf_list(request.session['token']) - result = {'instances': result, 'type': type, 'project_id': project_id} + result = {'instances': instance_list['data'] if instance_list and instance_list['error'] is False else [], + 'type': type, 'project_id': project_id} return __response_handler(request, result, 'instance_list.html') @@ -69,24 +74,29 @@ def create(request, project_id=None): ns_data["vnf"] = ns_config["vnf"] print ns_data client = Client() - result = client.ns_create(ns_data) - return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id) + result = client.ns_create(request.session['token'], ns_data) + return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', + project_id=project_id) + @login_required def ns_operations(request, project_id=None, instance_id=None, type=None): client = Client() - result = client.ns_op_list(instance_id) - return __response_handler(request, {'operations': result, 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html') + op_list = client.ns_op_list(request.session['token'], instance_id) + return __response_handler(request, + {'operations': op_list['data'] if op_list and op_list['error'] is False else [], + 'type': 'ns', 'project_id': project_id}, 'instance_operations_list.html') + @login_required def ns_operation(request, op_id, project_id=None, instance_id=None, type=None): client = Client() - result = client.ns_op(op_id) - return __response_handler(request, result) + result = client.ns_op(request.session['token'], op_id) + return __response_handler(request, result['data']) + @login_required def action(request, project_id=None, instance_id=None, type=None): - client = Client() # result = client.ns_action(instance_id, action_payload) @@ -98,8 +108,14 @@ def action(request, project_id=None, instance_id=None, type=None): "primitive_params": {k: v for k, v in zip(primitive_param_keys, primitive_param_value) if len(k) > 0} } - result = client.ns_action(instance_id, action_payload) - return __response_handler(request, result, None, to_redirect=False, status=result['status'] if 'status' in result else None ) + result = client.ns_action(request.session['token'], instance_id, action_payload) + print result + if result['error']: + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + + else: + return __response_handler(request, {}, url=None, status=200) @login_required @@ -107,9 +123,10 @@ def delete(request, project_id=None, instance_id=None, type=None): force = bool(request.GET.get('force', False)) result = {} client = Client() - result = client.ns_delete(instance_id, force) + result = client.ns_delete(request.session['token'], instance_id, force) print result - return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', project_id=project_id) + return __response_handler(request, result, 'projects:instances:list', to_redirect=True, type='ns', + project_id=project_id) @login_required @@ -117,15 +134,15 @@ def show(request, project_id=None, instance_id=None, type=None): # result = {} client = Client() if type == 'ns': - result = client.ns_get(instance_id) + result = client.ns_get(request.session['token'], instance_id) elif type == 'vnf': - result = client.vnf_get(instance_id) + result = client.vnf_get(request.session['token'], instance_id) print result return __response_handler(request, result) + @login_required def export_metric(request, project_id=None, instance_id=None, type=None): - metric_data = request.POST.dict() client = Client() @@ -137,10 +154,15 @@ def export_metric(request, project_id=None, instance_id=None, type=None): "collection_unit"] metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items())) - response = client.ns_metric_export(instance_id, metric_data) + result = client.ns_metric_export(request.session['token'], instance_id, metric_data) + + if result['error']: + print result + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) - return __response_handler(request, {}, None, to_redirect=False, - status=response.status_code) @login_required def create_alarm(request, project_id=None, instance_id=None, type=None): @@ -148,7 +170,6 @@ def create_alarm(request, project_id=None, instance_id=None, type=None): print metric_data client = Client() - keys = ["threshold_value", "vnf_member_index", "metric_name", @@ -160,18 +181,20 @@ def create_alarm(request, project_id=None, instance_id=None, type=None): "severity"] metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items())) - result = client.ns_alarm_create(instance_id, metric_data) - return __response_handler(request, {}, None, to_redirect=False, - status=result['status'] if 'status' in result else None) + result = client.ns_alarm_create(request.session['token'], instance_id, metric_data) + if result['error']: + print result + return __response_handler(request, result['data'], url=None, + status=result['data']['status'] if 'status' in result['data'] else 500) + else: + return __response_handler(request, {}, url=None, status=200) def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') if 'application/json' in raw_content_types or url is None: - return JsonResponse(data_res, *args, **kwargs) + return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs) elif to_redirect: return redirect(url, *args, **kwargs) else: return render(request, url, data_res) - - diff --git a/lib/osm/osmclient/client.py b/lib/osm/osmclient/client.py index a8a5fd0..87e712b 100644 --- a/lib/osm/osmclient/client.py +++ b/lib/osm/osmclient/client.py @@ -14,7 +14,7 @@ log = logging.getLogger('helper.py') class Client(object): - def __init__(self, host=os.getenv('OSM_SERVER', "192.168.100.199"), so_port=9999, so_project='admin', ro_host=None, ro_port=9090, **kwargs): + def __init__(self, host=os.getenv('OSM_SERVER', "192.168.1.73"), so_port=9999, so_project='admin', ro_host=None, ro_port=9090, **kwargs): self._user = 'admin' self._password = 'admin' @@ -321,7 +321,6 @@ class Client(object): headers['Authorization'] = 'Bearer {}'.format(token) headers['Content-Type'] = 'application/json' #headers['accept'] = 'application/json' - print "DIO" _url = "{0}/test/message/metric_request".format(self._base_path) print _url return self._send_post(_url, headers=headers, json=metric_payload) diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index ad58de3..bebeb26 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -17,7 +17,7 @@ class Client(object): def __init__(self): self._token_endpoint = 'admin/v1/tokens' self._user_endpoint = 'admin/v1/users' - self._host = os.getenv('OSM_SERVER', "192.168.1.73") + self._host = os.getenv('OSM_SERVER', "localhost") self._so_port = 9999 self._base_path = "https://{0}:{1}/osm".format(self._host, self._so_port) @@ -39,3 +39,513 @@ class Client(object): return result + def nsd_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/nsd/v1/ns_descriptors_content".format(self._base_path) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def vnfd_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def ns_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def vnf_list(self, token): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nslcm/v1/vnfrs".format(self._base_path) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def nsd_delete(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id) + try: + r = requests.delete(_url, params=None, verify=False,headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def vnfd_delete(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id) + try: + r = requests.delete(_url, params=None, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def nsd_onboard(self, token, package): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/gzip", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + with open('/tmp/'+package.name, 'wb+') as destination: + for chunk in package.chunks(): + destination.write(chunk) + headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb')) + _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path) + try: + r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.created: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def vnfd_onboard(self, token, package): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/gzip", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + with open('/tmp/'+package.name, 'wb+') as destination: + for chunk in package.chunks(): + destination.write(chunk) + headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb')) + _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path) + try: + r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.created: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def nsd_update(self, token, id, data): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/gzip", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + # get the package onboarded + tar_pkg = self.get_nsd_pkg(token, id) + tarf = tarfile.open(fileobj=tar_pkg) + + tarf = self._descriptor_update(tarf, data) + headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb')) + + _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id) + + try: + r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False, + headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.no_content: + result['error'] = False + + return result + + def vnfd_update(self, token, id, data): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/gzip", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + # get the package onboarded + tar_pkg = self.get_vnfd_pkg(token, id) + tarf = tarfile.open(fileobj=tar_pkg) + + tarf = self._descriptor_update(tarf, data) + headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb')) + + _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id) + + try: + r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False, + headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.no_content: + result['error'] = False + + return result + + def get_nsd_pkg(self, token, id): + result = {'error': True, 'data': ''} + headers = { "accept": "application/zip", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + tarf = StringIO.StringIO(r.content) + return tarf + return result + + def get_vnfd_pkg(self, token, id): + result = {'error': True, 'data': ''} + headers = {"accept": "application/zip", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + print r.status_code + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + tarf = StringIO.StringIO(r.content) + return tarf + return result + + def _descriptor_update(self, tarf, data): + print tarf.getnames() + # extract the package on a tmp directory + tarf.extractall('/tmp') + + for name in tarf.getnames(): + if name.endswith(".yaml") or name.endswith(".yml"): + with open('/tmp/' + name, 'w') as outfile: + yaml.safe_dump(data, outfile, default_flow_style=False) + break + + tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz") + # tarf_temp = tarfile.open("pippo.tar.gz", "w:gz") + print tarf_temp.getnames() + # tarf_temp.add('/tmp/'+tarf.getnames()[0]) + for tarinfo in tarf: + # if tarinfo.name.startswith(tarf.getnames()[0]): + # new_name = tarinfo.name[len(tarf.getnames()[0]):] + tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False) + print tarf_temp.getnames() + tarf_temp.close() + return tarf + + def nsd_get(self, token, id): + result = {'error': True, 'data': ''} + headers = {'Content-Type': 'application/yaml', + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + return yaml.load(r.text) + else: + try: + result['data'] = r.json() + except Exception as e: + result['data'] = {} + return result + + def vnfd_get(self, token, id): + result = {'error': True, 'data': ''} + headers = {'Content-Type': 'application/yaml', + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + return yaml.load(r.text) + else: + try: + result['data'] = r.json() + except Exception as e: + result['data'] = {} + return result + + def nsd_artifacts(self, token, id): + result = {'error': True, 'data': ''} + headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain', + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = r.text + else: + try: + result['data'] = r.json() + except Exception as e: + result['data'] = {} + + return result + + def vnf_packages_artifacts(self, token, id): + result = {'error': True, 'data': ''} + headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain', + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id) + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = r.text + else: + try: + result['data'] = r.json() + except Exception as e: + result['data'] = {} + + return result + + def ns_create(self, token, ns_data): + token = self.get_token() + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path) + + try: + r = requests.post(_url, json=ns_data, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def ns_op_list(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id) + + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def ns_op(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id) + + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + + return result + + def ns_action(self, token, id, action_payload): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + + _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id) + + try: + r = requests.post(_url, json=action_payload, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + print r.status_code + if r.status_code == requests.codes.created: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def ns_delete(self, token, id, force=None): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/yaml", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + query_path = '' + if force: + query_path = '?FORCE=true' + _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path) + try: + r = requests.delete(_url, params=None, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def ns_get(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id) + + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def vnf_get(self, token, id): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", "accept": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id) + + try: + r = requests.get(_url, params=None, verify=False, stream=True, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + if r.status_code == requests.codes.ok: + result['error'] = False + result['data'] = Util.json_loads_byteified(r.text) + return result + + def ns_alarm_create(self, token, id, alarm_payload): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/test/message/alarm_request".format(self._base_path) + try: + r = requests.post(_url, json=alarm_payload, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + print r.status_code + if r.status_code == requests.codes.ok: + result['error'] = False + #result['data'] = Util.json_loads_byteified(r.text) + result['data'] = r.text + return result + + def ns_metric_export(self, token, id, metric_payload): + result = {'error': True, 'data': ''} + headers = {"Content-Type": "application/json", + 'Authorization': 'Bearer {}'.format(token['id'])} + _url = "{0}/test/message/metric_request".format(self._base_path) + try: + r = requests.post(_url, json=metric_payload, verify=False, headers=headers) + except Exception as e: + log.exception(e) + result['data'] = str(e) + return result + print r.status_code + if r.status_code == requests.codes.ok: + result['error'] = False + #result['data'] = Util.json_loads_byteified(r.text) + result['data'] = r.text + return result + + @staticmethod + def md5(f): + hash_md5 = hashlib.md5() + for chunk in iter(lambda: f.read(1024), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() diff --git a/lib/util.py b/lib/util.py index 71fc36b..9d33aa6 100644 --- a/lib/util.py +++ b/lib/util.py @@ -78,7 +78,7 @@ class Util(object): def json2yaml(cls, object_json): """Converts a json object into a yaml representation""" log.debug('json2yaml') - return yaml.safe_dump(object_json, default_flow_style=False) if not object_json is None else None + return yaml.safe_dump(object_json, default_flow_style=False, indent=2) if not object_json is None else None @classmethod def openfile(cls, filepath, mode='r', buffering=1): diff --git a/projecthandler/template/project/osm/descriptor/descriptor_new.html b/projecthandler/template/project/osm/descriptor/descriptor_new.html index 460d8ca..799026b 100644 --- a/projecthandler/template/project/osm/descriptor/descriptor_new.html +++ b/projecthandler/template/project/osm/descriptor/descriptor_new.html @@ -14,7 +14,7 @@ Onboard new {{ descriptor_type }} package {% block breadcrumb_body %} {{ block.super }} -
  • {{project_overview_data.name}}
  • +
  • {{project_id}}
  • {{ descriptor_type }} Descriptors
  • Create Descriptor
  • {% endblock %} diff --git a/projecthandler/template/project/osm/descriptor/descriptor_view.html b/projecthandler/template/project/osm/descriptor/descriptor_view.html index 5a082de..fe2d78c 100644 --- a/projecthandler/template/project/osm/descriptor/descriptor_view.html +++ b/projecthandler/template/project/osm/descriptor/descriptor_view.html @@ -15,7 +15,7 @@ Edit {{ descriptor_type|upper }} Descriptor {% block breadcrumb_body %} {{ block.super }} -
  • {{project_overview_data.name}}
  • +
  • {{project_id}}
  • {{ descriptor_type }} Descriptors
  • {{descriptor_id}}
  • {% endblock %} diff --git a/projecthandler/template/project/project_descriptors.html b/projecthandler/template/project/project_descriptors.html index 6f7345e..3759fcd 100644 --- a/projecthandler/template/project/project_descriptors.html +++ b/projecthandler/template/project/project_descriptors.html @@ -22,14 +22,14 @@ {% block content_body %} {{ block.super }} {% if alert_message %} - {% if alert_message.success %} + {% if alert_message.success == True %}

    Success

    {{alert_message.message}}
    {% endif %} - {% if not alert_message.success %} + {% if alert_message.success == False %}

    Alert!

    diff --git a/projecthandler/views.py b/projecthandler/views.py index de21e9a..6194b0a 100644 --- a/projecthandler/views.py +++ b/projecthandler/views.py @@ -15,7 +15,9 @@ # import json +import logging +import yaml from django.contrib.auth.decorators import login_required from django.http import HttpResponse, JsonResponse from django.middleware.csrf import get_token @@ -25,6 +27,8 @@ from projecthandler.osm_model import OsmProject from lib.osm.osmclient.clientv2 import Client +logging.basicConfig(level=logging.DEBUG) +log = logging.getLogger('projecthandler/view.py') @login_required @@ -54,10 +58,11 @@ def open_project(request, project_id=None): try: user = request.user client = Client() - nsd = client.nsd_list() - vnfd = client.vnfd_list() - ns = client.ns_list() - vnf = client.vnf_list() + ##TODO change with adhoc api call + nsd = client.nsd_list(request.session['token']) + vnfd = client.vnfd_list(request.session['token']) + ns = client.ns_list(request.session['token']) + vnf = client.vnf_list(request.session['token']) project_overview = { 'owner': user.username, 'name': project_id, @@ -65,10 +70,10 @@ def open_project(request, project_id=None): 'created_date': '-', 'info': '-', 'type': 'osm', - 'nsd': len(nsd) if nsd else 0, - 'vnfd': len(vnfd) if vnfd else 0, - 'ns': len(ns) if ns else 0, - 'vnf': len(vnf) if vnf else 0, + 'nsd': len(nsd['data']) if nsd and nsd['error'] is False else 0, + 'vnfd': len(vnfd['data']) if vnfd and vnfd['error'] is False else 0, + 'ns': len(ns['data']) if ns and ns['error'] is False else 0, + 'vnf': len(vnf['data']) if vnf and vnf['error'] is False else 0, } return render(request, 'osm/osm_project_details.html', {'project_overview': project_overview, 'project_id': project_id}) @@ -101,191 +106,210 @@ def delete_project(request, project_id=None): @login_required def show_descriptors(request, project_id=None, descriptor_type=None): - csrf_token_value = get_token(request) - client = Client() + print request.GET.dict() try: if descriptor_type == 'nsd': - descriptors = client.nsd_list() - + descriptors = client.nsd_list(request.session['token']) elif descriptor_type == 'vnfd': - descriptors = client.vnfd_list() + descriptors = client.vnfd_list(request.session['token']) except Exception as e: + log.exception(e) descriptors = [] url = 'osm/osm_project_descriptors.html' return __response_handler(request, { - 'descriptors': descriptors, + 'descriptors': descriptors['data'] if descriptors and descriptors['error'] is False else [], 'project_id': project_id, 'project_type': 'osm', - "csrf_token_value": csrf_token_value, 'descriptor_type': descriptor_type },url) @login_required def delete_descriptor(request, project_id=None, descriptor_type=None, descriptor_id=None): - csrf_token_value = get_token(request) try: client = Client() if descriptor_type == 'nsd': - result = client.nsd_delete(descriptor_id) + result = client.nsd_delete(request.session['token'], descriptor_id) elif descriptor_type == 'vnfd': - result = client.vnfd_delete(descriptor_id) - - else: - return False + result = client.vnfd_delete(request.session['token'], descriptor_id) + except Exception as e: + log.exception(e) + result = {'error': True, 'data': str(e)} + url = 'osm/osm_project_descriptors.html' + descriptors = {} + try: + if descriptor_type == 'nsd': + descriptors = client.nsd_list(request.session['token']) + elif descriptor_type == 'vnfd': + descriptors = client.vnfd_list(request.session['token']) except Exception as e: - result = False - project_overview = OsmProject.get_overview_data() - prj_token = project_overview['type'] - page = prj_token + '/' + prj_token + '_project_descriptors.html' + log.exception(e) - return render(request, page, { - 'descriptors': OsmProject.get_descriptors(descriptor_type), + return __response_handler(request, { + 'descriptors': descriptors['data'] if descriptors and descriptors['error'] is False else [], 'project_id': project_id, - 'project_overview_data': project_overview, - "csrf_token_value": csrf_token_value, + 'project_type': 'osm', 'descriptor_type': descriptor_type, - #'alert_message': { - # 'success': result, - # 'message': "Delete succeeded!" if result else 'Error in delete'} - }) + 'alert_message': { + 'success': False if result['error'] is True else True, + 'message': 'An error occurred while processing your request.' if result and result['error'] is True else "Record deleted successfully"} + }, url) + @login_required def new_descriptor(request, project_id=None, descriptor_type=None): - project_overview = OsmProject.get_overview_data() - prj_token = project_overview['type'] - page = prj_token + '/descriptor/descriptor_new.html' + page = 'osm/descriptor/descriptor_new.html' if request.method == 'GET': request_id = request.GET.get('id', '') - return render(request, page, { + + return __response_handler(request, { 'project_id': project_id, 'descriptor_type': descriptor_type, 'descriptor_id': request_id, - 'project_overview_data': project_overview - }) + }, page) elif request.method == 'POST': - csrf_token_value = get_token(request) data_type = request.POST.get('type') print "TYPE", data_type if data_type == "file": file_uploaded = request.FILES['file'] - text = file_uploaded.read() - data_type = file_uploaded.name.split(".")[-1] - desc_name = file_uploaded.name.split(".")[0] - result = OsmProject.create_descriptor(desc_name, descriptor_type, text, data_type, file_uploaded) + + try: + client = Client() + if descriptor_type == 'nsd': + result = client.nsd_onboard(request.session['token'], file_uploaded) + elif descriptor_type == 'vnfd': + result = client.vnfd_onboard(request.session['token'], file_uploaded) + else: + log.debug('Create descriptor: Unknown data type') + result = {'error': True, 'data': 'Create descriptor: Unknown data type'} + + except Exception as e: + log.exception(e) + result = {'error': True, 'data': str(e)} else: - text = request.POST.get('text') - desc_name = request.POST.get('id') - result = OsmProject.create_descriptor(desc_name, descriptor_type, text, data_type) + result = {'error': True, 'data': 'Create descriptor: Unknown data type'} + if result['error']: + print result + return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500) - response_data = { - 'project_id': project_id, - 'descriptor_type': descriptor_type, - 'project_overview_data':OsmProject.get_overview_data(), - 'descriptor_id': result, - 'alert_message': { - 'success': True if result != False else False, - 'message': "Descriptor created" if result else 'Error in creation'} - } - status_code = 200 if result != False else 500 - response = HttpResponse(json.dumps(response_data), content_type="application/json", status=status_code) - response["Access-Control-Allow-Origin"] = "*" - return response + else: + return __response_handler(request, {}, url=None, status=200) @login_required def edit_descriptor(request, project_id=None, descriptor_id=None, descriptor_type=None): if request.method == 'POST': print "edit_descriptor" - result = OsmProject.edit_descriptor(descriptor_type, descriptor_id, request.POST.get('text'), - request.POST.get('type')) - response_data = { - 'project_id': project_id, - 'descriptor_type': descriptor_type, - #'project_overview_data': projects[0].get_overview_data(), - 'alert_message': { - 'success': True if result else False, - 'message': "Descriptor modified." if result else 'Error during descriptor editing.'} - } - status_code = 200 if result else 500 - response = HttpResponse(json.dumps(response_data), content_type="application/json", status=status_code) - response["Access-Control-Allow-Origin"] = "*" - return response + new_data = request.POST.get('text'), + data_type = request.POST.get('type') + #print new_data + try: + client = Client() + if descriptor_type == 'nsd': + if data_type == 'yaml': + new_data = yaml.load(request.POST.get('text')) + elif data_type == 'json': + new_data = json.loads(request.POST.get('text')) + print new_data + result = client.nsd_update(request.session['token'], descriptor_id, new_data) + elif descriptor_type == 'vnfd': + if data_type == 'yaml': + new_data = yaml.load(request.POST.get('text')) + elif data_type == 'json': + new_data = json.loads(request.POST.get('text')) + print new_data + result = client.vnfd_update(request.session['token'], descriptor_id, new_data) + + else: + log.debug('Update descriptor: Unknown data type') + result = {'error': True, 'data': 'Update descriptor: Unknown data type'} + except Exception as e: + log.exception(e) + result = {'error': True, 'data': str(e)} + print result + if result['error'] == True: + return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500) + + else: + return __response_handler(request, {}, url=None, status=200) elif request.method == 'GET': - csrf_token_value = get_token(request) - project_overview = OsmProject.get_overview_data() - print project_overview - prj_token = project_overview['type'] - page = prj_token + '/descriptor/descriptor_view.html' - descriptor = OsmProject.get_descriptor(descriptor_id, descriptor_type) + page = 'osm/descriptor/descriptor_view.html' + try: + client = Client() + if descriptor_type == 'nsd': + result = client.nsd_get(request.session['token'], descriptor_id) + print result + elif descriptor_type == 'vnfd': + result = client.vnfd_get(request.session['token'], descriptor_id) + + print result + except Exception as e: + log.exception(e) + result = {'error': True, 'data': str(e)} - descriptor_string_json = json.dumps(descriptor) - descriptor_string_yaml = Util.json2yaml(descriptor) + if isinstance(result, dict) and 'error' in result and result['error']: + return render(request, 'error.html') + + descriptor_string_json = json.dumps(result, indent=2) + descriptor_string_yaml = Util.json2yaml(result) # print descriptor return render(request, page, { 'project_id': project_id, 'descriptor_id': descriptor_id, - 'project_overview_data': OsmProject.get_overview_data(), 'descriptor_type': descriptor_type, 'descriptor_strings': {'descriptor_string_yaml': descriptor_string_yaml, 'descriptor_string_json': descriptor_string_json}}) -# OSM specific method # -def get_package_files_list(request, project_id, project, descriptor_id, descriptor_type): +@login_required +def get_package_files_list(request, project_id, descriptor_id, descriptor_type): files_list = [] try: - files_list = project.get_package_files_list(descriptor_type, descriptor_id) + client = Client() + if descriptor_type == 'nsd': + artifacts_res = client.nsd_artifacts(request.session['token'], descriptor_id) + elif descriptor_type == 'vnfd': + artifacts_res = client.vnf_packages_artifacts(request.session['token'], descriptor_id) + else: + return False + + files_list = yaml.load(artifacts_res['data'] if artifacts_res and artifacts_res['error'] is False else []) result = {'files': files_list} except Exception as e: - print e + log.exception(e) url = 'error.html' result = {'error_msg': 'Unknown error.'} return __response_handler(request, result) - +@login_required def download_pkg(request, project_id, descriptor_id, descriptor_type): - tar_pkg = OsmProject.download_pkg(descriptor_id, descriptor_type) + file_name = "osm_export.tar.gz" + tar_pkg = None + try: + client = Client() + if descriptor_type == 'nsd': + tar_pkg = client.get_nsd_pkg(request.session['token'], descriptor_id) + elif descriptor_type == 'vnfd': + tar_pkg = client.get_vnfd_pkg(request.session['token'], descriptor_id) + + except Exception as e: + log.exception(e) response = HttpResponse(content_type="application/tgz") - response["Content-Disposition"] = "attachment; filename=osm_export.tar.gz" + response["Content-Disposition"] = "attachment; filename="+ file_name response.write(tar_pkg.getvalue()) return response -def create_ns(request, project_id, project, descriptor_id, descriptor_type): - files_list = [] - try: - ns_data={ - "nsName": request.POST.get('nsName', 'WithoutName'), - "nsDescription": request.POST.get('nsDescription', ''), - "nsdId": request.POST.get('nsdId', ''), - "vimAccountId": request.POST.get('vimAccountId', ''), - "ssh-authorized-key": [ - { - request.POST.get('key-pair-ref', ''): request.POST.get('keyValue', '') - } - ] - } - #result = project.create_ns(descriptor_type, descriptor_id, ns_data) - - except Exception as e: - print e - url = 'error.html' - result = {'error_msg': 'Unknown error.'} - return __response_handler(request, result) - -# end OSM specific method # - @login_required def custom_action(request, project_id=None, descriptor_id=None, descriptor_type=None, action_name=None): if request.method == 'GET': @@ -295,8 +319,8 @@ def custom_action(request, project_id=None, descriptor_id=None, descriptor_type= def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') - if 'application/json' in raw_content_types: - return JsonResponse(data_res) + if 'application/json' in raw_content_types or url is None: + return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs) elif to_redirect: return redirect(url, *args, **kwargs) else: -- 2.25.1