| 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 | ## |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 24 | import asyncio |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 25 | import datetime |
| 26 | import json |
| 27 | import logging |
| 28 | import time |
| 29 | import uuid |
| 30 | |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 31 | from osm_policy_module.common.common_db_client import CommonDbClient |
| 32 | from osm_policy_module.common.message_bus_client import MessageBusClient |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 33 | from osm_policy_module.core.config import Config |
| 34 | |
| 35 | log = logging.getLogger(__name__) |
| 36 | |
| 37 | |
| 38 | class LcmClient: |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 39 | """ |
| 40 | Client to communicate with LCM through the message bus. |
| 41 | """ |
| 42 | |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 43 | def __init__(self, config: Config, loop=None): |
| 44 | self.db_client = CommonDbClient(config) |
| 45 | self.msg_bus = MessageBusClient(config) |
| Benjamin Diaz | 16256cb | 2018-10-11 12:34:20 -0300 | [diff] [blame] | 46 | if not loop: |
| 47 | loop = asyncio.get_event_loop() |
| 48 | self.loop = loop |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 49 | |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 50 | async def scale(self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str): |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 51 | """ |
| 52 | Sends scaling action to LCM through the message bus. |
| 53 | |
| 54 | :param nsr_id: Network service record id |
| 55 | :param scaling_group_name: Scaling group name |
| 56 | :param vnf_member_index: VNF member index |
| 57 | :param action: Scaling action to be executed. Valid values: scale_in, scale_out |
| 58 | :return: |
| 59 | """ |
| Benjamin Diaz | 4c5541f | 2018-10-10 10:53:49 -0300 | [diff] [blame] | 60 | log.debug("scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action) |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 61 | nsr = self.db_client.get_nsr(nsr_id) |
| 62 | nslcmop = self._generate_nslcmop(nsr_id, scaling_group_name, vnf_member_index, action, nsr['_admin']) |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 63 | self.db_client.create_nslcmop(nslcmop) |
| Benjamin Diaz | f7451f8 | 2019-04-01 14:56:26 -0300 | [diff] [blame] | 64 | log.debug("Sending scale action message: %s", json.dumps(nslcmop)) |
| Benjamin Diaz | a14cf16 | 2019-02-01 13:31:47 -0300 | [diff] [blame] | 65 | await self.msg_bus.aiowrite("ns", "scale", nslcmop) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 66 | |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 67 | def _generate_nslcmop(self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str, admin: dict): |
| 68 | """ |
| 69 | Builds scaling nslcmop. |
| 70 | |
| 71 | :param nsr_id: Network service record id |
| 72 | :param scaling_group_name: Scaling group name |
| 73 | :param vnf_member_index: VNF member index |
| 74 | :param action: Scaling action to be executed. Valid values: scale_in, scale_out |
| 75 | :param admin: Dict corresponding to the _admin section of the nsr. Required keys: projects_read, projects_write. |
| 76 | :return: |
| 77 | """ |
| 78 | log.debug("_generate_nslcmop %s %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action, admin) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 79 | _id = str(uuid.uuid4()) |
| 80 | now = time.time() |
| 81 | params = { |
| 82 | "scaleType": "SCALE_VNF", |
| 83 | "scaleVnfData": { |
| 84 | "scaleVnfType": action.upper(), |
| 85 | "scaleByStepData": { |
| 86 | "scaling-group-descriptor": scaling_group_name, |
| Benjamin Diaz | 3736ad8 | 2019-06-05 16:24:34 -0300 | [diff] [blame] | 87 | "member-vnf-index": vnf_member_index |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 88 | } |
| 89 | }, |
| 90 | "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat()) |
| 91 | } |
| 92 | |
| 93 | nslcmop = { |
| 94 | "id": _id, |
| 95 | "_id": _id, |
| 96 | "operationState": "PROCESSING", |
| 97 | "statusEnteredTime": now, |
| 98 | "nsInstanceId": nsr_id, |
| 99 | "lcmOperationType": "scale", |
| 100 | "startTime": now, |
| 101 | "isAutomaticInvocation": True, |
| 102 | "operationParams": params, |
| 103 | "isCancelPending": False, |
| 104 | "links": { |
| 105 | "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id, |
| 106 | "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id, |
| Benjamin Diaz | 87c4af9 | 2019-08-14 10:27:25 -0300 | [diff] [blame] | 107 | }, |
| 108 | "_admin": { |
| 109 | "projects_read": admin['projects_read'], |
| 110 | "projects_write": admin['projects_write'] |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | return nslcmop |