]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/ifupdownaddons/cache.py
.gitignore: pycharm remote execution update
[mirror_ifupdown2.git] / ifupdown2 / ifupdownaddons / cache.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6
7 import pprint
8
9
10 class MSTPAttrsCache():
11 bridges = {}
12
13 @classmethod
14 def get(cls, bridgename, default=None):
15 if bridgename in MSTPAttrsCache.bridges:
16 return MSTPAttrsCache.bridges[bridgename]
17 else:
18 return default
19
20 @classmethod
21 def set(cls, bridgename, attrs):
22 MSTPAttrsCache.bridges[bridgename] = attrs
23
24 @classmethod
25 def invalidate(cls):
26 MSTPAttrsCache.bridges = {}
27
28
29 class linkCache():
30 """ This class contains methods and instance variables to cache
31 link info """
32
33 """ { <ifacename> : { 'ifindex': <index>,
34 'mtu': <mtu>,
35 'state' : <state>',
36 'flags' : <flags>,
37 'kind' : <kind: bridge, bond, vlan>,
38 'linkinfo' : {<attr1> : <attrval1>,
39 <attr2> : <attrval2>,
40 <ports> : {
41 } """
42 links = {}
43 vrfs = {}
44
45 @classmethod
46 def get_attr(cls, mapList):
47 return reduce(lambda d, k: d[k], mapList, linkCache.links)
48
49 @classmethod
50 def set_attr(cls, mapList, value):
51 cls.get_attr(mapList[:-1])[mapList[-1]] = value
52
53 @classmethod
54 def del_attr(cls, mapList):
55 try:
56 del cls.get_attr(mapList[:-1])[mapList[-1]]
57 except:
58 pass
59
60 @classmethod
61 def update_attrdict(cls, mapList, valuedict):
62 try:
63 cls.get_attr(mapList[:-1])[mapList[-1]].update(valuedict)
64 except:
65 cls.get_attr(mapList[:-1])[mapList[-1]] = valuedict
66 pass
67
68 @classmethod
69 def append_to_attrlist(cls, mapList, value):
70 cls.get_attr(mapList[:-1])[mapList[-1]].append(value)
71
72 @classmethod
73 def remove_from_attrlist(cls, mapList, value):
74 try:
75 cls.get_attr(mapList[:-1])[mapList[-1]].remove(value)
76 except:
77 pass
78
79 @classmethod
80 def check_attr(cls, attrlist, value=None):
81 try:
82 cachedvalue = cls.get_attr(attrlist)
83 if value:
84 if cachedvalue == value:
85 return True
86 else:
87 return False
88 elif cachedvalue:
89 return True
90 else:
91 return False
92 except:
93 return False
94
95 @classmethod
96 def invalidate(cls):
97 cls.links = {}
98
99 @classmethod
100 def reset(cls):
101 cls.invalidate()
102 cls.vrfs = {}
103
104 @classmethod
105 def dump(cls):
106 print 'Dumping link cache'
107 pp = pprint.PrettyPrinter(indent=4)
108 pp.pprint(cls.links)
109
110 @classmethod
111 def dump_link(cls, linkname):
112 print 'Dumping link %s' % linkname
113 pp = pprint.PrettyPrinter(indent=4)
114 pp.pprint(cls.links.get(linkname))