| 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 | |
| 30 | from kafka import KafkaProducer |
| 31 | from osm_common import dbmongo |
| 32 | |
| 33 | from osm_policy_module.core.config import Config |
| 34 | |
| 35 | log = logging.getLogger(__name__) |
| 36 | |
| 37 | |
| 38 | class LcmClient: |
| 39 | def __init__(self): |
| 40 | cfg = Config.instance() |
| 41 | self.kafka_server = '{}:{}'.format(cfg.OSMPOL_MESSAGE_HOST, |
| 42 | cfg.OSMPOL_MESSAGE_PORT) |
| 43 | self.producer = KafkaProducer(bootstrap_servers=self.kafka_server, |
| 44 | key_serializer=str.encode, |
| 45 | value_serializer=str.encode) |
| 46 | self.common_db = dbmongo.DbMongo() |
| 47 | self.common_db.db_connect({'host': cfg.OSMPOL_DATABASE_HOST, |
| 48 | 'port': int(cfg.OSMPOL_DATABASE_PORT), |
| 49 | 'name': 'osm'}) |
| 50 | |
| 51 | def scale(self, nsr_id: str, scaling_group_name: str, vnf_member_index: int, action: str): |
| Benjamin Diaz | 4c5541f | 2018-10-10 10:53:49 -0300 | [diff] [blame^] | 52 | log.debug("scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 53 | nslcmop = self._generate_nslcmop(nsr_id, scaling_group_name, vnf_member_index, action) |
| 54 | self.common_db.create("nslcmops", nslcmop) |
| 55 | log.info("Sending scale action message: %s", json.dumps(nslcmop)) |
| 56 | self.producer.send(topic='ns', key='scale', value=json.dumps(nslcmop)) |
| 57 | self.producer.flush() |
| 58 | |
| 59 | def _generate_nslcmop(self, nsr_id: str, scaling_group_name: str, vnf_member_index: int, action: str): |
| Benjamin Diaz | 4c5541f | 2018-10-10 10:53:49 -0300 | [diff] [blame^] | 60 | log.debug("_generate_nslcmop %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action) |
| Benjamin Diaz | 7f11ecf | 2018-09-14 12:03:38 -0300 | [diff] [blame] | 61 | _id = str(uuid.uuid4()) |
| 62 | now = time.time() |
| 63 | params = { |
| 64 | "scaleType": "SCALE_VNF", |
| 65 | "scaleVnfData": { |
| 66 | "scaleVnfType": action.upper(), |
| 67 | "scaleByStepData": { |
| 68 | "scaling-group-descriptor": scaling_group_name, |
| 69 | "member-vnf-index": str(vnf_member_index) |
| 70 | } |
| 71 | }, |
| 72 | "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat()) |
| 73 | } |
| 74 | |
| 75 | nslcmop = { |
| 76 | "id": _id, |
| 77 | "_id": _id, |
| 78 | "operationState": "PROCESSING", |
| 79 | "statusEnteredTime": now, |
| 80 | "nsInstanceId": nsr_id, |
| 81 | "lcmOperationType": "scale", |
| 82 | "startTime": now, |
| 83 | "isAutomaticInvocation": True, |
| 84 | "operationParams": params, |
| 85 | "isCancelPending": False, |
| 86 | "links": { |
| 87 | "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id, |
| 88 | "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id, |
| 89 | } |
| 90 | } |
| 91 | return nslcmop |