Debian Codegraph @

networking_l2gw/services/l2gateway/common/l2gw_validators.py

raw
1 # Copyright 2015 OpenStack Foundation
2 # Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
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 neutron_lib.api import validators
17 from neutron_lib import exceptions
18
19 from networking_l2gw._i18n import _
20 from networking_l2gw.services.l2gateway.common import constants
21
22 ALLOWED_CONNECTION_ATTRIBUTES = {constants.NETWORK_ID,
23 constants.SEG_ID,
24 constants.L2GATEWAY_ID}
25
26
27 def validate_gwdevice_list(data, valid_values=None):
28 """Validate the list of devices."""
29 if not data:
30 # Devices must be provided
31 msg = _("Cannot create a gateway with an empty device list")
32 return msg
33 try:
34 for device in data:
35 interface_data = device.get(constants.IFACE_NAME_ATTR)
36 device_name = device.get(constants.DEVICE_ID_ATTR)
37 if not device_name:
38 msg = _("Cannot create a gateway with an empty device_name")
39 return msg
40 if not interface_data:
41 msg = _("Cannot create a gateway with an empty interfaces")
42 return msg
43 if not isinstance(interface_data, list):
44 msg = _("interfaces format is not a type list of dicts")
45 return msg
46 for int_dict in interface_data:
47 if not isinstance(int_dict, dict):
48 msg = _("interfaces format is not a type dict")
49 return msg
50 err_msg = validators.validate_dict(int_dict, None)
51 if not int_dict.get('name'):
52 msg = _("Cannot create a gateway with an empty "
53 "interface name")
54 return msg
55 if constants.SEG_ID in int_dict:
56 seg_id_list = int_dict.get(constants.SEG_ID)
57 if seg_id_list and type(seg_id_list) is not list:
58 msg = _("segmentation_id type should be of list type ")
59 return msg
60 if not seg_id_list:
61 msg = _("segmentation_id_list should not be empty")
62 return msg
63 for seg_id in seg_id_list:
64 is_valid_vlan_id(seg_id)
65 if err_msg:
66 return err_msg
67 except TypeError:
68 return (_("%s: provided data are not iterable") %
69 validate_gwdevice_list.__name__)
70
71
72 def validate_network_mapping_list(network_mapping, check_vlan):
73 """Validate network mapping list in connection."""
74 if network_mapping.get('segmentation_id'):
75 if check_vlan:
76 raise exceptions.InvalidInput(
77 error_message=_("default segmentation_id should not be"
78 " provided when segmentation_id is assigned"
79 " during l2gateway creation"))
80 seg_id = network_mapping.get(constants.SEG_ID)
81 is_valid_vlan_id(seg_id)
82
83 if not network_mapping.get('segmentation_id'):
84 if check_vlan is False:
85 raise exceptions.InvalidInput(
86 error_message=_("Segmentation id must be specified in create "
87 "l2gateway connections"))
88 network_id = network_mapping.get(constants.NETWORK_ID)
89 if not network_id:
90 raise exceptions.InvalidInput(
91 error_message=_("A valid network identifier must be specified "
92 "when connecting a network to a network "
93 "gateway. Unable to complete operation"))
94 connection_attrs = set(network_mapping.keys())
95 if not connection_attrs.issubset(ALLOWED_CONNECTION_ATTRIBUTES):
96 raise exceptions.InvalidInput(
97 error_message=(_("Invalid keys found among the ones provided "
98 "in request : %(connection_attrs)s."),
99 connection_attrs))
100 return network_id
101
102
103 def is_valid_vlan_id(seg_id):
104 try:
105 int_seg_id = int(seg_id)
106 except ValueError:
107 msg = _("Segmentation id must be a valid integer")
108 raise exceptions.InvalidInput(error_message=msg)
109 if int_seg_id < 0 or int_seg_id >= 4095:
110 msg = _("Segmentation id is out of range")
111 raise exceptions.InvalidInput(error_message=msg)