Completed
Push — master ( 25ce4f...b9bc4f )
by Thomas
11:58
created

)   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 1
dl 4
loc 4
rs 10
c 0
b 0
f 0
1
# encoding: utf-8
2
"""
3
announce/label.py
4
5
Created by Thomas Mangin on 2017-07-05.
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.ip import NoNextHop
11
12
from exabgp.rib.change import Change
13
14
from exabgp.bgp.message import OUT
15
16
from exabgp.protocol.family import AFI
17
from exabgp.protocol.family import SAFI
18
19
from exabgp.bgp.message.update.nlri.inet import INET
20
from exabgp.bgp.message.update.nlri.cidr import CIDR
21
from exabgp.bgp.message.update.attribute import Attributes
22
23
from exabgp.configuration.announce import ParseAnnounce
24
from exabgp.configuration.announce.ip import AnnounceIP
25
26
from exabgp.configuration.static.parser import prefix
27
from exabgp.configuration.static.parser import path_information
28
29
30
class AnnouncePath (AnnounceIP):
31
	# put next-hop first as it is a requirement atm
32
	definition = [
33
		'label <15 bits number>',
34
	] + AnnounceIP.definition
35
36
	syntax = \
37
		'<safi> <ip>/<netmask> { ' \
38
		'\n   ' + ' ;\n   '.join(definition) + '\n}'
39
40
	known = dict(AnnounceIP.known,**{
41
		'path-information':    path_information,
42
	})
43
44
	action = dict(AnnounceIP.action,**{
45
		'path-information':    'nlri-set',
46
	})
47
48
	assign = dict(AnnounceIP.assign,**{
49
		'path-information':    'path_info',
50
	})
51
52
	name = 'path'
53
	afi = None
54
55
	def __init__ (self, tokeniser, scope, error, logger):
56
		AnnounceIP.__init__(self,tokeniser,scope,error,logger)
57
58
	def clear (self):
59
		return True
60
61
	@staticmethod
62
	def check (change,afi):
63
		if not AnnounceIP.check(change,afi):
64
			return False
65
66
		return True
67
68
69 View Code Duplication
def ip_unicast (tokeniser,afi,safi):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
70
	action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW
71
	ipmask = prefix(tokeniser)
72
73
	nlri = INET(afi, safi, action)
74
	nlri.cidr = CIDR(ipmask.pack(),ipmask.mask)
75
76
	change = Change(
77
		nlri,
78
		Attributes()
79
	)
80
81
	while True:
82
		command = tokeniser()
83
84
		if not command:
85
			break
86
87
		action = AnnouncePath.action.get(command,'')
88
89
		if action == 'attribute-add':
90
			change.attributes.add(AnnouncePath.known[command](tokeniser))
91
		elif action == 'nlri-set':
92
			change.nlri.assign(AnnouncePath.assign[command],AnnouncePath.known[command](tokeniser))
93
		elif action == 'nexthop-and-attribute':
94
			nexthop,attribute = AnnouncePath.known[command](tokeniser)
95
			change.nlri.nexthop = nexthop
96
			change.attributes.add(attribute)
97
		else:
98
			raise ValueError('unknown command "%s"' % command)
99
100
	if not AnnouncePath.check(change,afi):
101
		raise ValueError('invalid announcement (missing next-hop ?)')
102
103
	return [change]
104
105
106
@ParseAnnounce.register('unicast','extend-name','ipv4')
107
def unicast_v4 (tokeniser):
108
	return ip_unicast(tokeniser,AFI.ipv4,SAFI.unicast)
109
110
111
@ParseAnnounce.register('unicast','extend-name','ipv6')
112
def unicast_v6 (tokeniser):
113
	return ip_unicast(tokeniser,AFI.ipv6,SAFI.unicast)
114