X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fviews%2Focloud_view.py;h=15a6e1def57d262f077828e6467cbd7bb656a9d0;hb=76a5a4a59a09dab75b7584c787c14d89f7a0a180;hp=c685898f8ac91a0ef06004924ebe45bd7eb99c42;hpb=4dd941a359733bffe3fe6f55cdb1880eea9a30dc;p=pti%2Fo2.git diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index c685898..15a6e1d 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -23,7 +23,8 @@ from o2common.service import unit_of_work from o2common.config import config from o2common.views.view import gen_filter, check_filter from o2common.views.pagination_view import Pagination -from o2common.views.route_exception import BadRequestException +from o2common.views.route_exception import BadRequestException, \ + NotFoundException from o2ims.domain import ocloud from o2ims.views.ocloud_dto import SubscriptionDTO @@ -76,11 +77,16 @@ def resource_pool_one(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork): with uow: first = uow.resource_pools.get(resourcePoolId) - return first.serialize() if first is not None else None + return first.serialize() if first else None def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork, **kwargs): + with uow: + first = uow.resource_pools.get(resourcePoolId) + if first is None: + raise NotFoundException("ResourcePool {} doesn't exist".format( + resourcePoolId)) pagination = Pagination(**kwargs) # filter key should be the same with database name query_kwargs = pagination.get_pagination() @@ -97,8 +103,6 @@ def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork, query_kwargs['resourceTypeId'] = restype_id args = gen_filter( ocloud.Resource, kwargs['filter']) if 'filter' in kwargs else [] - args.append(ocloud.Resource.resourcePoolId == resourcePoolId) - # args.append(ocloud.Resource.parentId == None) if 'parentId' in kwargs: query_kwargs['parentId'] = kwargs['parentId'] @@ -112,10 +116,19 @@ def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork, return pagination.get_result(ret) -def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork): +def resource_one(resourceId: str, + uow: unit_of_work.AbstractUnitOfWork, resourcePoolId: str): with uow: - first = uow.resources.get(resourceId) - return first.serialize() if first is not None else None + resoucePool = uow.resource_pools.get(resourcePoolId) + if resoucePool is None: + raise NotFoundException("ResourcePool {} doesn't exist".format( + resourcePoolId)) + + first = uow.resources.get(resourceId) + if first is None: + raise NotFoundException("Resource {} doesn't exist".format( + resourceId)) + return first.serialize() def deployment_managers(uow: unit_of_work.AbstractUnitOfWork, **kwargs): @@ -173,6 +186,7 @@ def deployment_manager_one(deploymentManagerId: str, def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict: + shared_folder = config.get_containers_shared_folder() data = config.gen_k8s_config_dict( kubeconfig.pop('cluster_api_endpoint', None), @@ -191,6 +205,7 @@ def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict: current_time = datetime.now().strftime("%Y%m%d%H%M%S") tmp_file_name = 'kubeconfig_' + name_key + "_" + current_time kube_config_name = 'kubeconfig_' + name_key + '.config' + kube_config_path = f'{shared_folder}/{kube_config_name}' # write down the yaml file of kubectl into tmp folder with open('/tmp/' + tmp_file_name, 'w') as file: @@ -198,12 +213,12 @@ def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict: # generate the kube config file if not exist or update the file if it # changes - if not os.path.exists('/configs/' + kube_config_name) or not \ - filecmp.cmp('/tmp/'+tmp_file_name, '/configs/'+kube_config_name): + if not os.path.exists(kube_config_path) or not \ + filecmp.cmp('/tmp/'+tmp_file_name, kube_config_path): shutil.move(os.path.join('/tmp', tmp_file_name), - os.path.join('/configs', kube_config_name)) + os.path.join(shared_folder, kube_config_name)) - return '/configs/'+kube_config_name + return kube_config_path def subscriptions(uow: unit_of_work.AbstractUnitOfWork, **kwargs): @@ -228,7 +243,7 @@ def subscription_create(subscriptionDto: SubscriptionDTO.subscription_create, filter = subscriptionDto.get('filter', '') consumer_subs_id = subscriptionDto.get('consumerSubscriptionId', '') - check_filter(ocloud.Resource, filter) + _check_subscription_filter(filter) sub_uuid = str(uuid.uuid4()) subscription = Subscription( @@ -250,9 +265,69 @@ def subscription_create(subscriptionDto: SubscriptionDTO.subscription_create, return first.serialize() +def _check_subscription_filter(filter: str): + if not filter: + return + + def _sub_filter_checking(sub_filter: str): + exprs = sub_filter.split(';') + objectType = False + objectTypeValue = '' + for expr in exprs: + expr_strip = expr.strip(' ()') + items = expr_strip.split(',') + if len(items) < 3: + raise BadRequestException("invalid filter {}".format(expr)) + item_key = items[1].strip() + if item_key != 'objectType': + continue + item_op = items[0].strip() + if item_op != 'eq': + raise BadRequestException( + "Filter objectType only support 'eq' operation") + objectType = True + objectTypeValue = items[2].strip() + if not objectType: + # if there has no objectType specific, by default is ResourceInfo + check_filter(ocloud.Resource, sub_filter) + # return 'ResourceInfo' + return + if objectTypeValue == 'ResourceTypeInfo': + check_filter(ocloud.ResourceType, sub_filter) + elif objectTypeValue == 'ResourcePoolInfo': + check_filter(ocloud.ResourcePool, sub_filter) + elif objectTypeValue == 'DeploymentManagerInfo': + check_filter(ocloud.DeploymentManager, sub_filter) + elif objectTypeValue == 'CloudInfo': + check_filter(ocloud.Ocloud, sub_filter) + elif objectTypeValue == 'ResourceInfo': + check_filter(ocloud.Resource, sub_filter) + else: + raise BadRequestException( + "Filter ObjectType {} not support.".format(items[2])) + # return objectTypeValue + filter_strip = filter.strip(' []') + filter_list = filter_strip.split('|') + # check_duplication = dict() + for sub_filter in filter_list: + _sub_filter_checking(sub_filter) + # obj_type = _sub_filter_checking(sub_filter) + # if obj_type not in check_duplication: + # check_duplication[obj_type] = 0 + # check_duplication[obj_type] += 1 + # if check_duplication[obj_type] > 1: + # raise BadRequestException( + # "Filter objectType {} only support one in each." + # .format(obj_type)) + + def subscription_delete(subscriptionId: str, uow: unit_of_work.AbstractUnitOfWork): with uow: + first = uow.subscriptions.get(subscriptionId) + if not first: + raise NotFoundException( + "Subscription {} not found.".format(subscriptionId)) uow.subscriptions.delete(subscriptionId) uow.commit() return True