Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / plugins / ml2 / drivers / type_flat.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_db import exception as db_exc
18 from oslo_log import log
19 import six
20 import sqlalchemy as sa
21
22 from neutron._i18n import _, _LI, _LW
23 from neutron.common import exceptions as exc
24 from neutron.db import model_base
25 from neutron.plugins.common import constants as p_const
26 from neutron.plugins.ml2 import driver_api as api
27 from neutron.plugins.ml2.drivers import helpers
28
29 LOG = log.getLogger(__name__)
30
31 flat_opts = [
32     cfg.ListOpt('flat_networks',
33                 default='*',
34                 help=_("List of physical_network names with which flat "
35                        "networks can be created. Use default '*' to allow "
36                        "flat networks with arbitrary physical_network names. "
37                        "Use an empty list to disable flat networks."))
38 ]
39
40 cfg.CONF.register_opts(flat_opts, "ml2_type_flat")
41
42
43 class FlatAllocation(model_base.BASEV2):
44     """Represent persistent allocation state of a physical network.
45
46     If a record exists for a physical network, then that physical
47     network has been allocated as a flat network.
48     """
49
50     __tablename__ = 'ml2_flat_allocations'
51
52     physical_network = sa.Column(sa.String(64), nullable=False,
53                                  primary_key=True)
54
55
56 class FlatTypeDriver(helpers.BaseTypeDriver):
57     """Manage state for flat networks with ML2.
58
59     The FlatTypeDriver implements the 'flat' network_type. Flat
60     network segments provide connectivity between VMs and other
61     devices using any connected IEEE 802.1D conformant
62     physical_network, without the use of VLAN tags, tunneling, or
63     other segmentation mechanisms. Therefore at most one flat network
64     segment can exist on each available physical_network.
65     """
66
67     def __init__(self):
68         super(FlatTypeDriver, self).__init__()
69         self._parse_networks(cfg.CONF.ml2_type_flat.flat_networks)
70
71     def _parse_networks(self, entries):
72         self.flat_networks = entries
73         if '*' in self.flat_networks:
74             LOG.info(_LI("Arbitrary flat physical_network names allowed"))
75             self.flat_networks = None
76         elif not self.flat_networks:
77             LOG.info(_LI("Flat networks are disabled"))
78         else:
79             LOG.info(_LI("Allowable flat physical_network names: %s"),
80                      self.flat_networks)
81
82     def get_type(self):
83         return p_const.TYPE_FLAT
84
85     def initialize(self):
86         LOG.info(_LI("ML2 FlatTypeDriver initialization complete"))
87
88     def is_partial_segment(self, segment):
89         return False
90
91     def validate_provider_segment(self, segment):
92         physical_network = segment.get(api.PHYSICAL_NETWORK)
93         if not physical_network:
94             msg = _("physical_network required for flat provider network")
95             raise exc.InvalidInput(error_message=msg)
96         if self.flat_networks is not None and not self.flat_networks:
97             msg = _("Flat provider networks are disabled")
98             raise exc.InvalidInput(error_message=msg)
99         if self.flat_networks and physical_network not in self.flat_networks:
100             msg = (_("physical_network '%s' unknown for flat provider network")
101                    % physical_network)
102             raise exc.InvalidInput(error_message=msg)
103
104         for key, value in six.iteritems(segment):
105             if value and key not in [api.NETWORK_TYPE,
106                                      api.PHYSICAL_NETWORK]:
107                 msg = _("%s prohibited for flat provider network") % key
108                 raise exc.InvalidInput(error_message=msg)
109
110     def reserve_provider_segment(self, session, segment):
111         physical_network = segment[api.PHYSICAL_NETWORK]
112         with session.begin(subtransactions=True):
113             try:
114                 LOG.debug("Reserving flat network on physical "
115                           "network %s", physical_network)
116                 alloc = FlatAllocation(physical_network=physical_network)
117                 alloc.save(session)
118             except db_exc.DBDuplicateEntry:
119                 raise exc.FlatNetworkInUse(
120                     physical_network=physical_network)
121             segment[api.MTU] = self.get_mtu(alloc.physical_network)
122         return segment
123
124     def allocate_tenant_segment(self, session):
125         # Tenant flat networks are not supported.
126         return
127
128     def release_segment(self, session, segment):
129         physical_network = segment[api.PHYSICAL_NETWORK]
130         with session.begin(subtransactions=True):
131             count = (session.query(FlatAllocation).
132                      filter_by(physical_network=physical_network).
133                      delete())
134         if count:
135             LOG.debug("Releasing flat network on physical network %s",
136                       physical_network)
137         else:
138             LOG.warning(_LW("No flat network found on physical network %s"),
139                         physical_network)
140
141     def get_mtu(self, physical_network):
142         seg_mtu = super(FlatTypeDriver, self).get_mtu()
143         mtu = []
144         if seg_mtu > 0:
145             mtu.append(seg_mtu)
146         if physical_network in self.physnet_mtus:
147             mtu.append(int(self.physnet_mtus[physical_network]))
148         return min(mtu) if mtu else 0