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

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

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 50.77 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Srv6VpnSid.unpack() 8 8 2
A Srv6VpnSid.pack() 8 8 1
A Srv6VpnSid.json() 2 2 1
A Srv6VpnSid.__repr__() 2 2 1
A Srv6VpnSid.__init__() 3 3 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/srv6vpnsid.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
# |   SID-Type    |   SID-Flags   |                               |
22
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               |
23
# |                                                               |
24
# |                        IPv6 SID (16 octets)                   |
25
# |                                                               |
26
# |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27
# |                               |
28
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29
# 3.2.  SRv6 VPN SID
30
31
32 View Code Duplication
@PrefixSid.register()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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=vpnsid,packed=data)
62
63
	def json (self, compact=None):
64
		return '"srv6-vpn-sid": "%s"' % (self.vpnsid)
65