Code Duplication    Length = 33-38 lines in 3 locations

lib/exabgp/bgp/message/update/attribute/sr/labelindex.py 1 location

@@ 26-63 (lines=38) @@
23
# 3.1.  Label-Index TLV
24
25
26
@PrefixSid.register()
27
class SrLabelIndex(object):
28
	TLV = 1
29
	LENGTH = 7
30
31
	def __init__ (self, labelindex, packed=None):
32
		self.labelindex = labelindex
33
		self.packed = self.pack()
34
35
	def __repr__ (self):
36
		return "%s" % (self.labelindex)
37
38
	def pack (self):
39
		return concat_bytes(
40
			pack('!B', self.TLV),
41
			pack('!H', self.LENGTH),
42
			pack('!B',0),  # reserved
43
			pack('!H',0),  # flags
44
			pack('!I',self.labelindex)
45
		)
46
47
	@classmethod
48
	def unpack (cls,data,length):
49
		labelindex = -1
50
		if cls.LENGTH != length:
51
			raise Notify(3,5, "Invalid TLV size. Should be 7 but {0} received".format(length))
52
		# Shift reserved bits
53
		data = data[1:]
54
		# Shift Flags
55
		# Flags: 16 bits of flags.  None is defined by this document.  The
56
		# flag field MUST be clear on transmission and MUST be ignored at
57
		# reception.
58
		data = data[2:6]
59
		labelindex = unpack('!I',data)[0]
60
		return cls(labelindex=labelindex,packed=data)
61
62
	def json (self,compact=None):
63
		return '"sr-label-index": %d' % (self.labelindex)
64

lib/exabgp/bgp/message/update/attribute/sr/ipv6sid.py 1 location

@@ 32-65 (lines=34) @@
29
# 3.2.  IPv6 SID
30
31
32
@PrefixSid.register()
33
class SrV6Sid(object):
34
	TLV = 2
35
	LENGTH = 19
36
37
	def __init__ (self,v6sid,packed=None):
38
		self.v6sid = v6sid
39
		self.packed = self.pack()
40
41
	def __repr__ (self):
42
		return "sr-v6-sid %s" % (self.v6sid)
43
44
	def pack (self):
45
		return concat_bytes(
46
			pack('!B', self.TLV),
47
			pack('!H', self.LENGTH),
48
			pack('!B', 0),
49
			pack('!H', 0),
50
			IP.pton(self.v6sid)
51
		)
52
53
	@classmethod
54
	def unpack (cls,data,length):
55
		v6sid = -1
56
		if cls.LENGTH != length:
57
			raise Notify(3,5, "Invalid TLV size. Should be {0} but {1} received".format(cls.LENGTH, length))
58
		# RESERVED: 24 bit field for future use.  MUST be clear on
59
		# transmission an MUST be ignored at reception.
60
		data = data[3:19]
61
		v6sid = IP.unpack(data)
62
		return cls(v6sid=str(v6sid),packed=data)
63
64
	def json (self, compact=None):
65
		return '"sr-v6-sid": "%s"' % (self.v6sid)
66

lib/exabgp/bgp/message/update/attribute/sr/srv6vpnsid.py 1 location

@@ 32-64 (lines=33) @@
29
# 3.2.  SRv6 VPN SID
30
31
32
@PrefixSid.register()
33
class Srv6VpnSid(object):
34
	TLV = 4
35
	LENGTH = 19
36
37
	def __init__ (self,vpnsid,packed=None):
38
		self.vpnsid = vpnsid
39
		self.packed = self.pack()
40
41
	def __repr__ (self):
42
		return "srv6-vpn-sid %s" % (self.vpnsid)
43
44
	def pack (self):
45
		return concat_bytes(
46
			pack('!B', self.TLV),
47
			pack('!H', self.LENGTH),
48
			pack('!B', 0),
49
			pack('!B', 0),
50
			pack('!B', 0),
51
			IP.pton(self.vpnsid)
52
		)
53
54
	@classmethod
55
	def unpack (cls,data,length):
56
		vpnsid = -1
57
		if cls.LENGTH != length:
58
			raise Notify(3,5, "Invalid TLV size. Should be {0} but {1} received".format(cls.LENGTH, length))
59
		data = data[3:19]
60
		vpnsid = IP.unpack(data)
61
		return cls(vpnsid=str(vpnsid),packed=data)
62
63
	def json (self, compact=None):
64
		return '"srv6-vpn-sid": "%s"' % (self.vpnsid)
65