blob: e78420da71e8a628eec51ef441a600162a790729 [file] [log] [blame]
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -03001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
4# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License. You may obtain
11# a copy of the License at
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18# License for the specific language governing permissions and limitations
19# under the License.
20
21# For those usages not covered by the Apache License, Version 2.0 please
22# contact: bdiaz@whitestack.com or glavado@whitestack.com
23##
24import datetime
25import json
26import logging
27import time
28import uuid
29
Benjamin Diaza14cf162019-02-01 13:31:47 -030030from osm_policy_module.common.common_db_client import CommonDbClient
31from osm_policy_module.common.message_bus_client import MessageBusClient
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030032from osm_policy_module.core.config import Config
33
34log = logging.getLogger(__name__)
35
36
37class LcmClient:
Benjamin Diaz87c4af92019-08-14 10:27:25 -030038 """
39 Client to communicate with LCM through the message bus.
40 """
41
Mark Beierld37c54c2023-05-10 11:15:10 -040042 def __init__(self, config: Config):
Benjamin Diaza14cf162019-02-01 13:31:47 -030043 self.db_client = CommonDbClient(config)
44 self.msg_bus = MessageBusClient(config)
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030045
garciadeblas4584f8e2021-05-14 16:50:06 +020046 async def scale(
47 self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str
48 ):
Benjamin Diaz87c4af92019-08-14 10:27:25 -030049 """
50 Sends scaling action to LCM through the message bus.
51
52 :param nsr_id: Network service record id
53 :param scaling_group_name: Scaling group name
54 :param vnf_member_index: VNF member index
55 :param action: Scaling action to be executed. Valid values: scale_in, scale_out
56 :return:
57 """
garciadeblas4584f8e2021-05-14 16:50:06 +020058 log.debug(
59 "scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action
60 )
Benjamin Diaz87c4af92019-08-14 10:27:25 -030061 nsr = self.db_client.get_nsr(nsr_id)
garciadeblas4584f8e2021-05-14 16:50:06 +020062 nslcmop = self._generate_nslcmop(
63 nsr_id, scaling_group_name, vnf_member_index, action, nsr["_admin"]
64 )
Benjamin Diaza14cf162019-02-01 13:31:47 -030065 self.db_client.create_nslcmop(nslcmop)
Benjamin Diazf7451f82019-04-01 14:56:26 -030066 log.debug("Sending scale action message: %s", json.dumps(nslcmop))
Benjamin Diaza14cf162019-02-01 13:31:47 -030067 await self.msg_bus.aiowrite("ns", "scale", nslcmop)
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030068
garciadeblas4584f8e2021-05-14 16:50:06 +020069 def _generate_nslcmop(
70 self,
71 nsr_id: str,
72 scaling_group_name: str,
73 vnf_member_index: str,
74 action: str,
75 admin: dict,
76 ):
Benjamin Diaz87c4af92019-08-14 10:27:25 -030077 """
78 Builds scaling nslcmop.
79
80 :param nsr_id: Network service record id
81 :param scaling_group_name: Scaling group name
82 :param vnf_member_index: VNF member index
83 :param action: Scaling action to be executed. Valid values: scale_in, scale_out
84 :param admin: Dict corresponding to the _admin section of the nsr. Required keys: projects_read, projects_write.
85 :return:
86 """
garciadeblas4584f8e2021-05-14 16:50:06 +020087 log.debug(
88 "_generate_nslcmop %s %s %s %s %s",
89 nsr_id,
90 scaling_group_name,
91 vnf_member_index,
92 action,
93 admin,
94 )
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030095 _id = str(uuid.uuid4())
96 now = time.time()
97 params = {
98 "scaleType": "SCALE_VNF",
99 "scaleVnfData": {
100 "scaleVnfType": action.upper(),
101 "scaleByStepData": {
102 "scaling-group-descriptor": scaling_group_name,
garciadeblas4584f8e2021-05-14 16:50:06 +0200103 "member-vnf-index": vnf_member_index,
104 },
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300105 },
garciadeblas4584f8e2021-05-14 16:50:06 +0200106 "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat()),
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300107 }
108
109 nslcmop = {
110 "id": _id,
111 "_id": _id,
112 "operationState": "PROCESSING",
113 "statusEnteredTime": now,
114 "nsInstanceId": nsr_id,
115 "lcmOperationType": "scale",
116 "startTime": now,
117 "isAutomaticInvocation": True,
118 "operationParams": params,
119 "isCancelPending": False,
120 "links": {
121 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
122 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
Benjamin Diaz87c4af92019-08-14 10:27:25 -0300123 },
124 "_admin": {
garciadeblas4584f8e2021-05-14 16:50:06 +0200125 "projects_read": admin["projects_read"],
126 "projects_write": admin["projects_write"],
127 },
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300128 }
129 return nslcmop
sritharan7ef2b882022-04-25 12:37:55 +0000130
131 async def heal(
garciadeblasbe42d542022-11-14 00:29:47 +0100132 self,
133 nsr_id: str,
134 vnfinstance_id: str,
135 vdur_name: str,
136 vdu_id: str,
137 vnf_member_index: str,
138 heal_type: str,
139 day1: bool,
140 count_index: int,
141 ):
sritharan7ef2b882022-04-25 12:37:55 +0000142 """
143 Sends healing action to LCM through the message bus.
144
145 param nsr_id: Network service record id
146 param vdu_id: Scaling vdu id
147 param vnf_member_index: VNF member index
148 param heal_type: healing action to be executed. Valid values: restart,respawn
149 param day1: To run day1 operations
150 param cause: cause of healing
151 return
152 """
153 log.debug(
154 "heal %s %s %s %s %s %s %s %s",
155 nsr_id,
156 vnfinstance_id,
157 vdur_name,
158 vdu_id,
159 vnf_member_index,
160 heal_type,
161 day1,
162 count_index,
163 )
164 nsr = self.db_client.get_nsr(nsr_id)
165 nslcmop = self._generate_nslcmop_heal(
garciadeblasbe42d542022-11-14 00:29:47 +0100166 nsr_id,
167 vnfinstance_id,
168 vdur_name,
169 vdu_id,
170 vnf_member_index,
171 heal_type,
172 day1,
173 count_index,
174 nsr["_admin"],
sritharan7ef2b882022-04-25 12:37:55 +0000175 )
176 self.db_client.create_nslcmop(nslcmop)
177 log.debug("Sending heal action message: %s", json.dumps(nslcmop))
178 await self.msg_bus.aiowrite("ns", "heal", nslcmop)
179
180 def _generate_nslcmop_heal(
181 self,
182 nsr_id: str,
183 vnfinstance_id: str,
184 vdur_name: str,
185 vdu_id: str,
186 vnf_member_index: str,
187 heal_type: str,
188 day1: bool,
189 count_index: int,
190 admin: dict,
191 ):
192 """
193 Builds healing nslcmop.
194 param nsr_id: Network service record id
195 param vnf_member_index: VNF member index
196 param action: healing action to be executed. Valid values: restart, respawn
197 param admin: Dict corresponding to the _admin section of the nsr. Required keys: projects_read, projects_write.
198 return:
199 """
200 log.debug(
201 "_generate_nslcmop_heal %s %s %s %s %s %s %s %s %s",
202 nsr_id,
203 vnfinstance_id,
204 vdur_name,
205 vdu_id,
206 vnf_member_index,
207 heal_type,
208 day1,
209 count_index,
210 admin,
211 )
212 _id = str(uuid.uuid4())
213 now = time.time()
214 params = {
215 "lcmOperationType": "heal",
216 "nsInstanceId": nsr_id,
217 "healVnfData": [
218 {
219 "vnfInstanceId": vnfinstance_id,
220 "cause": "default",
221 "additionalParams": {
222 "run-day1": day1,
223 "vdu": [
224 {
225 "run-day1": day1,
226 "count-index": count_index,
garciadeblasbe42d542022-11-14 00:29:47 +0100227 "vdu-id": vdu_id,
sritharan7ef2b882022-04-25 12:37:55 +0000228 }
garciadeblasbe42d542022-11-14 00:29:47 +0100229 ],
230 },
sritharan7ef2b882022-04-25 12:37:55 +0000231 }
garciadeblasbe42d542022-11-14 00:29:47 +0100232 ],
sritharan7ef2b882022-04-25 12:37:55 +0000233 }
234
235 nslcmop = {
236 "id": _id,
237 "_id": _id,
238 "operationState": "PROCESSING",
239 "statusEnteredTime": now,
240 "nsInstanceId": nsr_id,
241 "member-vnf-index": vnf_member_index,
242 "lcmOperationType": "heal",
243 "startTime": now,
244 "location": "default",
245 "isAutomaticInvocation": True,
246 "operationParams": params,
247 "isCancelPending": False,
248 "links": {
249 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
250 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
251 },
252 "_admin": {
garciadeblasbe42d542022-11-14 00:29:47 +0100253 "projects_read": admin["projects_read"],
254 "projects_write": admin["projects_write"],
255 },
sritharan7ef2b882022-04-25 12:37:55 +0000256 }
257 return nslcmop