2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.parser.impl.message;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
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;
41 * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
43 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
46 public class BGPUpdateMessageParser {
48 private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
51 * Size of the withdrawn_routes_length field, in bytes.
53 public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
56 * Size of the total_path_attr_length field, in bytes.
58 public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
60 // Constructors -------------------------------------------------------
62 public BGPUpdateMessageParser() {
66 // Getters & setters --------------------------------------------------
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));
75 final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
76 byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
78 final BGPUpdateEventBuilder eventBuilder = new BGPUpdateEventBuilder();
79 eventBuilder.setWithdrawnRoutesLength(withdrawnRoutesLength);
81 Set<Prefix<IPv4Address>> withdrawnRoutes;
82 if (withdrawnRoutesLength > 0) {
83 withdrawnRoutes = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, withdrawnRoutesLength));
84 byteOffset += withdrawnRoutesLength;
86 withdrawnRoutes = Collections.emptySet();
88 eventBuilder.setWithdrawnRoutes(withdrawnRoutes);
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);
94 if (withdrawnRoutesLength + totalPathAttrLength + BGPMessageHeader.COMMON_HEADER_LENGTH > msgLength)
95 throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
97 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
98 final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
100 private static final long serialVersionUID = 5709361453437508337L;
103 public BGPTableType getTableType() {
104 return new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast);
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() {
118 private static final long serialVersionUID = -6026212683738125407L;
121 public BGPTableType getTableType() {
122 return new BGPTableType(BGPAddressFamily.IPv6, BGPSubsequentAddressFamily.Unicast);
127 } else if (pathAttributes.get(0).getValue() == null) {
128 final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
130 private static final long serialVersionUID = 5888562784007786559L;
133 public BGPTableType getTableType() {
134 return new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Unicast);
142 pathAttributes = Collections.emptyList();
144 eventBuilder.setPathAttributes(pathAttributes);
146 final Set<Prefix<IPv4Address>> nlri = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, bytes.length - byteOffset));
147 eventBuilder.setNlri(nlri);
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);
158 * Parse different Path Attributes from given bytes.
160 * @param bytes byte array to be parsed
161 * @return list of Path Attributes
162 * @throws BGPParsingException
164 private static List<PathAttribute> parsePathAttributes(byte[] bytes) throws BGPDocumentedException {
165 if (bytes.length == 0) {
166 return Collections.emptyList();
168 final List<PathAttribute> list = Lists.newArrayList();
169 while (bytes.length != 0) {
172 attr = PathAttributeParser.parseAttribute(bytes);
173 bytes = ByteArray.cutBytes(bytes,
174 PathAttribute.ATTR_FLAGS_SIZE + PathAttribute.ATTR_TYPE_CODE_SIZE + attr.getAttrLengthSize() + attr.getLength());
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);