2 * Copyright (c) 2016 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;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
16 import java.util.Optional;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
21 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
23 import org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
24 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.RouteRefresh;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.RouteRefreshBuilder;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public final class BGPRouteRefreshMessageParser implements MessageParser, MessageSerializer {
33 // https://tools.ietf.org/html/rfc2918#section-3
35 private static final Logger LOG = LoggerFactory.getLogger(BGPRouteRefreshMessageParser.class);
37 public static final int TYPE = 5;
38 private static final int TRIPLET_BYTE_SIZE = 4;
40 private final AddressFamilyRegistry afiReg;
41 private final SubsequentAddressFamilyRegistry safiReg;
43 public BGPRouteRefreshMessageParser(final AddressFamilyRegistry afiReg, final SubsequentAddressFamilyRegistry safiReg) {
44 this.afiReg = requireNonNull(afiReg);
45 this.safiReg = requireNonNull(safiReg);
49 * Serializes BGP Route Refresh message.
51 * @param message to be serialized
52 * @param bytes ByteBuf where the message will be serialized
55 public void serializeMessage(final Notification message, final ByteBuf bytes) {
56 Preconditions.checkArgument(message instanceof RouteRefresh, "Message is not of type RouteRefresh.");
57 final RouteRefresh msg = (RouteRefresh) message;
59 final ByteBuf msgBuf = Unpooled.buffer(TRIPLET_BYTE_SIZE);
60 MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(this.afiReg, this.safiReg, msg.getAfi(), msg.getSafi(), msgBuf);
62 LOG.trace("RouteRefresh message serialized to: {}", ByteBufUtil.hexDump(msgBuf));
63 MessageUtil.formatMessage(TYPE, msgBuf, bytes);
67 * Parses BGP Route Refresh message to bytes.
69 * @param body ByteBuf to be parsed
70 * @param messageLength the length of the message
71 * @return {@link RouteRefresh} which represents BGP notification message
72 * @throws BGPDocumentedException if parsing goes wrong
75 public RouteRefresh parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
76 Preconditions.checkArgument(body != null, "Body buffer cannot be null.");
77 if (body.readableBytes() < TRIPLET_BYTE_SIZE) {
78 throw BGPDocumentedException.badMessageLength("RouteRefresh message is too small.", messageLength);
80 final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, this.afiReg, this.safiReg);
81 if (!parsedAfiSafi.isPresent()) {
82 throw new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
84 return new RouteRefreshBuilder(parsedAfiSafi.get()).build();