Completed
Pull Request — master (#946)
by
unknown
24:25 queued 09:28
created

exabgp.bgp.message.update.attribute.sr.srv6l3vpnsid   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 52.31 %

Importance

Changes 0
Metric Value
eloc 34
dl 34
loc 65
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Srv6L3vpnSid.unpack() 8 8 2
A Srv6L3vpnSid.__init__() 3 3 1
A Srv6L3vpnSid.pack() 9 9 1
A Srv6L3vpnSid.__repr__() 2 2 1
A Srv6L3vpnSid.json() 2 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# encoding: utf-8
2
"""
3
sr/srv6l3vpnsid.py
4
5
Created by Hiroki Shirokura 2020-01-09
6
Copyright (c) 2020 Hiroki Shirokura . All rights reserved.
7
"""
8
from struct import pack
9
10
from exabgp.util import concat_bytes
11
from exabgp.protocol.ip import IP
12
13
from exabgp.bgp.message.notification import Notify
14
from exabgp.bgp.message.update.attribute.sr.prefixsid import PrefixSid
15
16
# 0                   1                   2                   3
17
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
18
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
19
# |       Type    |             Length            |   RESERVED    |
20
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21
# |                                                               |
22
# |                      L3VPN SID (16 octets)                    |
23
# |                                                               |
24
# |                                                               |
25
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26
# |  SID Flags   |       Endpoint Behavior       |   RESERVED    |
27
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28
# 3.2.  SRv6 L3 Service
29
30
31 View Code Duplication
@PrefixSid.register()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
32
class Srv6L3vpnSid(object):
33
	TLV = 5
34
	LENGTH = 21
35
36
	def __init__ (self,l3vpnsid,packed=None):
37
		self.l3vpnsid = l3vpnsid
38
		self.packed = self.pack()
39
40
	def __repr__ (self):
41
		return "srv6-l3vpn-sid %s" % (self.l3vpnsid)
42
43
	def pack (self):
44
		return concat_bytes(
45
			pack('!B', self.TLV),
46
			pack('!H', self.LENGTH),
47
			pack('!B', 0),
48
			IP.pton(self.l3vpnsid),
49
			pack('!B', 0),
50
			pack('!H', 0xffff),
51
			pack('!B', 0),
52
		)
53
54
	@classmethod
55
	def unpack (cls,data,length):
56
		l3vpnsid = -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[1:17]
60
		l3vpnsid = IP.unpack(data)
61
		return cls(l3vpnsid=l3vpnsid,packed=data)
62
63
	def json (self, compact=None):
64
		return '"srv6-vpn-sid": "%s"' % (self.l3vpnsid)
65