Completed
Push — master ( ae553d...3df76a )
by Thomas
25:18 queued 11:15
created

lib/exabgp/bgp/message/open/capability/addpath.py (2 issues)

1
# encoding: utf-8
2
"""
3
addpath.py
4
5
Created by Thomas Mangin on 2012-07-17.
6
Copyright (c) 2009-2017 Exa Networks. All rights reserved.
7
License: 3-clause BSD. (See the COPYRIGHT file)
8
"""
9
10
from struct import pack
11
from exabgp.protocol.family import AFI
12
from exabgp.protocol.family import SAFI
13
from exabgp.bgp.message.open.capability.capability import Capability
14
15
# ====================================================================== AddPath
16
#
17
18
19
@Capability.register()
20
class AddPath(Capability, dict):
21
    ID = Capability.CODE.ADD_PATH
22
23
    string = {
24
        0: 'disabled',
25
        1: 'receive',
26
        2: 'send',
27
        3: 'send/receive',
28
    }
29
30
    def __init__(self, families=(), send_receive=0):
31
        for afi, safi in families:
32
            self.add_path(afi, safi, send_receive)
33
34
    def add_path(self, afi, safi, send_receive):
35
        self[(afi, safi)] = send_receive
36
37
    def __str__(self):
38
        return (
39
            'AddPath('
40
            + ','.join(
41
                [
42
                    "%s %s %s" % (self.string[self[aafi]], xafi, xsafi)
43
                    for (aafi, xafi, xsafi) in [((afi, safi), str(afi), str(safi)) for (afi, safi) in self]
44
                ]
45
            )
46
            + ')'
47
        )
48
49
    def json(self):
50
        families = ','.join(
51
            '"%s/%s": "%s"' % (xafi, xsafi, self.string[self[aafi]])
52
            for (aafi, xafi, xsafi) in (((afi, safi), str(afi), str(safi)) for (afi, safi) in self)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable safi does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable afi does not seem to be defined.
Loading history...
53
        )
54
        return '{ "name": "addpath"%s%s }' % (', ' if families else '', families)
55
56
    def extract(self):
57
        rs = b''
58
        for v in self:
59
            if self[v]:
60
                rs += v[0].pack() + v[1].pack() + pack('!B', self[v])
61
        return [
62
            rs,
63
        ]
64
65
    @staticmethod
66
    def unpack_capability(instance, data, capability=None):  # pylint: disable=W0613
67
        # XXX: FIXME: should check that we have not yet seen the capability
68
        while data:
69
            afi = AFI.unpack(data[:2])
70
            safi = SAFI.unpack(data[2])
71
            sr = data[3]
72
            instance.add_path(afi, safi, sr)
73
            data = data[4:]
74
        return instance
75