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