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 com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
17 import java.util.Arrays;
18 import java.util.List;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPError;
22 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
23 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
24 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
25 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
26 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
27 import org.opendaylight.protocol.concepts.Ipv4Util;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
35 import org.opendaylight.yangtools.yang.binding.Notification;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
42 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
44 public class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
45 public static final int TYPE = 2;
47 private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
50 * Size of the withdrawn_routes_length field, in bytes.
52 public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
54 * Size of the total_path_attr_length field, in bytes.
56 public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
58 private final AttributeRegistry reg;
60 // Constructors -------------------------------------------------------
61 public BGPUpdateMessageParser(final AttributeRegistry reg) {
62 this.reg = Preconditions.checkNotNull(reg);
65 // Getters & setters --------------------------------------------------
68 public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
69 Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
70 LOG.trace("Started parsing of update message: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
72 final int withdrawnRoutesLength = buffer.readUnsignedShort();
73 final UpdateBuilder eventBuilder = new UpdateBuilder();
75 if (withdrawnRoutesLength > 0) {
76 final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
77 eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
79 final int totalPathAttrLength = buffer.readUnsignedShort();
81 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
82 return eventBuilder.build();
84 if (totalPathAttrLength > 0) {
86 final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
87 buffer.skipBytes(totalPathAttrLength);
88 eventBuilder.setPathAttributes(pathAttributes);
89 } catch (final BGPParsingException | RuntimeException e) {
90 // Catch everything else and turn it into a BGPDocumentedException
91 LOG.warn("Could not parse BGP attributes", e);
92 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
95 final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
96 if (nlri != null && !nlri.isEmpty()) {
97 eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
99 Update msg = eventBuilder.build();
100 LOG.debug("BGP Update message was parsed {}.", msg);
105 public void serializeMessage(Notification message,ByteBuf bytes) {
106 if (message == null) {
107 throw new IllegalArgumentException("BGPUpdate message cannot be null");
109 LOG.trace("Started serializing update message: {}", message);
110 final Update update = (Update) message;
112 ByteBuf messageBody = Unpooled.buffer();
114 if (update.getPathAttributes() != null) {
115 this.reg.serializeAttribute(update.getPathAttributes(), messageBody);
118 LOG.trace("Update message serialized to {}", ByteBufUtil.hexDump(messageBody));
119 //FIXME: switch to ByteBuf
120 bytes.writeBytes(MessageUtil.formatMessage(TYPE,ByteArray.getAllBytes(messageBody)));