Debian Codegraph @

neutron_vpnaas/services/vpn/service_drivers/__init__.py

raw
1 # Copyright 2013, Nachi Ueno, NTT I3, Inc.
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 import abc
17
18 from neutron_lib.plugins import constants
19 from neutron_lib.plugins import directory
20 from neutron_lib import rpc as n_rpc
21 from oslo_log import log as logging
22 import oslo_messaging
23
24 from neutron_vpnaas.services.vpn.service_drivers import driver_validator
25
26 LOG = logging.getLogger(__name__)
27
28
29 class VpnDriver(metaclass=abc.ABCMeta):
30
31 def __init__(self, service_plugin, validator=None):
32 self.service_plugin = service_plugin
33 if validator is None:
34 validator = driver_validator.VpnDriverValidator(self)
35 self.validator = validator
36 self.name = ''
37
38 @property
39 def l3_plugin(self):
40 return directory.get_plugin(constants.L3)
41
42 @property
43 def service_type(self):
44 pass
45
46 @abc.abstractmethod
47 def create_vpnservice(self, context, vpnservice):
48 pass
49
50 @abc.abstractmethod
51 def update_vpnservice(
52 self, context, old_vpnservice, vpnservice):
53 pass
54
55 @abc.abstractmethod
56 def delete_vpnservice(self, context, vpnservice):
57 pass
58
59 @abc.abstractmethod
60 def create_ipsec_site_connection(self, context, ipsec_site_connection):
61 pass
62
63 @abc.abstractmethod
64 def update_ipsec_site_connection(self, context, old_ipsec_site_connection,
65 ipsec_site_connection):
66 pass
67
68 @abc.abstractmethod
69 def delete_ipsec_site_connection(self, context, ipsec_site_connection):
70 pass
71
72
73 class BaseIPsecVpnAgentApi:
74 """Base class for IPSec API to agent."""
75
76 def __init__(self, topic, default_version, driver):
77 self.topic = topic
78 self.driver = driver
79 target = oslo_messaging.Target(topic=topic, version=default_version)
80 self.client = n_rpc.get_client(target)
81
82 def _agent_notification(self, context, method, router_id,
83 version=None, **kwargs):
84 """Notify update for the agent.
85
86 This method will find where is the router, and
87 dispatch notification for the agent.
88 """
89 admin_context = context if context.is_admin else context.elevated()
90 if not version:
91 version = self.target.version
92 l3_agents = self.driver.l3_plugin.get_l3_agents_hosting_routers(
93 admin_context, [router_id],
94 admin_state_up=True,
95 active=True)
96 for l3_agent in l3_agents:
97 LOG.debug('Notify agent at %(topic)s.%(host)s the message '
98 '%(method)s %(args)s',
99 {'topic': self.topic,
100 'host': l3_agent.host,
101 'method': method,
102 'args': kwargs})
103 cctxt = self.client.prepare(server=l3_agent.host, version=version)
104 cctxt.cast(context, method, **kwargs)
105
106 def vpnservice_updated(self, context, router_id, **kwargs):
107 """Send update event of vpnservices."""
108 kwargs['router'] = {'id': router_id}
109 self._agent_notification(context, 'vpnservice_updated', router_id,
110 **kwargs)