blob: 102b0dc8dfb1e3c195a1d93a35078800df64097c [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##
Benjamin Diaz16256cb2018-10-11 12:34:20 -030024import asyncio
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030025import datetime
26import json
27import logging
28import time
29import uuid
30
Benjamin Diaza14cf162019-02-01 13:31:47 -030031from osm_policy_module.common.common_db_client import CommonDbClient
32from osm_policy_module.common.message_bus_client import MessageBusClient
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030033from osm_policy_module.core.config import Config
34
35log = logging.getLogger(__name__)
36
37
38class LcmClient:
Benjamin Diaz87c4af92019-08-14 10:27:25 -030039 """
40 Client to communicate with LCM through the message bus.
41 """
42
Benjamin Diaza14cf162019-02-01 13:31:47 -030043 def __init__(self, config: Config, loop=None):
44 self.db_client = CommonDbClient(config)
45 self.msg_bus = MessageBusClient(config)
Benjamin Diaz16256cb2018-10-11 12:34:20 -030046 if not loop:
47 loop = asyncio.get_event_loop()
48 self.loop = loop
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030049
garciadeblas4584f8e2021-05-14 16:50:06 +020050 async def scale(
51 self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str
52 ):
Benjamin Diaz87c4af92019-08-14 10:27:25 -030053 """
54 Sends scaling action to LCM through the message bus.
55
56 :param nsr_id: Network service record id
57 :param scaling_group_name: Scaling group name
58 :param vnf_member_index: VNF member index
59 :param action: Scaling action to be executed. Valid values: scale_in, scale_out
60 :return:
61 """
garciadeblas4584f8e2021-05-14 16:50:06 +020062 log.debug(
63 "scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action
64 )
Benjamin Diaz87c4af92019-08-14 10:27:25 -030065 nsr = self.db_client.get_nsr(nsr_id)
garciadeblas4584f8e2021-05-14 16:50:06 +020066 nslcmop = self._generate_nslcmop(
67 nsr_id, scaling_group_name, vnf_member_index, action, nsr["_admin"]
68 )
Benjamin Diaza14cf162019-02-01 13:31:47 -030069 self.db_client.create_nslcmop(nslcmop)
Benjamin Diazf7451f82019-04-01 14:56:26 -030070 log.debug("Sending scale action message: %s", json.dumps(nslcmop))
Benjamin Diaza14cf162019-02-01 13:31:47 -030071 await self.msg_bus.aiowrite("ns", "scale", nslcmop)
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030072
garciadeblas4584f8e2021-05-14 16:50:06 +020073 def _generate_nslcmop(
74 self,
75 nsr_id: str,
76 scaling_group_name: str,
77 vnf_member_index: str,
78 action: str,
79 admin: dict,
80 ):
Benjamin Diaz87c4af92019-08-14 10:27:25 -030081 """
82 Builds scaling nslcmop.
83
84 :param nsr_id: Network service record id
85 :param scaling_group_name: Scaling group name
86 :param vnf_member_index: VNF member index
87 :param action: Scaling action to be executed. Valid values: scale_in, scale_out
88 :param admin: Dict corresponding to the _admin section of the nsr. Required keys: projects_read, projects_write.
89 :return:
90 """
garciadeblas4584f8e2021-05-14 16:50:06 +020091 log.debug(
92 "_generate_nslcmop %s %s %s %s %s",
93 nsr_id,
94 scaling_group_name,
95 vnf_member_index,
96 action,
97 admin,
98 )
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030099 _id = str(uuid.uuid4())
100 now = time.time()
101 params = {
102 "scaleType": "SCALE_VNF",
103 "scaleVnfData": {
104 "scaleVnfType": action.upper(),
105 "scaleByStepData": {
106 "scaling-group-descriptor": scaling_group_name,
garciadeblas4584f8e2021-05-14 16:50:06 +0200107 "member-vnf-index": vnf_member_index,
108 },
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300109 },
garciadeblas4584f8e2021-05-14 16:50:06 +0200110 "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat()),
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300111 }
112
113 nslcmop = {
114 "id": _id,
115 "_id": _id,
116 "operationState": "PROCESSING",
117 "statusEnteredTime": now,
118 "nsInstanceId": nsr_id,
119 "lcmOperationType": "scale",
120 "startTime": now,
121 "isAutomaticInvocation": True,
122 "operationParams": params,
123 "isCancelPending": False,
124 "links": {
125 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
126 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
Benjamin Diaz87c4af92019-08-14 10:27:25 -0300127 },
128 "_admin": {
garciadeblas4584f8e2021-05-14 16:50:06 +0200129 "projects_read": admin["projects_read"],
130 "projects_write": admin["projects_write"],
131 },
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -0300132 }
133 return nslcmop
sritharan7ef2b882022-04-25 12:37:55 +0000134
135 async def heal(
136 self, nsr_id: str, vnfinstance_id: str, vdur_name: str, vdu_id: str,
137 vnf_member_index: str, heal_type: str, day1: bool, count_index: int):
138 """
139 Sends healing action to LCM through the message bus.
140
141 param nsr_id: Network service record id
142 param vdu_id: Scaling vdu id
143 param vnf_member_index: VNF member index
144 param heal_type: healing action to be executed. Valid values: restart,respawn
145 param day1: To run day1 operations
146 param cause: cause of healing
147 return
148 """
149 log.debug(
150 "heal %s %s %s %s %s %s %s %s",
151 nsr_id,
152 vnfinstance_id,
153 vdur_name,
154 vdu_id,
155 vnf_member_index,
156 heal_type,
157 day1,
158 count_index,
159 )
160 nsr = self.db_client.get_nsr(nsr_id)
161 nslcmop = self._generate_nslcmop_heal(
162 nsr_id, vnfinstance_id, vdur_name, vdu_id, vnf_member_index, heal_type, day1,
163 count_index, nsr['_admin']
164 )
165 self.db_client.create_nslcmop(nslcmop)
166 log.debug("Sending heal action message: %s", json.dumps(nslcmop))
167 await self.msg_bus.aiowrite("ns", "heal", nslcmop)
168
169 def _generate_nslcmop_heal(
170 self,
171 nsr_id: str,
172 vnfinstance_id: str,
173 vdur_name: str,
174 vdu_id: str,
175 vnf_member_index: str,
176 heal_type: str,
177 day1: bool,
178 count_index: int,
179 admin: dict,
180 ):
181 """
182 Builds healing nslcmop.
183 param nsr_id: Network service record id
184 param vnf_member_index: VNF member index
185 param action: healing action to be executed. Valid values: restart, respawn
186 param admin: Dict corresponding to the _admin section of the nsr. Required keys: projects_read, projects_write.
187 return:
188 """
189 log.debug(
190 "_generate_nslcmop_heal %s %s %s %s %s %s %s %s %s",
191 nsr_id,
192 vnfinstance_id,
193 vdur_name,
194 vdu_id,
195 vnf_member_index,
196 heal_type,
197 day1,
198 count_index,
199 admin,
200 )
201 _id = str(uuid.uuid4())
202 now = time.time()
203 params = {
204 "lcmOperationType": "heal",
205 "nsInstanceId": nsr_id,
206 "healVnfData": [
207 {
208 "vnfInstanceId": vnfinstance_id,
209 "cause": "default",
210 "additionalParams": {
211 "run-day1": day1,
212 "vdu": [
213 {
214 "run-day1": day1,
215 "count-index": count_index,
216 "vdu-id": vdu_id
217 }
218 ]
219 }
220 }
221 ]
222 }
223
224 nslcmop = {
225 "id": _id,
226 "_id": _id,
227 "operationState": "PROCESSING",
228 "statusEnteredTime": now,
229 "nsInstanceId": nsr_id,
230 "member-vnf-index": vnf_member_index,
231 "lcmOperationType": "heal",
232 "startTime": now,
233 "location": "default",
234 "isAutomaticInvocation": True,
235 "operationParams": params,
236 "isCancelPending": False,
237 "links": {
238 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
239 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
240 },
241 "_admin": {
242 "projects_read": admin['projects_read'],
243 "projects_write": admin['projects_write']
244 }
245 }
246 return nslcmop