23a6398492629ecac0945ad869d825d445355e6a
[vfc/gvnfm/vnfmgr.git] / mgr / mgr / vnfreg / views.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
16 import json
17 import traceback
18
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from rest_framework.response import Response
22
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
27
28 logger = logging.getLogger(__name__)
29
30
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")
35     try:
36         if VnfRegModel.objects.filter(id=vnf_inst_id):
37             raise Exception("Vnf(%s) already exists." % vnf_inst_id)
38         VnfRegModel(
39             id=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)
49
50
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)
56     try:
57         vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
58         if not vnf:
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':
62             ret = {
63                 "vnfInstId": vnf_inst_id,
64                 "ip": vnf[0].ip,
65                 "port": vnf[0].port,
66                 "username": vnf[0].username,
67                 "password": vnf[0].password
68             }
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")
75             if ip:
76                 vnf[0].ip = ip
77             if port:
78                 vnf[0].port = port
79             if username:
80                 vnf[0].username = username
81             if password:
82                 vnf[0].password = password
83             vnf[0].save()
84             ret = {}
85             normal_status = status.HTTP_202_ACCEPTED
86         else:
87             vnf.delete()
88             ret = {}
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)
95
96
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")
101     try:
102         vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
103         if not vnf:
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",
111             method="POST",
112             content=json.dumps(request.data))
113         if ret[0] != 0:
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)