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         json.loads(\r
71             desc['inputParams']) if desc['inputParams'] else None\r
72         json.loads(\r
73             desc['outputParams']) if desc['outputParams'] else None\r
74         return\r
75     except json.decoder.JSONDecodeError as e:\r
76         logger.debug("NfDeploymentDesc validate error with: %s" % (str(e)))\r
77         raise e\r
78     except Exception as e:\r
79         logger.debug("NfDeploymentDesc validate error with: %s" % (str(e)))\r
80         raise e\r
81 \r
82 \r
83 def lcm_nfdeploymentdesc_update(\r
84         nfdeploymentdescriptorid: str,\r
85         input: DmsLcmNfDeploymentDescriptorDTO.NfDeploymentDescriptor_update,\r
86         uow: unit_of_work.AbstractUnitOfWork):\r
87 \r
88     with uow:\r
89         entity = uow.nfdeployment_descs.get(nfdeploymentdescriptorid)\r
90         entity.name = input['name']\r
91         entity.description = input['description']\r
92         entity.inputParams = input['inputParams']\r
93         entity.outputParams = input['outputParams']\r
94         entity.artifactRepoUrl = input['artifactRepoUrl']\r
95         entity.artifactName = input['artifactName']\r
96         uow.commit()\r
97     return True\r
98 \r
99 \r
100 def lcm_nfdeploymentdesc_delete(\r
101         nfdeploymentdescriptorid: str, uow: unit_of_work.AbstractUnitOfWork):\r
102 \r
103     with uow:\r
104         # check dependency\r
105         _check_dependencies(nfdeploymentdescriptorid, uow)\r
106         uow.nfdeployment_descs.delete(nfdeploymentdescriptorid)\r
107         uow.commit()\r
108     return True\r
109 \r
110 \r
111 def _check_dependencies(\r
112     descriptorId: str, uow: unit_of_work.AbstractUnitOfWork\r
113 ):\r
114     # check if nfdeployment depends on it\r
115     if uow.nfdeployments.count(descriptorId=descriptorId) > 0:\r
116         raise Exception(\r
117             "NfDeployment with descriptorId {} exists".format(\r
118                 descriptorId))\r