1
|
|
|
# encoding: utf-8 |
2
|
|
|
""" |
3
|
|
|
community.py |
4
|
|
|
|
5
|
|
|
Created by Thomas Mangin on 2009-11-05. |
6
|
|
|
Copyright (c) 2009-2017 Exa Networks. All rights reserved. |
7
|
|
|
License: 3-clause BSD. (See the COPYRIGHT file) |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
from struct import pack |
11
|
|
|
from struct import unpack |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
# ==================================================================== Community |
15
|
|
|
# |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class Community(object): |
19
|
|
|
MAX = 0xFFFFFFFF |
20
|
|
|
|
21
|
|
|
NO_EXPORT = pack('!L', 0xFFFFFF01) |
|
|
|
|
22
|
|
|
NO_ADVERTISE = pack('!L', 0xFFFFFF02) |
23
|
|
|
NO_EXPORT_SUBCONFED = pack('!L', 0xFFFFFF03) |
24
|
|
|
NO_PEER = pack('!L', 0xFFFFFF04) |
25
|
|
|
BLACKHOLE = pack('!L', 0xFFFF029A) |
26
|
|
|
|
27
|
|
|
cache = {} |
28
|
|
|
caching = True |
29
|
|
|
|
30
|
|
|
def __init__(self, community): |
31
|
|
|
self.community = community |
32
|
|
|
if community == self.NO_EXPORT: |
33
|
|
|
self._str = 'no-export' |
34
|
|
|
elif community == self.NO_ADVERTISE: |
35
|
|
|
self._str = 'no-advertise' |
36
|
|
|
elif community == self.NO_EXPORT_SUBCONFED: |
37
|
|
|
self._str = 'no-export-subconfed' |
38
|
|
|
elif community == self.NO_PEER: |
39
|
|
|
self._str = 'no-peer' |
40
|
|
|
elif community == self.BLACKHOLE: |
41
|
|
|
self._str = 'blackhole' |
42
|
|
|
else: |
43
|
|
|
self._str = "%d:%d" % unpack('!HH', self.community) |
44
|
|
|
|
45
|
|
|
def __eq__(self, other): |
46
|
|
|
return self.community == other.community |
47
|
|
|
|
48
|
|
|
def __ne__(self, other): |
49
|
|
|
return self.community != other.community |
50
|
|
|
|
51
|
|
|
def __lt__(self, other): |
52
|
|
|
return self.community < other.community |
53
|
|
|
|
54
|
|
|
def __le__(self, other): |
55
|
|
|
return self.community <= other.community |
56
|
|
|
|
57
|
|
|
def __gt__(self, other): |
58
|
|
|
return self.community > other.community |
59
|
|
|
|
60
|
|
|
def __ge__(self, other): |
61
|
|
|
return self.community >= other.community |
62
|
|
|
|
63
|
|
|
def json(self): |
64
|
|
|
return "[ %d, %d ]" % unpack('!HH', self.community) |
65
|
|
|
|
66
|
|
|
def pack(self, negotiated=None): |
67
|
|
|
return self.community |
68
|
|
|
|
69
|
|
|
def __repr__(self): |
70
|
|
|
return self._str |
71
|
|
|
|
72
|
|
|
def __len__(self): |
73
|
|
|
return 4 |
74
|
|
|
|
75
|
|
|
@classmethod |
76
|
|
|
def unpack(cls, community, negotiated): |
77
|
|
|
return cls(community) |
78
|
|
|
|
79
|
|
|
@classmethod |
80
|
|
|
def cached(cls, community): |
81
|
|
|
if not cls.caching: |
82
|
|
|
return cls(community) |
83
|
|
|
if community in cls.cache: |
84
|
|
|
return cls.cache[community] |
85
|
|
|
instance = cls(community) |
86
|
|
|
cls.cache[community] = instance |
87
|
|
|
return instance |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
# Always cache well-known communities, they will be used a lot |
91
|
|
|
if not Community.cache: |
92
|
|
|
Community.cache[Community.NO_EXPORT] = Community(Community.NO_EXPORT) |
93
|
|
|
Community.cache[Community.NO_ADVERTISE] = Community(Community.NO_ADVERTISE) |
94
|
|
|
Community.cache[Community.NO_EXPORT_SUBCONFED] = Community(Community.NO_EXPORT_SUBCONFED) |
95
|
|
|
Community.cache[Community.NO_PEER] = Community(Community.NO_PEER) |
96
|
|
|
Community.cache[Community.BLACKHOLE] = Community(Community.BLACKHOLE) |
97
|
|
|
|