Fix vfc-vnfmgr master download url
[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
18 from rest_framework import status
19 from rest_framework.decorators import api_view
20 from rest_framework.response import Response
21
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
26
27 logger = logging.getLogger(__name__)
28
29
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")
34     try:
35         if VnfRegModel.objects.filter(id=vnf_inst_id):
36             raise Exception("Vnf(%s) already exists." % vnf_inst_id)
37         VnfRegModel(
38             id=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         logger.error(e.message)
45         return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
46     return Response(data={"vnfInstId": vnf_inst_id}, status=status.HTTP_201_CREATED)
47
48
49 @api_view(http_method_names=['GET', 'PUT', 'DELETE'])
50 def access_vnf(request, *args, **kwargs):
51     vnf_inst_id = ignore_case_get(kwargs, "vnfInstId")
52     logger.info("Enter %s, method is %s, ", fun_name(), request.method)
53     logger.info("vnfInstId is %s, data is %s", vnf_inst_id, request.data)
54     # ret, normal_status = None, None
55     try:
56         vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
57         if not vnf:
58             err_msg = "Vnf(%s) does not exist." % vnf_inst_id
59             return Response(data={'error': err_msg}, status=status.HTTP_404_NOT_FOUND)
60         if request.method == 'GET':
61             ret = {
62                 "vnfInstId": vnf_inst_id,
63                 "ip": vnf[0].ip,
64                 "port": vnf[0].port,
65                 "username": vnf[0].username,
66                 "password": vnf[0].password
67             }
68             normal_status = status.HTTP_200_OK
69         elif request.method == 'PUT':
70             ip = ignore_case_get(request.data, "ip")
71             port = ignore_case_get(request.data, "port")
72             username = ignore_case_get(request.data, "username")
73             password = ignore_case_get(request.data, "password")
74             if ip:
75                 vnf[0].ip = ip
76             if port:
77                 vnf[0].port = port
78             if username:
79                 vnf[0].username = username
80             if password:
81                 vnf[0].password = password
82             vnf[0].save()
83             ret = {}
84             normal_status = status.HTTP_202_ACCEPTED
85         else:
86             vnf.delete()
87             ret = {}
88             normal_status = status.HTTP_204_NO_CONTENT
89     except Exception as e:
90         logger.error(e.message)
91         return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92     return Response(data=ret, status=normal_status)
93
94
95 @api_view(http_method_names=['POST'])
96 def vnf_config(request, *args, **kwargs):
97     logger.info("Enter %s, data is %s", fun_name(), request.data)
98     vnf_inst_id = ignore_case_get(request.data, "vnfInstanceId")
99     try:
100         vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
101         if not vnf:
102             raise Exception("Vnf(%s) does not exist." % vnf_inst_id)
103         ret = restcall.call_req(
104             base_url="http://%s:%s/" % (vnf[0].ip, vnf[0].port),
105             user=vnf[0].username,
106             passwd=vnf[0].password,
107             auth_type=restcall.rest_no_auth,
108             resource="v1/vnfconfig",
109             method="POST",
110             content=json.dumps(request.data))
111         if ret[0] != 0:
112             raise Exception("Failed to config Vnf(%s): %s" % (vnf_inst_id, ret[1]))
113     except Exception as e:
114         logger.error(e.message)
115         return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
116     return Response(data={}, status=status.HTTP_202_ACCEPTED)