3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17 from cmdatahandlers.api import utils
19 class Config(config.Config):
20 def __init__(self, confman):
21 super(Config, self).__init__(confman)
22 self.ROOT = 'cloud.network_profiles'
23 self.DOMAIN = 'network_profiles'
30 self._validate_network_profiles()
32 def _validate_network_profiles(self):
33 profiles = self.get_network_profiles()
34 utils.validate_list_items_unique(profiles)
35 for profile in profiles:
36 self._validate_network_profile(profile)
38 def _validate_network_profile(self, profile):
39 bondinginterfaces = None
41 bondinginterfaces = self.get_profile_bonding_interfaces(profile)
42 except configerror.ConfigError:
46 utils.validate_list_items_unique(bondinginterfaces)
48 for bond in bondinginterfaces:
49 bondedinterfaces = self.get_profile_bonded_interfaces(profile, bond)
50 utils.validate_list_items_unique(bondedinterfaces)
51 if len(bondedinterfaces) < 2:
52 raise configerror.ConfigError('Number of bonded interfaces should be at least 2 in %s' % bond)
54 mappedinterfaces = self.get_profile_network_mapped_interfaces(profile)
56 utils.validate_list_items_unique(mappedinterfaces)
58 netconf = self.confman.get_networking_config_handler()
59 validnetworks = netconf.get_networks()
60 for interface in mappedinterfaces:
61 networks = self.get_profile_interface_mapped_networks(profile, interface)
62 utils.validate_list_items_unique(networks)
63 for network in networks:
64 if network not in validnetworks:
65 raise configerror.ConfigError('Network %s is not valid' % network)
67 def is_valid_profile(self, profile):
69 Check if given profile exists
75 True if given profile exists
78 ConfigError in-case of an error
81 profiles = self.get_network_profiles()
82 if profile not in profiles:
83 raise configerror.ConfigError('Invalid profile name %s' % profile)
85 def get_network_profiles(self):
87 Get the network profiles list
90 A list of network profile(s) names
93 ConfigError in-case of an error
96 return self.config[self.ROOT].keys()
98 def get_profile_linux_bonding_options(self, profile):
100 Get the linux bonding options of a profile
106 The linux bonding options
109 ConfigError in-case of an error
111 self.is_valid_profile(profile)
113 if 'linux_bonding_options' not in self.config[self.ROOT][profile]:
114 raise configerror.ConfigError('profile %s has no linux bonding options' % profile)
116 return self.config[self.ROOT][profile]['linux_bonding_options']
118 def get_profile_bonding_interfaces(self, profile):
120 Get the bonding interfaces in a profile
126 A list of bonding interfaces names
129 ConfigError in-case of an error
131 self.is_valid_profile(profile)
133 if 'bonding_interfaces' not in self.config[self.ROOT][profile]:
134 raise configerror.ConfigError('Profile %s has no bonding interfaces' % profile)
136 return self.config[self.ROOT][profile]['bonding_interfaces'].keys()
138 def get_profile_bonded_interfaces(self, profile, bond):
140 Get the bonded interfaces in bond interface
143 The name of the profile
144 The name of the bond interface
147 A list of bonded interfaces names
150 ConfigError in-case of an error
153 bondinterfaces = self.get_profile_bonding_interfaces(profile)
154 if bond not in bondinterfaces:
155 raise configerror.ConfigError('Invalid bond interface name %s in profile %s' % (bond, profile))
157 return self.config[self.ROOT][profile]['bonding_interfaces'][bond]
159 def get_profile_network_mapped_interfaces(self, profile):
161 Get the network mapped interfaces
167 A list of network mapped interfaces
170 ConfigError in-case of an error
172 self.is_valid_profile(profile)
174 if 'interface_net_mapping' not in self.config[self.ROOT][profile]:
175 raise configerror.ConfigError('Profile %s has now interface to network mapping' % profile)
177 return self.config[self.ROOT][profile]['interface_net_mapping'].keys()
179 def get_profile_interface_mapped_networks(self, profile, interface):
181 Get the networks mapped to a specific interface
188 A list of network names
191 ConfigError in-case of an error
193 self.is_valid_profile(profile)
194 mappedinterfaces = self.get_profile_network_mapped_interfaces(profile)
195 if interface not in mappedinterfaces:
196 raise configerror.ConfigError('Interface %s is not valid for profile %s' % (interface, profile))
198 return self.config[self.ROOT][profile]['interface_net_mapping'][interface]
200 def get_profile_provider_network_interfaces(self, profile):
202 Get the list of provider network interfaces
208 A sorted list of network interface names
211 ConfigError in-case of an error
213 self.is_valid_profile(profile)
214 if 'provider_network_interfaces' not in self.config[self.ROOT][profile]:
215 raise configerror.ConfigError('Profile %s has no provider network interfaces' % profile)
217 return sorted(self.config[self.ROOT][profile]['provider_network_interfaces'].keys())
219 def _get_profile_provider_network_interface_dict(self, profile, interface):
220 self.is_valid_profile(profile)
221 interfaces = self.get_profile_provider_network_interfaces(profile)
222 if interface not in interfaces:
223 raise configerror.ConfigError('Profile %s has no provider interface with name %s' % (profile, interface))
225 return self.config[self.ROOT][profile]['provider_network_interfaces'][interface]
227 def get_profile_provider_network_interface_type(self, profile, interface):
229 Get the type of a provider network interface
236 The type of the network interface
239 ConfigError in-case of an error
241 iface_dict = self._get_profile_provider_network_interface_dict(profile, interface)
242 if 'type' not in iface_dict:
243 raise configerror.ConfigError('Provider network interface %s in profile %s does not have a type!' % (interface, profile))
245 return iface_dict['type']
247 def get_profile_provider_interface_networks(self, profile, interface):
249 Get provider networks for the interface
256 A list of provider network names
259 ConfigError in-case of an error
261 iface_dict = self._get_profile_provider_network_interface_dict(profile, interface)
262 if 'provider_networks' not in iface_dict:
263 raise configerror.ConfigError('Profile %s has no provider networks for interface %s' % (profile, interface))
265 return iface_dict['provider_networks']
267 def get_profile_sriov_provider_networks(self, profile):
269 Get SR-IOV provider networks
275 A list of SR-IOV provider network names
278 ConfigError in-case of an error
280 self.is_valid_profile(profile)
281 if 'sriov_provider_networks' not in self.config[self.ROOT][profile]:
282 raise configerror.ConfigError('Profile %s has no SR-IOV provider networks' % profile)
284 return self.config[self.ROOT][profile]['sriov_provider_networks'].keys()
286 def get_profile_sriov_network_interfaces(self, profile, network):
288 Get SR-IOV provider network interfaces
292 The SR-IOV network name
295 A list of SR-IOV provider network interface names
298 ConfigError in-case of an error
300 if network not in self.get_profile_sriov_provider_networks(profile):
301 raise configerror.ConfigError('Profile %s has no SR-IOV provider network %s' % (profile, network))
303 if 'interfaces' not in self.config[self.ROOT][profile]['sriov_provider_networks'][network]:
304 raise configerror.ConfigError('Profile %s has no SR-IOV provider network interfaces for the network %s' % (profile, network))
306 return self.config[self.ROOT][profile]['sriov_provider_networks'][network]['interfaces']