-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUnicastPrefix.py
More file actions
148 lines (130 loc) · 5.66 KB
/
UnicastPrefix.py
File metadata and controls
148 lines (130 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Copyright (c) 2015-2016 Cisco Systems, Inc. and others. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v1.0 which accompanies this distribution,
and is available at http:#www.eclipse.org/legal/epl-v10.html
"""
from Base import Base
from FieldProcessors import ParseNullAsEmpty, ParseLongEmptyAsZero, ParseInt, NotNull, ParseTimestamp, ParseLong
from Message import Message
from MsgBusFields import MsgBusFields
class UnicastPrefix(Base):
"""
Format class for unicast_prefix parsed messages (openbmp.parsed.unicast_prefix)
Schema Version: 1.4
"""
minimum_header_names = [
MsgBusFields.ACTION.get_name(),
MsgBusFields.SEQUENCE.get_name(),
MsgBusFields.HASH.get_name(),
MsgBusFields.ROUTER_HASH.get_name(),
MsgBusFields.ROUTER_IP.get_name(),
MsgBusFields.BASE_ATTR_HASH.get_name(),
MsgBusFields.PEER_HASH.get_name(),
MsgBusFields.PEER_IP.get_name(),
MsgBusFields.PEER_ASN.get_name(),
MsgBusFields.TIMESTAMP.get_name(),
MsgBusFields.PREFIX.get_name(),
MsgBusFields.PREFIX_LEN.get_name(),
MsgBusFields.IS_IPV4.get_name(),
MsgBusFields.ORIGIN.get_name(),
MsgBusFields.AS_PATH.get_name(),
MsgBusFields.AS_PATH_COUNT.get_name(),
MsgBusFields.ORIGIN_AS.get_name(),
MsgBusFields.NEXTHOP.get_name(),
MsgBusFields.MED.get_name(),
MsgBusFields.LOCAL_PREF.get_name(),
MsgBusFields.AGGREGATOR.get_name(),
MsgBusFields.COMMUNITY_LIST.get_name(),
MsgBusFields.EXT_COMMUNITY_LIST.get_name(),
MsgBusFields.CLUSTER_LIST.get_name(),
MsgBusFields.ISATOMICAGG.get_name(),
MsgBusFields.IS_NEXTHOP_IPV4.get_name(),
MsgBusFields.ORIGINATOR_ID.get_name()
]
def __init__(self, message, validate=True, required_fields=None):
"""
Handle the message by parsing it and storing the data in memory.
:param message: 'Message' object.
:param validate: If required to validate every field with its corresponding processor
:param validate: If required to validate every field with its corresponding processor
:param required_fields: If needed to parse only feq fields ans speed up parsing.
Example: {10: 'prefix', 11: "prefix_len"} where:
"10" and "11" - positions of fields in MESSAGE_BUS_API,
"prefix" and "prefix_len" - name of parsed fields in resulting dictionary.
"""
if not isinstance(message, Message):
raise TypeError("Expected Message object instead of type " + type(message))
version = message.get_version()
data = message.get_content()
super(UnicastPrefix, self).__init__()
if version >= float(1.3):
version_specific_headers = [
MsgBusFields.PATH_ID.get_name(),
MsgBusFields.LABELS.get_name(),
MsgBusFields.ISPREPOLICY.get_name(),
MsgBusFields.IS_ADJ_RIB_IN.get_name()
]
elif version >= float(1.1):
version_specific_headers = [
MsgBusFields.PATH_ID.get_name(),
MsgBusFields.LABELS.get_name()
]
else:
version_specific_headers = []
# Concatenate minimum header names and version specific header names.
self.header_names = UnicastPrefix.minimum_header_names + version_specific_headers
self.spec_version = version
self.processors = self.get_processors()
if data:
self.parse(version, data, validate=validate, required_fields=required_fields)
def get_processors(self):
"""
Processors used for each field.
Order matters and must match the same order as defined in headerNames
:return: array of cell processors.
"""
default_cell_processors = [
NotNull(), # action
ParseLong(), # seq
NotNull(), # hash
NotNull(), # router hash
NotNull(), # router_ip
ParseNullAsEmpty(), # base_attr_hash
NotNull(), # peer_hash
NotNull(), # peer_ip
ParseLong(), # peer_asn
ParseTimestamp(), # timestamp
NotNull(), # prefix
ParseInt(), # prefix_len
ParseInt(), # isIPv4
ParseNullAsEmpty(), # origin
ParseNullAsEmpty(), # as_path
ParseLongEmptyAsZero(), # as_path_count
ParseLongEmptyAsZero(), # origin_as
ParseNullAsEmpty(), # nexthop
ParseLongEmptyAsZero(), # med
ParseLongEmptyAsZero(), # local_pref
ParseNullAsEmpty(), # aggregator
ParseNullAsEmpty(), # community_list
ParseNullAsEmpty(), # ext_community_list
ParseNullAsEmpty(), # cluster_list
ParseLongEmptyAsZero(), # isAtomicAgg
ParseLongEmptyAsZero(), # isNexthopIPv4
ParseNullAsEmpty(), # originator_id
]
if self.spec_version >= float(1.3):
version_specific_processors = [
ParseLongEmptyAsZero(), # Path ID
ParseNullAsEmpty(), # Labels
ParseLongEmptyAsZero(), # isPrePolicy
ParseLongEmptyAsZero() # isAdjRibIn
]
elif self.spec_version >= float(1.1):
version_specific_processors = [
ParseLongEmptyAsZero(), # Path ID
ParseNullAsEmpty(), # Labels
]
else:
version_specific_processors = []
return default_cell_processors + version_specific_processors