Passed
Pull Request — master (#923)
by
unknown
12:29
created

ExtendedCommunitiesIPv6.unpack()   A

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nop 2
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
1
# encoding: utf-8
2
"""
3
communities.py
4
5
Created by Thomas Mangin on 2009-11-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.util import ordinal
11
from exabgp.bgp.message.update.attribute.attribute import Attribute
12
from exabgp.bgp.message.update.attribute.community.extended.community import ExtendedCommunity
13
from exabgp.bgp.message.update.attribute.community.extended.community import ExtendedCommunityIPv6
14
from exabgp.bgp.message.update.attribute.community.initial.communities import Communities
15
16
from exabgp.bgp.message.notification import Notify
17
18
19
# ===================================================== ExtendedCommunities (16)
20
# https://www.iana.org/assignments/bgp-extended-communities
21
22
@Attribute.register()
23
class ExtendedCommunities (Communities):
24
	ID = Attribute.CODE.EXTENDED_COMMUNITY
25
26
	@staticmethod
27
	def unpack (data, negotiated):
28
		communities = ExtendedCommunities()
29
		while data:
30
			if data and len(data) < 8:
31
				raise Notify(3,1,'could not decode extended community %s' % str([hex(ordinal(_)) for _ in data]))
32
			communities.add(ExtendedCommunity.unpack(data[:8],negotiated))
33
			data = data[8:]
34
		return communities
35
36
# ===================================================== ExtendedCommunitiesIPv6 (25)
37
# RFC 5701
38
@Attribute.register()
39
class ExtendedCommunitiesIPv6 (Communities):
40
	ID = Attribute.CODE.IPV6_EXTENDED_COMMUNITY
41
42
	@staticmethod
43
	def unpack (data, negotiated):
44
		communities = ExtendedCommunitiesIPv6()
45
		while data:
46
			if data and len(data) < 20:
47
				raise Notify(3,1,'could not decode ipv6 extended community %s' % str([hex(ordinal(_)) for _ in data]))
48
			communities.add(ExtendedCommunityIPv6.unpack(data[:20],negotiated))
49
			data = data[20:]
50
		return communities
51