1 # Copyright (C) 2021 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from o2common.service import unit_of_work
18 from o2ims.views.ocloud_dto import SubscriptionDTO
19 from o2ims.domain.subscription_obj import Subscription
21 from o2common.helper import o2logging
22 logger = o2logging.get_logger(__name__)
25 def oclouds(uow: unit_of_work.AbstractUnitOfWork):
27 li = uow.oclouds.list()
28 return [r.serialize() for r in li]
31 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):
33 first = uow.oclouds.get(ocloudid)
34 return first.serialize() if first is not None else None
37 def resource_types(uow: unit_of_work.AbstractUnitOfWork):
39 li = uow.resource_types.list()
40 return [r.serialize() for r in li]
43 def resource_type_one(resourceTypeId: str,
44 uow: unit_of_work.AbstractUnitOfWork):
46 first = uow.resource_types.get(resourceTypeId)
47 return first.serialize() if first is not None else None
50 def resource_pools(uow: unit_of_work.AbstractUnitOfWork):
52 li = uow.resource_pools.list()
53 return [r.serialize() for r in li]
56 def resource_pool_one(resourcePoolId: str,
57 uow: unit_of_work.AbstractUnitOfWork):
59 first = uow.resource_pools.get(resourcePoolId)
60 return first.serialize() if first is not None else None
63 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
66 filter_kwargs = {} # filter key should be the same with database name
67 if 'resourceTypeName' in kwargs:
68 resource_type_name = kwargs['resourceTypeName']
70 # res_types = uow.resource_types.list()
72 # restype.resourceTypeId for restype in res_types
73 # if resourceTypeName == restype.name]
74 # restype_id = '' if len(restype_ids) == 0 else restype_ids[0]
75 res_type = uow.resource_types.get_by_name(resource_type_name)
76 restype_id = '' if res_type is None else res_type.resourceTypeId
77 filter_kwargs['resourceTypeId'] = restype_id
79 # li = uow.resources.list(resourcePoolId)
80 # return [r.serialize() for r in li if r.resourceTypeId == restype_id]
81 if 'parentId' in kwargs:
82 filter_kwargs['parentId'] = kwargs['parentId']
85 li = uow.resources.list(resourcePoolId, **filter_kwargs)
86 return [r.serialize() for r in li]
89 def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):
91 first = uow.resources.get(resourceId)
92 return first.serialize() if first is not None else None
95 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):
97 li = uow.deployment_managers.list()
98 return [r.serialize() for r in li]
101 def deployment_manager_one(deploymentManagerId: str,
102 uow: unit_of_work.AbstractUnitOfWork):
104 first = uow.deployment_managers.get(deploymentManagerId)
105 return first.serialize() if first is not None else None
108 def subscriptions(uow: unit_of_work.AbstractUnitOfWork):
110 li = uow.subscriptions.list()
111 return [r.serialize() for r in li]
114 def subscription_one(subscriptionId: str,
115 uow: unit_of_work.AbstractUnitOfWork):
117 first = uow.subscriptions.get(subscriptionId)
118 return first.serialize() if first is not None else None
121 def subscription_create(subscriptionDto: SubscriptionDTO.subscription,
122 uow: unit_of_work.AbstractUnitOfWork):
124 sub_uuid = str(uuid.uuid4())
125 subscription = Subscription(
126 sub_uuid, subscriptionDto['callback'],
127 subscriptionDto['consumerSubscriptionId'],
128 subscriptionDto['filter'])
130 uow.subscriptions.add(subscription)
132 return {"subscriptionId": sub_uuid}
135 def subscription_delete(subscriptionId: str,
136 uow: unit_of_work.AbstractUnitOfWork):
138 uow.subscriptions.delete(subscriptionId)