Validate nfdeployment descriptor params
[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 import json\r
16 from sqlalchemy import select\r
17 import uuid\r
18 from o2common.service import unit_of_work\r
19 from o2dms.adapter.orm import nfDeploymentDesc\r
20 from o2dms.api.dms_dto import DmsLcmNfDeploymentDescriptorDTO\r
21 from o2dms.domain.dms import NfDeploymentDesc\r
22 from o2common.helper import o2logging\r
23 logger = o2logging.get_logger(__name__)\r
24 \r
25 \r
26 def lcm_nfdeploymentdesc_list(deploymentManagerID: str,\r
27                               uow: unit_of_work.AbstractUnitOfWork):\r
28     with uow:\r
29         res = uow.session.execute(select(nfDeploymentDesc).where(\r
30             nfDeploymentDesc.c.deploymentManagerId == deploymentManagerID))\r
31     return [dict(r) for r in res]\r
32 \r
33 \r
34 def lcm_nfdeploymentdesc_one(nfdeploymentdescriptorid: str,\r
35                              uow: unit_of_work.AbstractUnitOfWork):\r
36     with uow:\r
37         res = uow.session.execute(select(nfDeploymentDesc).where(\r
38             nfDeploymentDesc.c.id == nfdeploymentdescriptorid))\r
39         first = res.first()\r
40     return None if first is None else dict(first)\r
41 \r
42 \r
43 def _check_duplication(name: str, uow: unit_of_work.AbstractUnitOfWork):\r
44     if uow.nfdeployment_descs.count(name=name) > 0:\r
45         raise Exception(\r
46             "NfDeploymentDescriptor with name {} exists already".format(name))\r
47 \r
48 \r
49 def lcm_nfdeploymentdesc_create(\r
50         deploymentManagerId: str,\r
51         input: DmsLcmNfDeploymentDescriptorDTO.\r
52         NfDeploymentDescriptor_create,\r
53         uow: unit_of_work.AbstractUnitOfWork):\r
54 \r
55     with uow:\r
56         _check_duplication(input['name'], uow)\r
57         id = str(uuid.uuid4())\r
58         entity = NfDeploymentDesc(\r
59             id, input['name'], deploymentManagerId, input['description'],\r
60             input['inputParams'], input['outputParams'],\r
61             input['artifactRepoUrl'], input['artifactName'])\r
62         _nfdeploymentdesc_validate(entity)\r
63         uow.nfdeployment_descs.add(entity)\r
64         uow.commit()\r
65     return id\r
66 \r
67 \r
68 def _nfdeploymentdesc_validate(desc: NfDeploymentDesc):\r
69     try:\r
70         if desc.inputParams:\r
71             json.loads(desc.inputParams)\r
72         if desc.outputParams:\r
73             json.loads(desc.outputParams)\r
74         if not desc.deploymentManagerId:\r
75             raise Exception("Invalid deploymentManager Id")\r
76         if not desc.artifactRepoUrl:\r
77             raise Exception("Invalid artifactRepoUrl")\r
78         if not desc.artifactName:\r
79             raise Exception("Invalid artifactName")\r
80         return\r
81     except json.decoder.JSONDecodeError as e:\r
82         logger.debug("NfDeploymentDesc json error with: %s" % (str(e)))\r
83         raise e\r
84     except Exception as e:\r
85         logger.debug("NfDeploymentDesc validate error with: %s" % (str(e)))\r
86         raise e\r
87 \r
88 \r
89 def lcm_nfdeploymentdesc_update(\r
90         nfdeploymentdescriptorid: str,\r
91         input: DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_update,\r
92         uow: unit_of_work.AbstractUnitOfWork):\r
93 \r
94     with uow:\r
95         entity = uow.nfdeployment_descs.get(nfdeploymentdescriptorid)\r
96         entity.name = input['name']\r
97         entity.description = input['description']\r
98         entity.inputParams = input['inputParams']\r
99         entity.outputParams = input['outputParams']\r
100         entity.artifactRepoUrl = input['artifactRepoUrl']\r
101         entity.artifactName = input['artifactName']\r
102         uow.commit()\r
103     return True\r
104 \r
105 \r
106 def lcm_nfdeploymentdesc_delete(\r
107         nfdeploymentdescriptorid: str, uow: unit_of_work.AbstractUnitOfWork):\r
108 \r
109     with uow:\r
110         # check dependency\r
111         _check_dependencies(nfdeploymentdescriptorid, uow)\r
112         uow.nfdeployment_descs.delete(nfdeploymentdescriptorid)\r
113         uow.commit()\r
114     return True\r
115 \r
116 \r
117 def _check_dependencies(\r
118     descriptorId: str, uow: unit_of_work.AbstractUnitOfWork\r
119 ):\r
120     # check if nfdeployment depends on it\r
121     if uow.nfdeployments.count(descriptorId=descriptorId) > 0:\r
122         raise Exception(\r
123             "NfDeployment with descriptorId {} exists".format(\r
124                 descriptorId))\r