1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
30 from kafka
import KafkaProducer
31 from osm_common
import dbmongo
33 from osm_policy_module
.core
.config
import Config
35 log
= logging
.getLogger(__name__
)
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
),
51 def scale(self
, nsr_id
: str, scaling_group_name
: str, vnf_member_index
: int, action
: str):
52 nslcmop
= self
._generate
_nslcmop
(nsr_id
, scaling_group_name
, vnf_member_index
, action
)
53 self
.common_db
.create("nslcmops", nslcmop
)
54 log
.info("Sending scale action message: %s", json
.dumps(nslcmop
))
55 self
.producer
.send(topic
='ns', key
='scale', value
=json
.dumps(nslcmop
))
58 def _generate_nslcmop(self
, nsr_id
: str, scaling_group_name
: str, vnf_member_index
: int, action
: str):
59 _id
= str(uuid
.uuid4())
62 "scaleType": "SCALE_VNF",
64 "scaleVnfType": action
.upper(),
66 "scaling-group-descriptor": scaling_group_name
,
67 "member-vnf-index": str(vnf_member_index
)
70 "scaleTime": "{}Z".format(datetime
.datetime
.utcnow().isoformat())
76 "operationState": "PROCESSING",
77 "statusEnteredTime": now
,
78 "nsInstanceId": nsr_id
,
79 "lcmOperationType": "scale",
81 "isAutomaticInvocation": True,
82 "operationParams": params
,
83 "isCancelPending": False,
85 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
86 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,