Code Duplication    Length = 25-26 lines in 5 locations

lib/exabgp/bgp/message/update/attribute/community/extended/traffic.py 5 locations

@@ 132-157 (lines=26) @@
129
		return TrafficRedirect(ASN(asn),target,data[:8])
130
131
132
@ExtendedCommunity.register
133
class TrafficRedirectASN4 (ExtendedCommunity):
134
	COMMUNITY_TYPE = 0x82
135
	COMMUNITY_SUBTYPE = 0x08
136
137
	__slots__ = ['asn', 'target']
138
139
	def __init__(self, asn, target, community=None):
140
		self.asn = asn
141
		self.target = target
142
		ExtendedCommunity.__init__(
143
			self,
144
			community if community is not None else pack(
145
				"!2sLH",
146
				self._subtype(),
147
					asn, target
148
			)
149
		)
150
151
	def __str__(self):
152
		return "redirect:%s:%s" % (self.asn, self.target)
153
154
	@staticmethod
155
	def unpack(data):
156
		asn, target = unpack('!LH', data[2:8])
157
		return TrafficRedirectASN4(ASN4(asn), target, data[:8])
158
159
# ================================================================== TrafficMark
160
# RFC 5575
@@ 104-129 (lines=26) @@
101
# ============================================================== TrafficRedirect
102
# RFC 5575 and 7674
103
104
@ExtendedCommunity.register
105
class TrafficRedirect (ExtendedCommunity):
106
	COMMUNITY_TYPE = 0x80
107
	COMMUNITY_SUBTYPE = 0x08
108
109
	__slots__ = ['asn','target']
110
111
	def __init__ (self, asn, target, community=None):
112
		self.asn = asn
113
		self.target = target
114
		ExtendedCommunity.__init__(
115
			self,
116
			community if community is not None else pack(
117
				"!2sHL",
118
				self._subtype(),
119
				asn,target
120
			)
121
		)
122
123
	def __repr__ (self):
124
		return "redirect:%s:%s" % (self.asn,self.target)
125
126
	@staticmethod
127
	def unpack (data):
128
		asn,target = unpack('!HL',data[2:8])
129
		return TrafficRedirect(ASN(asn),target,data[:8])
130
131
132
@ExtendedCommunity.register
@@ 24-49 (lines=26) @@
21
# ================================================================== TrafficRate
22
# RFC 5575
23
24
@ExtendedCommunity.register
25
class TrafficRate (ExtendedCommunity):
26
	COMMUNITY_TYPE = 0x80
27
	COMMUNITY_SUBTYPE = 0x06
28
29
	__slots__ = ['asn','rate']
30
31
	def __init__ (self, asn, rate, community=None):
32
		self.asn = asn
33
		self.rate = rate
34
		ExtendedCommunity.__init__(
35
			self,
36
			community if community is not None else pack(
37
				"!2sHf",
38
				self._subtype(),
39
				asn,rate
40
			)
41
		)
42
43
	def __repr__ (self):
44
		return "rate-limit:%d" % self.rate
45
46
	@staticmethod
47
	def unpack (data):
48
		asn,rate = unpack('!Hf',data[2:8])
49
		return TrafficRate(ASN(asn),rate,data[:8])
50
51
52
# ================================================================ TrafficAction
@@ 257-281 (lines=25) @@
254
255
# XXX: FIXME: I guess this should be a subclass of NextHop or IP ..
256
257
@ExtendedCommunity.register
258
class TrafficNextHopSimpson (ExtendedCommunity):
259
	COMMUNITY_TYPE = 0x08
260
	COMMUNITY_SUBTYPE = 0x00
261
262
	__slots__ = ['copy']
263
264
	def __init__ (self, copy, community=None):
265
		self.copy = copy
266
		ExtendedCommunity.__init__(
267
			self,
268
			community if community is not None else pack(
269
				"!2sLH",
270
				self._subtype(),
271
				0,1 if copy else 0
272
			)
273
		)
274
275
	def __repr__ (self):
276
		return "copy-to-nexthop" if self.copy else "redirect-to-nexthop"
277
278
	@staticmethod
279
	def unpack (data):
280
		bit, = unpack('!B',data[7:8])
281
		return TrafficNextHopSimpson(bool(bit & 0x01), data[:8])
282
283
284
# ============================================================ TrafficRedirectIP
@@ 162-186 (lines=25) @@
159
# ================================================================== TrafficMark
160
# RFC 5575
161
162
@ExtendedCommunity.register
163
class TrafficMark (ExtendedCommunity):
164
	COMMUNITY_TYPE = 0x80
165
	COMMUNITY_SUBTYPE = 0x09
166
167
	__slots__ = ['dscp']
168
169
	def __init__ (self, dscp, community=None):
170
		self.dscp = dscp
171
		ExtendedCommunity.__init__(
172
			self,
173
			community if community is not None else pack(
174
				"!2sLBB",
175
				self._subtype(),
176
				0,0,dscp
177
			)
178
		)
179
180
	def __repr__ (self):
181
		return "mark %d" % self.dscp
182
183
	@staticmethod
184
	def unpack (data):
185
		dscp, = unpack('!B',data[7:8])
186
		return TrafficMark(dscp,data[:8])
187
188
# =============================================================== TrafficNextHopIPv4IETF
189
# draft-ietf-idr-flowspec-redirect-02