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