]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/policymanager.py
vrf: finish support for vrf-table auto
[mirror_ifupdown2.git] / ifupdown / policymanager.py
1 #!/usr/bin/python
2 #
3 # Copyright 2015 Cumulus Networks, Inc. All rights reserved.
4 #
5 #
6 '''
7 The PolicyManager should be subclassed by addon modules
8 to read a JSON policy config file that is later used to
9 set defaults:
10
11 Initialize: This module defines a list of config file location based
12 on module. There are defined in the __init__(): All the
13 addon modules need to do is import the policymanager module.
14
15 import ifupdown.policymanager as policymanager
16
17
18 Provides: an API to retrieve link attributes based on addon module name,
19 interface name, and attribute.
20
21 The ifupdown.policymanager module provides a global object policymanager_api
22 that can be called like so:
23
24 speed_default = policymanager.policymanager_api.get_default(
25 module_name='ethtool',
26 ifname=ifaceobj.name,
27 attr='link-speed'
28 )
29 '''
30
31 import json
32 import logging
33 import glob
34
35 class policymanager():
36 def __init__(self):
37 # we should check for these files in order
38 # so that customers can override the /var/lib file settings
39 self.logger = logging.getLogger('ifupdown.' +
40 self.__class__.__name__)
41
42 # we grab the json files from a known location and make sure that
43 # the defaults_policy is checked first
44 user_files = glob.glob('/etc/network/ifupdown2/policy.d/*.json')
45 # grab the default module files
46 default_files = glob.glob('/var/lib/ifupdown2/policy.d/*.json')
47 # keep an array of defaults indexed by module name
48 self.system_policy_array = {}
49 for filename in default_files:
50 system_array = {}
51 try:
52 fd = open(filename,'r')
53 system_array = json.load(fd)
54 self.logger.debug('reading %s system policy defaults config' \
55 % filename)
56 except Exception, e:
57 self.logger.debug('could not read %s system policy defaults config' \
58 % filename)
59 self.logger.debug(' exception is %s' % str(e))
60 for module in system_array.keys():
61 if self.system_policy_array.has_key(module):
62 self.logger.debug('warning: overwriting system module %s from file %s' \
63 % (module,filename))
64 self.system_policy_array[module] = system_array[module]
65
66 # take care of user defined policy defaults
67 self.user_policy_array = {}
68 for filename in user_files:
69 user_array = {}
70 try:
71 fd = open(filename,'r')
72 user_array = json.load(fd)
73 self.logger.debug('reading %s policy user defaults config' \
74 % filename)
75 except Exception, e:
76 self.logger.debug('could not read %s user policy defaults config' \
77 % filename)
78 self.logger.debug(' exception is %s' % str(e))
79 # customer added module attributes
80 for module in user_array.keys():
81 if self.system_policy_array.has_key(module):
82 # warn user that we are overriding the system module setting
83 self.logger.debug('warning: overwriting system with user module %s from file %s' \
84 % (module,filename))
85 self.user_policy_array[module] = user_array[module]
86 return
87
88 def get_iface_default(self,module_name=None,ifname=None,attr=None):
89 '''
90 get_iface_default: Addon modules must use one of two types of access methods to
91 the default configs. In this method, we expect the default to be
92 either in
93 [module]['iface_defaults'][ifname][attr] or
94 [module]['defaults'][attr]
95 We first check the user_policy_array and return that value. But if
96 the user did not specify an override, we use the system_policy_array.
97 '''
98 # make sure we have an index
99 if (not ifname or not attr or not module_name):
100 return None
101
102 val = None
103 # users can specify defaults to override the systemwide settings
104 # look for user specific interface attribute iface_defaults first
105 try:
106 # looks for user specified value
107 val = self.user_policy_array[module_name]['iface_defaults'][ifname][attr]
108 return val
109 except (TypeError, KeyError, IndexError):
110 pass
111 try:
112 # failing that, there may be a user default for all intefaces
113 val = self.user_policy_array[module_name]['defaults'][attr]
114 return val
115 except (TypeError, KeyError, IndexError):
116 pass
117 try:
118 # failing that, look for system setting for the interface
119 val = self.system_policy_array[module_name]['iface_defaults'][ifname][attr]
120 return val
121 except (TypeError, KeyError, IndexError):
122 pass
123 try:
124 # failing that, look for system setting for all interfaces
125 val = self.system_policy_array[module_name]['defaults'][attr]
126 return val
127 except (TypeError, KeyError, IndexError):
128 pass
129
130 # could not find any system or user default so return Non
131 return val
132
133 def get_attr_default(self,module_name=None,attr=None):
134 '''
135 get_attr_default: Addon modules must use one of two types of access methods to
136 the default configs. In this method, we expect the default to be in
137
138 [module]['defaults'][attr]
139
140 We first check the user_policy_array and return that value. But if
141 the user did not specify an override, we use the system_policy_array.
142 '''
143 if (not attr or not module_name):
144 return None
145 # users can specify defaults to override the systemwide settings
146 # look for user specific attribute defaults first
147 val = None
148 try:
149 # looks for user specified value
150 val = self.user_policy_array[module_name]['defaults'][attr]
151 return val
152 except (TypeError, KeyError, IndexError):
153 pass
154 try:
155 # failing that, look for system setting
156 val = self.system_policy_array[module_name]['defaults'][attr]
157 return val
158 except (TypeError, KeyError, IndexError):
159 pass
160
161 return val
162
163 def get_module_globals(self,module_name=None,attr=None):
164 '''
165 get_module_globals: Addon modules must use one of two types of access methods to
166 the default configs. In this method, we expect the default to be in
167
168 [module]['module_globals'][attr]
169
170 We first check the user_policy_array and return that value. But if
171 the user did not specify an override, we use the system_policy_array.
172 '''
173 if (not attr or not module_name):
174 return None
175 # users can specify defaults to override the systemwide settings
176 # look for user specific attribute defaults first
177 val = None
178 try:
179 # looks for user specified value
180 val = self.user_policy_array[module_name]['module_globals'][attr]
181 return val
182 except (TypeError, KeyError, IndexError):
183 pass
184 try:
185 # failing that, look for system setting
186 val = self.system_policy_array[module_name]['module_globals'][attr]
187 return val
188 except (TypeError, KeyError, IndexError):
189 pass
190
191 return val
192
193 def get_module_default(self,module_name=None):
194 '''
195 get_module_default: Addon modules can also access the entire config
196 This method returns indexed by "system" and "user": these are the
197 system-wide and user-defined policy arrays for a specific module.
198 '''
199 if not module_name:
200 return None
201 if self.system_policy_array.get(module_name) and \
202 self.user_policy_array.get(module_name):
203 mod_array = {"system":self.system_policy_array[module_name],
204 "user":self.user_policy_array[module_name]}
205 else:
206 # the module must not have these defined, return None
207 mod_array = None
208
209 return mod_array
210
211 policymanager_api = policymanager()