From: Litao Gao Date: Fri, 24 Feb 2017 05:58:14 +0000 (-0500) Subject: Implements NBIs of multivimbroker X-Git-Tag: release/mercury~3 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=multicloud%2Fframework.git;a=commitdiff_plain;h=53ef35bf7af5bf1eec8d8e3908faace5ad2193e4 Implements NBIs of multivimbroker This Implementation accept the NB request and route the request to corresponding vim driver according to the vimid specified. Change-Id: Iccaf1204245bf15cad77f1bb0d47fab499e5f8cf Issue-Id: MULTIVIM-21 Signed-off-by: Litao Gao --- diff --git a/multivimbroker/multivimbroker/forwarder/__init__.py b/multivimbroker/multivimbroker/forwarder/__init__.py new file mode 100644 index 0000000..10dcb38 --- /dev/null +++ b/multivimbroker/multivimbroker/forwarder/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2017 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/multivimbroker/multivimbroker/forwarder/tests/__init__.py b/multivimbroker/multivimbroker/forwarder/tests/__init__.py new file mode 100644 index 0000000..10dcb38 --- /dev/null +++ b/multivimbroker/multivimbroker/forwarder/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2017 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/multivimbroker/multivimbroker/forwarder/tests/test_reqeust_forward.py b/multivimbroker/multivimbroker/forwarder/tests/test_reqeust_forward.py new file mode 100644 index 0000000..0b85b60 --- /dev/null +++ b/multivimbroker/multivimbroker/forwarder/tests/test_reqeust_forward.py @@ -0,0 +1,26 @@ +# Copyright 2017 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.test import TestCase + + +class TestNetworksRequest(TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def assert_true_result(self): + self.assertTrue(1) diff --git a/multivimbroker/multivimbroker/forwarder/urls.py b/multivimbroker/multivimbroker/forwarder/urls.py new file mode 100644 index 0000000..5598c44 --- /dev/null +++ b/multivimbroker/multivimbroker/forwarder/urls.py @@ -0,0 +1,24 @@ +# Copyright 2017 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from django.conf.urls import url +from rest_framework.urlpatterns import format_suffix_patterns + +from . import views + +urlpatterns = [ + url(r'^openoapi/multivim/v1/(?P[0-9a-zA-Z_-]+)', views.route) +] + +urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/multivimbroker/multivimbroker/forwarder/views.py b/multivimbroker/multivimbroker/forwarder/views.py new file mode 100644 index 0000000..e1013c7 --- /dev/null +++ b/multivimbroker/multivimbroker/forwarder/views.py @@ -0,0 +1,52 @@ +# Copyright 2017 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import logging +import re + +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_exempt + +from rest_framework import status + +from multivimbroker.pub.utils.restcall import req_by_msb +from multivimbroker.pub.msapi.extsys import get_vim_by_id + +logger = logging.getLogger(__name__) + + +@csrf_exempt +def route(request, vimid=''): + """ get vim info from vimid from local cache first + and then to ESR if cache miss + """ + content = "" + status_code = status.HTTP_200_OK + try: + vim = get_vim_by_id(vimid) + + # if vim type is openstack, use latest "newton" version as default + if vim["type"] == "openstack": + vim["type"] = "multivim-newton" + + route_uri = re.sub('multivim', vim["type"], request.get_full_path()) + retcode, content, status_code = \ + req_by_msb(route_uri, request.method, request.body) + if retcode != 0: + # Execptions are handled within req_by_msb + logger.error("Status code is %s, detail is %s.", + status_code, content) + except Exception as e: + content = e + status_code = status.HTTP_500_INTERNAL_SERVER_ERROR + return HttpResponse(content, status_code) diff --git a/multivimbroker/multivimbroker/pub/config/config.py b/multivimbroker/multivimbroker/pub/config/config.py index 2a75e94..a15fae1 100644 --- a/multivimbroker/multivimbroker/pub/config/config.py +++ b/multivimbroker/multivimbroker/pub/config/config.py @@ -15,8 +15,12 @@ import os MSB_SERVICE_IP = '127.0.0.1' MSB_SERVICE_PORT = '10080' +# [ESR] +ESR_GET_VIM_URI = "/openoapi/extsys/v1/vims" + # [IMAGE LOCAL PATH] -ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +ROOT_PATH = os.path.dirname( + os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # [REDIS] REDIS_HOST = '127.0.0.1' diff --git a/multivimbroker/multivimbroker/pub/msapi/extsys.py b/multivimbroker/multivimbroker/pub/msapi/extsys.py index 4683ae5..ac2fa13 100644 --- a/multivimbroker/multivimbroker/pub/msapi/extsys.py +++ b/multivimbroker/multivimbroker/pub/msapi/extsys.py @@ -14,12 +14,13 @@ import logging from multivimbroker.pub.exceptions import VimBrokerException from multivimbroker.pub.utils.restcall import req_by_msb +from multivimbroker.pub.config.config import ESR_GET_VIM_URI logger = logging.getLogger(__name__) def get_vims(): - ret = req_by_msb("/openoapi/extsys/v1/vims", "GET") + ret = req_by_msb(ESR_GET_VIM_URI, "GET") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) raise VimBrokerException("Failed to query VIMs from extsys.") @@ -27,8 +28,9 @@ def get_vims(): def get_vim_by_id(vim_id): - ret = req_by_msb("/openoapi/extsys/v1/vims/%s" % vim_id, "GET") + ret = req_by_msb("%s/%s" % (ESR_GET_VIM_URI, vim_id), "GET") if ret[0] != 0: logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) - raise VimBrokerException("Failed to query VIM with id (%s) from extsys." % vim_id) + raise VimBrokerException( + "Failed to query VIM with id (%s) from extsys." % vim_id) return json.JSONDecoder().decode(ret[1]) diff --git a/multivimbroker/multivimbroker/pub/utils/restcall.py b/multivimbroker/multivimbroker/pub/utils/restcall.py index 440ed24..d1c7b77 100644 --- a/multivimbroker/multivimbroker/pub/utils/restcall.py +++ b/multivimbroker/multivimbroker/pub/utils/restcall.py @@ -42,6 +42,7 @@ def call_req(base_url, user, passwd, auth_type, resource, method, content=''): http = httplib2.Http(ca_certs=ca_certs, disable_ssl_certificate_validation=(auth_type == rest_no_auth)) http.follow_all_redirects = True try: + logger.debug("request=%s)" % full_url) resp, resp_content = http.request(full_url, method=method.upper(), body=content, headers=headers) resp_status, resp_body = resp['status'], resp_content.decode('UTF-8') logger.debug("[%s][%d]status=%s,resp_body=%s)" % (callid, retry_times, resp_status, resp_body)) diff --git a/multivimbroker/multivimbroker/settings.py b/multivimbroker/multivimbroker/settings.py index 3741d2d..2cff49b 100644 --- a/multivimbroker/multivimbroker/settings.py +++ b/multivimbroker/multivimbroker/settings.py @@ -12,11 +12,6 @@ import os import sys -import redisco - -from multivimbroker.pub.config.config import REDIS_HOST, REDIS_PORT, REDIS_PASSWD -from multivimbroker.pub.config.config import DB_NAME, DB_IP, DB_USER, DB_PASSWD, DB_PORT - # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -73,16 +68,11 @@ REST_FRAMEWORK = { DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': DB_NAME, - 'HOST': DB_IP, - 'PORT': DB_PORT, - 'USER': DB_USER, - 'PASSWORD': DB_PASSWD, - }, + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } } -redisco.connection_setup(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWD, db=0) # CACHE_BACKEND = 'redis_cache.cache://%s@%s:%s' % (REDIS_PASSWD, REDIS_HOST, REDIS_PORT) TIME_ZONE = 'UTC' diff --git a/multivimbroker/multivimbroker/urls.py b/multivimbroker/multivimbroker/urls.py index b26d82f..c8e0f42 100644 --- a/multivimbroker/multivimbroker/urls.py +++ b/multivimbroker/multivimbroker/urls.py @@ -10,16 +10,18 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. from django.conf.urls import include, url -from multivimbroker.pub.config.config import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM +from multivimbroker.pub.config.config \ + import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM urlpatterns = [ url(r'^', include('multivimbroker.swagger.urls')), url(r'^', include('multivimbroker.samples.urls')), + url(r'^', include('multivimbroker.forwarder.urls')), ] -#url(r'^', include('multivimbroker.forward.urls')), # regist to MSB when startup if REG_TO_MSB_WHEN_START: import json from multivimbroker.pub.utils.restcall import req_by_msb - req_by_msb(REG_TO_MSB_REG_URL, "POST", json.JSONEncoder().encode(REG_TO_MSB_REG_PARAM)) + req_by_msb(REG_TO_MSB_REG_URL, "POST", + json.JSONEncoder().encode(REG_TO_MSB_REG_PARAM)) diff --git a/multivimbroker/requirements.txt b/multivimbroker/requirements.txt index 2590479..6d58957 100644 --- a/multivimbroker/requirements.txt +++ b/multivimbroker/requirements.txt @@ -2,9 +2,6 @@ Django==1.9.6 djangorestframework==3.3.3 -# for access MySQL -MySQL-python==1.2.5 - # redis cache redis==2.10.5