blob: 1a2925c4e8091379fa920e8b7868e8753e691677 [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 (
21 ACTIVITY_UPDATE_NS_STATE,
22 ACTIVITY_CHECK_NS_INSTANTIATION_FINISHED,
23 ACTIVITY_PREPARE_VNF_RECORDS,
24 ACTIVITY_DEPLOY_NS,
25)
26from osm_common.dataclasses.temporal_dataclasses import (
27 NsInstantiateInput,
28 UpdateNsStateInput,
29 VduInstantiateInput,
30)
31from osm_lcm.temporal.juju_paas_activities import CharmInfoUtils
32
33
34class NsOperations:
35 def __init__(self, db):
36 self.db = db
37 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
38
39 # TODO: Change to a workflow OSM-990
40 @activity.defn(name=ACTIVITY_DEPLOY_NS)
41 async def deploy_ns(self, ns_instantiate_input: NsInstantiateInput) -> None:
42 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_instantiate_input.ns_uuid})
43 for vnfr in vnfrs:
44 await self.deploy_vnf(vnfr)
45
46 async def deploy_vnf(self, vnfr: dict):
47 vim_id = vnfr.get("vim-account-id")
48 model_name = vnfr.get("namespace")
49 vnfd_id = vnfr["vnfd-id"]
50 vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
51 sw_image_descs = vnfd.get("sw-image-desc")
52 for vdu in vnfd.get("vdu"):
53 await self.deploy_vdu(vdu, sw_image_descs, vim_id, model_name)
54
55 async def deploy_vdu(
56 self, vdu: dict, sw_image_descs: str, vim_id: str, model_name: str
57 ) -> None:
58 vdu_info = CharmInfoUtils.get_charm_info(vdu, sw_image_descs)
59 vdu_instantiate_input = VduInstantiateInput(vim_id, model_name, vdu_info)
60 workflow_id = (
61 vdu_instantiate_input.model_name + vdu_instantiate_input.charm_info.app_name
62 )
63 self.logger.info(f"TODO: Start VDU Workflow {workflow_id}")
64
65 @activity.defn(name=ACTIVITY_CHECK_NS_INSTANTIATION_FINISHED)
66 async def check_ns_instantiate_finished(
67 self, ns_instantiate: NsInstantiateInput
68 ) -> None:
69 # TODO: Implement OSM-993
70 pass
71
72
73class NsDbActivity:
74
75 """Perform Database operations for NS accounts.
76
77 Args:
78 db (object): Data Access Object
79 """
80
81 def __init__(self, db):
82 self.db = db
83 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
84
85 @activity.defn(name=ACTIVITY_PREPARE_VNF_RECORDS)
86 async def prepare_vnf_records(
87 self, ns_instantiate_input: NsInstantiateInput
88 ) -> None:
89 """Prepare VNFs to be deployed: Add namespace to the VNFr.
90
91 Collaborators:
92 DB Write: vnfrs
93
94 Raises (Retryable):
95 DbException If the target DB record does not exist or DB is not reachable.
96
97 Activity Lifecycle:
98 This activity will not report a heartbeat due to its
99 short-running nature.
100
101 As this is a direct DB update, it is not recommended to have
102 any specific retry policy
103
104 """
105 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_instantiate_input.ns_uuid})
106 for vnfr in vnfrs:
107 self._prepare_vnf_record(vnfr)
108
109 def _get_namespace(self, ns_id: str, vim_id: str) -> str:
110 """The NS namespace is the combination if the NS ID and the VIM ID."""
111 return ns_id[-12:] + "-" + vim_id[-12:]
112
113 def _prepare_vnf_record(self, vnfr: dict) -> None:
114 """Add namespace to the VNFr."""
115 ns_id = vnfr["nsr-id-ref"]
116 vim_id = vnfr["vim-account-id"]
117 namespace = self._get_namespace(ns_id, vim_id)
118 update_namespace = {"namespace": namespace}
119 self.db.set_one("vnfrs", {"_id": vnfr["_id"]}, update_namespace)
120
121 @activity.defn(name=ACTIVITY_UPDATE_NS_STATE)
122 async def update_ns_state(self, data: UpdateNsStateInput) -> None:
123 """
124 Changes the state of the NS itself.
125
126 Collaborators:
127 DB Write: nsrs
128
129 Raises (Retryable):
130 DbException If the target DB record does not exist or DB is not reachable.
131
132 Activity Lifecycle:
133 This activity will not report a heartbeat due to its
134 short-running nature.
135
136 As this is a direct DB update, it is not recommended to have
137 any specific retry policy
138 """
139 update_ns_state = {
140 "nsState": data.state.name,
141 # "errorDescription" : data.message,
142 "_admin.nsState": data.state.name,
143 "_admin.detailed-status": data.message,
144 "_admin.modified": time(),
145 }
146 self.db.set_one("nsrs", {"_id": data.ns_uuid}, update_ns_state)
147 self.logger.debug(f"Updated NS {data.ns_uuid} to {data.state.name}")