Simplify REC network profiles userconfighandler
[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
20 """
21 This plugin is used to handle REC specific network profiles configs. Currently
22 its sole purpuse is to set default type for any provider networks.
23 """
24
25
26 class recnetworkprofileshandler(cmuserconfig.CMUserConfigPlugin):
27     defaul_sriov_net_type = 'caas'
28     defaul_provider_interface_type = 'caas'
29
30     def __init__(self):
31         super(recnetworkprofileshandler, self).__init__()
32
33     def handle(self, confman):
34         try:
35             self._set_default_type_for_provider_networks(confman)
36         except configerror.ConfigError as exp:
37             raise cmerror.CMError(str(exp))
38
39     def _set_default_type_for_provider_networks(self, confman):
40         netprofconf = confman.get_network_profiles_config_handler()
41         network_profiles = netprofconf.get_network_profiles()
42         for profile in network_profiles:
43             try:
44                 self._set_default_provider_network_type(netprofconf, profile)
45                 self._set_default_sriov_provider_network_type(netprofconf, profile)
46             except configerror.ConfigError:
47                 pass
48
49     def _set_default_provider_network_type(self, netprofconf, profile):
50         for interface in netprofconf.get_profile_provider_network_interfaces(profile):
51             netprofconf.set_profile_provider_network_interface_type(
52                     profile, interface, self.defaul_provider_interface_type)
53
54     def _set_default_sriov_provider_network_type(self, netprofconf, profile):
55         for sriov_net in netprofconf.get_profile_sriov_provider_networks(profile):
56             netprofconf.set_profile_sriov_provider_network_type(
57                     profile, sriov_net, self.defaul_sriov_net_type)