1 # Copyright (c) 2013 OpenStack Foundation.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
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
27 class SegmentsSetInConjunctionWithProviders(nexception.InvalidInput):
28 message = _("Segments and provider values cannot both be set.")
31 class SegmentsContainDuplicateEntry(nexception.InvalidInput):
32 message = _("Duplicate segment entry in request.")
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)
41 segment[pnet.SEGMENTATION_ID] = attr.convert_to_int(
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)
53 def check_duplicate_segments(segments, is_partial_func=None):
54 """Helper function checking duplicate segments.
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
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()
69 attr.validators['type:convert_segments'] = (
70 _convert_and_validate_segments)
73 EXTENDED_ATTRIBUTES_2_0 = {
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,
85 class Multiprovidernet(extensions.ExtensionDescriptor):
86 """Extension class supporting multiple provider networks.
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.
94 With admin rights, network dictionaries returned will also include
100 return "Multi Provider Network"
104 return "multi-provider"
107 def get_description(cls):
108 return ("Expose mapping of virtual networks to multiple physical "
112 def get_updated(cls):
113 return "2013-06-27T10:00:00-00:00"
115 def get_extended_resources(self, version):
117 return EXTENDED_ATTRIBUTES_2_0