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.List;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
19 import org.opendaylight.protocol.concepts.Ipv4Util;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Update;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.UpdateBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.NlriBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.WithdrawnRoutesBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Preconditions;
33 * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
35 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
38 public class BGPUpdateMessageParser implements MessageParser {
39 public static final int TYPE = 2;
41 private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
44 * Size of the withdrawn_routes_length field, in bytes.
46 public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
49 * Size of the total_path_attr_length field, in bytes.
51 public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
53 private final AttributeRegistry reg;
55 // Constructors -------------------------------------------------------
56 public BGPUpdateMessageParser(final AttributeRegistry reg) {
57 this.reg = Preconditions.checkNotNull(reg);
60 // Getters & setters --------------------------------------------------
63 public Update parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
64 if (body == null || body.length == 0) {
65 throw new IllegalArgumentException("Byte array cannot be null or empty.");
67 logger.trace("Started parsing of update message: {}", Arrays.toString(body));
71 final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(body, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
72 byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
74 final UpdateBuilder eventBuilder = new UpdateBuilder();
76 if (withdrawnRoutesLength > 0) {
77 final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.subByte(body, byteOffset, withdrawnRoutesLength));
78 byteOffset += withdrawnRoutesLength;
79 eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
82 final int totalPathAttrLength = ByteArray.bytesToInt(ByteArray.subByte(body, byteOffset, TOTAL_PATH_ATTR_LENGTH_SIZE));
83 byteOffset += TOTAL_PATH_ATTR_LENGTH_SIZE;
85 if (withdrawnRoutesLength + totalPathAttrLength > body.length) {
86 throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
89 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
90 return eventBuilder.build();
93 if (totalPathAttrLength > 0) {
95 final PathAttributes pathAttributes = reg.parseAttributes(ByteArray.subByte(body, byteOffset, totalPathAttrLength));
96 byteOffset += totalPathAttrLength;
97 eventBuilder.setPathAttributes(pathAttributes);
98 } catch (final BGPParsingException e) {
99 logger.warn("Could not parse BGP attributes", e);
100 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
104 final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.subByte(body, byteOffset, body.length - byteOffset));
105 if (nlri != null && !nlri.isEmpty()) {
106 eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
108 logger.trace("Update message was parsed.");
109 return eventBuilder.build();