| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import random |
| 17 |
|
| 18 |
from oslo_log import log as logging |
| 19 |
from oslo_serialization import jsonutils |
| 20 |
from oslo_utils import excutils |
| 21 |
|
| 22 |
from networking_l2gw.services.l2gateway.agent.ovsdb import base_connection |
| 23 |
from networking_l2gw.services.l2gateway.common import constants as n_const |
| 24 |
from networking_l2gw.services.l2gateway.common import ovsdb_schema |
| 25 |
from networking_l2gw.services.l2gateway import exceptions |
| 26 |
|
| 27 |
LOG = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
class OVSDBWriter(base_connection.BaseConnection): |
| 31 |
"""Performs transactions to OVSDB server tables.""" |
| 32 |
def __init__(self, conf, gw_config, mgr=None): |
| 33 |
super().__init__(conf, gw_config, mgr=None) |
| 34 |
self.mgr = mgr |
| 35 |
|
| 36 |
def _process_response(self, op_id): |
| 37 |
result = self._response(op_id) |
| 38 |
error = result.get("error", None) |
| 39 |
if error: |
| 40 |
raise exceptions.OVSDBError( |
| 41 |
message="Error from the OVSDB server: %s" % error |
| 42 |
) |
| 43 |
|
| 44 |
outcomes = result.get("result", None) |
| 45 |
if outcomes: |
| 46 |
for outcome in outcomes: |
| 47 |
error = outcome.get("error", None) |
| 48 |
if error: |
| 49 |
raise exceptions.OVSDBError( |
| 50 |
message="Error from the OVSDB server: %s" % error) |
| 51 |
return result |
| 52 |
|
| 53 |
def _get_reply(self, operation_id, ovsdb_identifier): |
| 54 |
count = 0 |
| 55 |
while count <= n_const.MAX_RETRIES: |
| 56 |
response = self._recv_data(ovsdb_identifier) |
| 57 |
LOG.debug("Response from OVSDB server = %s", str(response)) |
| 58 |
if response: |
| 59 |
try: |
| 60 |
json_m = jsonutils.loads(response) |
| 61 |
self.responses.append(json_m) |
| 62 |
method_type = json_m.get('method', None) |
| 63 |
if method_type == "echo" and self.enable_manager: |
| 64 |
self.ovsdb_dicts.get(ovsdb_identifier).send( |
| 65 |
jsonutils.dumps( |
| 66 |
{"result": json_m.get("params", None), |
| 67 |
"error": None, "id": json_m['id']})) |
| 68 |
else: |
| 69 |
if self._process_response(operation_id): |
| 70 |
return True |
| 71 |
except Exception as ex: |
| 72 |
with excutils.save_and_reraise_exception(): |
| 73 |
LOG.exception("Exception while receiving the " |
| 74 |
"response for the write request:" |
| 75 |
" [%s]", ex) |
| 76 |
count += 1 |
| 77 |
with excutils.save_and_reraise_exception(): |
| 78 |
LOG.error("Could not obtain response from the OVSDB server " |
| 79 |
"for the request") |
| 80 |
|
| 81 |
def _send_and_receive(self, query, operation_id, ovsdb_identifier, |
| 82 |
rcv_required): |
| 83 |
if not self.send(query, addr=ovsdb_identifier): |
| 84 |
return |
| 85 |
if rcv_required: |
| 86 |
self._get_reply(operation_id, ovsdb_identifier) |
| 87 |
|
| 88 |
def delete_logical_switch(self, logical_switch_uuid, ovsdb_identifier, |
| 89 |
rcv_required=True): |
| 90 |
"""Delete an entry from Logical_Switch OVSDB table.""" |
| 91 |
commit_dict = {"op": "commit", "durable": True} |
| 92 |
op_id = str(random.getrandbits(128)) |
| 93 |
query = {"method": "transact", |
| 94 |
"params": [n_const.OVSDB_SCHEMA_NAME, |
| 95 |
{"op": "delete", |
| 96 |
"table": "Logical_Switch", |
| 97 |
"where": [["_uuid", "==", |
| 98 |
["uuid", logical_switch_uuid]]]}, |
| 99 |
commit_dict], |
| 100 |
"id": op_id} |
| 101 |
LOG.debug("delete_logical_switch: query: %s", query) |
| 102 |
self._send_and_receive(query, op_id, ovsdb_identifier, rcv_required) |
| 103 |
|
| 104 |
def insert_ucast_macs_remote(self, l_switch_dict, locator_dict, |
| 105 |
mac_dict, ovsdb_identifier, |
| 106 |
rcv_required=True): |
| 107 |
"""Insert an entry in Ucast_Macs_Remote OVSDB table.""" |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
logical_switch = ovsdb_schema.LogicalSwitch(l_switch_dict['uuid'], |
| 112 |
l_switch_dict['name'], |
| 113 |
l_switch_dict['key'], |
| 114 |
l_switch_dict['description' |
| 115 |
]) |
| 116 |
locator = ovsdb_schema.PhysicalLocator(locator_dict['uuid'], |
| 117 |
locator_dict['dst_ip']) |
| 118 |
macObject = ovsdb_schema.UcastMacsRemote(mac_dict['uuid'], |
| 119 |
mac_dict['mac'], |
| 120 |
mac_dict['logical_switch_id'], |
| 121 |
mac_dict['physical_locator_id' |
| 122 |
], |
| 123 |
mac_dict['ip_address']) |
| 124 |
|
| 125 |
commit_dict = {"op": "commit", "durable": True} |
| 126 |
op_id = str(random.getrandbits(128)) |
| 127 |
params = [n_const.OVSDB_SCHEMA_NAME] |
| 128 |
|
| 129 |
if locator.uuid: |
| 130 |
locator_list = ['uuid', locator.uuid] |
| 131 |
else: |
| 132 |
locator.uuid = ''.join(['a', str(random.getrandbits(128))]) |
| 133 |
locator_list = ["named-uuid", locator.uuid] |
| 134 |
params.append(self._get_physical_locator_dict(locator)) |
| 135 |
|
| 136 |
if logical_switch.uuid: |
| 137 |
l_switches = ['uuid', logical_switch.uuid] |
| 138 |
else: |
| 139 |
logical_switch.uuid = ''.join(['a', str(random.getrandbits(128))]) |
| 140 |
l_switches = ["named-uuid", logical_switch.uuid] |
| 141 |
params.append(self._get_logical_switch_dict(logical_switch)) |
| 142 |
|
| 143 |
params.append(self._get_ucast_macs_remote_dict( |
| 144 |
macObject, locator_list, l_switches)) |
| 145 |
params.append(commit_dict) |
| 146 |
query = {"method": "transact", |
| 147 |
"params": params, |
| 148 |
"id": op_id} |
| 149 |
LOG.debug("insert_ucast_macs_remote: query: %s", query) |
| 150 |
self._send_and_receive(query, op_id, ovsdb_identifier, rcv_required) |
| 151 |
|
| 152 |
def update_ucast_macs_remote(self, locator_dict, mac_dict, |
| 153 |
ovsdb_identifier, |
| 154 |
rcv_required=True): |
| 155 |
"""Update an entry in Ucast_Macs_Remote OVSDB table.""" |
| 156 |
|
| 157 |
locator = ovsdb_schema.PhysicalLocator(locator_dict['uuid'], |
| 158 |
locator_dict['dst_ip']) |
| 159 |
macObject = ovsdb_schema.UcastMacsRemote(mac_dict['uuid'], |
| 160 |
mac_dict['mac'], |
| 161 |
mac_dict['logical_switch_id'], |
| 162 |
mac_dict['physical_locator_id' |
| 163 |
], |
| 164 |
mac_dict['ip_address']) |
| 165 |
|
| 166 |
commit_dict = {"op": "commit", "durable": True} |
| 167 |
op_id = str(random.getrandbits(128)) |
| 168 |
params = [n_const.OVSDB_SCHEMA_NAME] |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if locator.uuid: |
| 173 |
locator_list = ['uuid', locator.uuid] |
| 174 |
else: |
| 175 |
locator.uuid = ''.join(['a', str(random.getrandbits(128))]) |
| 176 |
locator_list = ["named-uuid", locator.uuid] |
| 177 |
params.append(self._get_physical_locator_dict(locator)) |
| 178 |
|
| 179 |
params.append(self._get_dict_for_update_ucast_mac_remote( |
| 180 |
macObject, locator_list)) |
| 181 |
params.append(commit_dict) |
| 182 |
query = {"method": "transact", |
| 183 |
"params": params, |
| 184 |
"id": op_id} |
| 185 |
LOG.debug("update_ucast_macs_remote: query: %s", query) |
| 186 |
self._send_and_receive(query, op_id, ovsdb_identifier, rcv_required) |
| 187 |
|
| 188 |
def delete_ucast_macs_remote(self, logical_switch_uuid, macs, |
| 189 |
ovsdb_identifier, |
| 190 |
rcv_required=True): |
| 191 |
"""Delete entries from Ucast_Macs_Remote OVSDB table.""" |
| 192 |
commit_dict = {"op": "commit", "durable": True} |
| 193 |
op_id = str(random.getrandbits(128)) |
| 194 |
params = [n_const.OVSDB_SCHEMA_NAME] |
| 195 |
for mac in macs: |
| 196 |
sub_query = {"op": "delete", |
| 197 |
"table": "Ucast_Macs_Remote", |
| 198 |
"where": [["MAC", |
| 199 |
"==", |
| 200 |
mac], |
| 201 |
["logical_switch", |
| 202 |
"==", |
| 203 |
["uuid", |
| 204 |
logical_switch_uuid]]]} |
| 205 |
params.append(sub_query) |
| 206 |
params.append(commit_dict) |
| 207 |
query = {"method": "transact", |
| 208 |
"params": params, |
| 209 |
"id": op_id} |
| 210 |
LOG.debug("delete_ucast_macs_remote: query: %s", query) |
| 211 |
self._send_and_receive(query, op_id, ovsdb_identifier, rcv_required) |
| 212 |
|
| 213 |
def update_connection_to_gateway(self, logical_switch_dict, |
| 214 |
locator_dicts, mac_dicts, |
| 215 |
port_dicts, ovsdb_identifier, |
| 216 |
op_method, |
| 217 |
rcv_required=True): |
| 218 |
"""Updates Physical Port's VNI to VLAN binding.""" |
| 219 |
|
| 220 |
|
| 221 |
update_dicts = self._get_bindings_to_update(logical_switch_dict, |
| 222 |
locator_dicts, |
| 223 |
mac_dicts, |
| 224 |
port_dicts, |
| 225 |
op_method) |
| 226 |
op_id = str(random.getrandbits(128)) |
| 227 |
query = {"method": "transact", |
| 228 |
"params": update_dicts, |
| 229 |
"id": op_id} |
| 230 |
LOG.debug("update_connection_to_gateway: query = %s", query) |
| 231 |
self._send_and_receive(query, op_id, ovsdb_identifier, rcv_required) |
| 232 |
|
| 233 |
def _recv_data(self, ovsdb_identifier): |
| 234 |
chunks = [] |
| 235 |
lc = rc = 0 |
| 236 |
prev_char = None |
| 237 |
while True: |
| 238 |
try: |
| 239 |
if self.enable_manager: |
| 240 |
response = self.ovsdb_dicts.get(ovsdb_identifier).recv( |
| 241 |
n_const.BUFFER_SIZE) |
| 242 |
else: |
| 243 |
response = self.socket.recv(n_const.BUFFER_SIZE) |
| 244 |
if response: |
| 245 |
response = response.decode('utf8') |
| 246 |
for i, c in enumerate(response): |
| 247 |
if c == '{' and not (prev_char and |
| 248 |
prev_char == '\\'): |
| 249 |
lc += 1 |
| 250 |
elif c == '}' and not (prev_char and |
| 251 |
prev_char == '\\'): |
| 252 |
rc += 1 |
| 253 |
if lc == rc and lc != 0: |
| 254 |
chunks.append(response[0:i + 1]) |
| 255 |
message = "".join(chunks) |
| 256 |
return message |
| 257 |
prev_char = c |
| 258 |
chunks.append(response) |
| 259 |
else: |
| 260 |
LOG.warning("Did not receive any reply from the OVSDB " |
| 261 |
"server") |
| 262 |
return |
| 263 |
except (OSError, TimeoutError): |
| 264 |
LOG.warning("Did not receive any reply from the OVSDB " |
| 265 |
"server") |
| 266 |
return |
| 267 |
|
| 268 |
def _get_bindings_to_update(self, l_switch_dict, locator_dicts, |
| 269 |
mac_dicts, port_dicts, op_method): |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
locator_list = [] |
| 280 |
port_list = [] |
| 281 |
ls_list = [] |
| 282 |
logical_switch = None |
| 283 |
|
| 284 |
if l_switch_dict: |
| 285 |
logical_switch = ovsdb_schema.LogicalSwitch( |
| 286 |
l_switch_dict['uuid'], |
| 287 |
l_switch_dict['name'], |
| 288 |
l_switch_dict['key'], |
| 289 |
l_switch_dict['description']) |
| 290 |
|
| 291 |
|
| 292 |
for locator in locator_dicts: |
| 293 |
locator_list.append(ovsdb_schema.PhysicalLocator(locator['uuid'], |
| 294 |
locator['dst_ip']) |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
locator_macs = {} |
| 300 |
for locator_ip, mac_list in mac_dicts.items(): |
| 301 |
mac_object_list = [] |
| 302 |
for mac_dict in mac_list: |
| 303 |
mac_object = ovsdb_schema.UcastMacsRemote( |
| 304 |
mac_dict['uuid'], |
| 305 |
mac_dict['mac'], |
| 306 |
mac_dict['logical_switch_id'], |
| 307 |
mac_dict['physical_locator_id'], |
| 308 |
mac_dict['ip_address']) |
| 309 |
mac_object_list.append(mac_object) |
| 310 |
locator_macs[locator_ip] = mac_object_list |
| 311 |
|
| 312 |
|
| 313 |
for port in port_dicts: |
| 314 |
phys_port = ovsdb_schema.PhysicalPort(port['uuid'], |
| 315 |
port['name'], |
| 316 |
port['physical_switch_id'], |
| 317 |
port['vlan_bindings'], |
| 318 |
port['port_fault_status']) |
| 319 |
port_list.append(phys_port) |
| 320 |
|
| 321 |
bindings = [] |
| 322 |
bindings.append(n_const.OVSDB_SCHEMA_NAME) |
| 323 |
|
| 324 |
|
| 325 |
commit_dict = {"op": "commit", "durable": True} |
| 326 |
params = [n_const.OVSDB_SCHEMA_NAME] |
| 327 |
|
| 328 |
|
| 329 |
if logical_switch: |
| 330 |
ls_list = self._form_logical_switch(logical_switch, params) |
| 331 |
|
| 332 |
|
| 333 |
if locator_list: |
| 334 |
self._form_physical_locators(ls_list, locator_list, locator_macs, |
| 335 |
params) |
| 336 |
|
| 337 |
|
| 338 |
self._form_ports(ls_list, port_list, params, op_method) |
| 339 |
params.append(commit_dict) |
| 340 |
return params |
| 341 |
|
| 342 |
def _form_logical_switch(self, logical_switch, params): |
| 343 |
ls_list = [] |
| 344 |
if logical_switch.uuid: |
| 345 |
ls_list = ['uuid', logical_switch.uuid] |
| 346 |
else: |
| 347 |
logical_switch.uuid = ''.join(['a', str(random.getrandbits(128))]) |
| 348 |
ls_list = ["named-uuid", logical_switch.uuid] |
| 349 |
params.append(self._get_logical_switch_dict(logical_switch)) |
| 350 |
return ls_list |
| 351 |
|
| 352 |
def _form_physical_locators(self, ls_list, locator_list, locator_macs, |
| 353 |
params): |
| 354 |
for locator in locator_list: |
| 355 |
if locator.uuid: |
| 356 |
loc_list = ['uuid', locator.uuid] |
| 357 |
else: |
| 358 |
locator.uuid = ''.join(['a', str(random.getrandbits(128))]) |
| 359 |
loc_list = ["named-uuid", locator.uuid] |
| 360 |
params.append(self._get_physical_locator_dict(locator)) |
| 361 |
macs = locator_macs.get(locator.dst_ip, None) |
| 362 |
if macs: |
| 363 |
for mac in macs: |
| 364 |
query = self._get_ucast_macs_remote_dict(mac, |
| 365 |
loc_list, |
| 366 |
ls_list) |
| 367 |
params.append(query) |
| 368 |
|
| 369 |
def _form_ports(self, ls_list, port_list, params, op_method): |
| 370 |
for port in port_list: |
| 371 |
port_vlan_bindings = [] |
| 372 |
outer_list = [] |
| 373 |
port_vlan_bindings.append("map") |
| 374 |
if port.vlan_bindings: |
| 375 |
for vlan_binding in port.vlan_bindings: |
| 376 |
if vlan_binding.logical_switch_uuid: |
| 377 |
outer_list.append([vlan_binding.vlan, |
| 378 |
['uuid', |
| 379 |
vlan_binding.logical_switch_uuid]]) |
| 380 |
else: |
| 381 |
outer_list.append([vlan_binding.vlan, |
| 382 |
ls_list]) |
| 383 |
port_vlan_bindings.append(outer_list) |
| 384 |
if op_method == 'CREATE': |
| 385 |
update_dict = {"op": "mutate", |
| 386 |
"table": "Physical_Port", |
| 387 |
"where": [["_uuid", "==", |
| 388 |
["uuid", port.uuid]]], |
| 389 |
"mutations": [["vlan_bindings", |
| 390 |
"insert", |
| 391 |
port_vlan_bindings]]} |
| 392 |
elif op_method == 'DELETE': |
| 393 |
update_dict = {"op": "mutate", |
| 394 |
"table": "Physical_Port", |
| 395 |
"where": [["_uuid", "==", |
| 396 |
["uuid", port.uuid]]], |
| 397 |
"mutations": [["vlan_bindings", |
| 398 |
"delete", |
| 399 |
port_vlan_bindings]]} |
| 400 |
params.append(update_dict) |
| 401 |
|
| 402 |
def _get_physical_locator_dict(self, locator): |
| 403 |
return {"op": "insert", |
| 404 |
"table": "Physical_Locator", |
| 405 |
"uuid-name": locator.uuid, |
| 406 |
"row": {"dst_ip": locator.dst_ip, |
| 407 |
"encapsulation_type": "vxlan_over_ipv4"}} |
| 408 |
|
| 409 |
def _get_logical_switch_dict(self, logical_switch): |
| 410 |
return {"op": "insert", |
| 411 |
"uuid-name": logical_switch.uuid, |
| 412 |
"table": "Logical_Switch", |
| 413 |
"row": {"description": logical_switch.description, |
| 414 |
"name": logical_switch.name, |
| 415 |
"tunnel_key": int(logical_switch.key)}} |
| 416 |
|
| 417 |
def _get_ucast_macs_remote_dict(self, mac, locator_list, |
| 418 |
logical_switch_list): |
| 419 |
named_string = str(random.getrandbits(128)) |
| 420 |
return {"op": "insert", |
| 421 |
"uuid-name": ''.join(['a', named_string]), |
| 422 |
"table": "Ucast_Macs_Remote", |
| 423 |
"row": {"MAC": mac.mac, |
| 424 |
"ipaddr": mac.ip_address, |
| 425 |
"locator": locator_list, |
| 426 |
"logical_switch": logical_switch_list}} |
| 427 |
|
| 428 |
def _get_dict_for_update_ucast_mac_remote(self, mac, locator_list): |
| 429 |
return {"op": "update", |
| 430 |
"table": "Ucast_Macs_Remote", |
| 431 |
"where": [["_uuid", "==", |
| 432 |
["uuid", mac.uuid]]], |
| 433 |
"row": {"locator": locator_list}} |