diff options
46 files changed, 1011 insertions, 60 deletions
diff --git a/newton/.gitignore b/newton/.gitignore index d0dc4903..224c808e 100644 --- a/newton/.gitignore +++ b/newton/.gitignore @@ -15,3 +15,5 @@ openstack_multicloud.egg-info/ .eggs/ build/ test-reports/ +coverage.xml + diff --git a/newton/newton/extensions/views/epacaps.py b/newton/newton/extensions/views/epacaps.py index 77542b31..7efb71a6 100644 --- a/newton/newton/extensions/views/epacaps.py +++ b/newton/newton/extensions/views/epacaps.py @@ -23,7 +23,7 @@ from newton_base.extensions import epacaps as newton_epacaps logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class EpaCaps(newton_epacaps.EpaCaps): diff --git a/newton/newton/extensions/views/extensions.py b/newton/newton/extensions/views/extensions.py index 851bc7ea..a40ccdd3 100644 --- a/newton/newton/extensions/views/extensions.py +++ b/newton/newton/extensions/views/extensions.py @@ -19,7 +19,7 @@ from newton_base.extensions import extensions as newton_extensions logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Extensions(newton_extensions.Extensions): diff --git a/newton/newton/proxy/views/identityV3.py b/newton/newton/proxy/views/identityV3.py index dd280314..c831d017 100644 --- a/newton/newton/proxy/views/identityV3.py +++ b/newton/newton/proxy/views/identityV3.py @@ -19,7 +19,7 @@ from newton_base.proxy import identityV3 as newton_identityV3 logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Tokens(newton_identityV3.Tokens): diff --git a/newton/newton/proxy/views/services.py b/newton/newton/proxy/views/services.py index 4bfecdbc..c1d4f194 100644 --- a/newton/newton/proxy/views/services.py +++ b/newton/newton/proxy/views/services.py @@ -21,7 +21,7 @@ from newton_base.proxy import services as newton_services logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Services(newton_services.Services): diff --git a/newton/newton/registration/views/registration.py b/newton/newton/registration/views/registration.py index e9f1a357..f07428a1 100644 --- a/newton/newton/registration/views/registration.py +++ b/newton/newton/registration/views/registration.py @@ -26,7 +26,7 @@ from newton_base.openoapi.flavor import Flavors logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Registry(newton_registration.Registry): diff --git a/newton/newton/resource/__init__.py b/newton/newton/resource/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/newton/newton/resource/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/newton/newton/resource/tests/__init__.py b/newton/newton/resource/tests/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/newton/newton/resource/tests/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/newton/newton/resource/tests/test_capacity.py b/newton/newton/resource/tests/test_capacity.py new file mode 100644 index 00000000..071997e3 --- /dev/null +++ b/newton/newton/resource/tests/test_capacity.py @@ -0,0 +1,120 @@ +# Copyright (c) 2017-2018 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 mock + +from rest_framework import status + +from common.utils import restcall +from newton_base.tests import mock_info +from newton_base.tests import test_base +from newton_base.util import VimDriverUtils + +MOCK_GET_TENANT_LIMIT_RESPONSE = { + "limits" : { + "rate" : [], + "absolute" : { + "maxTotalRAMSize" : 128*1024, + "totalRAMUsed" : 8*1024, + "totalCoresUsed" : 4, + "maxTotalCores" : 20, + } + } +} + +MOCK_GET_HYPER_STATATICS_RESPONSE = { + "hypervisor_statistics" : { + "vcpus_used" : 4, + "free_ram_mb" : 120*1024, + "vcpus" : 10, + "free_disk_gb" : 300 + } +} + +MOCK_GET_STORAGE_RESPONSE = { + "limits" : { + "rate" : [], + "absolute" : { + "totalGigabytesUsed" : 200, + "maxTotalVolumeGigabytes" : 500, + } + } +} + +TEST_REQ_SUCCESS_SOURCE = { + "vCPU": "4", + "Memory": "4096", + "Storage": "200" +} + +TEST_REQ_FAILED_SOURCE = { + "vCPU": "17", + "Memory": "4096", + "Storage": "200" +} + +class TestCapacity(test_base.TestRequest): + def setUp(self): + super(TestCapacity, self).setUp() + + def _get_mock_response(self, return_value=None): + mock_response = mock.Mock(spec=test_base.MockResponse) + mock_response.status_code = status.HTTP_200_OK + mock_response.json.return_value = return_value + return mock_response + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_capacity_check_success(self, mock_get_vim_info, mock_get_session): + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + mock_get_session.return_value = test_base.get_mock_session( + ["get"], { + "side_effect": [ + self._get_mock_response(MOCK_GET_TENANT_LIMIT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPER_STATATICS_RESPONSE), + self._get_mock_response(MOCK_GET_STORAGE_RESPONSE), + ] + }) + + response = self.client.post(( + "/api/%s/v0/windriver-hudson-dc_RegionOne/" + "capacity_check" % test_base.MULTIVIM_VERSION), + TEST_REQ_SUCCESS_SOURCE, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEqual({"result": True}, response.data) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_capacity_check_nova_limits_failed(self, mock_get_vim_info, mock_get_session): + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + mock_get_session.return_value = test_base.get_mock_session( + ["get"], { + "side_effect": [ + self._get_mock_response(MOCK_GET_TENANT_LIMIT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPER_STATATICS_RESPONSE), + self._get_mock_response(MOCK_GET_STORAGE_RESPONSE), + ] + }) + + response = self.client.post(( + "/api/%s/v0/windriver-hudson-dc_RegionOne/" + "capacity_check" % test_base.MULTIVIM_VERSION), + TEST_REQ_FAILED_SOURCE, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEqual({"result": False}, response.data) + diff --git a/newton/newton/resource/views/__init__.py b/newton/newton/resource/views/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/newton/newton/resource/views/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/newton/newton/resource/views/capacity.py b/newton/newton/resource/views/capacity.py new file mode 100644 index 00000000..d73cc0fb --- /dev/null +++ b/newton/newton/resource/views/capacity.py @@ -0,0 +1,117 @@ +# Copyright (c) 2017-2018 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 json +import traceback + +from rest_framework import status + +from django.conf import settings +from common.exceptions import VimDriverNewtonException +from newton_base.util import VimDriverUtils + +from keystoneauth1.exceptions import HttpError +from rest_framework import status +from rest_framework.response import Response +from rest_framework.views import APIView +from common.msapi import extsys + + +logger = logging.getLogger(__name__) + + +class CapacityCheck(APIView): + + def __init__(self): + self._logger = logger + + def post(self, request, vimid=""): + self._logger.info("CapacityCheck--post::vimid, data> %s, %s" % (vimid, request.data)) + self._logger.debug("CapacityCheck--post::META> %s" % request.META) + + hasEnoughResource = False + try : + resource_demand = request.data + + #get token: + cloud_owner, regionid = extsys.decode_vim_id(vimid) + interface = 'public' + service = {'service_type': 'compute', + 'interface': interface, + 'region_id': regionid} + + tenant_name = None + vim = VimDriverUtils.get_vim_info(vimid) + sess = VimDriverUtils.get_session(vim, tenant_name) + + #get limit for this tenant + req_resouce = "/limits" + resp = sess.get(req_resouce, endpoint_filter=service) + content = resp.json() + compute_limits = content['limits']['absolute'] + + #get total resource of this cloud region + req_resouce = "/os-hypervisors/statistics" + resp = sess.get(req_resouce, endpoint_filter=service) + content = resp.json() + hypervisor_statistics = content['hypervisor_statistics'] + + #get storage limit for this tenant + service['service_type'] = 'volumev2' + req_resouce = "/limits" + resp = sess.get(req_resouce, endpoint_filter=service) + content = resp.json() + storage_limits = content['limits']['absolute'] + + # compute actual available resource for this tenant + remainVCPU = compute_limits['maxTotalCores'] - compute_limits['totalCoresUsed'] + + if (compute_limits['maxTotalCores'] > hypervisor_statistics['vcpus']): + if hypervisor_statistics['vcpus'] > compute_limits['totalCoresUsed']: + remainVCPU = hypervisor_statistics['vcpus'] - compute_limits['totalCoresUsed'] + else: + remainVCPU = 0 + + remainMEM = compute_limits['maxTotalRAMSize'] - compute_limits['totalRAMUsed'] + if hypervisor_statistics['free_ram_mb'] > remainMEM: + remainMEM = hypervisor_statistics['free_ram_mb'] + + remainStorage = storage_limits['maxTotalVolumeGigabytes'] - storage_limits['totalGigabytesUsed'] + if (remainStorage < hypervisor_statistics['free_disk_gb']): + remainStorage = hypervisor_statistics['free_disk_gb'] + + # compare resource demanded with available + if (int(resource_demand['vCPU']) >= remainVCPU): + hasEnoughResource = False + elif (int(resource_demand['Memory']) >= remainMEM): + hasEnoughResource = False + elif (int(resource_demand['Storage']) >= remainStorage): + hasEnoughResource = False + else: + hasEnoughResource = True + + return Response(data={'result': hasEnoughResource}, status=status.HTTP_200_OK) + except VimDriverNewtonException as e: + return Response(data={'result': hasEnoughResource,'error': e.content}, status=e.status_code) + except HttpError as e: + self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) + resp = e.response.json() + resp.update({'result': hasEnoughResource}) + return Response(data=e.response.json(), status=e.http_status) + except Exception as e: + self._logger.error(traceback.format_exc()) + return Response(data={'result': hasEnoughResource, 'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + diff --git a/newton/newton/settings.py b/newton/newton/settings.py index a5585a68..4a78f98b 100644 --- a/newton/newton/settings.py +++ b/newton/newton/settings.py @@ -38,9 +38,9 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = '3o-wney!99y)^h3v)0$j16l9=fdjxcb+a8g+q3tfbahcnu2b0o' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +#DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['*'] # Application definition diff --git a/newton/newton/urls.py b/newton/newton/urls.py index ca827480..ab09346d 100644 --- a/newton/newton/urls.py +++ b/newton/newton/urls.py @@ -16,6 +16,7 @@ from django.conf.urls import include, url from newton.registration.views import registration from newton_base.openoapi import tenants +from newton.resource.views import capacity urlpatterns = [ url(r'^', include('newton.swagger.urls')), @@ -32,6 +33,9 @@ urlpatterns = [ tenants.Tenants.as_view()), url(r'^api/multicloud-newton/v0/(?P<vimid>[0-9a-zA-Z_-]+)/' '(?P<tenantid>[0-9a-zA-Z_-]{20,})/', include('newton.requests.urls')), + # CapacityCheck + url(r'^api/multicloud-newton/v0/(?P<vimid>[0-9a-zA-Z_-]+)/capacity_check/?$', + capacity.CapacityCheck.as_view()), ] diff --git a/newton/pom.xml b/newton/pom.xml index 4f16383f..1fda1076 100644 --- a/newton/pom.xml +++ b/newton/pom.xml @@ -36,7 +36,7 @@ <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.sources>.,../share</sonar.sources> <sonar.junit.reportsPath>xunit-results.xml</sonar.junit.reportsPath> <sonar.python.coverage.reportPath>coverage.xml</sonar.python.coverage.reportPath> <sonar.language>py</sonar.language> @@ -52,7 +52,7 @@ <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> - <executable>${session.executionRootDirectory}/mvn-phase-script.sh</executable> + <executable>${project.basedir}/mvn-phase-script.sh</executable> <environmentVariables> <!-- make mvn properties as env for our script --> <MVN_PROJECT_GROUPID>${project.groupId}</MVN_PROJECT_GROUPID> diff --git a/newton/tox.ini b/newton/tox.ini index e3334cf1..ec17886d 100644 --- a/newton/tox.ini +++ b/newton/tox.ini @@ -16,12 +16,12 @@ setenv = deps = -r{toxinidir}/requirements.txt commands = coverage run --branch manage.py test newton - coverage report --omit="./venv-tox/*,./.tox/*,*tests*,*__init__.py,*newton_base*,*common*" --fail-under=30 + coverage report --omit="./venv-tox/*,./.tox/*,*tests*,*__init__.py" --fail-under=30 [testenv:pep8] deps=flake8 commands=flake8 [testenv:cov] -commands = coverage xml --omit="./venv-tox/*,./.tox/*,*tests*,*__init__.py,*newton_base*,*common*, *site-packages*" +commands = coverage xml --omit="./venv-tox/*,./.tox/*,*tests*,*__init__.py, *site-packages*" diff --git a/ocata/.gitignore b/ocata/.gitignore index e86d02b0..06570027 100644 --- a/ocata/.gitignore +++ b/ocata/.gitignore @@ -8,4 +8,6 @@ logs/*.log .tox .coverage htmlcov/ +coverage.xml +test-reports/ diff --git a/ocata/ocata/registration/tests/test_registration.py b/ocata/ocata/registration/tests/test_registration.py index e7d089b9..7ab7e306 100644 --- a/ocata/ocata/registration/tests/test_registration.py +++ b/ocata/ocata/registration/tests/test_registration.py @@ -49,9 +49,55 @@ MOCK_GET_FLAVOR_RESPONSE = { "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, "swap": False, "os-flavor-access:is_public": True, "OS-FLV-DISABLED:disabled": True, - "properties": "hw:cpu_policy, hw:cpu_thread_policy" + "properties": "hw:cpu_policy=dedicated, hw:cpu_thread_policy=prefer" + }, + { + "id": "4", "name": "onap.cpu_topology", "vcpus": 128, "ram": "2MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + "properties": "hw:cpu_sockets=4, hw:cpu_cores=4, hw:cpu_threads=8" + }, + { + "id": "5", "name": "onap.base_capabilities", "vcpus": 2, "ram": "2MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + }, + { + "id": "6", "name": "onap.numa", "vcpus": 6, "ram": "6144MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + "properties": "hw:numa_nodes=2, hw:numa_cpus.0=0,1, hw:numa_mem.0=2048, hw:numa_cpus.1=2,3,4,5 hw:numa_mem.1=4096" + }, + { + "id": "7", "name": "onap.local_storage", "vcpus": 2, "ram": "2MB", + "disk": "4096", "OS-FLV-EXT-DATA:ephemeral": "163840", + "swap": "8192", "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + }, + { + "id": "8", "name": "onap.huge_page", "vcpus": 2, "ram": "2MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + "properties": "hw:huge_page_size" + }, + { + "id": "9", "name": "onap.iax", "vcpus": 2, "ram": "2MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + "properties": "hw:capabilities:cpu_info:features=aes" + }, + { + "id": "10", "name": "onap.pci_passthrough", "vcpus": 2, "ram": "2MB", + "disk": "2G", "OS-FLV-EXT-DATA:ephemeral": True, + "swap": False, "os-flavor-access:is_public": True, + "OS-FLV-DISABLED:disabled": True, + "properties": "pci_passthrough:alias=sriov-pf-intel-8086-10fb:1" }, - ] } diff --git a/ocata/ocata/registration/views/registration.py b/ocata/ocata/registration/views/registration.py index 5d663fe1..e3121240 100644 --- a/ocata/ocata/registration/views/registration.py +++ b/ocata/ocata/registration/views/registration.py @@ -43,7 +43,7 @@ class Registry(newton_registration.Registry): for flavor in self._get_list_resources( "/flavors/detail", "compute", session, viminfo, vimid, "flavors"): - + flavor_info = { 'flavor-id': flavor['id'], 'flavor-name': flavor['name'], @@ -55,46 +55,178 @@ class Registry(newton_registration.Registry): 'flavor-is-public': flavor['os-flavor-access:is_public'], 'flavor-disabled': flavor['OS-FLV-DISABLED:disabled'], } - if flavor.get('link') and len(flavor['link']) > 0: flavor_info['flavor-selflink'] = flavor['link'][0]['href'] or 'http://0.0.0.0', else: flavor_info['flavor-selflink'] = 'http://0.0.0.0', - + # add hpa capabilities if (flavor['name'].find('onap.') == -1): continue - + properties = flavor['properties'].split(', ') - if len(properties): - flavor_info['flavor-properties'] = flavor['properties'] - # add hpa capability cpu pinning - if (flavor['name'].find('onap.cpu_pinning') != -1): - uuid1 = uuid.uuid4() - hpa_caps.append("{'hpaCapabilityID': '" + str(uuid1) + "', ") - hpa_caps.append("'hpaFeature': 'cpuPinning', ") - hpa_caps.append("'hardwareArchitecture': 'generic', ") - hpa_caps.append("'version': 'v1', ") + uuid4 = uuid.uuid4() + # add hpa capability cpu pinning + if (flavor['name'].find('onap.cpu_pinning') != -1): + hpa_caps.append("{'hpaCapabilityID': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'cpuPinning', ") + hpa_caps.append("'hardwareArchitecture': 'generic', ") + hpa_caps.append("'version': 'v1', ") + if len(properties): + flavor_info['flavor-properties'] = flavor['properties'] hpa_caps.append("[") for p in range(len(properties)): - if (properties[p] == "hw:cpu_policy") : + value = properties[p].split('=')[1] + if (properties[p].find("hw:cpu_policy") != -1) : hpa_caps.append("{'hpa-attribute-key':'logicalCpuThreadPinningPolicy', ") - hpa_caps.append("'hpa-attribute-value': {'value':'prefer'}}, ") - if (properties[p] == "hw:cpu_thread_policy") : + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + "'}}, ") + if (properties[p].find("hw:cpu_thread_policy") != -1) : hpa_caps.append("{'hpa-attribute-key':'logicalCpuPinningPolicy', ") - hpa_caps.append("'hpa-attribute-value': {'value':'dedicated'}}, ") - hpa_caps.append("]},") - else: - self._logger.info("can not support this properties") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + "'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.cpu_topology') != -1): + hpa_caps.append("{'hpaCapabilityID': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'cpuTopology', ") + hpa_caps.append("'hardwareArchitecture': 'generic', ") + hpa_caps.append("'version': 'v1', ") + + if len(properties): + flavor_info['flavor-properties'] = flavor['properties'] + hpa_caps.append("[") + for p in range(len(properties)): + value = properties[p].split('=')[1] + if (properties[p].find("hw:cpu_sockets") != -1) : + hpa_caps.append("{'hpa-attribute-key':'numCpuSockets', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + "'}}, ") + if (properties[p].find("hw:cpu_cores") != -1) : + hpa_caps.append("{'hpa-attribute-key':'numCpuCores', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + "'}}, ") + if (properties[p].find("hw:cpu_threads") != -1) : + hpa_caps.append("{'hpa-attribute-key':'numCpuThreads', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + "'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.base_capabilities') != -1): + hpa_caps.append("{'hpaCapabilityID': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'baseCapabilities', ") + hpa_caps.append("'hardwareArchitecture': 'generic', ") + hpa_caps.append("'version': 'v1', ") + + hpa_caps.append("[") + hpa_caps.append("{'hpa-attribute-key':'numVirtualCpu', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + str(flavor_info['vcpus']) + "'}}, ") + hpa_caps.append("{'hpa-attribute-key':'virtualMemSize', ") + hpa_caps.append("'hpa-attribute-value': {'value':" + str(flavor_info['mem']) + ", unit:'MB'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.local_storage') != -1): + hpa_caps.append("{'hpaCapabilityID': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'localStorage', ") + hpa_caps.append("'hardwareArchitecture': 'generic', ") + hpa_caps.append("'version': 'v1', ") + + hpa_caps.append("[") + hpa_caps.append("{'hpa-attribute-key':'diskSize', ") + hpa_caps.append("'hpa-attribute-value': {'value':" + str(flavor_info['disk']) + ", unit:'MB'}}, ") + hpa_caps.append("{'hpa-attribute-key':'ephemeralDiskSize', ") + hpa_caps.append("'hpa-attribute-value': {'value':" + str(flavor_info['OS-FLV-EXT-DATA:ephemeral']) + ", unit:'MB'}}, ") + hpa_caps.append("{'hpa-attribute-key':'swapMemSize', ") + hpa_caps.append("'hpa-attribute-value': {'value':" + str(flavor_info['swap']) + ", unit:'MB'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.numa') != -1): + hpa_caps.append("{'hpaCapabilityID': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'numa', ") + hpa_caps.append("'hardwareArchitecture': 'generic', ") + hpa_caps.append("'version': 'v1', ") + + if len(properties): + flavor_info['flavor-properties'] = flavor['properties'] + hpa_caps.append("[") + for p in range(len(properties)): + p_arr = properties[p].split('=') + value = p_arr[1] + index = p_arr[0].split('.')[1] + if (properties[p].find("hw:numa_nodes") != -1) : + hpa_caps.append("{'hpa-attribute-key':'numNodes', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + "'}}, ") + if (properties[p].find("hw:numa_cpus") != -1) : + hpa_caps.append("{'hpa-attribute-key':'numaCpus-" + index + "', ") + hpa_caps.append("'hpa-attribute-value': {'value':'[" + value + "]'}}, ") + if (properties[p] == ("hw:numa_mem") != -1) : + hpa_caps.append("{'hpa-attribute-key':'numaMem-"+ index +"', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value + ", unit:'MB'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.huge_page') != -1): + hpa_caps.append("{'hpaCapabilityId': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'hugePages', ") + hpa_caps.append("'hardwareArchitecture': 'generic', ") + hpa_caps.append("'version': 'v1', ") + + if len(properties): + flavor_info['flavor-properties'] = flavor['properties'] + hpa_caps.append("[") + values = flavor['name'].split('_') + for p in range(len(properties)): + if (properties[p] == "hw:mem_page_size") : + hpa_caps.append("{'hpa-attribute-key':'memoryPageSize', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + values[2] + "'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.iax') != -1): + hpa_caps.append("{'hpaCapabilityId': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'instructionSetExtensions', ") + hpa_caps.append("'hardwareArchitecture': 'Intel64', ") + hpa_caps.append("'version': 'v1', ") + + if len(properties): + flavor_info['flavor-properties'] = flavor['properties'] + hpa_caps.append("[") + value = flavor['properties'].split('=')[1] + for p in range(len(properties)): + if (properties[p].find("hw:capabilities:cpu_info:features") != -1) : + hpa_caps.append("{'hpa-attribute-key':'instructionSetExtensions', ") + hpa_caps.append("'hpa-attribute-value': {'value':[" + value + "]}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + + elif (flavor['name'].find('onap.pci_passthrough') != -1) : + hpa_caps.append("{'hpaCapabilityId': '" + str(uuid4) + "', ") + hpa_caps.append("'hpaFeature': 'pciPassthrough', ") + hpa_caps.append("'version': 'v1', ") + if len(properties): + values = properties[0].split('-') + hpa_caps.append("'hardwareArchitecture': '" + values[2] + "', ") + + flavor_info['flavor-properties'] = flavor['properties'] + hpa_caps.append("[") + value = values[4].split(':') + hpa_caps.append("{'hpa-attribute-key':'pciCount', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value[1] + "'}}, ") + hpa_caps.append("{'hpa-attribute-key':'pciVendorId', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + values[3] + "'}}, ") + hpa_caps.append("{'hpa-attribute-key':'pciDeviceId', ") + hpa_caps.append("'hpa-attribute-value': {'value':'" + value[0] + "'}}, ") + hpa_caps.append("]") + hpa_caps.append("},") + else: self._logger.info("can not support this flavor type") hpa_caps.append("]") str_hpa_caps = '' flavor_info['hpa_capabilities'] = str_hpa_caps.join(hpa_caps) self._logger.debug("flavor_info: %s" % flavor_info) - + self._update_resoure( cloud_owner, cloud_region_id, flavor['id'], flavor_info, "flavor") diff --git a/ocata/ocata/resource/__init__.py b/ocata/ocata/resource/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/ocata/ocata/resource/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/ocata/ocata/resource/tests/__init__.py b/ocata/ocata/resource/tests/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/ocata/ocata/resource/tests/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/ocata/ocata/resource/tests/test_capacity.py b/ocata/ocata/resource/tests/test_capacity.py new file mode 100644 index 00000000..071997e3 --- /dev/null +++ b/ocata/ocata/resource/tests/test_capacity.py @@ -0,0 +1,120 @@ +# Copyright (c) 2017-2018 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 mock + +from rest_framework import status + +from common.utils import restcall +from newton_base.tests import mock_info +from newton_base.tests import test_base +from newton_base.util import VimDriverUtils + +MOCK_GET_TENANT_LIMIT_RESPONSE = { + "limits" : { + "rate" : [], + "absolute" : { + "maxTotalRAMSize" : 128*1024, + "totalRAMUsed" : 8*1024, + "totalCoresUsed" : 4, + "maxTotalCores" : 20, + } + } +} + +MOCK_GET_HYPER_STATATICS_RESPONSE = { + "hypervisor_statistics" : { + "vcpus_used" : 4, + "free_ram_mb" : 120*1024, + "vcpus" : 10, + "free_disk_gb" : 300 + } +} + +MOCK_GET_STORAGE_RESPONSE = { + "limits" : { + "rate" : [], + "absolute" : { + "totalGigabytesUsed" : 200, + "maxTotalVolumeGigabytes" : 500, + } + } +} + +TEST_REQ_SUCCESS_SOURCE = { + "vCPU": "4", + "Memory": "4096", + "Storage": "200" +} + +TEST_REQ_FAILED_SOURCE = { + "vCPU": "17", + "Memory": "4096", + "Storage": "200" +} + +class TestCapacity(test_base.TestRequest): + def setUp(self): + super(TestCapacity, self).setUp() + + def _get_mock_response(self, return_value=None): + mock_response = mock.Mock(spec=test_base.MockResponse) + mock_response.status_code = status.HTTP_200_OK + mock_response.json.return_value = return_value + return mock_response + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_capacity_check_success(self, mock_get_vim_info, mock_get_session): + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + mock_get_session.return_value = test_base.get_mock_session( + ["get"], { + "side_effect": [ + self._get_mock_response(MOCK_GET_TENANT_LIMIT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPER_STATATICS_RESPONSE), + self._get_mock_response(MOCK_GET_STORAGE_RESPONSE), + ] + }) + + response = self.client.post(( + "/api/%s/v0/windriver-hudson-dc_RegionOne/" + "capacity_check" % test_base.MULTIVIM_VERSION), + TEST_REQ_SUCCESS_SOURCE, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEqual({"result": True}, response.data) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_capacity_check_nova_limits_failed(self, mock_get_vim_info, mock_get_session): + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + mock_get_session.return_value = test_base.get_mock_session( + ["get"], { + "side_effect": [ + self._get_mock_response(MOCK_GET_TENANT_LIMIT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPER_STATATICS_RESPONSE), + self._get_mock_response(MOCK_GET_STORAGE_RESPONSE), + ] + }) + + response = self.client.post(( + "/api/%s/v0/windriver-hudson-dc_RegionOne/" + "capacity_check" % test_base.MULTIVIM_VERSION), + TEST_REQ_FAILED_SOURCE, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEqual({"result": False}, response.data) + diff --git a/ocata/ocata/resource/views/__init__.py b/ocata/ocata/resource/views/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/ocata/ocata/resource/views/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/ocata/ocata/resource/views/capacity.py b/ocata/ocata/resource/views/capacity.py new file mode 100644 index 00000000..d73cc0fb --- /dev/null +++ b/ocata/ocata/resource/views/capacity.py @@ -0,0 +1,117 @@ +# Copyright (c) 2017-2018 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 json +import traceback + +from rest_framework import status + +from django.conf import settings +from common.exceptions import VimDriverNewtonException +from newton_base.util import VimDriverUtils + +from keystoneauth1.exceptions import HttpError +from rest_framework import status +from rest_framework.response import Response +from rest_framework.views import APIView +from common.msapi import extsys + + +logger = logging.getLogger(__name__) + + +class CapacityCheck(APIView): + + def __init__(self): + self._logger = logger + + def post(self, request, vimid=""): + self._logger.info("CapacityCheck--post::vimid, data> %s, %s" % (vimid, request.data)) + self._logger.debug("CapacityCheck--post::META> %s" % request.META) + + hasEnoughResource = False + try : + resource_demand = request.data + + #get token: + cloud_owner, regionid = extsys.decode_vim_id(vimid) + interface = 'public' + service = {'service_type': 'compute', + 'interface': interface, + 'region_id': regionid} + + tenant_name = None + vim = VimDriverUtils.get_vim_info(vimid) + sess = VimDriverUtils.get_session(vim, tenant_name) + + #get limit for this tenant + req_resouce = "/limits" + resp = sess.get(req_resouce, endpoint_filter=service) + content = resp.json() + compute_limits = content['limits']['absolute'] + + #get total resource of this cloud region + req_resouce = "/os-hypervisors/statistics" + resp = sess.get(req_resouce, endpoint_filter=service) + content = resp.json() + hypervisor_statistics = content['hypervisor_statistics'] + + #get storage limit for this tenant + service['service_type'] = 'volumev2' + req_resouce = "/limits" + resp = sess.get(req_resouce, endpoint_filter=service) + content = resp.json() + storage_limits = content['limits']['absolute'] + + # compute actual available resource for this tenant + remainVCPU = compute_limits['maxTotalCores'] - compute_limits['totalCoresUsed'] + + if (compute_limits['maxTotalCores'] > hypervisor_statistics['vcpus']): + if hypervisor_statistics['vcpus'] > compute_limits['totalCoresUsed']: + remainVCPU = hypervisor_statistics['vcpus'] - compute_limits['totalCoresUsed'] + else: + remainVCPU = 0 + + remainMEM = compute_limits['maxTotalRAMSize'] - compute_limits['totalRAMUsed'] + if hypervisor_statistics['free_ram_mb'] > remainMEM: + remainMEM = hypervisor_statistics['free_ram_mb'] + + remainStorage = storage_limits['maxTotalVolumeGigabytes'] - storage_limits['totalGigabytesUsed'] + if (remainStorage < hypervisor_statistics['free_disk_gb']): + remainStorage = hypervisor_statistics['free_disk_gb'] + + # compare resource demanded with available + if (int(resource_demand['vCPU']) >= remainVCPU): + hasEnoughResource = False + elif (int(resource_demand['Memory']) >= remainMEM): + hasEnoughResource = False + elif (int(resource_demand['Storage']) >= remainStorage): + hasEnoughResource = False + else: + hasEnoughResource = True + + return Response(data={'result': hasEnoughResource}, status=status.HTTP_200_OK) + except VimDriverNewtonException as e: + return Response(data={'result': hasEnoughResource,'error': e.content}, status=e.status_code) + except HttpError as e: + self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json())) + resp = e.response.json() + resp.update({'result': hasEnoughResource}) + return Response(data=e.response.json(), status=e.http_status) + except Exception as e: + self._logger.error(traceback.format_exc()) + return Response(data={'result': hasEnoughResource, 'error': str(e)}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + diff --git a/ocata/ocata/urls.py b/ocata/ocata/urls.py index b1d198d7..b6312cfb 100644 --- a/ocata/ocata/urls.py +++ b/ocata/ocata/urls.py @@ -16,6 +16,7 @@ from django.conf.urls import include, url from ocata.registration.views import registration from newton_base.openoapi import tenants +from ocata.resource.views import capacity urlpatterns = [ url(r'^', include('ocata.swagger.urls')), @@ -32,6 +33,9 @@ urlpatterns = [ tenants.Tenants.as_view()), url(r'^api/multicloud-ocata/v0/(?P<vimid>[0-9a-zA-Z_-]+)/' '(?P<tenantid>[0-9a-zA-Z_-]{20,})/', include('ocata.requests.urls')), + # CapacityCheck + url(r'^api/multicloud-ocata/v0/(?P<vimid>[0-9a-zA-Z_-]+)/capacity_check/?$', + capacity.CapacityCheck.as_view()), ] diff --git a/ocata/pom.xml b/ocata/pom.xml index bf90f6e1..29032167 100644 --- a/ocata/pom.xml +++ b/ocata/pom.xml @@ -49,7 +49,7 @@ <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> - <executable>${session.executionRootDirectory}/mvn-phase-script.sh</executable> + <executable>${project.basedir}/mvn-phase-script.sh</executable> <environmentVariables> <!-- make mvn properties as env for our script --> <MVN_PROJECT_GROUPID>${project.groupId}</MVN_PROJECT_GROUPID> diff --git a/share/newton_base/extensions/extensions.py b/share/newton_base/extensions/extensions.py index 6b3ecb57..bd43c6ca 100644 --- a/share/newton_base/extensions/extensions.py +++ b/share/newton_base/extensions/extensions.py @@ -30,7 +30,7 @@ from common.msapi import extsys logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Extensions(APIView): diff --git a/share/newton_base/proxy/dnsaasdelegate.py b/share/newton_base/proxy/dnsaasdelegate.py index 8a384527..66d123f1 100644 --- a/share/newton_base/proxy/dnsaasdelegate.py +++ b/share/newton_base/proxy/dnsaasdelegate.py @@ -32,7 +32,7 @@ from newton_base.util import VimDriverUtils logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class DnsaasDelegate(Services): ''' diff --git a/share/newton_base/proxy/identityV3.py b/share/newton_base/proxy/identityV3.py index 057bdf3e..a7c8c546 100644 --- a/share/newton_base/proxy/identityV3.py +++ b/share/newton_base/proxy/identityV3.py @@ -30,7 +30,7 @@ from newton_base.proxy.proxy_utils import ProxyUtils logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True v3_version_detail = { "version": { diff --git a/share/newton_base/proxy/proxy_utils.py b/share/newton_base/proxy/proxy_utils.py index bad5b60f..98ef0a20 100644 --- a/share/newton_base/proxy/proxy_utils.py +++ b/share/newton_base/proxy/proxy_utils.py @@ -23,7 +23,7 @@ from common.exceptions import VimDriverNewtonException logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True #MULTICLOUD_PREFIX = "http://%s:%s/api/multicloud-newton/v0" %(config.MSB_SERVICE_IP, config.MSB_SERVICE_PORT) class ProxyUtils(object): diff --git a/share/newton_base/proxy/services.py b/share/newton_base/proxy/services.py index fb0cf60a..85b3ec49 100644 --- a/share/newton_base/proxy/services.py +++ b/share/newton_base/proxy/services.py @@ -29,7 +29,7 @@ from newton_base.util import VimDriverUtils logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class HasValidToken(BasePermission): diff --git a/share/newton_base/util.py b/share/newton_base/util.py index 36c47620..e925d99a 100644 --- a/share/newton_base/util.py +++ b/share/newton_base/util.py @@ -90,7 +90,7 @@ class VimDriverUtils(object): if auth_state: auth.set_auth_state(auth_state) - return session.Session(auth=auth, verify=False) + return session.Session(auth=auth, verify=(vim['insecure']==False)) @staticmethod def get_auth_state(session_obj): diff --git a/windriver/.gitignore b/windriver/.gitignore index e86d02b0..17e6ddd9 100644 --- a/windriver/.gitignore +++ b/windriver/.gitignore @@ -8,4 +8,5 @@ logs/*.log .tox .coverage htmlcov/ - +coverage.xml +test-reports/ diff --git a/windriver/pom.xml b/windriver/pom.xml index 2c08bdad..0b216c59 100644 --- a/windriver/pom.xml +++ b/windriver/pom.xml @@ -49,7 +49,7 @@ <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> - <executable>${session.executionRootDirectory}/mvn-phase-script.sh</executable> + <executable>${project.basedir}/mvn-phase-script.sh</executable> <environmentVariables> <!-- make mvn properties as env for our script --> <MVN_PROJECT_GROUPID>${project.groupId}</MVN_PROJECT_GROUPID> diff --git a/windriver/requirements.txt b/windriver/requirements.txt index c3576f5d..3d769c15 100644 --- a/windriver/requirements.txt +++ b/windriver/requirements.txt @@ -20,5 +20,5 @@ mock==2.0.0 unittest_xml_reporting==1.12.0 # for onap logging -onappylog>=1.0.5 +onappylog>=1.0.6 diff --git a/windriver/titanium_cloud/extensions/views/epacaps.py b/windriver/titanium_cloud/extensions/views/epacaps.py index 77542b31..7efb71a6 100644 --- a/windriver/titanium_cloud/extensions/views/epacaps.py +++ b/windriver/titanium_cloud/extensions/views/epacaps.py @@ -23,7 +23,7 @@ from newton_base.extensions import epacaps as newton_epacaps logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class EpaCaps(newton_epacaps.EpaCaps): diff --git a/windriver/titanium_cloud/extensions/views/extensions.py b/windriver/titanium_cloud/extensions/views/extensions.py index 680e2ca6..d331b7b5 100644 --- a/windriver/titanium_cloud/extensions/views/extensions.py +++ b/windriver/titanium_cloud/extensions/views/extensions.py @@ -27,7 +27,7 @@ from newton_base.extensions import extensions as newton_extensions logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Extensions(newton_extensions.Extensions): diff --git a/windriver/titanium_cloud/extensions/views/fcaps.py b/windriver/titanium_cloud/extensions/views/fcaps.py index d5be095f..c36b2642 100644 --- a/windriver/titanium_cloud/extensions/views/fcaps.py +++ b/windriver/titanium_cloud/extensions/views/fcaps.py @@ -35,7 +35,7 @@ from common.msapi import extsys logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True #dict to store running worker threads running_threads = {} diff --git a/windriver/titanium_cloud/middleware.py b/windriver/titanium_cloud/middleware.py new file mode 100644 index 00000000..cae9e9b1 --- /dev/null +++ b/windriver/titanium_cloud/middleware.py @@ -0,0 +1,65 @@ +# Copyright (c) 2017-2018 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 uuid +from django.conf import settings +from onaplogging.mdcContext import MDC + +FORWARDED_FOR_FIELDS = ["HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED_HOST", + "HTTP_X_FORWARDED_SERVER"] + +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 propageted Id from other component. if do not fetch id, + # generate one. + ReqeustID = request.META.get("HTTP_X_TRANSACTIONID", None) + if ReqeustID is None: + ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, settings.MULTIVIM_VERSION) + MDC.put("requestID", ReqeustID) + # generate the reqeust id + InvocationID = uuid.uuid3(uuid.NAMESPACE_DNS, settings.MULTIVIM_VERSION) + MDC.put("invocationID", InvocationID) + MDC.put("serviceName", settings.MULTIVIM_VERSION) + MDC.put("serviceIP", self._getLastIp(request)) + return None + + def process_response(self, request, response): + + MDC.clear() + return response + diff --git a/windriver/titanium_cloud/proxy/views/identityV3.py b/windriver/titanium_cloud/proxy/views/identityV3.py index dd280314..c831d017 100644 --- a/windriver/titanium_cloud/proxy/views/identityV3.py +++ b/windriver/titanium_cloud/proxy/views/identityV3.py @@ -19,7 +19,7 @@ from newton_base.proxy import identityV3 as newton_identityV3 logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Tokens(newton_identityV3.Tokens): diff --git a/windriver/titanium_cloud/proxy/views/services.py b/windriver/titanium_cloud/proxy/views/services.py index 4bfecdbc..c1d4f194 100644 --- a/windriver/titanium_cloud/proxy/views/services.py +++ b/windriver/titanium_cloud/proxy/views/services.py @@ -21,7 +21,7 @@ from newton_base.proxy import services as newton_services logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Services(newton_services.Services): diff --git a/windriver/titanium_cloud/pub/config/log.yml b/windriver/titanium_cloud/pub/config/log.yml index 7dab297b..95e1945a 100644 --- a/windriver/titanium_cloud/pub/config/log.yml +++ b/windriver/titanium_cloud/pub/config/log.yml @@ -20,14 +20,14 @@ handlers: class: "logging.handlers.RotatingFileHandler" filename: "/var/log/onap/multicloud/openstack/windriver/titanium_cloud.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/windriver/titanium_cloud/registration/views/registration.py b/windriver/titanium_cloud/registration/views/registration.py index da83efaa..a7b2831f 100644 --- a/windriver/titanium_cloud/registration/views/registration.py +++ b/windriver/titanium_cloud/registration/views/registration.py @@ -20,10 +20,11 @@ from newton_base.registration import registration as newton_registration logger = logging.getLogger(__name__) -DEBUG=True +#DEBUG=True class Registry(newton_registration.Registry): def __init__(self): self.proxy_prefix = settings.MULTICLOUD_PREFIX + self.aai_base_url = settings.AAI_BASE_URL self._logger = logger diff --git a/windriver/titanium_cloud/resource/tests/__init__.py b/windriver/titanium_cloud/resource/tests/__init__.py new file mode 100644 index 00000000..afa702d3 --- /dev/null +++ b/windriver/titanium_cloud/resource/tests/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017-2018 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/windriver/titanium_cloud/resource/tests/test_capacity.py b/windriver/titanium_cloud/resource/tests/test_capacity.py new file mode 100644 index 00000000..3dae1080 --- /dev/null +++ b/windriver/titanium_cloud/resource/tests/test_capacity.py @@ -0,0 +1,121 @@ +# Copyright (c) 2017-2018 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 mock + +from rest_framework import status + +from common.utils import restcall +from newton_base.tests import mock_info +from newton_base.tests import test_base +from newton_base.util import VimDriverUtils + +MOCK_GET_TENANT_LIMIT_RESPONSE = { + "limits" : { + "rate" : [], + "absolute" : { + "maxTotalRAMSize" : 128*1024, + "totalRAMUsed" : 8*1024, + "totalCoresUsed" : 4, + "maxTotalCores" : 20, + } + } +} + +MOCK_GET_HYPER_STATATICS_RESPONSE = { + "hypervisor_statistics" : { + "vcpus_used" : 4, + "free_ram_mb" : 120*1024, + "vcpus" : 10, + "free_disk_gb" : 300 + } +} + +MOCK_GET_STORAGE_RESPONSE = { + "limits" : { + "rate" : [], + "absolute" : { + "totalGigabytesUsed" : 200, + "maxTotalVolumeGigabytes" : 500, + } + } +} + +TEST_REQ_SUCCESS_SOURCE = { + "vCPU": "4", + "Memory": "4096", + "Storage": "200" +} + + +TEST_REQ_FAILED_SOURCE = { + "vCPU": "17", + "Memory": "4096", + "Storage": "200" +} + +class TestCapacity(test_base.TestRequest): + def setUp(self): + super(TestCapacity, self).setUp() + + def _get_mock_response(self, return_value=None): + mock_response = mock.Mock(spec=test_base.MockResponse) + mock_response.status_code = status.HTTP_200_OK + mock_response.json.return_value = return_value + return mock_response + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_capacity_check_success(self, mock_get_vim_info, mock_get_session): + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + mock_get_session.return_value = test_base.get_mock_session( + ["get"], { + "side_effect": [ + self._get_mock_response(MOCK_GET_TENANT_LIMIT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPER_STATATICS_RESPONSE), + self._get_mock_response(MOCK_GET_STORAGE_RESPONSE), + ] + }) + + response = self.client.post(( + "/api/%s/v0/windriver-hudson-dc_RegionOne/" + "capacity_check" % test_base.MULTIVIM_VERSION), + TEST_REQ_SUCCESS_SOURCE, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEqual({"result": True}, response.data) + + @mock.patch.object(VimDriverUtils, 'get_session') + @mock.patch.object(VimDriverUtils, 'get_vim_info') + def test_capacity_check_nova_limits_failed(self, mock_get_vim_info, mock_get_session): + mock_get_vim_info.return_value = mock_info.MOCK_VIM_INFO + mock_get_session.return_value = test_base.get_mock_session( + ["get"], { + "side_effect": [ + self._get_mock_response(MOCK_GET_TENANT_LIMIT_RESPONSE), + self._get_mock_response(MOCK_GET_HYPER_STATATICS_RESPONSE), + self._get_mock_response(MOCK_GET_STORAGE_RESPONSE), + ] + }) + + response = self.client.post(( + "/api/%s/v0/windriver-hudson-dc_RegionOne/" + "capacity_check" % test_base.MULTIVIM_VERSION), + TEST_REQ_FAILED_SOURCE, + HTTP_X_AUTH_TOKEN=mock_info.MOCK_TOKEN_ID) + + self.assertEquals(status.HTTP_200_OK, response.status_code) + self.assertEqual({"result": False}, response.data) + diff --git a/windriver/titanium_cloud/resource/views/capacity.py b/windriver/titanium_cloud/resource/views/capacity.py index 6d20075c..d73cc0fb 100644 --- a/windriver/titanium_cloud/resource/views/capacity.py +++ b/windriver/titanium_cloud/resource/views/capacity.py @@ -43,7 +43,7 @@ class CapacityCheck(APIView): hasEnoughResource = False try : - resource_demand = json.load(request.data) + resource_demand = request.data #get token: cloud_owner, regionid = extsys.decode_vim_id(vimid) @@ -93,11 +93,11 @@ class CapacityCheck(APIView): remainStorage = hypervisor_statistics['free_disk_gb'] # compare resource demanded with available - if (resource_demand['vCPU'] >= remainVCPU): + if (int(resource_demand['vCPU']) >= remainVCPU): hasEnoughResource = False - elif (resource_demand['Memory'] >= remainMEM): + elif (int(resource_demand['Memory']) >= remainMEM): hasEnoughResource = False - elif (resource_demand['Storage'] >= remainStorage): + elif (int(resource_demand['Storage']) >= remainStorage): hasEnoughResource = False else: hasEnoughResource = True diff --git a/windriver/titanium_cloud/settings.py b/windriver/titanium_cloud/settings.py index 88bba77d..f05991a9 100644 --- a/windriver/titanium_cloud/settings.py +++ b/windriver/titanium_cloud/settings.py @@ -32,9 +32,9 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = '3o-wney!99y)^h3v)0$j16l9=fdjxcb+a8g+q3tfbahcnu2b0o' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +#DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['*'] # Application definition @@ -56,6 +56,7 @@ MIDDLEWARE_CLASSES = [ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'titanium_cloud.middleware.LogContextMiddleware', ] ROOT_URLCONF = 'titanium_cloud.urls' |