Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / extensions / multiprovidernet.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 import webob.exc
17
18 from neutron._i18n import _
19 from neutron.api import extensions
20 from neutron.api.v2 import attributes as attr
21 from neutron.common import exceptions as nexception
22 from neutron.extensions import providernet as pnet
23
24 SEGMENTS = 'segments'
25
26
27 class SegmentsSetInConjunctionWithProviders(nexception.InvalidInput):
28     message = _("Segments and provider values cannot both be set.")
29
30
31 class SegmentsContainDuplicateEntry(nexception.InvalidInput):
32     message = _("Duplicate segment entry in request.")
33
34
35 def _convert_and_validate_segments(segments, valid_values=None):
36     for segment in segments:
37         segment.setdefault(pnet.NETWORK_TYPE, attr.ATTR_NOT_SPECIFIED)
38         segment.setdefault(pnet.PHYSICAL_NETWORK, attr.ATTR_NOT_SPECIFIED)
39         segmentation_id = segment.get(pnet.SEGMENTATION_ID)
40         if segmentation_id:
41             segment[pnet.SEGMENTATION_ID] = attr.convert_to_int(
42                 segmentation_id)
43         else:
44             segment[pnet.SEGMENTATION_ID] = attr.ATTR_NOT_SPECIFIED
45         if len(segment.keys()) != 3:
46             msg = (_("Unrecognized attribute(s) '%s'") %
47                    ', '.join(set(segment.keys()) -
48                              set([pnet.NETWORK_TYPE, pnet.PHYSICAL_NETWORK,
49                                   pnet.SEGMENTATION_ID])))
50             raise webob.exc.HTTPBadRequest(msg)
51
52
53 def check_duplicate_segments(segments, is_partial_func=None):
54     """Helper function checking duplicate segments.
55
56     If is_partial_funcs is specified and not None, then
57     SegmentsContainDuplicateEntry is raised if two segments are identical and
58     non partially defined (is_partial_func(segment) == False).
59     Otherwise SegmentsContainDuplicateEntry is raised if two segment are
60     identical.
61     """
62     if is_partial_func is not None:
63         segments = [s for s in segments if not is_partial_func(s)]
64     fully_specifieds = [tuple(sorted(s.items())) for s in segments]
65     if len(set(fully_specifieds)) != len(fully_specifieds):
66         raise SegmentsContainDuplicateEntry()
67
68
69 attr.validators['type:convert_segments'] = (
70     _convert_and_validate_segments)
71
72
73 EXTENDED_ATTRIBUTES_2_0 = {
74     'networks': {
75         SEGMENTS: {'allow_post': True, 'allow_put': True,
76                    'validate': {'type:convert_segments': None},
77                    'convert_list_to': attr.convert_kvp_list_to_dict,
78                    'default': attr.ATTR_NOT_SPECIFIED,
79                    'enforce_policy': True,
80                    'is_visible': True},
81     }
82 }
83
84
85 class Multiprovidernet(extensions.ExtensionDescriptor):
86     """Extension class supporting multiple provider networks.
87
88     This class is used by neutron's extension framework to make
89     metadata about the multiple provider network extension available to
90     clients. No new resources are defined by this extension. Instead,
91     the existing network resource's request and response messages are
92     extended with 'segments' attribute.
93
94     With admin rights, network dictionaries returned will also include
95     'segments' attribute.
96     """
97
98     @classmethod
99     def get_name(cls):
100         return "Multi Provider Network"
101
102     @classmethod
103     def get_alias(cls):
104         return "multi-provider"
105
106     @classmethod
107     def get_description(cls):
108         return ("Expose mapping of virtual networks to multiple physical "
109                 "networks")
110
111     @classmethod
112     def get_updated(cls):
113         return "2013-06-27T10:00:00-00:00"
114
115     def get_extended_resources(self, version):
116         if version == "2.0":
117             return EXTENDED_ATTRIBUTES_2_0
118         else:
119             return {}