Add CaaS networks related validations
[ta/cm-plugins.git] / recuserconfighandlers / recnetworkprofileshandler / recnetworkprofileshandler.py
1 #! /usr/bin/python
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain 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,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from cmframework.apis import cmuserconfig
17 from cmframework.apis import cmerror
18 from cmdatahandlers.api import configerror
19 from cmdatahandlers.network_profiles.config import config as net_prof_config
20
21 """
22 This plugin is used to handle REC specific network profiles configs. Currently
23 its sole purpuse is to set default type for any provider networks.
24 """
25
26
27 class recnetworkprofileshandler(cmuserconfig.CMUserConfigPlugin):
28     defaul_sriov_net_type = 'caas'
29     defaul_provider_interface_type = 'caas'
30
31     def __init__(self):
32         super(recnetworkprofileshandler, self).__init__()
33
34     def handle(self, confman):
35         try:
36             self._set_default_type_for_provider_networks(confman)
37         except configerror.ConfigError as exp:
38             raise cmerror.CMError(str(exp))
39
40     def _set_default_type_for_provider_networks(self, confman):
41         netprofconf = confman.get_network_profiles_config_handler()
42         network_profiles = netprofconf.get_network_profiles()
43         for profile in network_profiles:
44             try:
45                 self._set_default_provider_network_type(netprofconf, profile)
46                 self._set_default_sriov_provider_network_type(netprofconf, profile)
47             except (net_prof_config.MissingProviderNetworkInterfaces, net_prof_config.MissingSriovProviderNetworks):
48                 pass
49
50     def _set_default_provider_network_type(self, netprofconf, profile):
51         for interface in netprofconf.get_profile_provider_network_interfaces(profile):
52             try:
53                 netprofconf.get_profile_provider_network_interface_type(
54                     profile, interface)
55             except net_prof_config.MissingProviderNetworkInterfaceType:
56                 netprofconf.set_profile_provider_network_interface_type(
57                         profile, interface, self.defaul_provider_interface_type)
58
59     def _set_default_sriov_provider_network_type(self, netprofconf, profile):
60         for sriov_net in netprofconf.get_profile_sriov_provider_networks(profile):
61             if not netprofconf.get_profile_sriov_provider_network_type(
62                     profile, sriov_net):
63                 netprofconf.set_profile_sriov_provider_network_type(
64                         profile, sriov_net, self.defaul_sriov_net_type)