1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from rest_framework.response import Response
23 from mgr.pub.utils.values import ignore_case_get
24 from mgr.pub.utils.syscomm import fun_name
25 from mgr.pub.database.models import VnfRegModel
26 from mgr.pub.utils import restcall
28 logger = logging.getLogger(__name__)
31 @api_view(http_method_names=['POST'])
32 def add_vnf(request, *args, **kwargs):
33 logger.info("Enter %s, data is %s", fun_name(), request.data)
34 vnf_inst_id = ignore_case_get(request.data, "vnfInstId")
36 if VnfRegModel.objects.filter(id=vnf_inst_id):
37 raise Exception("Vnf(%s) already exists." % vnf_inst_id)
40 ip=ignore_case_get(request.data, "ip"),
41 port=ignore_case_get(request.data, "port"),
42 username=ignore_case_get(request.data, "username"),
43 password=ignore_case_get(request.data, "password")).save()
44 except Exception as e:
45 logger.error(e.message)
46 logger.error(traceback.format_exc())
47 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
48 return Response(data={"vnfInstId": vnf_inst_id}, status=status.HTTP_201_CREATED)
51 @api_view(http_method_names=['GET', 'PUT', 'DELETE'])
52 def access_vnf(request, *args, **kwargs):
53 vnf_inst_id = ignore_case_get(kwargs, "vnfInstId")
54 logger.info("Enter %s, method is %s, ", fun_name(), request.method)
55 logger.info("vnfInstId is %s, data is %s", vnf_inst_id, request.data)
57 vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
59 err_msg = "Vnf(%s) does not exist." % vnf_inst_id
60 return Response(data={'error': err_msg}, status=status.HTTP_404_NOT_FOUND)
61 if request.method == 'GET':
63 "vnfInstId": vnf_inst_id,
66 "username": vnf[0].username,
67 "password": vnf[0].password
69 normal_status = status.HTTP_200_OK
70 elif request.method == 'PUT':
71 ip = ignore_case_get(request.data, "ip")
72 port = ignore_case_get(request.data, "port")
73 username = ignore_case_get(request.data, "username")
74 password = ignore_case_get(request.data, "password")
80 vnf[0].username = username
82 vnf[0].password = password
85 normal_status = status.HTTP_202_ACCEPTED
89 normal_status = status.HTTP_204_NO_CONTENT
90 except Exception as e:
91 logger.error(e.message)
92 logger.error(traceback.format_exc())
93 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
94 return Response(data=ret, status=normal_status)
97 @api_view(http_method_names=['POST'])
98 def vnf_config(request, *args, **kwargs):
99 logger.info("Enter %s, data is %s", fun_name(), request.data)
100 vnf_inst_id = ignore_case_get(request.data, "vnfInstanceId")
102 vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
104 raise Exception("Vnf(%s) does not exist." % vnf_inst_id)
105 ret = restcall.call_req(
106 base_url="http://%s:%s/" % (vnf[0].ip, vnf[0].port),
107 user=vnf[0].username,
108 passwd=vnf[0].password,
109 auth_type=restcall.rest_no_auth,
110 resource="v1/vnfconfig",
112 content=json.dumps(request.data))
114 raise Exception("Failed to config Vnf(%s): %s" % (vnf_inst_id, ret[1]))
115 except Exception as e:
116 logger.error(e.message)
117 logger.error(traceback.format_exc())
118 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
119 return Response(data={}, status=status.HTTP_202_ACCEPTED)