Add the command that registers to the SMO; Make the create registration and create...
[pti/o2.git] / o2ims / views / ocloud_view.py
1 # Copyright (C) 2021 Wind River Systems, Inc.\r
2 #\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
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\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
14 \r
15 import logging\r
16 import uuid\r
17 from datetime import datetime\r
18 \r
19 from o2common.service import unit_of_work, messagebus\r
20 from o2ims.domain import events\r
21 from o2ims.views.ocloud_dto import RegistrationDTO, SubscriptionDTO\r
22 from o2ims.domain.subscription_obj import Registration, Subscription\r
23 \r
24 \r
25 def oclouds(uow: unit_of_work.AbstractUnitOfWork):\r
26     with uow:\r
27         li = uow.oclouds.list()\r
28     return [r.serialize() for r in li]\r
29 \r
30 \r
31 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):\r
32     with uow:\r
33         first = uow.oclouds.get(ocloudid)\r
34         return first.serialize() if first is not None else None\r
35 \r
36 \r
37 def resource_types(uow: unit_of_work.AbstractUnitOfWork):\r
38     with uow:\r
39         li = uow.resource_types.list()\r
40     return [r.serialize() for r in li]\r
41 \r
42 \r
43 def resource_type_one(resourceTypeId: str,\r
44                       uow: unit_of_work.AbstractUnitOfWork):\r
45     with uow:\r
46         first = uow.resource_types.get(resourceTypeId)\r
47         return first.serialize() if first is not None else None\r
48 \r
49 \r
50 def resource_pools(uow: unit_of_work.AbstractUnitOfWork):\r
51     with uow:\r
52         li = uow.resource_pools.list()\r
53     return [r.serialize() for r in li]\r
54 \r
55 \r
56 def resource_pool_one(resourcePoolId: str,\r
57                       uow: unit_of_work.AbstractUnitOfWork):\r
58     with uow:\r
59         first = uow.resource_pools.get(resourcePoolId)\r
60         return first.serialize() if first is not None else None\r
61 \r
62 \r
63 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork):\r
64     with uow:\r
65         li = uow.resources.list(resourcePoolId)\r
66     return [r.serialize() for r in li]\r
67 \r
68 \r
69 def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):\r
70     with uow:\r
71         first = uow.resources.get(resourceId)\r
72         return first.serialize() if first is not None else None\r
73 \r
74 \r
75 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):\r
76     with uow:\r
77         li = uow.deployment_managers.list()\r
78     return [r.serialize() for r in li]\r
79 \r
80 \r
81 def deployment_manager_one(deploymentManagerId: str,\r
82                            uow: unit_of_work.AbstractUnitOfWork):\r
83     with uow:\r
84         first = uow.deployment_managers.get(deploymentManagerId)\r
85         return first.serialize() if first is not None else None\r
86 \r
87 \r
88 def subscriptions(uow: unit_of_work.AbstractUnitOfWork):\r
89     with uow:\r
90         li = uow.subscriptions.list()\r
91     return [r.serialize() for r in li]\r
92 \r
93 \r
94 def subscription_one(subscriptionId: str,\r
95                      uow: unit_of_work.AbstractUnitOfWork):\r
96     with uow:\r
97         first = uow.subscriptions.get(subscriptionId)\r
98         return first.serialize() if first is not None else None\r
99 \r
100 \r
101 def subscription_create(subscriptionDto: SubscriptionDTO.subscription,\r
102                         uow: unit_of_work.AbstractUnitOfWork):\r
103 \r
104     sub_uuid = str(uuid.uuid4())\r
105     subscription = Subscription(\r
106         sub_uuid, subscriptionDto['callback'],\r
107         subscriptionDto['consumerSubscriptionId'],\r
108         subscriptionDto['filter'])\r
109     with uow:\r
110         uow.subscriptions.add(subscription)\r
111         uow.commit()\r
112     return {"subscriptionId": sub_uuid}\r
113 \r
114 \r
115 def subscription_delete(subscriptionId: str,\r
116                         uow: unit_of_work.AbstractUnitOfWork):\r
117     with uow:\r
118         uow.subscriptions.delete(subscriptionId)\r
119         uow.commit()\r
120     return True\r
121 \r
122 \r
123 def registrations(uow: unit_of_work.AbstractUnitOfWork):\r
124     with uow:\r
125         li = uow.registrations.list()\r
126     return [r.serialize() for r in li]\r
127 \r
128 \r
129 def registration_one(registrationId: str,\r
130                      uow: unit_of_work.AbstractUnitOfWork):\r
131     with uow:\r
132         first = uow.registrations.get(registrationId)\r
133         return first.serialize() if first is not None else None\r
134 \r
135 \r
136 def registration_create(registrationDto: RegistrationDTO.registration,\r
137                         bus: messagebus.MessageBus):\r
138 \r
139     reg_uuid = str(uuid.uuid4())\r
140     registration = Registration(\r
141         reg_uuid, registrationDto['callback'])\r
142     with bus.uow as uow:\r
143         uow.registrations.add(registration)\r
144         logging.debug('before event length {}'.format(\r
145             len(registration.events)))\r
146         registration.events.append(events.RegistrationChanged(\r
147             reg_uuid,\r
148             datetime.now()))\r
149         logging.debug('after event length {}'.format(len(registration.events)))\r
150         uow.commit()\r
151     _handle_events(bus)\r
152     return {"registrationId": reg_uuid}\r
153 \r
154 \r
155 def registration_delete(registrationId: str,\r
156                         uow: unit_of_work.AbstractUnitOfWork):\r
157     with uow:\r
158         uow.registrations.delete(registrationId)\r
159         uow.commit()\r
160     return True\r
161 \r
162 \r
163 def _handle_events(bus: messagebus.MessageBus):\r
164     # handle events\r
165     events = bus.uow.collect_new_events()\r
166     for event in events:\r
167         bus.handle(event)\r
168     return True\r