]>
Commit | Line | Data |
---|---|---|
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 | ||
7 | import pprint | |
8 | from collections import OrderedDict | |
9 | ||
10 | class 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 = {} | |
05ac52f0 RP |
26 | vrfs = {} |
27 | ||
15ef32ea RP |
28 | @classmethod |
29 | def get_attr(cls, mapList): | |
30 | return reduce(lambda d, k: d[k], mapList, linkCache.links) | |
31 | ||
32 | @classmethod | |
33 | def set_attr(cls, mapList, value): | |
34 | cls.get_attr(mapList[:-1])[mapList[-1]] = value | |
35 | ||
36 | @classmethod | |
37 | def del_attr(cls, mapList): | |
38 | try: | |
39 | del cls.get_attr(mapList[:-1])[mapList[-1]] | |
40 | except: | |
41 | pass | |
42 | ||
43 | @classmethod | |
44 | def update_attrdict(cls, mapList, valuedict): | |
45 | try: | |
46 | cls.get_attr(mapList[:-1])[mapList[-1]].update(valuedict) | |
47 | except: | |
48 | cls.get_attr(mapList[:-1])[mapList[-1]] = valuedict | |
49 | pass | |
50 | ||
51 | @classmethod | |
52 | def append_to_attrlist(cls, mapList, value): | |
53 | cls.get_attr(mapList[:-1])[mapList[-1]].append(value) | |
54 | ||
55 | @classmethod | |
56 | def remove_from_attrlist(cls, mapList, value): | |
57 | try: | |
58 | cls.get_attr(mapList[:-1])[mapList[-1]].remove(value) | |
59 | except: | |
60 | pass | |
61 | ||
62 | @classmethod | |
63 | def check_attr(cls, attrlist, value=None): | |
64 | try: | |
65 | cachedvalue = cls.get_attr(attrlist) | |
66 | if value: | |
67 | if cachedvalue == value: | |
68 | return True | |
69 | else: | |
70 | return False | |
71 | elif cachedvalue: | |
72 | return True | |
73 | else: | |
74 | return False | |
75 | except: | |
76 | return False | |
77 | ||
78 | @classmethod | |
79 | def invalidate(cls): | |
80 | cls.links = {} | |
81 | ||
82 | @classmethod | |
83 | def dump(cls): | |
84 | print 'Dumping link cache' | |
85 | pp = pprint.PrettyPrinter(indent=4) | |
86 | pp.pprint(cls.links) | |
87 | ||
88 | @classmethod | |
89 | def dump_link(cls, linkname): | |
90 | print 'Dumping link %s' %linkname | |
91 | pp = pprint.PrettyPrinter(indent=4) | |
92 | pp.pprint(cls.links.get(linkname)) |