Completed
Push — master ( 229cac...232edf )
by Thomas
10:56
created

lib/exabgp/configuration/flow/__init__.py (1 issue)

1
# encoding: utf-8
2
"""
3
__init__.py
4
5
Created by Thomas Mangin on 2015-06-04.
6
Copyright (c) 2009-2017 Exa Networks. All rights reserved.
7
License: 3-clause BSD. (See the COPYRIGHT file)
8
"""
9
10
from exabgp.protocol.family import SAFI
11
12
from exabgp.configuration.core import Section
13
14
from exabgp.configuration.flow.route import ParseFlowRoute
15
from exabgp.configuration.flow.route import ParseFlowMatch
16
from exabgp.configuration.flow.route import ParseFlowThen
17
from exabgp.configuration.flow.route import ParseFlowScope
18
19
from exabgp.rib.change import Change
20
from exabgp.bgp.message.update.nlri import Flow
21
from exabgp.bgp.message.update.attribute import Attributes
22
from exabgp.bgp.message.update.nlri.qualifier import RouteDistinguisher
23
24
25
class ParseFlow(Section):
26
    syntax = 'flow {\n' '  %s' '}' % ';\n  '.join(ParseFlowRoute.syntax.split('\n'))
27
28
    name = 'flow'
29
30
    known = dict(ParseFlowMatch.known)
31
    known.update(ParseFlowThen.known)
32
    known.update(ParseFlowScope.known)
33
34
    action = dict(ParseFlowMatch.action)
35
    action.update(ParseFlowThen.action)
36
    action.update(ParseFlowScope.action)
37
38
    def __init__(self, tokeniser, scope, error):
39
        Section.__init__(self, tokeniser, scope, error)
40
41
    def clear(self):
42
        pass
43
44
    def pre(self):
45
        return True
46
47
    def post(self):
48
        self.scope.set('routes', self.scope.get_routes())
49
        return True
50
51
    def check(self):
52
        return True
53
54
55
@ParseFlow.register('route', 'append-route')
56
def route(tokeniser):
57
    change = Change(Flow(), Attributes())
58
59 View Code Duplication
    while True:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
60
        command = tokeniser()
61
62
        if not command:
63
            break
64
65
        action = ParseFlow.action[command]
66
67
        if action == 'nlri-add':
68
            for adding in ParseFlow.known[command](tokeniser):
69
                change.nlri.add(adding)
70
        elif action == 'attribute-add':
71
            change.attributes.add(ParseFlow.known[command](tokeniser))
72
        elif action == 'nexthop-and-attribute':
73
            nexthop, attribute = ParseFlow.known[command](tokeniser)
74
            change.nlri.nexthop = nexthop
75
            change.attributes.add(attribute)
76
        elif action == 'nop':
77
            pass  # yes nothing to do !
78
        else:
79
            raise ValueError('flow: unknown command "%s"' % command)
80
81
    if change.nlri.rd is not RouteDistinguisher.NORD:
82
        change.nlri.safi = SAFI.flow_vpn
83
84
    return [change]
85