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.views import APIView
19 from rest_framework.decorators import api_view
20 from rest_framework import status
21 from rest_framework.response import Response
23 from mgr.pub.utils.restcall import req_by_msb
24 from mgr.pub.config.config import REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM
26 logger = logging.getLogger(__name__)
29 ("GET", "/api/ms1/v1/samples/1"): (200, {"status": "ok"})
33 class SampleList(APIView):
34 def get(self, request, format=None):
36 return Response({"status": "active"})
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)
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)
55 with open("/tmp/%s" % file_name) as url_mapping_file:
56 for block in url_mapping_file.read().split("=##="):
57 items = block.split("|")
59 logger.warn("Abnormal block: %s", block)
61 method = items[0].strip()
62 uri = items[1].strip()
63 code = int(items[2].strip())
64 data = json.loads(items[3].strip())
65 _stub_mapping_[(method, uri)] = (code, data)
66 return Response(data={"reloadstub": "ok"}, status=status.HTTP_200_OK)
69 @api_view(http_method_names=['POST', 'GET', 'DELETE', 'PUT'])
70 def stub(request, *args, **kwargs):
71 logger.info("[stub][%s][%s], data=%s", request.method, request.path, request.data)
73 match_result = _stub_mapping_.get((request.method.upper(), request.path))
75 return Response(data=match_result[1], status=match_result[0])
76 return Response(data={"stub": "stub"}, status=status.HTTP_200_OK)