1 | # encoding: utf-8 |
||
2 | """ |
||
3 | srprefix.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.bgp.message.notification import Notify |
||
14 | from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE |
||
15 | from exabgp.bgp.message.update.attribute.bgpls.linkstate import LsGenericFlags |
||
16 | |||
17 | # draft-gredler-idr-bgp-ls-segment-routing-ext-03 |
||
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 | Algorithm | Reserved | |
||
24 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
25 | # | SID/Index/Label (variable) | |
||
26 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
27 | |||
28 | # draft-ietf-isis-segment-routing-extensions Prefix-SID Sub-TLV |
||
29 | |||
30 | View Code Duplication | @LINKSTATE.register() |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
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 |