Completed
Pull Request — master (#964)
by Thomas
13:14
created

exabgp.bgp.message.update.attribute.bgpls.link.sradjlan   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 59.14 %

Importance

Changes 0
Metric Value
eloc 42
dl 55
loc 93
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A SrAdjacencyLan.json() 2 2 1
A SrAdjacencyLan.__repr__() 2 2 1
A SrAdjacencyLan.reset() 3 3 1
A SrAdjacencyLan.__init__() 2 2 1
B SrAdjacencyLan.unpack() 37 37 6

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
sradjlan.py
4
5
Created by Evelio Vila
6
Copyright (c) 2014-2017 Exa Networks. All rights reserved.
7
"""
8
9
import json
10
from struct import unpack
11
from exabgp.vendoring import six
12
from exabgp.util import hexstring
13
14
from exabgp.vendoring.bitstring import BitArray
15
from exabgp.protocol.iso import ISO
16
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE, LsGenericFlags
17
from exabgp.bgp.message.notification import Notify
18
19
#   0                   1                   2                   3
20
#   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
21
#  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22
#  |              Type             |            Length             |
23
#  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24
#  |     Flags     |     Weight    |            Reserved           |
25
#  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26
#
27
#   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28
#   |             OSPF Neighbor ID / IS-IS System-ID                |
29
#   +                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30
#   |                               |
31
#   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32
#
33
#   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34
#   |                    SID/Label/Index (variable)                 |
35
#   +---------------------------------------------------------------+
36
#		draft-gredler-idr-bgp-ls-segment-routing-ext-03
37
38 View Code Duplication
@LINKSTATE.register()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
class SrAdjacencyLan(object):
40
	TLV = 1100
41
	_sr_adj_lan_sids = []
42
43
	def __init__ (self):
44
		self.sr_adj_lan_sids = SrAdjacencyLan._sr_adj_lan_sids.copy()
45
46
	def __repr__ (self):
47
		return "sr-adj-lan-sids: {}".format(self.sr_adj_lan_sids)
48
49
	@classmethod
50
	def unpack (cls,data,length):
51
		# We only support IS-IS flags for now.
52
		flags = LsGenericFlags.unpack(data[0:1],LsGenericFlags.ISIS_SR_ADJ_FLAGS).flags
53
		# Parse adj weight
54
		weight = six.indexbytes(data,1)
55
		# Parse neighbor System-ID
56
		system_id = ISO.unpack_sysid(data[4:10])
57
		# Move pointer 10 bytes: Flags(1) + Weight(1) + Reserved(2) + System-ID(6)
58
		data = data[10:]
59
     	# SID/Index/Label: according to the V and L flags, it contains
60
      	# either:
61
		# *  A 3 octet local label where the 20 rightmost bits are used for
62
		#	 encoding the label value.  In this case the V and L flags MUST
63
		#	 be set.
64
		#
65
		# *  A 4 octet index defining the offset in the SID/Label space
66
		# 	 advertised by this router using the encodings defined in
67
		#  	 Section 3.1.  In this case V and L flags MUST be unset.
68
		raw = []
69
		while data:
70
			# Range Size: 3 octet value indicating the number of labels in
71
			# the range.
72
			if int(flags['V']) and int(flags['L']):
73
				b = BitArray(bytes=data[:3])
74
				sid = b.unpack('uintbe:24')[0]
75
				data = data[3:]
76
			elif (not flags['V']) and (not flags['L']):
77
				sid = unpack('!I',data[:4])[0]
78
				data = data[4:]
79
			else:
80
				raw.append(hexstring(data))
81
				break
82
		cls._sr_adj_lan_sids.append(
83
			{'flags': flags, 'weight': weight, 'system-id': system_id, 'sid': sid, 'undecoded': raw}
0 ignored issues
show
introduced by
The variable sid does not seem to be defined in case the while loop on line 69 is not entered. Are you sure this can never be the case?
Loading history...
84
		)
85
		return cls()
86
87
	def json (self,compact=None):
88
		return '"sr-adj-lan-sids": {}'.format(json.dumps(self.sr_adj_lan_sids))
89
90
	@classmethod
91
	def reset(cls):
92
		cls._sr_adj_sids = []
93