| 1 |
# Copyright 2013, Nachi Ueno, NTT I3, Inc. |
| 2 |
# Copyright 2017, Fujitsu Limited |
| 3 |
# All Rights Reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 |
# not use this file except in compliance with the License. You may obtain |
| 7 |
# a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 |
# License for the specific language governing permissions and limitations |
| 15 |
# under the License. |
| 16 |
|
| 17 |
|
| 18 |
from neutron_lib.agent import l3_extension |
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_log import log as logging |
| 21 |
|
| 22 |
from neutron_vpnaas._i18n import _ |
| 23 |
from neutron_vpnaas.services.vpn import vpn_service |
| 24 |
|
| 25 |
LOG = logging.getLogger(__name__) |
| 26 |
|
| 27 |
vpn_agent_opts = [ |
| 28 |
cfg.MultiStrOpt( |
| 29 |
'vpn_device_driver', |
| 30 |
default=['neutron_vpnaas.services.vpn.device_drivers.' |
| 31 |
'ipsec.OpenSwanDriver'], |
| 32 |
sample_default=['neutron_vpnaas.services.vpn.device_drivers.ipsec.' |
| 33 |
'OpenSwanDriver, ' |
| 34 |
'neutron_vpnaas.services.vpn.device_drivers.' |
| 35 |
'strongswan_ipsec.StrongSwanDriver, ' |
| 36 |
'neutron_vpnaas.services.vpn.device_drivers.' |
| 37 |
'libreswan_ipsec.LibreSwanDriver'], |
| 38 |
help=_("The vpn device drivers Neutron will use")), |
| 39 |
] |
| 40 |
cfg.CONF.register_opts(vpn_agent_opts, 'vpnagent') |
| 41 |
|
| 42 |
|
| 43 |
class VPNAgent(l3_extension.L3AgentExtension): |
| 44 |
"""VPNaaS Agent support to be used by Neutron L3 agent.""" |
| 45 |
|
| 46 |
def initialize(self, connection, driver_type): |
| 47 |
LOG.debug("Loading VPNaaS") |
| 48 |
|
| 49 |
def consume_api(self, agent_api): |
| 50 |
LOG.debug("Loading consume_api for VPNaaS") |
| 51 |
self.agent_api = agent_api |
| 52 |
|
| 53 |
def __init__(self, host, conf): |
| 54 |
LOG.debug("Initializing VPNaaS agent") |
| 55 |
self.agent_api = None |
| 56 |
self.conf = conf |
| 57 |
self.host = host |
| 58 |
self.service = vpn_service.VPNService(self) |
| 59 |
self.device_drivers = self.service.load_device_drivers(self.host) |
| 60 |
|
| 61 |
def add_router(self, context, data): |
| 62 |
"""Handles router add event""" |
| 63 |
ri = self.agent_api.get_router_info(data['id']) |
| 64 |
if ri is not None: |
| 65 |
for device_driver in self.device_drivers: |
| 66 |
device_driver.create_router(ri) |
| 67 |
device_driver.sync(context, [ri]) |
| 68 |
else: |
| 69 |
LOG.debug("Router %s was concurrently deleted while " |
| 70 |
"creating VPN for it", data['id']) |
| 71 |
|
| 72 |
def update_router(self, context, data): |
| 73 |
"""Handles router update event""" |
| 74 |
ri = self.agent_api.get_router_info(data['id']) |
| 75 |
if ri is not None: |
| 76 |
for device_driver in self.device_drivers: |
| 77 |
device_driver.sync(context, [ri]) |
| 78 |
else: |
| 79 |
LOG.debug("Router %s was concurrently deleted while " |
| 80 |
"updating VPN for it", data['id']) |
| 81 |
|
| 82 |
def delete_router(self, context, data): |
| 83 |
"""Handles router delete event""" |
| 84 |
for device_driver in self.device_drivers: |
| 85 |
device_driver.destroy_router(data['id']) |
| 86 |
|
| 87 |
def ha_state_change(self, context, data): |
| 88 |
"""Enable the vpn process when router transitioned to master. |
| 89 |
|
| 90 |
And disable vpn process for backup router. |
| 91 |
""" |
| 92 |
router_id = data['router_id'] |
| 93 |
state = data['state'] |
| 94 |
for device_driver in self.device_drivers: |
| 95 |
if router_id in device_driver.processes: |
| 96 |
# NOTE(mnaser): We need to update the router object so it has |
| 97 |
# the new HA state so we can do status updates. |
| 98 |
device_driver.routers[router_id].ha_state = state |
| 99 |
|
| 100 |
process = device_driver.processes[router_id] |
| 101 |
if state in ('master', 'primary'): |
| 102 |
process.enable() |
| 103 |
else: |
| 104 |
process.disable() |
| 105 |
|
| 106 |
def update_network(self, context, data): |
| 107 |
pass |
| 108 |
|
| 109 |
|
| 110 |
class L3WithVPNaaS(VPNAgent): |
| 111 |
|
| 112 |
def __init__(self, conf=None): |
| 113 |
if conf: |
| 114 |
self.conf = conf |
| 115 |
else: |
| 116 |
self.conf = cfg.CONF |
| 117 |
super().__init__(host=self.conf.host, conf=self.conf) |