blob: 6834eecefd26caaca8ac41e4e315c2924bb182e8 [file] [log] [blame]
Patricia Reinoso52431352023-04-05 15:35:48 +00001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import logging
18from time import time
19from temporalio import activity
20from osm_common.temporal_constants import (
Patricia Reinoso52431352023-04-05 15:35:48 +000021 ACTIVITY_CHECK_NS_INSTANTIATION_FINISHED,
Patricia Reinoso52431352023-04-05 15:35:48 +000022 ACTIVITY_DEPLOY_NS,
Patricia Reinoso911946d2023-04-12 15:54:43 +000023 ACTIVITY_GET_MODEL_INFO,
24 ACTIVITY_PREPARE_VNF_RECORDS,
25 ACTIVITY_UPDATE_NS_STATE,
Patricia Reinoso52431352023-04-05 15:35:48 +000026)
27from osm_common.dataclasses.temporal_dataclasses import (
Patricia Reinoso911946d2023-04-12 15:54:43 +000028 ModelInfo,
Patricia Reinoso52431352023-04-05 15:35:48 +000029 NsInstantiateInput,
30 UpdateNsStateInput,
31 VduInstantiateInput,
32)
33from osm_lcm.temporal.juju_paas_activities import CharmInfoUtils
34
35
36class NsOperations:
37 def __init__(self, db):
38 self.db = db
39 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
40
41 # TODO: Change to a workflow OSM-990
42 @activity.defn(name=ACTIVITY_DEPLOY_NS)
43 async def deploy_ns(self, ns_instantiate_input: NsInstantiateInput) -> None:
44 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_instantiate_input.ns_uuid})
45 for vnfr in vnfrs:
46 await self.deploy_vnf(vnfr)
47
48 async def deploy_vnf(self, vnfr: dict):
49 vim_id = vnfr.get("vim-account-id")
Patricia Reinoso911946d2023-04-12 15:54:43 +000050 model_name = "model-name"
Patricia Reinoso52431352023-04-05 15:35:48 +000051 vnfd_id = vnfr["vnfd-id"]
52 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
53 sw_image_descs = vnfd.get("sw-image-desc")
54 for vdu in vnfd.get("vdu"):
55 await self.deploy_vdu(vdu, sw_image_descs, vim_id, model_name)
56
57 async def deploy_vdu(
58 self, vdu: dict, sw_image_descs: str, vim_id: str, model_name: str
59 ) -> None:
60 vdu_info = CharmInfoUtils.get_charm_info(vdu, sw_image_descs)
61 vdu_instantiate_input = VduInstantiateInput(vim_id, model_name, vdu_info)
62 workflow_id = (
63 vdu_instantiate_input.model_name + vdu_instantiate_input.charm_info.app_name
64 )
65 self.logger.info(f"TODO: Start VDU Workflow {workflow_id}")
66
67 @activity.defn(name=ACTIVITY_CHECK_NS_INSTANTIATION_FINISHED)
68 async def check_ns_instantiate_finished(
69 self, ns_instantiate: NsInstantiateInput
70 ) -> None:
71 # TODO: Implement OSM-993
72 pass
73
74
75class NsDbActivity:
76
77 """Perform Database operations for NS accounts.
78
79 Args:
80 db (object): Data Access Object
81 """
82
83 def __init__(self, db):
84 self.db = db
85 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
86
87 @activity.defn(name=ACTIVITY_PREPARE_VNF_RECORDS)
88 async def prepare_vnf_records(
89 self, ns_instantiate_input: NsInstantiateInput
90 ) -> None:
91 """Prepare VNFs to be deployed: Add namespace to the VNFr.
92
93 Collaborators:
94 DB Write: vnfrs
95
96 Raises (Retryable):
97 DbException If the target DB record does not exist or DB is not reachable.
98
99 Activity Lifecycle:
100 This activity will not report a heartbeat due to its
101 short-running nature.
102
103 As this is a direct DB update, it is not recommended to have
104 any specific retry policy
105
106 """
107 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_instantiate_input.ns_uuid})
108 for vnfr in vnfrs:
109 self._prepare_vnf_record(vnfr)
110
Patricia Reinoso911946d2023-04-12 15:54:43 +0000111 @activity.defn(name=ACTIVITY_GET_MODEL_INFO)
112 async def get_model_info(
113 self, ns_instantiate_input: NsInstantiateInput
114 ) -> ModelInfo:
115 """Returns a ModelInfo. Contains VIM ID and model name.
116
117 Collaborators:
118 DB Read: nsrs
119
120 Raises (Retryable):
121 DbException If the target DB record does not exist or DB is not reachable.
122
123 Activity Lifecycle:
124 This activity will not report a heartbeat due to its
125 short-running nature.
126
127 As this is a direct DB update, it is not recommended to have
128 any specific retry policy
129
130 """
131 ns_uuid = ns_instantiate_input.ns_uuid
132 nsr = self.db.get_one("nsrs", {"_id": ns_uuid})
133 vim_uuid = nsr.get("datacenter")
134 model_name = self._get_namespace(ns_uuid, vim_uuid)
135 return ModelInfo(vim_uuid, model_name)
136
Patricia Reinoso52431352023-04-05 15:35:48 +0000137 def _get_namespace(self, ns_id: str, vim_id: str) -> str:
138 """The NS namespace is the combination if the NS ID and the VIM ID."""
139 return ns_id[-12:] + "-" + vim_id[-12:]
140
141 def _prepare_vnf_record(self, vnfr: dict) -> None:
142 """Add namespace to the VNFr."""
143 ns_id = vnfr["nsr-id-ref"]
144 vim_id = vnfr["vim-account-id"]
145 namespace = self._get_namespace(ns_id, vim_id)
146 update_namespace = {"namespace": namespace}
147 self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, update_namespace)
148
149 @activity.defn(name=ACTIVITY_UPDATE_NS_STATE)
150 async def update_ns_state(self, data: UpdateNsStateInput) -> None:
151 """
152 Changes the state of the NS itself.
153
154 Collaborators:
155 DB Write: nsrs
156
157 Raises (Retryable):
158 DbException If the target DB record does not exist or DB is not reachable.
159
160 Activity Lifecycle:
161 This activity will not report a heartbeat due to its
162 short-running nature.
163
164 As this is a direct DB update, it is not recommended to have
165 any specific retry policy
166 """
167 update_ns_state = {
168 "nsState": data.state.name,
169 # "errorDescription" : data.message,
170 "_admin.nsState": data.state.name,
171 "_admin.detailed-status": data.message,
172 "_admin.modified": time(),
173 }
174 self.db.set_one("nsrs", {"_id": data.ns_uuid}, update_ns_state)
175 self.logger.debug(f"Updated NS {data.ns_uuid} to {data.state.name}")