558d6da137f714d8c94b0db5cde6a9f5f047660e
[openstack-build/neutron-build.git] / neutron / services / l3_router / l3_router_plugin.py
1 # Copyright (c) 2013 OpenStack Foundation.
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 oslo_config import cfg
17 from oslo_log import helpers as log_helpers
18 from oslo_utils import importutils
19
20 from neutron.api.rpc.agentnotifiers import l3_rpc_agent_api
21 from neutron.api.rpc.handlers import l3_rpc
22 from neutron.common import constants as n_const
23 from neutron.common import rpc as n_rpc
24 from neutron.common import topics
25 from neutron.db import common_db_mixin
26 from neutron.db import extraroute_db
27 from neutron.db import l3_db
28 from neutron.db import l3_dvrscheduler_db
29 from neutron.db import l3_gwmode_db
30 from neutron.db import l3_hamode_db
31 from neutron.db import l3_hascheduler_db
32 from neutron.plugins.common import constants
33 from neutron.quota import resource_registry
34 from neutron.services import service_base
35
36
37 class L3RouterPlugin(service_base.ServicePluginBase,
38                      common_db_mixin.CommonDbMixin,
39                      extraroute_db.ExtraRoute_db_mixin,
40                      l3_hamode_db.L3_HA_NAT_db_mixin,
41                      l3_gwmode_db.L3_NAT_db_mixin,
42                      l3_dvrscheduler_db.L3_DVRsch_db_mixin,
43                      l3_hascheduler_db.L3_HA_scheduler_db_mixin):
44
45     """Implementation of the Neutron L3 Router Service Plugin.
46
47     This class implements a L3 service plugin that provides
48     router and floatingip resources and manages associated
49     request/response.
50     All DB related work is implemented in classes
51     l3_db.L3_NAT_db_mixin, l3_hamode_db.L3_HA_NAT_db_mixin,
52     l3_dvr_db.L3_NAT_with_dvr_db_mixin, and extraroute_db.ExtraRoute_db_mixin.
53     """
54     supported_extension_aliases = ["dvr", "router", "ext-gw-mode",
55                                    "extraroute", "l3_agent_scheduler",
56                                    "l3-ha", "router_availability_zone"]
57
58     @resource_registry.tracked_resources(router=l3_db.Router,
59                                          floatingip=l3_db.FloatingIP)
60     def __init__(self):
61         self.router_scheduler = importutils.import_object(
62             cfg.CONF.router_scheduler_driver)
63         self.start_periodic_l3_agent_status_check()
64         super(L3RouterPlugin, self).__init__()
65         if 'dvr' in self.supported_extension_aliases:
66             l3_dvrscheduler_db.subscribe()
67         l3_db.subscribe()
68         self.start_rpc_listeners()
69
70     @log_helpers.log_method_call
71     def start_rpc_listeners(self):
72         # RPC support
73         self.topic = topics.L3PLUGIN
74         self.conn = n_rpc.create_connection()
75         self.agent_notifiers.update(
76             {n_const.AGENT_TYPE_L3: l3_rpc_agent_api.L3AgentNotifyAPI()})
77         self.endpoints = [l3_rpc.L3RpcCallback()]
78         self.conn.create_consumer(self.topic, self.endpoints,
79                                   fanout=False)
80         return self.conn.consume_in_threads()
81
82     def get_plugin_type(self):
83         return constants.L3_ROUTER_NAT
84
85     def get_plugin_description(self):
86         """returns string description of the plugin."""
87         return ("L3 Router Service Plugin for basic L3 forwarding"
88                 " between (L2) Neutron networks and access to external"
89                 " networks via a NAT gateway.")
90
91     def create_floatingip(self, context, floatingip):
92         """Create floating IP.
93
94         :param context: Neutron request context
95         :param floatingip: data for the floating IP being created
96         :returns: A floating IP object on success
97
98         As the l3 router plugin asynchronously creates floating IPs
99         leveraging the l3 agent, the initial status for the floating
100         IP object will be DOWN.
101         """
102         return super(L3RouterPlugin, self).create_floatingip(
103             context, floatingip,
104             initial_status=n_const.FLOATINGIP_STATUS_DOWN)