Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / agent / l3 / dvr.py
1 # Copyright (c) 2014 Openstack Foundation
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 import weakref
16
17 from neutron.agent.l3 import dvr_fip_ns
18 from neutron.agent.l3 import dvr_snat_ns
19
20
21 # TODO(Carl) Following constants retained to increase SNR during refactoring
22 SNAT_INT_DEV_PREFIX = dvr_snat_ns.SNAT_INT_DEV_PREFIX
23 SNAT_NS_PREFIX = dvr_snat_ns.SNAT_NS_PREFIX
24
25
26 class AgentMixin(object):
27     def __init__(self, host):
28         # dvr data
29         self._fip_namespaces = weakref.WeakValueDictionary()
30         super(AgentMixin, self).__init__(host)
31
32     def get_fip_ns(self, ext_net_id):
33         # TODO(Carl) is this necessary?  Code that this replaced was careful to
34         # convert these to string like this so I preserved that.
35         ext_net_id = str(ext_net_id)
36
37         fip_ns = self._fip_namespaces.get(ext_net_id)
38         if fip_ns and not fip_ns.destroyed:
39             return fip_ns
40
41         fip_ns = dvr_fip_ns.FipNamespace(ext_net_id,
42                                          self.conf,
43                                          self.driver,
44                                          self.use_ipv6)
45         self._fip_namespaces[ext_net_id] = fip_ns
46
47         return fip_ns
48
49     def get_ports_by_subnet(self, subnet_id):
50         return self.plugin_rpc.get_ports_by_subnet(self.context, subnet_id)
51
52     def _update_arp_entry(self, context, payload, action):
53         router_id = payload['router_id']
54         ri = self.router_info.get(router_id)
55         if not ri:
56             return
57
58         arp_table = payload['arp_table']
59         ip = arp_table['ip_address']
60         mac = arp_table['mac_address']
61         subnet_id = arp_table['subnet_id']
62
63         ri._update_arp_entry(ip, mac, subnet_id, action)
64
65     def add_arp_entry(self, context, payload):
66         """Add arp entry into router namespace.  Called from RPC."""
67         self._update_arp_entry(context, payload, 'add')
68
69     def del_arp_entry(self, context, payload):
70         """Delete arp entry from router namespace.  Called from RPC."""
71         self._update_arp_entry(context, payload, 'delete')
72
73     def fipnamespace_delete_on_ext_net(self, context, ext_net_id):
74         """Delete fip namespace after external network removed."""
75         fip_ns = self.get_fip_ns(ext_net_id)
76         if fip_ns.agent_gateway_port and not fip_ns.destroyed:
77             fip_ns.delete()