update from python2 to python3
[vfc/gvnfm/vnfmgr.git] / mgr / mgr / samples / 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 re
18
19 from rest_framework.views import APIView
20 from rest_framework.decorators import api_view
21 from rest_framework import status
22 from rest_framework.response import Response
23
24 from mgr.pub.utils.restcall import req_by_msb
25 from mgr.pub.config.config import REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM
26
27 logger = logging.getLogger(__name__)
28
29 # ("GET", "^/api/ms1/v1/samples/(?P<sampleId>[0-9a-zA-Z\-\_]+)$", 200, '{"sampleId": "<sampleId>"}')
30 _stub_mapping_ = []
31
32
33 class SampleList(APIView):
34     def get(self, request, format=None):
35         logger.debug("get")
36         return Response({"status": "active"})
37
38
39 @api_view(http_method_names=['GET'])
40 def reg2msb(request, *args, **kwargs):
41     ms_name = kwargs.get('msName')
42     logger.info("[reg2msb]ms name is %s", ms_name)
43     reg_param = REG_TO_MSB_REG_PARAM.copy()
44     reg_param['serviceName'] = ms_name
45     reg_param['url'] = '/api/%s/v1' % ms_name
46     req_by_msb(REG_TO_MSB_REG_URL, "POST", json.JSONEncoder().encode(reg_param))
47     return Response(data={"regok": ms_name}, status=status.HTTP_200_OK)
48
49
50 @api_view(http_method_names=['GET'])
51 def reloadstub(request, *args, **kwargs):
52     file_name = kwargs.get('fileName')
53     logger.info("[reloadstub]file name is %s", file_name)
54     global _stub_mapping_
55     _stub_mapping_ = []
56     with open("/tmp/%s" % file_name) as url_mapping_file:
57         for block in url_mapping_file.read().split("=##="):
58             if not block.strip():
59                 continue
60             items = block.split("##")
61             if len(items) != 4:
62                 logger.warn("Abnormal block: %s", block)
63                 continue
64             method = items[0].strip().upper()
65             uri_re = re.compile(items[1].strip())
66             code = int(items[2].strip())
67             data = items[3].strip()
68             _stub_mapping_.append((method, uri_re, code, data))
69     return Response(data={"reloadstub": len(_stub_mapping_)}, status=status.HTTP_200_OK)
70
71
72 @api_view(http_method_names=['POST', 'GET', 'DELETE', 'PUT'])
73 def stub(request, *args, **kwargs):
74     logger.info("[stub][%s][%s], data=%s", request.method, request.path, request.data)
75     global _stub_mapping_
76     for method, uri_re, code, data in _stub_mapping_:
77         if method != request.method.upper():
78             continue
79         re_match = uri_re.match(request.path)
80         if not re_match:
81             continue
82         for k, v in list(re_match.groupdict().items()):
83             data = data.replace('<%s>' % k, v)
84         return Response(data=json.loads(data), status=code)
85     return Response(data={"stub": "stub"}, status=status.HTTP_200_OK)