| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 1 | # -*- 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 | ## |
| 24 | import datetime |
| 25 | import json |
| 26 | import logging |
| 27 | import time |
| 28 | import uuid |
| 29 | |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 30 | from osm_policy_module.common.common_db_client import CommonDbClient |
| 31 | from osm_policy_module.common.message_bus_client import MessageBusClient |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 32 | from osm_policy_module.core.config import Config |
| 33 | |
| 34 | log = logging.getLogger(__name__) |
| 35 | |
| 36 | |
| 37 | class LcmClient: |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 38 | """ |
| 39 | Client to communicate with LCM through the message bus. |
| 40 | """ |
| 41 | |
| Mark Beierl | d37c54c | 2023-05-10 11:15:10 -0400 | [diff] [blame] | 42 | def __init__(self, config: Config): |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 43 | self.db_client = CommonDbClient(config) |
| 44 | self.msg_bus = MessageBusClient(config) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 45 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 46 | async def scale( |
| 47 | self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str |
| 48 | ): |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 49 | """ |
| 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 | """ |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 58 | log.debug( |
| 59 | "scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action |
| 60 | ) |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 61 | nsr = self.db_client.get_nsr(nsr_id) |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 62 | nslcmop = self._generate_nslcmop( |
| 63 | nsr_id, scaling_group_name, vnf_member_index, action, nsr["_admin"] |
| 64 | ) |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 65 | self.db_client.create_nslcmop(nslcmop) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 66 | log.debug("Sending scale action message: %s", json.dumps(nslcmop)) |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 67 | await self.msg_bus.aiowrite("ns", "scale", nslcmop) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 68 | |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 69 | 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 Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 77 | """ |
| 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 | """ |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 87 | 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 Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 95 | _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, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 103 | "member-vnf-index": vnf_member_index, |
| 104 | }, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 105 | }, |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 106 | "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat()), |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 107 | } |
| 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 Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 123 | }, |
| 124 | "_admin": { |
| garciadeblas | 4584f8e | 2021-05-14 16:50:06 +0200 | [diff] [blame] | 125 | "projects_read": admin["projects_read"], |
| 126 | "projects_write": admin["projects_write"], |
| 127 | }, |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 128 | } |
| 129 | return nslcmop |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 130 | |
| 131 | async def heal( |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 132 | 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 | ): |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 142 | """ |
| 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( |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 166 | 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"], |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 175 | ) |
| 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, |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 227 | "vdu-id": vdu_id, |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 228 | } |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 229 | ], |
| 230 | }, |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 231 | } |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 232 | ], |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 233 | } |
| 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": { |
| garciadeblas | be42d54 | 2022-11-14 00:29:47 +0100 | [diff] [blame] | 253 | "projects_read": admin["projects_read"], |
| 254 | "projects_write": admin["projects_write"], |
| 255 | }, |
| sritharan | 7ef2b88 | 2022-04-25 12:37:55 +0000 | [diff] [blame] | 256 | } |
| 257 | return nslcmop |