blob: e8f79cf23d732137de8f64425f81ead91c0e9a61 [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 Diaza14cf162019-02-01 13:31:47 -030039 def __init__(self, config: Config, loop=None):
40 self.db_client = CommonDbClient(config)
41 self.msg_bus = MessageBusClient(config)
Benjamin Diaz16256cb2018-10-11 12:34:20 -030042 if not loop:
43 loop = asyncio.get_event_loop()
44 self.loop = loop
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030045
Benjamin Diaz3736ad82019-06-05 16:24:34 -030046 async def scale(self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str):
Benjamin Diaz4c5541f2018-10-10 10:53:49 -030047 log.debug("scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action)
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030048 nslcmop = self._generate_nslcmop(nsr_id, scaling_group_name, vnf_member_index, action)
Benjamin Diaza14cf162019-02-01 13:31:47 -030049 self.db_client.create_nslcmop(nslcmop)
Benjamin Diazf7451f82019-04-01 14:56:26 -030050 log.debug("Sending scale action message: %s", json.dumps(nslcmop))
Benjamin Diaza14cf162019-02-01 13:31:47 -030051 await self.msg_bus.aiowrite("ns", "scale", nslcmop)
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030052
Benjamin Diaz3736ad82019-06-05 16:24:34 -030053 def _generate_nslcmop(self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str):
Benjamin Diaz4c5541f2018-10-10 10:53:49 -030054 log.debug("_generate_nslcmop %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action)
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030055 _id = str(uuid.uuid4())
56 now = time.time()
57 params = {
58 "scaleType": "SCALE_VNF",
59 "scaleVnfData": {
60 "scaleVnfType": action.upper(),
61 "scaleByStepData": {
62 "scaling-group-descriptor": scaling_group_name,
Benjamin Diaz3736ad82019-06-05 16:24:34 -030063 "member-vnf-index": vnf_member_index
Benjamin Diaz7f11ecf2018-09-14 12:03:38 -030064 }
65 },
66 "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat())
67 }
68
69 nslcmop = {
70 "id": _id,
71 "_id": _id,
72 "operationState": "PROCESSING",
73 "statusEnteredTime": now,
74 "nsInstanceId": nsr_id,
75 "lcmOperationType": "scale",
76 "startTime": now,
77 "isAutomaticInvocation": True,
78 "operationParams": params,
79 "isCancelPending": False,
80 "links": {
81 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
82 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
83 }
84 }
85 return nslcmop