| 1 |
# Copyright 2023 SysEleven GmbH |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from neutron.plugins.ml2.drivers.ovn.agent import neutron_agent |
| 16 |
from neutron.plugins.ml2.drivers.ovn.mech_driver.ovsdb import ovsdb_monitor |
| 17 |
from neutron_lib.plugins import constants as plugin_constants |
| 18 |
from neutron_lib.plugins import directory |
| 19 |
|
| 20 |
from neutron_vpnaas.services.vpn.common import constants |
| 21 |
|
| 22 |
|
| 23 |
class OVNVPNAgent(neutron_agent.NeutronAgent): |
| 24 |
agent_type = constants.AGENT_TYPE_VPN |
| 25 |
binary = "neutron-ovn-vpn-agent" |
| 26 |
|
| 27 |
@property |
| 28 |
def nb_cfg(self): |
| 29 |
return int(self.chassis_private.external_ids.get( |
| 30 |
constants.OVN_AGENT_VPN_SB_CFG_KEY, 0)) |
| 31 |
|
| 32 |
@staticmethod |
| 33 |
def id_from_chassis_private(chassis_private): |
| 34 |
return chassis_private.external_ids.get( |
| 35 |
constants.OVN_AGENT_VPN_ID_KEY) |
| 36 |
|
| 37 |
@property |
| 38 |
def agent_id(self): |
| 39 |
return self.id_from_chassis_private(self.chassis_private) |
| 40 |
|
| 41 |
@property |
| 42 |
def description(self): |
| 43 |
return self.chassis_private.external_ids.get( |
| 44 |
constants.OVN_AGENT_VPN_DESC_KEY, '') |
| 45 |
|
| 46 |
|
| 47 |
class ChassisVPNAgentWriteEvent(ovsdb_monitor.ChassisAgentEvent): |
| 48 |
events = (ovsdb_monitor.BaseEvent.ROW_CREATE, |
| 49 |
ovsdb_monitor.BaseEvent.ROW_UPDATE) |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
def _vpnagent_nb_cfg(row): |
| 53 |
return int( |
| 54 |
row.external_ids.get(constants.OVN_AGENT_VPN_SB_CFG_KEY, -1)) |
| 55 |
|
| 56 |
@staticmethod |
| 57 |
def agent_id(row): |
| 58 |
return row.external_ids.get(constants.OVN_AGENT_VPN_ID_KEY) |
| 59 |
|
| 60 |
def match_fn(self, event, row, old=None): |
| 61 |
if not self.agent_id(row): |
| 62 |
# Don't create a cached object with an agent_id of 'None' |
| 63 |
return False |
| 64 |
if event == self.ROW_CREATE: |
| 65 |
return True |
| 66 |
try: |
| 67 |
return self._vpnagent_nb_cfg(row) != self._vpnagent_nb_cfg(old) |
| 68 |
except (AttributeError, KeyError): |
| 69 |
return False |
| 70 |
|
| 71 |
def run(self, event, row, old): |
| 72 |
neutron_agent.AgentCache().update(constants.AGENT_TYPE_VPN, row, |
| 73 |
clear_down=True) |
| 74 |
|
| 75 |
|
| 76 |
class OVNVPNAgentMonitor: |
| 77 |
def watch_agent_events(self): |
| 78 |
l3_plugin = directory.get_plugin(plugin_constants.L3) |
| 79 |
sb_ovn = l3_plugin._sb_ovn |
| 80 |
if sb_ovn: |
| 81 |
idl = sb_ovn.ovsdb_connection.idl |
| 82 |
if isinstance(idl, ovsdb_monitor.OvnSbIdl): |
| 83 |
idl.notify_handler.watch_event( |
| 84 |
ChassisVPNAgentWriteEvent(idl.driver)) |