Add nfdeployment handlers
[pti/o2.git] / o2dms / api / dms_lcm_nfdeploymentdesc.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 from sqlalchemy import select\r
16 import uuid\r
17 from o2common.service import unit_of_work\r
18 from o2dms.adapter.orm import nfDeploymentDesc\r
19 from o2dms.api.dms_dto import DmsLcmNfDeploymentDescriptorDTO\r
20 from o2dms.domain.dms import NfDeploymentDesc\r
21 from o2common.helper import o2logging\r
22 logger = o2logging.get_logger(__name__)\r
23 \r
24 \r
25 def lcm_nfdeploymentdesc_list(deploymentManagerID: str,\r
26                               uow: unit_of_work.AbstractUnitOfWork):\r
27     with uow:\r
28         res = uow.session.execute(select(nfDeploymentDesc).where(\r
29             nfDeploymentDesc.c.deploymentManagerId == deploymentManagerID))\r
30     return [dict(r) for r in res]\r
31 \r
32 \r
33 def lcm_nfdeploymentdesc_one(nfdeploymentdescriptorid: str,\r
34                              uow: unit_of_work.AbstractUnitOfWork):\r
35     with uow:\r
36         res = uow.session.execute(select(nfDeploymentDesc).where(\r
37             nfDeploymentDesc.c.id == nfdeploymentdescriptorid))\r
38         first = res.first()\r
39     return None if first is None else dict(first)\r
40 \r
41 \r
42 def _check_duplication(name: str, uow: unit_of_work.AbstractUnitOfWork):\r
43     if uow.nfdeployment_descs.count(name=name) > 0:\r
44         raise Exception(\r
45             "NfDeploymentDescriptor with name {} exists already".format(name))\r
46 \r
47 \r
48 def lcm_nfdeploymentdesc_create(\r
49         deploymentManagerId: str,\r
50         input: DmsLcmNfDeploymentDescriptorDTO.\r
51         NfDeploymentDescriptor_create,\r
52         uow: unit_of_work.AbstractUnitOfWork):\r
53 \r
54     with uow:\r
55         _check_duplication(input['name'], uow)\r
56         id = str(uuid.uuid4())\r
57         entity = NfDeploymentDesc(\r
58             id, input['name'], deploymentManagerId, input['description'],\r
59             input['inputParams'], input['outputParams'],\r
60             input['artifactRepoUrl'], input['artifactName'])\r
61         uow.nfdeployment_descs.add(entity)\r
62         uow.commit()\r
63     return id\r
64 \r
65 \r
66 def lcm_nfdeploymentdesc_update(\r
67         nfdeploymentdescriptorid: str,\r
68         input: DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_update,\r
69         uow: unit_of_work.AbstractUnitOfWork):\r
70 \r
71     with uow:\r
72         entity = uow.nfdeployment_descs.get(nfdeploymentdescriptorid)\r
73         entity.name = input['name']\r
74         entity.description = input['description']\r
75         entity.inputParams = input['inputParams']\r
76         entity.outputParams = input['outputParams']\r
77         entity.artifactRepoUrl = input['artifactRepoUrl']\r
78         entity.artifactName = input['artifactName']\r
79         uow.commit()\r
80     return True\r
81 \r
82 \r
83 def lcm_nfdeploymentdesc_delete(\r
84         nfdeploymentdescriptorid: str, uow: unit_of_work.AbstractUnitOfWork):\r
85 \r
86     with uow:\r
87         # check dependency\r
88         _check_dependencies(nfdeploymentdescriptorid, uow)\r
89         uow.nfdeployment_descs.delete(nfdeploymentdescriptorid)\r
90         uow.commit()\r
91     return True\r
92 \r
93 \r
94 def _check_dependencies(\r
95     descriptorId: str, uow: unit_of_work.AbstractUnitOfWork\r
96 ):\r
97     # check if nfdeployment depends on it\r
98     if uow.nfdeployments.count(descriptorId=descriptorId) > 0:\r
99         raise Exception(\r
100             "NfDeployment with descriptorId {} exists".format(\r
101                 descriptorId))\r