9404d9a59001c92bb28360f61949c369d912df77
[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 uuid\r
16 from sqlalchemy import select\r
17 \r
18 from o2ims.adapter.orm import ocloud, resource, resourcetype, \\r
19     resourcepool, deploymentmanager, subscription\r
20 from o2common.service import unit_of_work\r
21 from o2ims.views.ocloud_dto import SubscriptionDTO\r
22 from o2ims.domain.ocloud import Subscription\r
23 \r
24 \r
25 def oclouds(uow: unit_of_work.AbstractUnitOfWork):\r
26     with uow:\r
27         res = uow.session.execute(select(ocloud))\r
28     return [dict(r) for r in res]\r
29 \r
30 \r
31 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):\r
32     with uow:\r
33         res = uow.session.execute(\r
34             select(ocloud).where(ocloud.c.oCloudId == ocloudid))\r
35         first = res.first()\r
36     return None if first is None else dict(first)\r
37 \r
38 \r
39 def resource_types(uow: unit_of_work.AbstractUnitOfWork):\r
40     with uow:\r
41         res = uow.session.execute(select(resourcetype))\r
42     return [dict(r) for r in res]\r
43 \r
44 \r
45 def resource_type_one(resourceTypeId: str,\r
46                       uow: unit_of_work.AbstractUnitOfWork):\r
47     with uow:\r
48         res = uow.session.execute(select(resourcetype).where(\r
49             resourcetype.c.resourceTypeId == resourceTypeId))\r
50         first = res.first()\r
51     return None if first is None else dict(first)\r
52 \r
53 \r
54 def resource_pools(uow: unit_of_work.AbstractUnitOfWork):\r
55     with uow:\r
56         res = uow.session.execute(select(resourcepool))\r
57     return [dict(r) for r in res]\r
58 \r
59 \r
60 def resource_pool_one(resourcePoolId: str,\r
61                       uow: unit_of_work.AbstractUnitOfWork):\r
62     with uow:\r
63         res = uow.session.execute(select(resourcepool).where(\r
64             resourcepool.c.resourcePoolId == resourcePoolId))\r
65         first = res.first()\r
66     return None if first is None else dict(first)\r
67 \r
68 \r
69 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork):\r
70     with uow:\r
71         res = uow.session.execute(select(resource).where(\r
72             resource.c.resourcePoolId == resourcePoolId))\r
73     return [dict(r) for r in res]\r
74 \r
75 \r
76 def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):\r
77     with uow:\r
78         # topq = uow.session.query(resource).filter(\r
79         #     resource.c.resourceId == resourceId).cte('cte', recursive=True)\r
80         # bootomq = uow.session.query(resource).join(\r
81         #     topq, resource.c.parentId == topq.c.resourceId)\r
82         # res = uow.session.query(topq.union(bootomq))\r
83         # print(res)\r
84         res = uow.session.execute(select(resource).where(\r
85             resource.c.resourceId == resourceId))\r
86         first = res.first()\r
87     return None if first is None else dict(first)\r
88 \r
89 \r
90 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):\r
91     with uow:\r
92         res = uow.session.execute(select(deploymentmanager))\r
93     return [dict(r) for r in res]\r
94 \r
95 \r
96 def deployment_manager_one(deploymentManagerId: str,\r
97                            uow: unit_of_work.AbstractUnitOfWork):\r
98     with uow:\r
99         res = uow.session.execute(select(deploymentmanager).where(\r
100             deploymentmanager.c.deploymentManagerId == deploymentManagerId))\r
101         first = res.first()\r
102     return None if first is None else dict(first)\r
103 \r
104 \r
105 def subscriptions(uow: unit_of_work.AbstractUnitOfWork):\r
106     with uow:\r
107         res = uow.session.execute(select(subscription))\r
108     return [dict(r) for r in res]\r
109 \r
110 \r
111 def subscription_one(subscriptionId: str,\r
112                      uow: unit_of_work.AbstractUnitOfWork):\r
113     with uow:\r
114         res = uow.session.execute(select(subscription).where(\r
115             subscription.c.subscriptionId == subscriptionId))\r
116         first = res.first()\r
117     return None if first is None else dict(first)\r
118 \r
119 \r
120 def subscription_create(subscriptionDto: SubscriptionDTO.subscription,\r
121                         uow: unit_of_work.AbstractUnitOfWork):\r
122 \r
123     sub_uuid = str(uuid.uuid4())\r
124     subscription = Subscription(\r
125         sub_uuid, subscriptionDto['callback'],\r
126         subscriptionDto['consumerSubscriptionId'],\r
127         subscriptionDto['filter'])\r
128     with uow:\r
129         uow.subscriptions.add(subscription)\r
130         uow.commit()\r
131     return {"subscriptionId": sub_uuid}\r
132 \r
133 \r
134 def subscription_delete(subscriptionId: str,\r
135                         uow: unit_of_work.AbstractUnitOfWork):\r
136     with uow:\r
137         uow.subscriptions.delete(subscriptionId)\r
138         uow.commit()\r
139     return True\r