0510e8708ffe2bd7ca3a6bc4962242ba5a9c1f98
[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
17 from rest_framework import status
18 from rest_framework.decorators import api_view
19 from rest_framework.response import Response
20
21 from mgr.pub.utils.values import ignore_case_get
22 from mgr.pub.utils.syscomm import fun_name
23 from mgr.pub.database.models import VnfRegModel
24
25 logger = logging.getLogger(__name__)
26
27 @api_view(http_method_names=['POST'])
28 def add_vnf(request, *args, **kwargs):
29     logger.info("Enter %s, data is %s", fun_name(), request.data)
30     vnf_inst_id = ignore_case_get(request.data, "vnfInstId")
31     try:
32         if VnfRegModel.objects.filter(id=vnf_inst_id):
33             raise Exception("Vnf(%s) already exists." % vnf_inst_id)
34         VnfRegModel(
35             id=vnf_inst_id,
36             ip=ignore_case_get(request.data, "ip"),
37             port=ignore_case_get(request.data, "port"),
38             username=ignore_case_get(request.data, "username"),
39             password=ignore_case_get(request.data, "password")).save()
40     except Exception as e:
41         return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
42     return Response(data={"vnfInstId": vnf_inst_id}, status=status.HTTP_201_CREATED)
43
44
45 @api_view(http_method_names=['GET', 'PUT', 'DELETE'])
46 def access_vnf(request, *args, **kwargs):
47     vnf_inst_id = ignore_case_get(kwargs, "vnfInstId")
48     logger.info("Enter %s, method is %s, ", fun_name(), request.method)
49     logger.info("vnfInstId is %s, data is %s", vnf_inst_id, request.data)
50     ret, normal_status = None, None
51     try:
52         vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
53         if not vnf:
54             err_msg = "Vnf(%s) does not exist." % vnf_inst_id
55             return Response(data={'error': err_msg}, status=status.HTTP_404_NOT_FOUND)
56         if request.method == 'GET':
57             ret = {
58                 "vnfInstId": vnf_inst_id,
59                 "ip": vnf[0].ip,
60                 "port": vnf[0].port,
61                 "username": vnf[0].username,
62                 "password": vnf[0].password
63             }
64             normal_status = status.HTTP_200_OK
65         elif request.method == 'PUT':
66             ip = ignore_case_get(request.data, "ip")
67             port = ignore_case_get(request.data, "port")
68             username = ignore_case_get(request.data, "username")
69             password = ignore_case_get(request.data, "password")
70             if ip:
71                 vnf[0].ip = ip
72             if port:
73                 vnf[0].port = port
74             if username:
75                 vnf[0].username = username
76             if password:
77                 vnf[0].password = password
78             vnf[0].save()
79             ret = {}
80             normal_status = status.HTTP_202_ACCEPTED
81         else:
82             vnf.delete()
83             ret = {}
84             normal_status = status.HTTP_204_NO_CONTENT
85     except Exception as e:
86         return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
87     return Response(data=ret, status=normal_status)
88
89 @api_view(http_method_names=['POST'])
90 def vnf_config(request, *args, **kwargs):
91     logger.info("Enter %s, data is %s", fun_name(), request.data)
92