Add stub support interface for vfc 56/70456/2 1.2.0
authorfujinhua <fu.jinhua@zte.com.cn>
Mon, 15 Oct 2018 09:15:42 +0000 (17:15 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Mon, 15 Oct 2018 10:17:20 +0000 (18:17 +0800)
Change-Id: I20c828af9a07a165fdebaf237e0fda3506c0f6ce
Issue-ID: VFC-1150
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
mgr/mgr/samples/urls.py
mgr/mgr/samples/views.py
mgr/mgr/urls.py

index e5e66d7..ade08a5 100644 (file)
@@ -16,5 +16,8 @@ from django.conf.urls import url
 from mgr.samples import views
 
 urlpatterns = [
-    url(r'^samples/$', views.SampleList.as_view())
+    url(r'^samples/$', views.SampleList.as_view()),
+    url(r'^api/vnfmgr/v1/reloadstub/(?P<fileName>[0-9a-zA-Z\-\_\.]+)$', views.reloadstub, name='reloadstub'),
+    url(r'^api/vnfmgr/v1/reg2msb/(?P<msName>[0-9a-zA-Z\-\_]+)$', views.reg2msb, name='reg2msb'),
+    url(r'^(?P<uri>[0-9a-zA-Z\-\_/]+)$', views.stub, name='stub')
 ]
index 5162f23..0c25d4f 100644 (file)
 # limitations under the License.
 
 import logging
+import json
 
 from rest_framework.views import APIView
+from rest_framework.decorators import api_view
+from rest_framework import status
 from rest_framework.response import Response
 
+from mgr.pub.utils.restcall import req_by_msb
+from mgr.pub.config.config import REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM
+
 logger = logging.getLogger(__name__)
 
+_stub_mapping_ = {
+    ("GET", "/api/ms1/v1/samples/1"): (200, {"status": "ok"})
+}
+
 
 class SampleList(APIView):
     def get(self, request, format=None):
         logger.debug("get")
         return Response({"status": "active"})
+
+
+@api_view(http_method_names=['GET'])
+def reg2msb(request, *args, **kwargs):
+    ms_name = kwargs.get('msName')
+    logger.info("[reg2msb]ms name is %s", ms_name)
+    reg_param = REG_TO_MSB_REG_PARAM.copy()
+    reg_param['serviceName'] = ms_name
+    reg_param['url'] = '/api/%s/v1' % ms_name
+    req_by_msb(REG_TO_MSB_REG_URL, "POST", json.JSONEncoder().encode(reg_param))
+    return Response(data={"regok": ms_name}, status=status.HTTP_200_OK)
+
+
+@api_view(http_method_names=['GET'])
+def reloadstub(request, *args, **kwargs):
+    file_name = kwargs.get('fileName')
+    logger.info("[reloadstub]file name is %s", file_name)
+    global _stub_mapping_
+    with open("/tmp/%s" % file_name) as url_mapping_file:
+        for block in url_mapping_file.read().split("=##="):
+            items = block.split("|")
+            if len(items) != 4:
+                logger.warn("Abnormal block: %s", block)
+                continue
+            method = items[0].strip()
+            uri = items[1].strip()
+            code = int(items[2].strip())
+            data = json.loads(items[3].strip())
+            _stub_mapping_[(method, uri)] = (code, data)
+    return Response(data={"reloadstub": "ok"}, status=status.HTTP_200_OK)
+
+
+@api_view(http_method_names=['POST', 'GET', 'DELETE', 'PUT'])
+def stub(request, *args, **kwargs):
+    logger.info("[stub][%s][%s], data=%s", request.method, request.path, request.data)
+    global _stub_mapping_
+    match_result = _stub_mapping_.get((request.method.upper(), request.path))
+    if match_result:
+        return Response(data=match_result[1], status=match_result[0])
+    return Response(data={"stub": "stub"}, status=status.HTTP_200_OK)
index 262da0b..ecd5d20 100644 (file)
@@ -17,9 +17,9 @@ from django.conf.urls import include, url
 from mgr.pub.config.config import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM
 
 urlpatterns = [
-    url(r'^', include('mgr.samples.urls')),
     url(r'^', include('mgr.vnfreg.urls')),
     url(r'^', include('mgr.swagger.urls')),
+    url(r'^', include('mgr.samples.urls')),
 ]
 
 # regist to MSB when startup