Initial commit
[ta/config-manager.git] / cmdatahandlers / src / cmdatahandlers / network_profiles / config.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from cmdatahandlers.api import configerror
16 from cmdatahandlers.api import config
17 from cmdatahandlers.api import utils
18
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'
24
25     def init(self):
26         pass
27
28     def validate(self):
29         self.validate_root()
30         self._validate_network_profiles()
31
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)
37
38     def _validate_network_profile(self, profile):
39         bondinginterfaces = None
40         try:
41             bondinginterfaces = self.get_profile_bonding_interfaces(profile)
42         except configerror.ConfigError:
43             pass
44
45         if bondinginterfaces:
46             utils.validate_list_items_unique(bondinginterfaces)
47
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)
53
54         mappedinterfaces = self.get_profile_network_mapped_interfaces(profile)
55
56         utils.validate_list_items_unique(mappedinterfaces)
57
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)
66
67     def is_valid_profile(self, profile):
68         """
69         Check if given profile exists
70
71         Arguments:
72             The profile name
73
74         Returns:
75             True if given profile exists
76
77         Raises:
78             ConfigError in-case of an error
79         """
80         self.validate_root()
81         profiles = self.get_network_profiles()
82         if profile not in profiles:
83             raise configerror.ConfigError('Invalid profile name %s' % profile)
84
85     def get_network_profiles(self):
86         """
87         Get the network profiles list
88
89         Returns:
90             A list of network profile(s) names
91
92         Raises:
93             ConfigError in-case of an error
94         """
95         self.validate_root()
96         return self.config[self.ROOT].keys()
97
98     def get_profile_linux_bonding_options(self, profile):
99         """
100         Get the linux bonding options of a profile
101
102         Arguments:
103             The profile name
104
105         Returns:
106             The linux bonding options
107
108         Raises:
109             ConfigError in-case of an error
110         """
111         self.is_valid_profile(profile)
112
113         if 'linux_bonding_options' not in self.config[self.ROOT][profile]:
114             raise configerror.ConfigError('profile %s has no linux bonding options' % profile)
115
116         return self.config[self.ROOT][profile]['linux_bonding_options']
117
118     def get_profile_bonding_interfaces(self, profile):
119         """
120         Get the bonding interfaces in a profile
121
122         Arguments:
123             The profile name
124
125         Returns:
126             A list of bonding interfaces names
127
128         Raises:
129             ConfigError in-case of an error
130         """
131         self.is_valid_profile(profile)
132
133         if 'bonding_interfaces' not in self.config[self.ROOT][profile]:
134             raise configerror.ConfigError('Profile %s has no bonding interfaces' % profile)
135
136         return self.config[self.ROOT][profile]['bonding_interfaces'].keys()
137
138     def get_profile_bonded_interfaces(self, profile, bond):
139         """
140         Get the bonded interfaces in bond interface
141
142         Arguments:
143             The name of the profile
144             The name of the bond interface
145
146         Returns:
147             A list of bonded interfaces names
148
149         Raises:
150             ConfigError in-case of an error
151         """
152         self.validate_root()
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))
156
157         return self.config[self.ROOT][profile]['bonding_interfaces'][bond]
158
159     def get_profile_network_mapped_interfaces(self, profile):
160         """
161         Get the network mapped interfaces
162
163         Arguments:
164             The profile name
165
166         Returns:
167             A list of network mapped interfaces
168
169         Raises:
170             ConfigError in-case of an error
171         """
172         self.is_valid_profile(profile)
173
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)
176
177         return self.config[self.ROOT][profile]['interface_net_mapping'].keys()
178
179     def get_profile_interface_mapped_networks(self, profile, interface):
180         """
181         Get the networks mapped to a specific interface
182
183         Arguments:
184             The profile name
185             The interface name
186
187         Returns:
188             A list of network names
189
190         Raises:
191             ConfigError in-case of an error
192         """
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))
197
198         return self.config[self.ROOT][profile]['interface_net_mapping'][interface]
199
200     def get_profile_provider_network_interfaces(self, profile):
201         """
202         Get the list of provider network interfaces
203
204         Arguments:
205             The profile name
206
207         Returns:
208             A sorted list of network interface names
209
210         Raises:
211             ConfigError in-case of an error
212         """
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)
216
217         return sorted(self.config[self.ROOT][profile]['provider_network_interfaces'].keys())
218
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))
224
225         return self.config[self.ROOT][profile]['provider_network_interfaces'][interface]
226
227     def get_profile_provider_network_interface_type(self, profile, interface):
228         """
229         Get the type of a provider network interface
230
231         Arguments:
232             The profile name
233             The interface name
234
235         Returns:
236             The type of the network interface
237
238         Raises:
239             ConfigError in-case of an error
240         """
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))
244
245         return iface_dict['type']
246
247     def get_profile_provider_interface_networks(self, profile, interface):
248         """
249         Get provider networks for the interface
250
251         Arguments:
252             The profile name
253             The interface name
254
255         Returns:
256             A list of provider network names
257
258         Raises:
259             ConfigError in-case of an error
260         """
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))
264
265         return iface_dict['provider_networks']
266
267     def get_profile_sriov_provider_networks(self, profile):
268         """
269         Get SR-IOV provider networks
270
271         Arguments:
272             The profile name
273
274         Returns:
275             A list of SR-IOV provider network names
276
277         Raises:
278             ConfigError in-case of an error
279         """
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)
283
284         return self.config[self.ROOT][profile]['sriov_provider_networks'].keys()
285
286     def get_profile_sriov_network_interfaces(self, profile, network):
287         """
288         Get SR-IOV provider network interfaces
289
290         Arguments:
291             The profile name
292             The SR-IOV network name
293
294         Returns:
295             A list of SR-IOV provider network interface names
296
297         Raises:
298             ConfigError in-case of an error
299         """
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))
302
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))
305
306         return self.config[self.ROOT][profile]['sriov_provider_networks'][network]['interfaces']