7d665e94bb4bddfa22476908129192ca88601c2d
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPUpdateMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.bgp.parser.impl.message;
10
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19
20 import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
21 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
22 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
23 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
24 import org.opendaylight.protocol.bgp.parser.BGPError;
25 import org.opendaylight.protocol.bgp.parser.BGPMessageHeader;
26 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
27 import org.opendaylight.protocol.bgp.parser.BGPUpdateEvent;
28 import org.opendaylight.protocol.bgp.parser.BGPUpdateSynchronized;
29 import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateEventBuilder;
30 import org.opendaylight.protocol.bgp.parser.impl.IPv6MP;
31 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute;
32 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute.TypeCode;
33 import org.opendaylight.protocol.bgp.parser.impl.message.update.PathAttributeParser;
34 import org.opendaylight.protocol.util.ByteArray;
35 import org.opendaylight.protocol.concepts.IPv4;
36 import org.opendaylight.protocol.concepts.IPv4Address;
37 import org.opendaylight.protocol.concepts.Prefix;
38 import com.google.common.collect.Lists;
39
40 /**
41  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
42  * 
43  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
44  * 
45  */
46 public class BGPUpdateMessageParser {
47
48         private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
49
50         /**
51          * Size of the withdrawn_routes_length field, in bytes.
52          */
53         public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
54
55         /**
56          * Size of the total_path_attr_length field, in bytes.
57          */
58         public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
59
60         // Constructors -------------------------------------------------------
61
62         public BGPUpdateMessageParser() {
63
64         }
65
66         // Getters & setters --------------------------------------------------
67
68         public static BGPUpdateEvent parse(final byte[] bytes, final int msgLength) throws BGPDocumentedException {
69                 if (bytes == null || bytes.length == 0)
70                         throw new IllegalArgumentException("Byte array cannot be null or empty.");
71                 logger.trace("Started parsing of update message: {}", Arrays.toString(bytes));
72
73                 int byteOffset = 0;
74
75                 final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
76                 byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
77
78                 final BGPUpdateEventBuilder eventBuilder = new BGPUpdateEventBuilder();
79                 eventBuilder.setWithdrawnRoutesLength(withdrawnRoutesLength);
80
81                 Set<Prefix<IPv4Address>> withdrawnRoutes;
82                 if (withdrawnRoutesLength > 0) {
83                         withdrawnRoutes = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, withdrawnRoutesLength));
84                         byteOffset += withdrawnRoutesLength;
85                 } else {
86                         withdrawnRoutes = Collections.emptySet();
87                 }
88                 eventBuilder.setWithdrawnRoutes(withdrawnRoutes);
89
90                 final int totalPathAttrLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TOTAL_PATH_ATTR_LENGTH_SIZE));
91                 byteOffset += TOTAL_PATH_ATTR_LENGTH_SIZE;
92                 eventBuilder.setTotalPathAttrLength(totalPathAttrLength);
93
94                 if (withdrawnRoutesLength + totalPathAttrLength + BGPMessageHeader.COMMON_HEADER_LENGTH > msgLength)
95                         throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
96
97                 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
98                         final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
99
100                                 private static final long serialVersionUID = 5709361453437508337L;
101
102                                 @Override
103                                 public BGPTableType getTableType() {
104                                         return new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast);
105                                 }
106                         };
107                         return event;
108                 }
109
110                 List<PathAttribute> pathAttributes;
111                 if (totalPathAttrLength > 0) {
112                         pathAttributes = parsePathAttributes(ByteArray.subByte(bytes, byteOffset, totalPathAttrLength));
113                         byteOffset += totalPathAttrLength;
114                         if (pathAttributes.get(0).getType() == TypeCode.MP_UNREACH_NLRI && totalPathAttrLength == 6) {
115                                 if (pathAttributes.get(0).getValue() instanceof IPv6MP) {
116                                         final BGPUpdateEvent event = new BGPUpdateSynchronized() {
117
118                                                 private static final long serialVersionUID = -6026212683738125407L;
119
120                                                 @Override
121                                                 public BGPTableType getTableType() {
122                                                         return new BGPTableType(BGPAddressFamily.IPv6, BGPSubsequentAddressFamily.Unicast);
123                                                 }
124
125                                         };
126                                         return event;
127                                 } else if (pathAttributes.get(0).getValue() == null) {
128                                         final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
129
130                                                 private static final long serialVersionUID = 5888562784007786559L;
131
132                                                 @Override
133                                                 public BGPTableType getTableType() {
134                                                         return new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Unicast);
135                                                 }
136
137                                         };
138                                         return event;
139                                 }
140                         }
141                 } else {
142                         pathAttributes = Collections.emptyList();
143                 }
144                 eventBuilder.setPathAttributes(pathAttributes);
145
146                 final Set<Prefix<IPv4Address>> nlri = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, bytes.length - byteOffset));
147                 eventBuilder.setNlri(nlri);
148
149                 try {
150                         logger.trace("Update message was parsed.");
151                         return eventBuilder.buildEvent();
152                 } catch (final BGPParsingException e) {
153                         throw new BGPDocumentedException("Parsing unsuccessful: {}" + e.getMessage(), BGPError.MALFORMED_ATTR_LIST);
154                 }
155         }
156
157         /**
158          * Parse different Path Attributes from given bytes.
159          * 
160          * @param bytes byte array to be parsed
161          * @return list of Path Attributes
162          * @throws BGPParsingException
163          */
164         private static List<PathAttribute> parsePathAttributes(byte[] bytes) throws BGPDocumentedException {
165                 if (bytes.length == 0) {
166                         return Collections.emptyList();
167                 }
168                 final List<PathAttribute> list = Lists.newArrayList();
169                 while (bytes.length != 0) {
170                         PathAttribute attr;
171                         try {
172                                 attr = PathAttributeParser.parseAttribute(bytes);
173                                 bytes = ByteArray.cutBytes(bytes,
174                                                 PathAttribute.ATTR_FLAGS_SIZE + PathAttribute.ATTR_TYPE_CODE_SIZE + attr.getAttrLengthSize() + attr.getLength());
175                                 list.add(attr);
176                         } catch (final BGPParsingException e) {
177                                 logger.warn("Could not parse BGP attributes: {}", e.getMessage(), e);
178                                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST);
179                         }
180                 }
181                 return list;
182         }
183 }