Convert file endlines to Unix (LF)
[pti/o2.git] / o2ims / views / ocloud_view.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import uuid
16
17 from o2common.service import unit_of_work
18 from o2ims.views.ocloud_dto import SubscriptionDTO
19 from o2ims.domain.subscription_obj import Subscription
20
21 from o2common.helper import o2logging
22 logger = o2logging.get_logger(__name__)
23
24
25 def oclouds(uow: unit_of_work.AbstractUnitOfWork):
26     with uow:
27         li = uow.oclouds.list()
28     return [r.serialize() for r in li]
29
30
31 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):
32     with uow:
33         first = uow.oclouds.get(ocloudid)
34         return first.serialize() if first is not None else None
35
36
37 def resource_types(uow: unit_of_work.AbstractUnitOfWork):
38     with uow:
39         li = uow.resource_types.list()
40     return [r.serialize() for r in li]
41
42
43 def resource_type_one(resourceTypeId: str,
44                       uow: unit_of_work.AbstractUnitOfWork):
45     with uow:
46         first = uow.resource_types.get(resourceTypeId)
47         return first.serialize() if first is not None else None
48
49
50 def resource_pools(uow: unit_of_work.AbstractUnitOfWork):
51     with uow:
52         li = uow.resource_pools.list()
53     return [r.serialize() for r in li]
54
55
56 def resource_pool_one(resourcePoolId: str,
57                       uow: unit_of_work.AbstractUnitOfWork):
58     with uow:
59         first = uow.resource_pools.get(resourcePoolId)
60         return first.serialize() if first is not None else None
61
62
63 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
64               **kwargs):
65
66     filter_kwargs = {}  # filter key should be the same with database name
67     if 'resourceTypeName' in kwargs:
68         resource_type_name = kwargs['resourceTypeName']
69         with uow:
70             # res_types = uow.resource_types.list()
71             # restype_ids = [
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
78
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']
83
84     with uow:
85         li = uow.resources.list(resourcePoolId, **filter_kwargs)
86     return [r.serialize() for r in li]
87
88
89 def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):
90     with uow:
91         first = uow.resources.get(resourceId)
92         return first.serialize() if first is not None else None
93
94
95 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):
96     with uow:
97         li = uow.deployment_managers.list()
98     return [r.serialize() for r in li]
99
100
101 def deployment_manager_one(deploymentManagerId: str,
102                            uow: unit_of_work.AbstractUnitOfWork):
103     with uow:
104         first = uow.deployment_managers.get(deploymentManagerId)
105         return first.serialize() if first is not None else None
106
107
108 def subscriptions(uow: unit_of_work.AbstractUnitOfWork):
109     with uow:
110         li = uow.subscriptions.list()
111     return [r.serialize() for r in li]
112
113
114 def subscription_one(subscriptionId: str,
115                      uow: unit_of_work.AbstractUnitOfWork):
116     with uow:
117         first = uow.subscriptions.get(subscriptionId)
118         return first.serialize() if first is not None else None
119
120
121 def subscription_create(subscriptionDto: SubscriptionDTO.subscription,
122                         uow: unit_of_work.AbstractUnitOfWork):
123
124     sub_uuid = str(uuid.uuid4())
125     subscription = Subscription(
126         sub_uuid, subscriptionDto['callback'],
127         subscriptionDto['consumerSubscriptionId'],
128         subscriptionDto['filter'])
129     with uow:
130         uow.subscriptions.add(subscription)
131         uow.commit()
132     return {"subscriptionId": sub_uuid}
133
134
135 def subscription_delete(subscriptionId: str,
136                         uow: unit_of_work.AbstractUnitOfWork):
137     with uow:
138         uow.subscriptions.delete(subscriptionId)
139         uow.commit()
140     return True