| 1 |
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from neutron_lib import rpc as n_rpc |
| 17 |
|
| 18 |
from networking_l2gw._i18n import _ |
| 19 |
from networking_l2gw.services.l2gateway.common import constants as n_const |
| 20 |
from networking_l2gw.services.l2gateway import exceptions as l2gw_exc |
| 21 |
|
| 22 |
import oslo_messaging as messaging |
| 23 |
|
| 24 |
|
| 25 |
class L2gatewayAgentApi: |
| 26 |
"""L2gateway plugin to agent RPC API.""" |
| 27 |
|
| 28 |
API_VERSION = '1.0' |
| 29 |
|
| 30 |
def __init__(self, topic, host): |
| 31 |
"""Initialize L2gateway plugin.""" |
| 32 |
self.host = host |
| 33 |
target = messaging.Target(topic=topic, version=self.API_VERSION) |
| 34 |
self.client = n_rpc.get_client(target) |
| 35 |
|
| 36 |
def set_monitor_agent(self, context, hostname): |
| 37 |
"""RPC to select Monitor/Transact agent.""" |
| 38 |
cctxt = self.client.prepare(fanout=True) |
| 39 |
return cctxt.cast(context, |
| 40 |
'set_monitor_agent', |
| 41 |
hostname=hostname) |
| 42 |
|
| 43 |
def add_vif_to_gateway(self, context, ovsdb_identifier, logical_switch, |
| 44 |
physical_locator, mac_remote): |
| 45 |
"""RPC to enter the VM MAC details to gateway.""" |
| 46 |
cctxt = self.client.prepare() |
| 47 |
return cctxt.call(context, |
| 48 |
'add_vif_to_gateway', |
| 49 |
ovsdb_identifier=ovsdb_identifier, |
| 50 |
logical_switch_dict=logical_switch, |
| 51 |
locator_dict=physical_locator, |
| 52 |
mac_dict=mac_remote) |
| 53 |
|
| 54 |
def update_vif_to_gateway(self, context, ovsdb_identifier, |
| 55 |
physical_locator, mac_remote): |
| 56 |
"""RPC to update the VM MAC details to gateway.""" |
| 57 |
cctxt = self.client.prepare() |
| 58 |
return cctxt.call(context, |
| 59 |
'update_vif_to_gateway', |
| 60 |
ovsdb_identifier=ovsdb_identifier, |
| 61 |
locator_dict=physical_locator, |
| 62 |
mac_dict=mac_remote) |
| 63 |
|
| 64 |
def delete_vif_from_gateway(self, context, ovsdb_identifier, |
| 65 |
logical_switch_uuid, macs): |
| 66 |
"""RPC to delete the VM MAC details from gateway.""" |
| 67 |
cctxt = self.client.prepare() |
| 68 |
return cctxt.call(context, |
| 69 |
'delete_vif_from_gateway', |
| 70 |
ovsdb_identifier=ovsdb_identifier, |
| 71 |
logical_switch_uuid=logical_switch_uuid, |
| 72 |
mac=macs) |
| 73 |
|
| 74 |
def delete_network(self, context, ovsdb_identifier, logical_switch_uuid): |
| 75 |
"""RPC to delete the Network from gateway.""" |
| 76 |
cctxt = self.client.prepare() |
| 77 |
return cctxt.cast(context, |
| 78 |
'delete_network', |
| 79 |
ovsdb_identifier=ovsdb_identifier, |
| 80 |
logical_switch_uuid=logical_switch_uuid) |
| 81 |
|
| 82 |
def _validate_request_op_method(self, context, op_method): |
| 83 |
"""validate the method in the request.""" |
| 84 |
method_list = [n_const.CREATE, n_const.DELETE] |
| 85 |
if op_method not in method_list: |
| 86 |
raise l2gw_exc.InvalidMethod(op_method=op_method) |
| 87 |
|
| 88 |
def update_connection_to_gateway(self, context, ovsdb_identifier, |
| 89 |
ls_dict, locator_list, mac_dict, |
| 90 |
port_dict, op_method): |
| 91 |
"""RPC to update the connection to gateway.""" |
| 92 |
self._validate_request_op_method(context, op_method) |
| 93 |
cctxt = self.client.prepare() |
| 94 |
try: |
| 95 |
return cctxt.call(context, |
| 96 |
'update_connection_to_gateway', |
| 97 |
ovsdb_identifier=ovsdb_identifier, |
| 98 |
logical_switch_dict=ls_dict, |
| 99 |
locator_dicts=locator_list, |
| 100 |
mac_dicts=mac_dict, |
| 101 |
port_dicts=port_dict, |
| 102 |
op_method=op_method) |
| 103 |
except messaging.MessagingTimeout: |
| 104 |
message = _("Communication error with the L2 gateway agent") |
| 105 |
raise l2gw_exc.OVSDBError(message=message) |
| 106 |
except Exception as ex: |
| 107 |
message = str(ex) |
| 108 |
msg_splits = message.split('\n') |
| 109 |
raise l2gw_exc.OVSDBError(message="Error on the OVSDB " |
| 110 |
"server: " + msg_splits[0]) |