Code Duplication    Length = 57-60 lines in 3 locations

lib/exabgp/bgp/message/update/attribute/bgpls/prefix/srprefix.py 1 location

@@ 30-89 (lines=60) @@
27
28
# 	draft-ietf-isis-segment-routing-extensions Prefix-SID Sub-TLV
29
30
@LINKSTATE.register()
31
class SrPrefix(LsGenericFlags):
32
    TLV = 1158
33
    FLAGS = ['R', 'N', 'P', 'E', 'V', 'L', 'RSV', 'RSV']
34
35
    def __init__(self, flags, sids, sr_algo, undecoded=[]):
36
        self.flags = flags
37
        self.sids = sids
38
        self.sr_algo = sr_algo
39
        self.undecoded = undecoded
40
41
    def __repr__(self):
42
        return "prefix_flags: %s, sids: %s, undecoded_sid: %s" % (self.flags, self.sids, self.undecoded)
43
44
    @classmethod
45
    def unpack(cls, data, length):
46
        # We only support IS-IS flags for now.
47
        flags = cls.unpack_flags(data[0:1])
48
        #
49
        # Parse Algorithm
50
        sr_algo = data[1]
51
        # Move pointer 4 bytes: Flags(1) + Algorithm(1) + Reserved(2)
52
        data = data[4:]
53
        # SID/Index/Label: according to the V and L flags, it contains
54
        # either:
55
        # *  A 3 octet local label where the 20 rightmost bits are used for
56
        # 	 encoding the label value.  In this case the V and L flags MUST
57
        # 	 be set.
58
        #
59
        # *  A 4 octet index defining the offset in the SID/Label space
60
        # 	 advertised by this router using the encodings defined in
61
        #  	 Section 3.1.  In this case V and L flags MUST be unset.
62
        sids = []
63
        raw = []
64
        while data:
65
            if flags['V'] and flags['L']:
66
                sid = unpack('!L', bytes([0]) + data[:3])[0]
67
                data = data[3:]
68
                sids.append(sid)
69
            elif (not flags['V']) and (not flags['L']):
70
                if len(data) != 4:
71
                    # Cisco IOS XR Software, Version 6.1.1.19I is not
72
                    # correctly setting the flags
73
                    raise Notify(3, 5, "SID/Label size doesn't match V and L flag state")
74
                sid = unpack('!I', data[:4])[0]
75
                data = data[4:]
76
                sids.append(sid)
77
            else:
78
                raw.append(hexstring(data))
79
                break
80
81
        return cls(flags=flags, sids=sids, sr_algo=sr_algo, undecoded=raw)
82
83
    def json(self, compact=None):
84
        return ', '.join(
85
            [
86
                '"sr-prefix-flags": {}'.format(LsGenericFlags.json(self)),
87
                '"sids": {}'.format(json.dumps(self.sids)),
88
                '"undecoded-sids": {}'.format(json.dumps(self.undecoded)),
89
                '"sr-algorithm": {}'.format(json.dumps(self.sr_algo)),
90
            ]
91
        )
92

lib/exabgp/bgp/message/update/attribute/bgpls/link/sradjlan.py 1 location

@@ 39-96 (lines=58) @@
36
37
#  draft-ietf-isis-segment-routing-extensions - Adj-SID IS-IS Flags
38
39
@LINKSTATE.register()
40
class SrAdjacencyLan(LsGenericFlags):
41
    TLV = 1100
42
    FLAGS = ['F', 'B', 'V', 'L', 'S', 'P', 'RSV', 'RSV']
43
44
    def __init__(self, flags, sids, weight, undecoded=[]):
45
        self.flags = flags
46
        self.sids = sids
47
        self.weight = weight
48
        self.undecoded = undecoded
49
50
    def __repr__(self):
51
        return "sr_adj_lan_flags: %s, sids: %s, undecoded_sid: %s" % (self.flags, self.sids, self.undecoded)
52
53
    @classmethod
54
    def unpack(cls, data, length):
55
        # We only support IS-IS flags for now.
56
        flags = cls.unpack_flags(data[0:1])
57
        # Parse adj weight
58
        weight = data[1]
59
        # Move pointer 4 bytes: Flags(1) + Weight(1) + Reserved(2)
60
        system_id = ISO.unpack_sysid(data[4:10])
61
        data = data[10:]
