diff options
Diffstat (limited to 'multivimbroker')
-rw-r--r-- | multivimbroker/multivimbroker/forwarder/urls.py | 3 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/forwarder/views.py | 49 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/middleware.py | 64 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/pub/config/config.py | 23 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/pub/config/log.yml | 8 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/pub/utils/syscomm.py | 8 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/settings.py | 2 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/tests/test_check_capacity.py | 54 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/tests/test_vim_types.py (renamed from multivimbroker/multivimbroker/tests/test_urls.py) | 19 | ||||
-rw-r--r-- | multivimbroker/multivimbroker/urls.py | 15 | ||||
-rw-r--r-- | multivimbroker/pom.xml | 7 | ||||
-rw-r--r-- | multivimbroker/requirements.txt | 2 | ||||
-rwxr-xr-x | multivimbroker/sonar.sh | 81 |
13 files changed, 273 insertions, 62 deletions
diff --git a/multivimbroker/multivimbroker/forwarder/urls.py b/multivimbroker/multivimbroker/forwarder/urls.py index 6a60df4..771f052 100644 --- a/multivimbroker/multivimbroker/forwarder/urls.py +++ b/multivimbroker/multivimbroker/forwarder/urls.py @@ -16,6 +16,7 @@ from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patterns +from multivimbroker.forwarder.views import CheckCapacity from multivimbroker.forwarder.views import Extension from multivimbroker.forwarder.views import Forward from multivimbroker.forwarder.views import Identity @@ -27,6 +28,8 @@ from multivimbroker.forwarder.views import VIMTypes urlpatterns = [ url(r'^api/multicloud/v0/vim_types$', VIMTypes.as_view()), + url(r'^api/multicloud/v0/check_vim_capacity$', + CheckCapacity.as_view()), url(r'^api/multicloud/v0/(?P<vimid>[0-9a-zA-Z_-]+)/identity/v3$', Identity.as_view()), url(r'^api/multicloud/v0/(?P<vimid>[0-9a-zA-Z_-]+)/identity/v3' diff --git a/multivimbroker/multivimbroker/forwarder/views.py b/multivimbroker/multivimbroker/forwarder/views.py index 7935642..c77fe94 100644 --- a/multivimbroker/multivimbroker/forwarder/views.py +++ b/multivimbroker/multivimbroker/forwarder/views.py @@ -20,8 +20,7 @@ from rest_framework.views import APIView from rest_framework.views import Response from rest_framework.views import status from multivimbroker.forwarder.base import BaseHandler - -# +from multivimbroker.pub.utils.syscomm import originHeaders class BaseServer(BaseHandler, APIView): @@ -50,18 +49,21 @@ class Identity(BaseServer): def get(self, request, vimid): - return self.send(vimid, request.get_full_path(), request.body, "GET") + return self.send(vimid, request.get_full_path(), request.body, "GET", + headers=originHeaders(request)) def post(self, request, vimid): - return self.send(vimid, request.get_full_path(), request.body, "POST") + return self.send(vimid, request.get_full_path(), request.body, "POST", + headers=originHeaders(request)) class Registry(BaseServer): def post(self, request, vimid): - return self.send(vimid, request.get_full_path(), request.body, "POST") + return self.send(vimid, request.get_full_path(), request.body, "POST", + headers=originHeaders(request)) class UnRegistry(BaseServer): @@ -69,14 +71,15 @@ class UnRegistry(BaseServer): def delete(self, request, vimid): return self.send(vimid, request.get_full_path(), request.body, - "DELETE") + "DELETE", headers=originHeaders(request)) class Extension(BaseServer): def get(self, request, vimid): - return self.send(vimid, request.get_full_path(), request.body, "GET") + return self.send(vimid, request.get_full_path(), request.body, "GET", + headers=originHeaders(request)) class VIMTypes(BaseServer): @@ -96,6 +99,38 @@ class VIMTypes(BaseServer): return Response(data=ret, status=status.HTTP_200_OK) +class CheckCapacity(BaseServer): + + def post(self, request): + try: + body = json.loads(request.body) + except json.JSONDecodeError as e: + return Response( + data={'error': 'Invalidate request body %s.' % e}, + status=status.HTTP_400_BAD_REQUEST) + + ret = {"VIMs": []} + newbody = { + "vCPU": body.get("vCPU", 0), + "Memory": body.get("Memory", 0), + "Storage": body.get("Storage", 0) + } + for vim in body.get("VIMs", []): + url = request.get_full_path().replace( + "check_vim_capacity", "%s/capacity_check" % vim) + resp = self.send(vim, url, newbody, "POST") + if resp.status_code != status.HTTP_200_OK: + continue + try: + resp_body = json.loads(resp.body) + except json.JSONDecodeError: + continue + if not resp_body.get("result", False): + continue + ret['VIMs'].append(vim) + return Response(data=ret, status=status.HTTP_200_OK) + + # forward handler class Forward(BaseServer): diff --git a/multivimbroker/multivimbroker/middleware.py b/multivimbroker/multivimbroker/middleware.py new file mode 100644 index 0000000..5b320a3 --- /dev/null +++ b/multivimbroker/multivimbroker/middleware.py @@ -0,0 +1,64 @@ +# Copyright (c) 2017-2018 VMware, 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. + + +import uuid +from onaplogging.mdcContext import MDC +from multivimbroker.pub.config.config import SERVICE_NAME +from multivimbroker.pub.config.config import FORWARDED_FOR_FIELDS + + +class LogContextMiddleware(object): + + # the last IP behind multiple proxies, if no exist proxies + # get local host ip. + def _getLastIp(self, request): + + ip = "" + try: + for field in FORWARDED_FOR_FIELDS: + if field in request.META: + if ',' in request.META[field]: + parts = request.META[field].split(',') + ip = parts[-1].strip().split(":")[0] + else: + ip = request.META[field].split(":")[0] + + if ip == "": + ip = request.META.get("HTTP_HOST").split(":")[0] + + except Exception: + pass + + return ip + + def process_request(self, request): + + # Fetch TRANSACTIONID Id and pass to plugin server + ReqeustID = request.META.get("HTTP_X_TRANSACTIONID", None) + if ReqeustID is None: + ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME) + request.META["HTTP_X_TRANSACTIONID"] = ReqeustID + MDC.put("requestID", ReqeustID) + # generate the unique id + InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME) + MDC.put("invocationID", InovocationID) + MDC.put("serviceName", SERVICE_NAME) + # access ip + MDC.put("serviceIP", self._getLastIp(request)) + + return None + + def process_response(self, request, response): + + MDC.clear() + return response diff --git a/multivimbroker/multivimbroker/pub/config/config.py b/multivimbroker/multivimbroker/pub/config/config.py index 8fba115..192c743 100644 --- a/multivimbroker/multivimbroker/pub/config/config.py +++ b/multivimbroker/multivimbroker/pub/config/config.py @@ -24,26 +24,15 @@ MSB_SERVICE_PORT = '10080' AAI_ADDR = "aai.api.simpledemo.openecomp.org" AAI_PORT = "8443" AAI_SERVICE_URL = 'https://%s:%s/aai' % (AAI_ADDR, AAI_PORT) -AAI_SCHEMA_VERSION = "v11" +AAI_SCHEMA_VERSION = "v13" AAI_USERNAME = 'AAI' AAI_PASSWORD = 'AAI' +# [MDC] +SERVICE_NAME = "multicloud-broker" +FORWARDED_FOR_FIELDS = ["HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED_HOST", + "HTTP_X_FORWARDED_SERVER"] + # [IMAGE LOCAL PATH] ROOT_PATH = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -# [register] -REG_TO_MSB_WHEN_START = False -REG_TO_MSB_REG_URL = "/api/microservices/v1/services" -REG_TO_MSB_REG_PARAM = { - "serviceName": "multicloud", - "version": "v0", - "url": "/api/multicloud/v0", - "protocol": "REST", - "visualRange": "1", - "nodes": [{ - "ip": "127.0.0.1", - "port": "9001", - "ttl": 0 - }] -} diff --git a/multivimbroker/multivimbroker/pub/config/log.yml b/multivimbroker/multivimbroker/pub/config/log.yml index 12da69f..09be40d 100644 --- a/multivimbroker/multivimbroker/pub/config/log.yml +++ b/multivimbroker/multivimbroker/pub/config/log.yml @@ -12,14 +12,14 @@ handlers: class: "logging.handlers.RotatingFileHandler" filename: "/var/log/onap/multicloud/multivimbroker/multivimbroker.log" formatter: "mdcFormat" - maxBytes: 1024*1024*50 + maxBytes: 52428800 backupCount: 10 formatters: standard: - format: "%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s" + format: "%(asctime)s|||||%(name)s||%(thread)||%(funcName)s||%(levelname)s||%(message)s" mdcFormat: - format: "%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d] [%(levelname)s]:[%(mdc)s]: %(message)s" - mdcfmt: "{requestID}" + format: "%(asctime)s|||||%(name)s||%(thread)s||%(funcName)s||%(levelname)s||%(message)s||||%(mdc)s \t" + mdcfmt: "{requestID} {invocationID} {serviceName} {serviceIP}" datefmt: "%Y-%m-%d %H:%M:%S" (): onaplogging.mdcformatter.MDCFormatter diff --git a/multivimbroker/multivimbroker/pub/utils/syscomm.py b/multivimbroker/multivimbroker/pub/utils/syscomm.py index 0b2b557..bd4bbb5 100644 --- a/multivimbroker/multivimbroker/pub/utils/syscomm.py +++ b/multivimbroker/multivimbroker/pub/utils/syscomm.py @@ -36,6 +36,14 @@ def getHeadersKeys(response): return [header for header in response.keys() if header not in hopbyhop] +# trim out 'HTTP_' prefix part and replace "_" wiht "-". +def originHeaders(request): + regex = re.compile('^HTTP_') + return dict((regex.sub('', header).replace("_", "-"), value) + for (header, value) in request.META.items() + if header.startswith('HTTP_')) + + def findMultivimDriver(vim=None): json_file = os.path.join(os.path.dirname(__file__), '../config/provider-plugin.json') diff --git a/multivimbroker/multivimbroker/settings.py b/multivimbroker/multivimbroker/settings.py index dca2dd6..8d1fba5 100644 --- a/multivimbroker/multivimbroker/settings.py +++ b/multivimbroker/multivimbroker/settings.py @@ -51,6 +51,7 @@ MIDDLEWARE_CLASSES = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'multivimbroker.middleware.LogContextMiddleware', ] ROOT_URLCONF = 'multivimbroker.urls' @@ -96,7 +97,6 @@ config.yamlConfig(filepath=LOGGING_FILE, watchDog=True) if 'test' in sys.argv: from multivimbroker.pub.config import config - config.REG_TO_MSB_WHEN_START = False DATABASES = {} DATABASES['default'] = { 'ENGINE': 'django.db.backends.sqlite3', diff --git a/multivimbroker/multivimbroker/tests/test_check_capacity.py b/multivimbroker/multivimbroker/tests/test_check_capacity.py new file mode 100644 index 0000000..0a852e8 --- /dev/null +++ b/multivimbroker/multivimbroker/tests/test_check_capacity.py @@ -0,0 +1,54 @@ +# Copyright (c) 2017-2018 VMware, 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. + +import mock +import unittest + +from rest_framework import status + +from multivimbroker.forwarder.views import CheckCapacity + + +class CheckCapacityTest(unittest.TestCase): + + def setUp(self): + self.view = CheckCapacity() + super(CheckCapacityTest, self).setUp() + + def tearDown(self): + pass + + def test_check_capacity_success(self): + req = mock.Mock() + req.body = """ + { + "vCPU": 1, + "Memory": 1, + "Storage": 500, + "VIMs": ["openstack_RegionOne"] + }""" + req.get_full_path.return_value = ("http://msb.onap.org/api/multicloud" + "/v0/check_vim_capacity") + with mock.patch.object(self.view, "send") as send: + plugin_resp = mock.Mock() + plugin_resp.body = """{ + "result": true + }""" + plugin_resp.status_code = status.HTTP_200_OK + send.return_value = plugin_resp + + resp = self.view.post(req) + expect_body = { + "VIMs": ["openstack_RegionOne"] + } + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertDictEqual(expect_body, resp.data) diff --git a/multivimbroker/multivimbroker/tests/test_urls.py b/multivimbroker/multivimbroker/tests/test_vim_types.py index 7990033..b8a5080 100644 --- a/multivimbroker/multivimbroker/tests/test_urls.py +++ b/multivimbroker/multivimbroker/tests/test_vim_types.py @@ -8,20 +8,19 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -import json import mock import unittest -from multivimbroker.pub.config import config -from multivimbroker import urls +from rest_framework.views import status + +from multivimbroker.forwarder.views import VIMTypes class TestUrls(unittest.TestCase): + def setUp(self): + self.view = VIMTypes() - def test_request_msb(self): - with mock.patch("multivimbroker.pub.utils.restcall." - "req_by_msb") as req_by_msb: - urls.req_msb(True) - req_by_msb.assert_called_once_with( - config.REG_TO_MSB_REG_URL, "POST", - json.JSONEncoder().encode(config.REG_TO_MSB_REG_PARAM)) + def test_vim_types_success(self): + resp = self.view.get(mock.Mock()) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(2, len(resp.data)) diff --git a/multivimbroker/multivimbroker/urls.py b/multivimbroker/multivimbroker/urls.py index 0843bb3..c393210 100644 --- a/multivimbroker/multivimbroker/urls.py +++ b/multivimbroker/multivimbroker/urls.py @@ -11,23 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. from django.conf.urls import include, url -import json - -from multivimbroker.pub.config import config - urlpatterns = [ url(r'^', include('multivimbroker.swagger.urls')), url(r'^', include('multivimbroker.forwarder.urls')), ] - - -def req_msb(request_when_start): - # regist to MSB when startup - if request_when_start: - from multivimbroker.pub.utils.restcall import req_by_msb - req_by_msb(config.REG_TO_MSB_REG_URL, "POST", - json.JSONEncoder().encode(config.REG_TO_MSB_REG_PARAM)) - - -req_msb(config.REG_TO_MSB_WHEN_START) diff --git a/multivimbroker/pom.xml b/multivimbroker/pom.xml index 274f54d..fbd40c0 100644 --- a/multivimbroker/pom.xml +++ b/multivimbroker/pom.xml @@ -32,13 +32,6 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <nexusproxy>https://nexus.onap.org</nexusproxy> - <sonar.sources>.</sonar.sources> - <sonar.junit.reportsPath>xunit-results.xml</sonar.junit.reportsPath> - <sonar.python.coverage.reportPath>vio/coverage.xml</sonar.python.coverage.reportPath> - <sonar.language>py</sonar.language> - <sonar.pluginName>Python</sonar.pluginName> - <sonar.inclusions>**/*.py</sonar.inclusions> - <sonar.exclusions>tests/*,setup.py</sonar.exclusions> </properties> <build> <plugins> diff --git a/multivimbroker/requirements.txt b/multivimbroker/requirements.txt index 1378612..ef24d49 100644 --- a/multivimbroker/requirements.txt +++ b/multivimbroker/requirements.txt @@ -24,4 +24,4 @@ mock==2.0.0 unittest_xml_reporting==1.12.0 # for onap logging -onappylog>=1.0.5
\ No newline at end of file +onappylog>=1.0.6
\ No newline at end of file diff --git a/multivimbroker/sonar.sh b/multivimbroker/sonar.sh new file mode 100755 index 0000000..e173d1f --- /dev/null +++ b/multivimbroker/sonar.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# Copyright 2018 VMware Corporation. +# +# 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. + + +set -e + +echo "running script: [$0] for module [$1] at stage [$2]" + +export SETTINGS_FILE=${SETTINGS_FILE:-$HOME/.m2/settings.xml} +MVN_PROJECT_MODULEID="$1" +MVN_PHASE="$2" + + +FQDN="${MVN_PROJECT_GROUPID}.${MVN_PROJECT_ARTIFACTID}" +if [ "$MVN_PROJECT_MODULEID" == "__" ]; then + MVN_PROJECT_MODULEID="" +fi + +if [ -z "$WORKSPACE" ]; then + WORKSPACE=$(pwd) +fi + +# mvn phase in life cycle +MVN_PHASE="$2" + + +echo "MVN_PROJECT_MODULEID is [$MVN_PROJECT_MODULEID]" +echo "MVN_PHASE is [$MVN_PHASE]" +echo "MVN_PROJECT_GROUPID is [$MVN_PROJECT_GROUPID]" +echo "MVN_PROJECT_ARTIFACTID is [$MVN_PROJECT_ARTIFACTID]" +echo "MVN_PROJECT_VERSION is [$MVN_PROJECT_VERSION]" + +run_tox_test() +{ + set -x + CURDIR=$(pwd) + TOXINIS=$(find . -name "tox.ini") + for TOXINI in "${TOXINIS[@]}"; do + DIR=$(echo "$TOXINI" | rev | cut -f2- -d'/' | rev) + cd "${CURDIR}/${DIR}" + rm -rf ./venv-tox ./.tox + virtualenv ./venv-tox + source ./venv-tox/bin/activate + pip install --upgrade pip + pip install --upgrade tox argparse + pip freeze + cd multivimbroker + tox -e cover + deactivate + cd .. + rm -rf ./venv-tox ./.tox + done +} + + +case $MVN_PHASE in +clean) + echo "==> clean phase script" + rm -rf ./venv-* + ;; +test) + echo "==> test phase script" + run_tox_test + ;; +*) + echo "==> unprocessed phase" + ;; +esac + |