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.
18 from rest_framework import status
19 from rest_framework.decorators import api_view
20 from rest_framework.response import Response
22 from mgr.pub.utils.values import ignore_case_get
23 from mgr.pub.utils.syscomm import fun_name
24 from mgr.pub.database.models import VnfRegModel
25 from mgr.pub.utils import restcall
27 logger = logging.getLogger(__name__)
30 @api_view(http_method_names=['POST'])
31 def add_vnf(request, *args, **kwargs):
32 logger.info("Enter %s, data is %s", fun_name(), request.data)
33 vnf_inst_id = ignore_case_get(request.data, "vnfInstId")
35 if VnfRegModel.objects.filter(id=vnf_inst_id):
36 raise Exception("Vnf(%s) already exists." % vnf_inst_id)
39 ip=ignore_case_get(request.data, "ip"),
40 port=ignore_case_get(request.data, "port"),
41 username=ignore_case_get(request.data, "username"),
42 password=ignore_case_get(request.data, "password")).save()
43 except Exception as e:
44 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
45 return Response(data={"vnfInstId": vnf_inst_id}, status=status.HTTP_201_CREATED)
48 @api_view(http_method_names=['GET', 'PUT', 'DELETE'])
49 def access_vnf(request, *args, **kwargs):
50 vnf_inst_id = ignore_case_get(kwargs, "vnfInstId")
51 logger.info("Enter %s, method is %s, ", fun_name(), request.method)
52 logger.info("vnfInstId is %s, data is %s", vnf_inst_id, request.data)
53 # ret, normal_status = None, None
55 vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
57 err_msg = "Vnf(%s) does not exist." % vnf_inst_id
58 return Response(data={'error': err_msg}, status=status.HTTP_404_NOT_FOUND)
59 if request.method == 'GET':
61 "vnfInstId": vnf_inst_id,
64 "username": vnf[0].username,
65 "password": vnf[0].password
67 normal_status = status.HTTP_200_OK
68 elif request.method == 'PUT':
69 ip = ignore_case_get(request.data, "ip")
70 port = ignore_case_get(request.data, "port")
71 username = ignore_case_get(request.data, "username")
72 password = ignore_case_get(request.data, "password")
78 vnf[0].username = username
80 vnf[0].password = password
83 normal_status = status.HTTP_202_ACCEPTED
87 normal_status = status.HTTP_204_NO_CONTENT
88 except Exception as e:
89 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
90 return Response(data=ret, status=normal_status)
93 @api_view(http_method_names=['POST'])
94 def vnf_config(request, *args, **kwargs):
95 logger.info("Enter %s, data is %s", fun_name(), request.data)
96 vnf_inst_id = ignore_case_get(request.data, "vnfInstanceId")
98 vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
100 raise Exception("Vnf(%s) does not exist." % vnf_inst_id)
101 ret = restcall.call_req(
102 base_url="http://%s:%s/" % (vnf[0].ip, vnf[0].port),
103 user=vnf[0].username,
104 passwd=vnf[0].password,
105 auth_type=restcall.rest_no_auth,
106 resource="v1/vnfconfig",
108 content=json.dumps(request.data))
110 raise Exception("Failed to config Vnf(%s): %s" % (vnf_inst_id, ret[1]))
111 except Exception as e:
112 return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
113 return Response(data={}, status=status.HTTP_202_ACCEPTED)