62
        # SID/Index/Label: according to the V and L flags, it contains
63
        # either:
64
        # *  A 3 octet local label where the 20 rightmost bits are used for
65
        # 	 encoding the label value.  In this case the V and L flags MUST
66
        # 	 be set.
67
        #
68
        # *  A 4 octet index defining the offset in the SID/Label space
69
        # 	 advertised by this router using the encodings defined in
70
        #  	 Section 3.1.  In this case V and L flags MUST be unset.
71
        sids = []
72
        raw = []
73
        while data:
74
            # Range Size: 3 octet value indicating the number of labels in
75
            # the range.
76
            if int(flags.flags['V']) and int(flags.flags['L']):
77
                sid = unpack('!L', bytes([0]) + data[:3])[0]
78
                data = data[3:]
79
                sids.append(sid)
80
            elif (not flags.flags['V']) and (not flags.flags['L']):
81
                sid = unpack('!I', data[:4])[0]
82
                data = data[4:]
83
                sids.append(sid)
84
            else:
85
                raw.append(hexstring(data))
86
                break
87
88
        return cls(flags=flags, sids=sids, weight=weight, undecoded=raw)
89
90
    def json(self, compact=None):
91
        return ', '.join(
92
            [
93
                '"sr-adj-lan-flags": {}'.format(LsGenericFlags.json(self)),
94
                '"sids": {}'.format(json.dumps(self.sids)),
95
                '"undecoded-sids": {}'.format(json.dumps(self.undecoded)),
96
                '"sr-adj-lan-weight": {}'.format(json.dumps(self.weight)),
97
            ]
98
        )
99

lib/exabgp/bgp/message/update/attribute/bgpls/link/sradj.py 1 location

@@ 30-86 (lines=57) @@
27
#
28
29
30
@LINKSTATE.register()
31
class SrAdjacency(LsGenericFlags):
32
    TLV = 1099
33
    FLAGS = ['F', 'B', 'V', 'L', 'S', 'P', 'RSV', 'RSV']
34
35
    def __init__(self, flags, sids, weight, undecoded=[]):
36
        self.flags = flags
37
        self.sids = sids
38
        self.weight = weight
39
        self.undecoded = undecoded
40
41
    def __repr__(self):
42
        return "adj_flags: %s, sids: %s, undecoded_sid" % (self.flags, self.sids, self.undecoded)
43
44
    @classmethod
45
    def unpack(cls, data, length):
46
        # We only support IS-IS flags for now.
47
        flags = cls.unpack_flags(data[0:1])
48
        # Parse adj weight
49
        weight = data[1]
50
        # Move pointer 4 bytes: Flags(1) + Weight(1) + Reserved(2)
51
        data = data[4:]
52
        # SID/Index/Label: according to the V and L flags, it contains
53
        # either:
54
        # *  A 3 octet local label where the 20 rightmost bits are used for
55
        # 	 encoding the label value.  In this case the V and L flags MUST
56
        # 	 be set.
57
        #
58
        # *  A 4 octet index defining the offset in the SID/Label space
59
        # 	 advertised by this router using the encodings defined in
60
        #  	 Section 3.1.  In this case V and L flags MUST be unset.
61
        sids = []
62
        raw = []
63
        while data:
64
            # Range Size: 3 octet value indicating the number of labels in
65
            # the range.
66
            if int(flags.flags['V']) and int(flags.flags['L']):
67
                sid = unpack('!L', bytes([0]) + data[:3])[0]
68
                data = data[3:]
69
                sids.append(sid)
70
            elif (not flags.flags['V']) and (not flags.flags['L']):
71
                sid = unpack('!I', data[:4])[0]
72
                data = data[4:]
73
                sids.append(sid)
74
            else:
75
                raw.append(hexstring(data))
76
                break
77
78
        return cls(flags=flags, sids=sids, weight=weight, undecoded=raw)
79
80
    def json(self, compact=None):
81
        return ', '.join(
82
            [
83
                '"sr-adj-flags": {}'.format(LsGenericFlags.json(self)),
84
                '"sids": {}'.format(json.dumps(self.sids)),
85
                '"undecoded-sids": {}'.format(json.dumps(self.undecoded)),
86
                '"sr-adj-weight": {}'.format(json.dumps(self.weight)),
87
            ]
88
        )
89