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.opendaylight.protocol.bgp.concepts.BGPTableType;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.BGPUpdateEvent;
21 import org.opendaylight.protocol.bgp.parser.BGPUpdateSynchronized;
22 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
23 import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateEventBuilder;
24 import org.opendaylight.protocol.bgp.parser.impl.IPv6MP;
25 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute;
26 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute.TypeCode;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.PathAttributeParser;
28 import org.opendaylight.protocol.concepts.IPv4;
29 import org.opendaylight.protocol.concepts.IPv4Address;
30 import org.opendaylight.protocol.concepts.Prefix;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.common.collect.Lists;
42 * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
44 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
47 public class BGPUpdateMessageParser {
49 private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
52 * Size of the withdrawn_routes_length field, in bytes.
54 public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
57 * Size of the total_path_attr_length field, in bytes.
59 public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
61 // Constructors -------------------------------------------------------
63 public BGPUpdateMessageParser() {
67 // Getters & setters --------------------------------------------------
69 public static BGPUpdateEvent parse(final byte[] bytes, final int msgLength) throws BGPDocumentedException {
70 if (bytes == null || bytes.length == 0) {
71 throw new IllegalArgumentException("Byte array cannot be null or empty.");
73 logger.trace("Started parsing of update message: {}", Arrays.toString(bytes));
77 final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
78 byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
80 final BGPUpdateEventBuilder eventBuilder = new BGPUpdateEventBuilder();
81 eventBuilder.setWithdrawnRoutesLength(withdrawnRoutesLength);
83 Set<Prefix<IPv4Address>> withdrawnRoutes;
84 if (withdrawnRoutesLength > 0) {
85 withdrawnRoutes = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, withdrawnRoutesLength));
86 byteOffset += withdrawnRoutesLength;
88 withdrawnRoutes = Collections.emptySet();
90 eventBuilder.setWithdrawnRoutes(withdrawnRoutes);
92 final int totalPathAttrLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TOTAL_PATH_ATTR_LENGTH_SIZE));
93 byteOffset += TOTAL_PATH_ATTR_LENGTH_SIZE;
94 eventBuilder.setTotalPathAttrLength(totalPathAttrLength);
96 if (withdrawnRoutesLength + totalPathAttrLength + BGPMessageFactoryImpl.COMMON_HEADER_LENGTH > msgLength) {
97 throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
100 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
101 final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
103 public BGPTableType getTableType() {
104 return new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
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 public BGPTableType getTableType() {
119 return new BGPTableType(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class);
123 } else if (pathAttributes.get(0).getValue() == null) {
124 final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
126 public BGPTableType getTableType() {
127 return new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
134 pathAttributes = Collections.emptyList();
136 eventBuilder.setPathAttributes(pathAttributes);
138 final Set<Prefix<IPv4Address>> nlri = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, bytes.length - byteOffset));
139 eventBuilder.setNlri(nlri);
142 logger.trace("Update message was parsed.");
143 return eventBuilder.buildEvent();
144 } catch (final BGPParsingException e) {
145 throw new BGPDocumentedException("Parsing unsuccessful: {}" + e.getMessage(), BGPError.MALFORMED_ATTR_LIST);
150 * Parse different Path Attributes from given bytes.
152 * @param bytes byte array to be parsed
153 * @return list of Path Attributes
154 * @throws BGPParsingException
156 private static List<PathAttribute> parsePathAttributes(byte[] bytes) throws BGPDocumentedException {
157 if (bytes.length == 0) {
158 return Collections.emptyList();
160 final List<PathAttribute> list = Lists.newArrayList();
161 while (bytes.length != 0) {
164 attr = PathAttributeParser.parseAttribute(bytes);
165 bytes = ByteArray.cutBytes(bytes,
166 PathAttribute.ATTR_FLAGS_SIZE + PathAttribute.ATTR_TYPE_CODE_SIZE + attr.getAttrLengthSize() + attr.getLength());
168 } catch (final BGPParsingException e) {
169 logger.warn("Could not parse BGP attributes: {}", e.getMessage(), e);
170 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST);