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
20 class Config(config.Config):
21 def __init__(self, confman):
22 super(Config, self).__init__(confman)
23 self.ROOT = 'cloud.network_profiles'
24 self.DOMAIN = 'network_profiles'
31 self._validate_network_profiles()
33 def _validate_network_profiles(self):
34 profiles = self.get_network_profiles()
35 utils.validate_list_items_unique(profiles)
36 for profile in profiles:
37 self._validate_network_profile(profile)
39 def _validate_network_profile(self, profile):
40 bondinginterfaces = None
42 bondinginterfaces = self.get_profile_bonding_interfaces(profile)
43 except configerror.ConfigError:
47 utils.validate_list_items_unique(bondinginterfaces)
49 for bond in bondinginterfaces:
50 bondedinterfaces = self.get_profile_bonded_interfaces(
52 utils.validate_list_items_unique(bondedinterfaces)
53 if len(bondedinterfaces) < 2:
54 raise configerror.ConfigError(
55 'Number of bonded interfaces should be at least 2 in %s' % bond)
57 mappedinterfaces = self.get_profile_network_mapped_interfaces(profile)
59 utils.validate_list_items_unique(mappedinterfaces)
61 netconf = self.confman.get_networking_config_handler()
62 validnetworks = netconf.get_networks()
63 for interface in mappedinterfaces:
64 networks = self.get_profile_interface_mapped_networks(
66 utils.validate_list_items_unique(networks)
67 for network in networks:
68 if network not in validnetworks:
69 raise configerror.ConfigError(
70 'Network %s is not valid' % network)
72 def is_valid_profile(self, profile):
74 Check if given profile exists
80 True if given profile exists
83 ConfigError in-case of an error
86 profiles = self.get_network_profiles()
87 if profile not in profiles:
88 raise configerror.ConfigError('Invalid profile name %s' % profile)
90 def get_network_profiles(self):
92 Get the network profiles list
95 A list of network profile(s) names
98 ConfigError in-case of an error
101 return self.config[self.ROOT].keys()
103 def get_profile_linux_bonding_options(self, profile):
105 Get the linux bonding options of a profile
111 The linux bonding options
114 ConfigError in-case of an error
116 self.is_valid_profile(profile)
118 if 'linux_bonding_options' not in self.config[self.ROOT][profile]:
119 raise configerror.ConfigError(
120 'profile %s has no linux bonding options' %
123 return self.config[self.ROOT][profile]['linux_bonding_options']
125 def get_profile_bonding_interfaces(self, profile):
127 Get the bonding interfaces in a profile
133 A list of bonding interfaces names
136 ConfigError in-case of an error
138 self.is_valid_profile(profile)
140 if 'bonding_interfaces' not in self.config[self.ROOT][profile]:
141 raise configerror.ConfigError(
142 'Profile %s has no bonding interfaces' %
145 return self.config[self.ROOT][profile]['bonding_interfaces'].keys()
147 def get_profile_bonded_interfaces(self, profile, bond):
149 Get the bonded interfaces in bond interface
152 The name of the profile
153 The name of the bond interface
156 A list of bonded interfaces names
159 ConfigError in-case of an error
162 bondinterfaces = self.get_profile_bonding_interfaces(profile)
163 if bond not in bondinterfaces:
164 raise configerror.ConfigError(
165 'Invalid bond interface name %s in profile %s' %
168 return self.config[self.ROOT][profile]['bonding_interfaces'][bond]
170 def get_profile_network_mapped_interfaces(self, profile):
172 Get the network mapped interfaces
178 A list of network mapped interfaces
181 ConfigError in-case of an error
183 self.is_valid_profile(profile)
185 if 'interface_net_mapping' not in self.config[self.ROOT][profile]:
186 raise configerror.ConfigError(
187 'Profile %s has now interface to network mapping' %
190 return self.config[self.ROOT][profile]['interface_net_mapping'].keys()
192 def get_profile_interface_mapped_networks(self, profile, interface):
194 Get the networks mapped to a specific interface
201 A list of network names
204 ConfigError in-case of an error
206 self.is_valid_profile(profile)
207 mappedinterfaces = self.get_profile_network_mapped_interfaces(profile)
208 if interface not in mappedinterfaces:
209 raise configerror.ConfigError(
210 'Interface %s is not valid for profile %s' %
211 (interface, profile))
213 return self.config[self.ROOT][profile]['interface_net_mapping'][interface]
215 def get_profile_provider_network_interfaces(self, profile):
217 Get the list of provider network interfaces
223 A sorted list of network interface names
226 ConfigError in-case of an error
228 self.is_valid_profile(profile)
229 if 'provider_network_interfaces' not in self.config[self.ROOT][profile]:
230 raise configerror.ConfigError(
231 'Profile %s has no provider network interfaces' %
234 return sorted(self.config[self.ROOT][profile]
235 ['provider_network_interfaces'].keys())
237 def _get_profile_provider_network_interface_dict(self, profile, interface):
238 self.is_valid_profile(profile)
239 interfaces = self.get_profile_provider_network_interfaces(profile)
240 if interface not in interfaces:
241 raise configerror.ConfigError(
242 'Profile %s has no provider interface with name %s' %
243 (profile, interface))
245 return self.config[self.ROOT][profile]['provider_network_interfaces'][interface]
247 def get_profile_provider_network_interface_type(self, profile, interface):
249 Get the type of a provider network interface
256 The type of the network interface
259 ConfigError in-case of an error
261 iface_dict = self._get_profile_provider_network_interface_dict(
263 if 'type' not in iface_dict:
264 raise configerror.ConfigError(
265 'Provider network interface %s in profile %s does not have a type!' %
266 (interface, profile))
268 return iface_dict['type']
270 def set_profile_provider_network_interface_type(self, profile, interface, type):
272 Set provider network type
277 The type of provider network
280 ConfigError in-case of an error
282 iface_dict = self._get_profile_provider_network_interface_dict(
284 if not isinstance(type, basestring):
285 raise configerror.ConfigError(
286 'Profile %s has invalid provider network type for interface %s: %s' %
287 (profile, interface, type))
289 iface_dict['type'] = type
291 def is_provider_network_type_caas(self, profile, interface):
293 Is provider network type caas
300 True if provider network is for CaaS
303 ConfigError in-case of an error
305 return self.get_profile_provider_network_interface_type(profile, interface) == 'caas'
307 def get_profile_provider_interface_networks(self, profile, interface):
309 Get provider networks for the interface
316 A list of provider network names
319 ConfigError in-case of an error
321 iface_dict = self._get_profile_provider_network_interface_dict(
323 if 'provider_networks' not in iface_dict:
324 raise configerror.ConfigError(
325 'Profile %s has no provider networks for interface %s' %
326 (profile, interface))
328 return iface_dict['provider_networks']
330 def get_profile_sriov_provider_networks(self, profile):
332 Get SR-IOV provider networks
338 A list of SR-IOV provider network names
341 ConfigError in-case of an error
343 self.is_valid_profile(profile)
344 if 'sriov_provider_networks' not in self.config[self.ROOT][profile]:
345 raise configerror.ConfigError(
346 'Profile %s has no SR-IOV provider networks' %
349 return self.config[self.ROOT][profile]['sriov_provider_networks'].keys()
351 def get_profile_sriov_network_interfaces(self, profile, network):
353 Get SR-IOV provider network interfaces
357 The SR-IOV network name
360 A list of SR-IOV provider network interface names
363 ConfigError in-case of an error
365 self._validate_network_is_sriov_provider_network(profile, network)
367 if 'interfaces' not in self.config[self.ROOT][profile]['sriov_provider_networks'][network]:
368 raise configerror.ConfigError(
369 'Profile %s has no SR-IOV provider network interfaces for the network %s' %
372 return self.config[self.ROOT][profile]['sriov_provider_networks'][network]['interfaces']
374 def set_profile_sriov_provider_network_type(self, profile, network, type):
376 Set SR-IOV provider network type
380 The SR-IOV network name
381 The type of SR-IOV network
384 ConfigError in-case of an error
386 self._validate_network_is_sriov_provider_network(profile, network)
388 if not isinstance(type, basestring):
389 raise configerror.ConfigError(
390 'Profile %s has invalid SR-IOV provider network type for the network %s: %s' %
391 (profile, network, type))
393 self.config[self.ROOT][profile]['sriov_provider_networks'][network]['type'] = type
395 def is_sriov_network_trusted(self, profile, network):
397 Is SR-IOV provider network trusted (VF parameter)
401 The SR-IOV network name
404 True if VFs should be configured as trusted in this network
407 ConfigError in-case of an error
409 self._validate_network_is_sriov_provider_network(profile, network)
411 return self.config[self.ROOT][profile]['sriov_provider_networks'][network].get('trusted') is True
413 def is_sriov_network_type_caas(self, profile, network):
415 Is SR-IOV network type caas
419 The SR-IOV network name
422 True if SR-IOV network is for CaaS
425 ConfigError in-case of an error
427 self._validate_network_is_sriov_provider_network(profile, network)
429 return self.config[self.ROOT][profile]['sriov_provider_networks'][network].get('type') == 'caas'
431 def _validate_network_is_sriov_provider_network(self, profile, network):
432 if network not in self.get_profile_sriov_provider_networks(profile):
433 raise configerror.ConfigError(
434 'Profile %s has no SR-IOV provider network %s' %
437 def get_profile(self, name):
439 Get the network profile data
445 A dict of network profile data
448 self.is_valid_profile(name)
449 return self.config[self.ROOT][name]
452 """ Dump all network profiles data. """
455 return self.config[self.ROOT]
457 def add_profile(self, name, profile):
458 """ Add network profile.
462 name : str, profile name.
463 profile : dict, profile data
466 if name in self.config[self.ROOT]:
467 raise configerror.ConfigError('Profile %s already exists' % name)
471 self.config[self.ROOT].update(data)
473 def delete_profile(self, name):
474 """ Delete network profile.
478 name : str, profile name.
481 self.is_valid_profile(name)
482 self.config[self.ROOT].pop(name)