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