| Total Complexity | 4 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # encoding: utf-8 |
||
| 2 | """ |
||
| 3 | igpmetric.py |
||
| 4 | |||
| 5 | Created by Evelio Vila on 2016-12-01. |
||
| 6 | Copyright (c) 2014-2017 Exa Networks. All rights reserved. |
||
| 7 | """ |
||
| 8 | |||
| 9 | from struct import unpack |
||
| 10 | |||
| 11 | from exabgp.bgp.message.notification import Notify |
||
| 12 | |||
| 13 | from exabgp.bgp.message.update.attribute.bgpls.linkstate import LinkState |
||
| 14 | from exabgp.bgp.message.update.attribute.bgpls.linkstate import BaseLS |
||
| 15 | |||
| 16 | |||
| 17 | # The IGP Metric TLV carries the metric for this link. The length of |
||
| 18 | # this TLV is variable, depending on the metric width of the underlying |
||
| 19 | # protocol. IS-IS small metrics have a length of 1 octet (the two most |
||
| 20 | # significant bits are ignored). OSPF link metrics have a length of 2 |
||
| 21 | # octets. IS-IS wide metrics have a length of 3 octets. |
||
| 22 | # |
||
| 23 | # 0 1 2 3 |
||
| 24 | # 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 |
||
| 25 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
| 26 | # | Type | Length | |
||
| 27 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
| 28 | # // IGP Link Metric (variable length) // |
||
| 29 | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
||
| 30 | |||
| 31 | |||
| 32 | @LinkState.register() |
||
| 33 | class IgpMetric(BaseLS): |
||
| 34 | TLV = 1095 |
||
| 35 | REPR = 'IGP Metric' |
||
| 36 | JSON = 'igp-metric' |
||
| 37 | |||
| 38 | @classmethod |
||
| 39 | def unpack(cls, data, length): |
||
| 40 | if len(data) == 2: |
||
| 41 | # OSPF |
||
| 42 | return cls(unpack('!H', data)[0]) |
||
| 43 | |||
| 44 | if len(data) == 1: |
||
| 45 | # ISIS small metrics |
||
| 46 | return cls(data[0]) |
||
| 47 | |||
| 48 | if len(data) == 3: |
||
| 49 | # ISIS wide metrics |
||
| 50 | return cls(unpack('!L', bytes([0]) + data)[0]) |
||
| 51 | |||
| 52 | raise Notify(3, 5, "Incorrect IGP Metric Size") |
||
| 53 |