Completed
Push — master ( 232edf...ae553d )
by Thomas
12:52
created

message/update/attribute/bgpls/link/sradjlan.py (1 issue)

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 LsGenericFlags
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 View Code Duplication
@LINKSTATE.register()
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
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