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
8 package org.opendaylight.protocol.bgp.parser.impl.message;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
21 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.protocol.util.Ipv4Util;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
32 import org.opendaylight.yangtools.yang.binding.Notification;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
39 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
41 public final class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
43 private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
45 public static final int TYPE = 2;
47 private static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
49 private static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
51 private final AttributeRegistry reg;
53 public BGPUpdateMessageParser(final AttributeRegistry reg) {
54 this.reg = Preconditions.checkNotNull(reg);
58 public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
59 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");
61 final UpdateBuilder builder = new UpdateBuilder();
63 final int withdrawnRoutesLength = buffer.readUnsignedShort();
64 if (withdrawnRoutesLength > 0) {
65 final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
66 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
68 final int totalPathAttrLength = buffer.readUnsignedShort();
70 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
71 return builder.build();
73 if (totalPathAttrLength > 0) {
75 final Attributes pathAttributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength));
76 builder.setAttributes(pathAttributes);
77 } catch (final BGPParsingException | RuntimeException e) {
78 // Catch everything else and turn it into a BGPDocumentedException
79 LOG.warn("Could not parse BGP attributes", e);
80 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
83 final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
84 if (nlri != null && !nlri.isEmpty()) {
85 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
87 final Update msg = builder.build();
88 LOG.debug("BGP Update message was parsed {}.", msg);
93 public void serializeMessage(final Notification message, final ByteBuf bytes) {
94 Preconditions.checkArgument(message instanceof Update, "Message needs to be of type Update");
95 final Update update = (Update) message;
97 final ByteBuf messageBody = Unpooled.buffer();
98 final WithdrawnRoutes withdrawnRoutes = update.getWithdrawnRoutes();
99 if (withdrawnRoutes != null) {
100 final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
101 for (final Ipv4Prefix prefix : withdrawnRoutes.getWithdrawnRoutes()) {
102 withdrawnRoutesBuf.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
104 messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
105 messageBody.writeBytes(withdrawnRoutesBuf);
107 messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE);
109 if (update.getAttributes() != null) {
110 final ByteBuf pathAttributesBuf = Unpooled.buffer();
111 this.reg.serializeAttribute(update.getAttributes(), pathAttributesBuf);
112 messageBody.writeShort(pathAttributesBuf.writerIndex());
113 messageBody.writeBytes(pathAttributesBuf);
115 messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE);
117 final Nlri nlri = update.getNlri();
119 for (final Ipv4Prefix prefix : nlri.getNlri()) {
120 messageBody.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
123 MessageUtil.formatMessage(TYPE, messageBody, bytes);