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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
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.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
23 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
24 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefresh;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefreshBuilder;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class BGPRouteRefreshMessageParser implements MessageParser, MessageSerializer {
32 // https://tools.ietf.org/html/rfc2918#section-3
34 private static final Logger LOG = LoggerFactory.getLogger(BGPRouteRefreshMessageParser.class);
36 public static final int TYPE = 5;
37 private static final int TRIPLET_BYTE_SIZE = 4;
38 private static final String ARGUMENT_ERROR = "Message is not of type RouteRefresh.";
40 private final AddressFamilyRegistry afiReg;
41 private final SubsequentAddressFamilyRegistry safiReg;
43 public BGPRouteRefreshMessageParser(final AddressFamilyRegistry afiReg,
44 final SubsequentAddressFamilyRegistry safiReg) {
45 this.afiReg = requireNonNull(afiReg);
46 this.safiReg = requireNonNull(safiReg);
50 * Serializes BGP Route Refresh message.
52 * @param message to be serialized
53 * @param bytes ByteBuf where the message will be serialized
56 public void serializeMessage(final Notification<?> message, final ByteBuf bytes) {
57 checkArgument(message instanceof RouteRefresh, ARGUMENT_ERROR);
58 final RouteRefresh msg = (RouteRefresh) message;
60 final ByteBuf msgBuf = Unpooled.buffer(TRIPLET_BYTE_SIZE);
61 MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(afiReg, safiReg,
62 msg.getAfi(), msg.getSafi(), msgBuf);
64 if (LOG.isTraceEnabled()) {
65 LOG.trace("RouteRefresh message serialized to: {}", ByteBufUtil.hexDump(msgBuf));
67 MessageUtil.formatMessage(TYPE, msgBuf, bytes);
71 * Parses BGP Route Refresh message to bytes.
73 * @param body ByteBuf to be parsed
74 * @param messageLength the length of the message
75 * @return {@link RouteRefresh} which represents BGP notification message
76 * @throws BGPDocumentedException if parsing goes wrong
79 public RouteRefresh parseMessageBody(final ByteBuf body, final int messageLength,
80 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
81 checkArgument(body != null, "Body buffer cannot be null.");
82 if (body.readableBytes() < TRIPLET_BYTE_SIZE) {
83 throw BGPDocumentedException.badMessageLength("RouteRefresh message is too small.", messageLength);
85 return new RouteRefreshBuilder(MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, afiReg, safiReg)
86 .orElseThrow(() -> new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.",
87 BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED))