]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/policymanager.py
Merge branch 'dev' into release/cl-stable
[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 with open(filename, 'r') as fd:
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.info('could not read %s system policy defaults config' \
58 % filename)
59 self.logger.info(' exception is %s' % str(e))
60
61 for module in system_array.keys():
62 if self.system_policy_array.has_key(module):
63 self.logger.debug('warning: overwriting system module %s from file %s' \
64 % (module,filename))
65 self.system_policy_array[module] = system_array[module]
66
67 # take care of user defined policy defaults
68 self.user_policy_array = {}
69 for filename in user_files:
70 user_array = {}
71 try:
72 with open(filename, 'r') as fd:
73 user_array = json.load(fd)
74 self.logger.debug('reading %s policy user defaults config' \
75 % filename)
76 except Exception, e:
77 self.logger.debug('could not read %s user policy defaults config' \
78 % filename)
79 self.logger.debug(' exception is %s' % str(e))
80 # customer added module attributes
81 for module in user_array.keys():
82 if self.system_policy_array.has_key(module):
83 # warn user that we are overriding the system module setting
84 self.logger.debug('warning: overwriting system with user module %s from file %s' \
85 % (module,filename))
86 self.user_policy_array[module] = user_array[module]
87 return
88
89 def get_iface_default(self,module_name=None,ifname=None,attr=None):
90 '''
91 get_iface_default: Addon modules must use one of two types of access methods to
92 the default configs. In this method, we expect the default to be
93 either in
94 [module]['iface_defaults'][ifname][attr] or
95 [module]['defaults'][attr]
96 We first check the user_policy_array and return that value. But if
97 the user did not specify an override, we use the system_policy_array.
98 '''
99 # make sure we have an index
100 if (not ifname or not attr or not module_name):
101 return None
102
103 val = None
104 # users can specify defaults to override the systemwide settings
105 # look for user specific interface attribute iface_defaults first
106 try:
107 # looks for user specified value
108 val = self.user_policy_array[module_name]['iface_defaults'][ifname][attr]
109 return val
110 except (TypeError, KeyError, IndexError):
111 pass
112 try:
113 # failing that, there may be a user default for all intefaces
114 val = self.user_policy_array[module_name]['defaults'][attr]
115 return val
116 except (TypeError, KeyError, IndexError):
117 pass
118 try:
119 # failing that, look for system setting for the interface
120 val = self.system_policy_array[module_name]['iface_defaults'][ifname][attr]
121 return val
122 except (TypeError, KeyError, IndexError):
123 pass
124 try:
125 # failing that, look for system setting for all interfaces
126 val = self.system_policy_array[module_name]['defaults'][attr]
127 return val
128 except (TypeError, KeyError, IndexError):
129 pass
130
131 # could not find any system or user default so return Non
132 return val
133
134 def get_attr_default(self,module_name=None,attr=None):
135 '''
136 get_attr_default: Addon modules must use one of two types of access methods to
137 the default configs. In this method, we expect the default to be in
138
139 [module]['defaults'][attr]
140
141 We first check the user_policy_array and return that value. But if
142 the user did not specify an override, we use the system_policy_array.
143 '''
144 if (not attr or not module_name):
145 return None
146 # users can specify defaults to override the systemwide settings
147 # look for user specific attribute defaults first
148 val = None
149 try:
150 # looks for user specified value
151 val = self.user_policy_array[module_name]['defaults'][attr]
152 return val
153 except (TypeError, KeyError, IndexError):
154 pass
155 try:
156 # failing that, look for system setting
157 val = self.system_policy_array[module_name]['defaults'][attr]
158 return val
159 except (TypeError, KeyError, IndexError):
160 pass
161
162 return val
163
164 def get_module_globals(self,module_name=None,attr=None):
165 '''
166 get_module_globals: Addon modules must use one of two types of access methods to
167 the default configs. In this method, we expect the default to be in
168
169 [module]['module_globals'][attr]
170
171 We first check the user_policy_array and return that value. But if
172 the user did not specify an override, we use the system_policy_array.
173 '''
174
175 if (not attr or not module_name):
176 return None
177 # users can specify defaults to override the systemwide settings
178 # look for user specific attribute defaults first
179 val = None
180 try:
181 # looks for user specified value
182 val = self.user_policy_array[module_name]['module_globals'][attr]
183 return val
184 except (TypeError, KeyError, IndexError):
185 pass
186 try:
187 # failing that, look for system setting
188 val = self.system_policy_array[module_name]['module_globals'][attr]
189 return val
190 except (TypeError, KeyError, IndexError):
191 pass
192
193 return val
194
195 def get_module_default(self,module_name=None):
196 '''
197 get_module_default: Addon modules can also access the entire config
198 This method returns indexed by "system" and "user": these are the
199 system-wide and user-defined policy arrays for a specific module.
200 '''
201 if not module_name:
202 return None
203 if self.system_policy_array.get(module_name) and \
204 self.user_policy_array.get(module_name):
205 mod_array = {"system":self.system_policy_array[module_name],
206 "user":self.user_policy_array[module_name]}
207 else:
208 # the module must not have these defined, return None
209 mod_array = None
210
211 return mod_array
212
213 policymanager_api = policymanager()