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
31 from osm_policy_module
.common
.common_db_client
import CommonDbClient
32 from osm_policy_module
.common
.message_bus_client
import MessageBusClient
33 from osm_policy_module
.core
.config
import Config
35 log
= logging
.getLogger(__name__
)
40 Client to communicate with LCM through the message bus.
43 def __init__(self
, config
: Config
, loop
=None):
44 self
.db_client
= CommonDbClient(config
)
45 self
.msg_bus
= MessageBusClient(config
)
47 loop
= asyncio
.get_event_loop()
51 self
, nsr_id
: str, scaling_group_name
: str, vnf_member_index
: str, action
: str
54 Sends scaling action to LCM through the message bus.
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
63 "scale %s %s %s %s", nsr_id
, scaling_group_name
, vnf_member_index
, action
65 nsr
= self
.db_client
.get_nsr(nsr_id
)
66 nslcmop
= self
._generate
_nslcmop
(
67 nsr_id
, scaling_group_name
, vnf_member_index
, action
, nsr
["_admin"]
69 self
.db_client
.create_nslcmop(nslcmop
)
70 log
.debug("Sending scale action message: %s", json
.dumps(nslcmop
))
71 await self
.msg_bus
.aiowrite("ns", "scale", nslcmop
)
73 def _generate_nslcmop(
76 scaling_group_name
: str,
77 vnf_member_index
: str,
82 Builds scaling nslcmop.
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.
92 "_generate_nslcmop %s %s %s %s %s",
99 _id
= str(uuid
.uuid4())
102 "scaleType": "SCALE_VNF",
104 "scaleVnfType": action
.upper(),
106 "scaling-group-descriptor": scaling_group_name
,
107 "member-vnf-index": vnf_member_index
,
110 "scaleTime": "{}Z".format(datetime
.datetime
.utcnow().isoformat()),
116 "operationState": "PROCESSING",
117 "statusEnteredTime": now
,
118 "nsInstanceId": nsr_id
,
119 "lcmOperationType": "scale",
121 "isAutomaticInvocation": True,
122 "operationParams": params
,
123 "isCancelPending": False,
125 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
126 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
129 "projects_read": admin
["projects_read"],
130 "projects_write": admin
["projects_write"],
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):
139 Sends healing action to LCM through the message bus.
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
150 "heal %s %s %s %s %s %s %s %s",
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']
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
)
169 def _generate_nslcmop_heal(
175 vnf_member_index
: str,
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.
190 "_generate_nslcmop_heal %s %s %s %s %s %s %s %s %s",
201 _id
= str(uuid
.uuid4())
204 "lcmOperationType": "heal",
205 "nsInstanceId": nsr_id
,
208 "vnfInstanceId": vnfinstance_id
,
210 "additionalParams": {
215 "count-index": count_index
,
227 "operationState": "PROCESSING",
228 "statusEnteredTime": now
,
229 "nsInstanceId": nsr_id
,
230 "member-vnf-index": vnf_member_index
,
231 "lcmOperationType": "heal",
233 "location": "default",
234 "isAutomaticInvocation": True,
235 "operationParams": params
,
236 "isCancelPending": False,
238 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
239 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
242 "projects_read": admin
['projects_read'],
243 "projects_write": admin
['projects_write']