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 io.netty.buffer.ByteBuf;
13 import java.util.Arrays;
14 import java.util.List;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
21 import org.opendaylight.protocol.concepts.Ipv4Util;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.base.Preconditions;
35 * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
37 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
40 public class BGPUpdateMessageParser implements MessageParser {
41 public static final int TYPE = 2;
43 private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
46 * Size of the withdrawn_routes_length field, in bytes.
48 public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
51 * Size of the total_path_attr_length field, in bytes.
53 public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
55 private final AttributeRegistry reg;
57 // Constructors -------------------------------------------------------
58 public BGPUpdateMessageParser(final AttributeRegistry reg) {
59 this.reg = Preconditions.checkNotNull(reg);
62 // Getters & setters --------------------------------------------------
65 public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
66 Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
67 LOG.trace("Started parsing of update message: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
69 final int withdrawnRoutesLength = buffer.readUnsignedShort();
70 final UpdateBuilder eventBuilder = new UpdateBuilder();
72 if (withdrawnRoutesLength > 0) {
73 final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
74 eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
76 final int totalPathAttrLength = buffer.readUnsignedShort();
78 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
79 return eventBuilder.build();
81 if (totalPathAttrLength > 0) {
83 final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
84 buffer.skipBytes(totalPathAttrLength);
85 eventBuilder.setPathAttributes(pathAttributes);
86 } catch (final BGPParsingException | RuntimeException e) {
87 // Catch everything else and turn it into a BGPDocumentedException
88 LOG.warn("Could not parse BGP attributes", e);
89 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
92 final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
93 if (nlri != null && !nlri.isEmpty()) {
94 eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
96 Update msg = eventBuilder.build();
97 LOG.debug("BGP Update message was parsed {}.", msg);