From d689cdb0854c953bb377f33699ba4f786c08519e Mon Sep 17 00:00:00 2001 From: Huang Haibin Date: Tue, 27 Mar 2018 23:00:15 +0800 Subject: [PATCH] Search hpa flavor from aai Change-Id: Iedea7fe5022a2a1cde67925ca627b7c5ffe31b82 Issue-ID: VFC-853 Signed-off-by: Huang Haibin --- lcm/lcm/pub/config/config.py | 5 ++++ lcm/lcm/pub/msapi/aai.py | 55 +++++++++++++++++++++++++++++++++++++++++ lcm/lcm/pub/vimapi/adaptor.py | 57 +++++++++++++++++++++++++++++-------------- 3 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 lcm/lcm/pub/msapi/aai.py diff --git a/lcm/lcm/pub/config/config.py b/lcm/lcm/pub/config/config.py index 6d25fc13..63c1784d 100644 --- a/lcm/lcm/pub/config/config.py +++ b/lcm/lcm/pub/config/config.py @@ -33,6 +33,11 @@ SERVICE_NAME = "vnflcm" FORWARDED_FOR_FIELDS = ["HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED_HOST", "HTTP_X_FORWARDED_SERVER"] +# [aai config] +AAI_BASE_URL = "http://127.0.0.1:80/aai/v13" +AAI_USER = "AAI" +AAI_PASSWD = "AAI" + # [register] REG_TO_MSB_WHEN_START = True REG_TO_MSB_REG_URL = "/api/microservices/v1/services" diff --git a/lcm/lcm/pub/msapi/aai.py b/lcm/lcm/pub/msapi/aai.py new file mode 100644 index 00000000..3dffa58b --- /dev/null +++ b/lcm/lcm/pub/msapi/aai.py @@ -0,0 +1,55 @@ +# Copyright 2016 ZTE 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. + +import json +import logging +import uuid + +from lcm.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWD +from lcm.pub.utils import restcall +logger = logging.getLogger(__name__) + + +def call_aai(resource, method, content=''): + additional_headers = { + 'X-FromAppId': 'VFC-CATALOG', + 'X-TransactionId': str(uuid.uuid1()) + } + return restcall.call_req(AAI_BASE_URL, + AAI_USER, + AAI_PASSWD, + restcall.rest_no_auth, + resource, + method, + content, + additional_headers) + + +def split_vim_to_owner_region(vim_id): + split_vim = vim_id.split('_') + cloud_owner = split_vim[0] + cloud_region = "".join(split_vim[1:]) + return cloud_owner, cloud_region + + +def get_flavor_info(vim_id): + cloud_owner, cloud_region = split_vim_to_owner_region(vim_id) + resource = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/flavors?depth=all" % \ + (cloud_owner, cloud_region) + + ret = call_aai(resource, "GET") + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + + return json.JSONDecoder().decode(ret[1]) if ret[1] else ret[1] diff --git a/lcm/lcm/pub/vimapi/adaptor.py b/lcm/lcm/pub/vimapi/adaptor.py index e0d8cb7c..d07a3569 100644 --- a/lcm/lcm/pub/vimapi/adaptor.py +++ b/lcm/lcm/pub/vimapi/adaptor.py @@ -16,6 +16,7 @@ import logging import time from lcm.pub.utils.values import ignore_case_get, set_opt_val +from lcm.pub.msapi.aai import get_flavor_info from . import api from .exceptions import VimException @@ -207,26 +208,46 @@ def create_flavor(vim_cache, res_cache, data, flavor, do_notify, res_type): "memory": int(flavor["nfv_compute"]["mem_size"].replace('GB', '').strip()), "isPublic": True } - for local_storage_id in ignore_case_get(flavor, "local_storages"): - for local_storage in local_storages: - if local_storage_id != local_storage["local_storage_id"]: - continue - disk_type = local_storage["properties"]["disk_type"] - disk_size = int(local_storage["properties"]["size"].replace('GB', '').strip()) - if disk_type == "root": - param["disk"] = disk_size - elif disk_type == "ephemeral": - param["ephemeral"] = disk_size - elif disk_type == "swap": - param["swap"] = disk_size flavor_extra_specs = ignore_case_get(flavor["nfv_compute"], "flavor_extra_specs") - extra_specs = [] - for es in flavor_extra_specs: - extra_specs.append({"keyName": es, "value": flavor_extra_specs[es]}) - set_opt_val(param, "extraSpecs", extra_specs) vim_id, tenant_name = location_info["vimid"], location_info["tenant"] - tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name) - ret = api.create_flavor(vim_id, tenant_id, param) + + # search aai flavor + find = False + aai_flavors = get_flavor_info(vim_id) + aai_flavor = aai_flavors["flavors"]["flavor"] + for i in range(len(aai_flavor)): + for fes in flavor_extra_specs: + if (str(aai_flavor[i].find(fes)) != -1): + find = True + break + if find: + break + + # add aai flavor + if find: + ret = aai_flavor[i] + else: + extra_specs = [] + for local_storage_id in ignore_case_get(flavor, "local_storages"): + for local_storage in local_storages: + if local_storage_id != local_storage["local_storage_id"]: + continue + disk_type = local_storage["properties"]["disk_type"] + disk_size = int(local_storage["properties"]["size"].replace('GB', '').strip()) + if disk_type == "root": + param["disk"] = disk_size + elif disk_type == "ephemeral": + param["ephemeral"] = disk_size + elif disk_type == "swap": + param["swap"] = disk_size + + for es in flavor_extra_specs: + extra_specs.append({"keyName": es, "value": flavor_extra_specs[es]}) + + set_opt_val(param, "extraSpecs", extra_specs) + tenant_id = get_tenant_id(vim_cache, vim_id, tenant_name) + ret = api.create_flavor(vim_id, tenant_id, param) + do_notify(res_type, ret) set_res_cache(res_cache, res_type, flavor["vdu_id"], ret["id"]) -- 2.16.6