Completed
Push — master ( 3df76a...5af33a )
by Thomas
10:27
created

SrAdjacencyLan.merge()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
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.util import hexstring
12
13
from exabgp.protocol.iso import ISO
14
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LinkState
15
from exabgp.bgp.message.update.attribute.bgpls.linkstate import FlagLS
16
17
18
#   0                   1                   2                   3
19
#   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
20
#  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21
#  |              Type             |            Length             |
22
#  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23
#  |     Flags     |     Weight    |            Reserved           |
24
#  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25
#
26
#   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27
#   |             OSPF Neighbor ID / IS-IS System-ID                |
28
#   +                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29
#   |                               |
30
#   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31
#
32
#   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33
#   |                    SID/Label/Index (variable)                 |
34
#   +---------------------------------------------------------------+
35
# 		draft-gredler-idr-bgp-ls-segment-routing-ext-03
36
37
#  draft-ietf-isis-segment-routing-extensions - Adj-SID IS-IS Flags
38
39
@LinkState.register()
40
class SrAdjacencyLan(FlagLS):
41
    TLV = 1100
42
    FLAGS = ['F', 'B', 'V', 'L', 'S', 'P', 'RSV', 'RSV']
43
    MERGE = True
44
45
    def __init__(self, sradjlans):
46
        self.sr_adj_lan_sids = []
47
48
    def __repr__(self):
49
        return "sr-adj-lan-sids: {}".format(self.sr_adj_lan_sids)
50
51
    @classmethod
52
    def unpack(cls, data, length):
53
        # We only support IS-IS flags for now.
54
        flags = cls.unpack_flags(data[0:1])
55
        # Parse adj weight
56
        weight = data[1]
57
        # Move pointer 4 bytes: Flags(1) + Weight(1) + Reserved(2)
58
        system_id = ISO.unpack_sysid(data[4:10])
59
        data = data[10:]
60
        # SID/Index/Label: according to the V and L flags, it contains
61
        # either:
62
        # *  A 3 octet local label where the 20 rightmost bits are used for
63
        # 	 encoding the label value.  In this case the V and L flags MUST
64
        # 	 be set.
65
        #
66
        # *  A 4 octet index defining the offset in the SID/Label space
67
        # 	 advertised by this router using the encodings defined in
68
        #  	 Section 3.1.  In this case V and L flags MUST be unset.
69
        sids = []
70
        raw = []
71
        while data:
72
            # Range Size: 3 octet value indicating the number of labels in
73
            # the range.
74
            if int(flags.flags['V']) and int(flags.flags['L']):
75
                sid = unpack('!L', bytes([0]) + data[:3])[0]
76
                data = data[3:]
77
                sids.append(sid)
78
            elif (not flags.flags['V']) and (not flags.flags['L']):
79
                sid = unpack('!I', data[:4])[0]
80
                data = data[4:]
81
                sids.append(sid)
82
            else:
83
                raw.append(hexstring(data))
84
                break
85
86
        return cls([{'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 71 is not entered. Are you sure this can never be the case?
Loading history...
87
88
    def json(self, compact=None):
89
        return '"sr-adj-lan-sids": {}'.format(json.dumps(self.sr_adj_lan_sids))
90
91
    def merge(self, klass):
92
        self.sr_adj_lan_sids.extend(klass.sr_adj_lan_sids)
93