5ed98b76c212ac69c6761c3e2d69b79f5c446740
[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'], input['artifactUrl'])\r
60         uow.nfdeployment_descs.add(entity)\r
61         uow.commit()\r
62     return id\r
63 \r
64 \r
65 def lcm_nfdeploymentdesc_update(\r
66         nfdeploymentdescriptorid: str,\r
67         input: DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_update,\r
68         uow: unit_of_work.AbstractUnitOfWork):\r
69 \r
70     with uow:\r
71         entity = uow.nfdeployment_descs.get(nfdeploymentdescriptorid)\r
72         entity.name = input['name']\r
73         entity.description = input['description']\r
74         entity.inputParams = input['inputParams']\r
75         entity.outputParams = input['outputParams']\r
76         entity.artifactUrl = input['artifactUrl']\r
77         uow.commit()\r
78     return True\r
79 \r
80 \r
81 def lcm_nfdeploymentdesc_delete(\r
82         nfdeploymentdescriptorid: str, uow: unit_of_work.AbstractUnitOfWork):\r
83 \r
84     with uow:\r
85         # check dependency\r
86         _check_dependencies(nfdeploymentdescriptorid, uow)\r
87         uow.nfdeployment_descs.delete(nfdeploymentdescriptorid)\r
88         uow.commit()\r
89     return True\r
90 \r
91 \r
92 def _check_dependencies(\r
93     descriptorId: str, uow: unit_of_work.AbstractUnitOfWork\r
94 ):\r
95     # check if nfdeployment depends on it\r
96     if uow.nfdeployments.count(descriptorId=descriptorId) > 0:\r
97         raise Exception(\r
98             "NfDeployment with descriptorId {} exists".format(\r
99                 descriptorId))\r