Enable black and pylint in tox.ini
[osm/POL.git] / osm_policy_module / common / lcm_client.py
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 asyncio
25 import datetime
26 import json
27 import logging
28 import time
29 import uuid
30
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
34
35 log = logging.getLogger(__name__)
36
37
38 class LcmClient:
39 """
40 Client to communicate with LCM through the message bus.
41 """
42
43 def __init__(self, config: Config, loop=None):
44 self.db_client = CommonDbClient(config)
45 self.msg_bus = MessageBusClient(config)
46 if not loop:
47 loop = asyncio.get_event_loop()
48 self.loop = loop
49
50 async def scale(
51 self, nsr_id: str, scaling_group_name: str, vnf_member_index: str, action: str
52 ):
53 """
54 Sends scaling action to LCM through the message bus.
55
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
60 :return:
61 """
62 log.debug(
63 "scale %s %s %s %s", nsr_id, scaling_group_name, vnf_member_index, action
64 )
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"]
68 )
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)
72
73 def _generate_nslcmop(
74 self,
75 nsr_id: str,
76 scaling_group_name: str,
77 vnf_member_index: str,
78 action: str,
79 admin: dict,
80 ):
81 """
82 Builds scaling nslcmop.
83
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.
89 :return:
90 """
91 log.debug(
92 "_generate_nslcmop %s %s %s %s %s",
93 nsr_id,
94 scaling_group_name,
95 vnf_member_index,
96 action,
97 admin,
98 )
99 _id = str(uuid.uuid4())
100 now = time.time()
101 params = {
102 "scaleType": "SCALE_VNF",
103 "scaleVnfData": {
104 "scaleVnfType": action.upper(),
105 "scaleByStepData": {
106 "scaling-group-descriptor": scaling_group_name,
107 "member-vnf-index": vnf_member_index,
108 },
109 },
110 "scaleTime": "{}Z".format(datetime.datetime.utcnow().isoformat()),
111 }
112
113 nslcmop = {
114 "id": _id,
115 "_id": _id,
116 "operationState": "PROCESSING",
117 "statusEnteredTime": now,
118 "nsInstanceId": nsr_id,
119 "lcmOperationType": "scale",
120 "startTime": now,
121 "isAutomaticInvocation": True,
122 "operationParams": params,
123 "isCancelPending": False,
124 "links": {
125 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
126 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
127 },
128 "_admin": {
129 "projects_read": admin["projects_read"],
130 "projects_write": admin["projects_write"],
131 },
132 }
133 return nslcmop
134
135 async def heal(
136 self,
137 nsr_id: str,
138 vnfinstance_id: str,
139 vdur_name: str,
140 vdu_id: str,
141 vnf_member_index: str,
142 heal_type: str,
143 day1: bool,
144 count_index: int,
145 ):
146 """
147 Sends healing action to LCM through the message bus.
148
149 param nsr_id: Network service record id
150 param vdu_id: Scaling vdu id
151 param vnf_member_index: VNF member index
152 param heal_type: healing action to be executed. Valid values: restart,respawn
153 param day1: To run day1 operations
154 param cause: cause of healing
155 return
156 """
157 log.debug(
158 "heal %s %s %s %s %s %s %s %s",
159 nsr_id,
160 vnfinstance_id,
161 vdur_name,
162 vdu_id,
163 vnf_member_index,
164 heal_type,
165 day1,
166 count_index,
167 )
168 nsr = self.db_client.get_nsr(nsr_id)
169 nslcmop = self._generate_nslcmop_heal(
170 nsr_id,
171 vnfinstance_id,
172 vdur_name,
173 vdu_id,
174 vnf_member_index,
175 heal_type,
176 day1,
177 count_index,
178 nsr["_admin"],
179 )
180 self.db_client.create_nslcmop(nslcmop)
181 log.debug("Sending heal action message: %s", json.dumps(nslcmop))
182 await self.msg_bus.aiowrite("ns", "heal", nslcmop)
183
184 def _generate_nslcmop_heal(
185 self,
186 nsr_id: str,
187 vnfinstance_id: str,
188 vdur_name: str,
189 vdu_id: str,
190 vnf_member_index: str,
191 heal_type: str,
192 day1: bool,
193 count_index: int,
194 admin: dict,
195 ):
196 """
197 Builds healing nslcmop.
198 param nsr_id: Network service record id
199 param vnf_member_index: VNF member index
200 param action: healing action to be executed. Valid values: restart, respawn
201 param admin: Dict corresponding to the _admin section of the nsr. Required keys: projects_read, projects_write.
202 return:
203 """
204 log.debug(
205 "_generate_nslcmop_heal %s %s %s %s %s %s %s %s %s",
206 nsr_id,
207 vnfinstance_id,
208 vdur_name,
209 vdu_id,
210 vnf_member_index,
211 heal_type,
212 day1,
213 count_index,
214 admin,
215 )
216 _id = str(uuid.uuid4())
217 now = time.time()
218 params = {
219 "lcmOperationType": "heal",
220 "nsInstanceId": nsr_id,
221 "healVnfData": [
222 {
223 "vnfInstanceId": vnfinstance_id,
224 "cause": "default",
225 "additionalParams": {
226 "run-day1": day1,
227 "vdu": [
228 {
229 "run-day1": day1,
230 "count-index": count_index,
231 "vdu-id": vdu_id,
232 }
233 ],
234 },
235 }
236 ],
237 }
238
239 nslcmop = {
240 "id": _id,
241 "_id": _id,
242 "operationState": "PROCESSING",
243 "statusEnteredTime": now,
244 "nsInstanceId": nsr_id,
245 "member-vnf-index": vnf_member_index,
246 "lcmOperationType": "heal",
247 "startTime": now,
248 "location": "default",
249 "isAutomaticInvocation": True,
250 "operationParams": params,
251 "isCancelPending": False,
252 "links": {
253 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id,
254 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id,
255 },
256 "_admin": {
257 "projects_read": admin["projects_read"],
258 "projects_write": admin["projects_write"],
259 },
260 }
261 return nslcmop