| 1 |
# Copyright 2020, SysEleven GbmH |
| 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 |
from neutron.api.rpc.agentnotifiers import utils as ag_utils |
| 16 |
from neutron_lib import rpc as n_rpc |
| 17 |
import oslo_messaging |
| 18 |
|
| 19 |
from neutron_vpnaas.services.vpn.common import topics |
| 20 |
|
| 21 |
# default messaging timeout is 60 sec, so 2 here is chosen to not block API |
| 22 |
# call for more than 2 minutes |
| 23 |
AGENT_NOTIFY_MAX_ATTEMPTS = 2 |
| 24 |
|
| 25 |
|
| 26 |
class VPNAgentNotifyAPI: |
| 27 |
"""API for plugin to notify VPN agent.""" |
| 28 |
|
| 29 |
def __init__(self, topic=topics.IPSEC_AGENT_TOPIC): |
| 30 |
target = oslo_messaging.Target(topic=topic, version='1.0') |
| 31 |
self.client = n_rpc.get_client(target) |
| 32 |
|
| 33 |
def agent_updated(self, context, admin_state_up, host): |
| 34 |
cctxt = self.client.prepare(server=host) |
| 35 |
cctxt.cast(context, 'agent_updated', |
| 36 |
payload={'admin_state_up': admin_state_up}) |
| 37 |
|
| 38 |
def vpnservice_removed_from_agent(self, context, router_id, host): |
| 39 |
"""Notify agent about removed VPN service(s) of a router.""" |
| 40 |
cctxt = self.client.prepare(server=host) |
| 41 |
cctxt.cast(context, 'vpnservice_removed_from_agent', |
| 42 |
router_id=router_id) |
| 43 |
|
| 44 |
def vpnservice_added_to_agent(self, context, router_ids, host): |
| 45 |
"""Notify agent about added VPN service(s) of router(s).""" |
| 46 |
# need to use call here as we want to be sure agent received |
| 47 |
# notification and router will not be "lost". However using call() |
| 48 |
# itself is not a guarantee, calling code should handle exceptions and |
| 49 |
# retry |
| 50 |
cctxt = self.client.prepare(server=host) |
| 51 |
call = ag_utils.retry(cctxt.call, AGENT_NOTIFY_MAX_ATTEMPTS) |
| 52 |
call(context, 'vpnservice_added_to_agent', router_ids=router_ids) |