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.rev180329.BgpTableType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefresh;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.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;
39 private static final String ARGUMENT_ERROR = "Message is not of type RouteRefresh.";
41 private final AddressFamilyRegistry afiReg;
42 private final SubsequentAddressFamilyRegistry safiReg;
44 public BGPRouteRefreshMessageParser(final AddressFamilyRegistry afiReg,
45 final SubsequentAddressFamilyRegistry safiReg) {
46 this.afiReg = requireNonNull(afiReg);
47 this.safiReg = requireNonNull(safiReg);
51 * Serializes BGP Route Refresh message.
53 * @param message to be serialized
54 * @param bytes ByteBuf where the message will be serialized
57 public void serializeMessage(final Notification message, final ByteBuf bytes) {
58 Preconditions.checkArgument(message instanceof RouteRefresh, ARGUMENT_ERROR);
59 final RouteRefresh msg = (RouteRefresh) message;
61 final ByteBuf msgBuf = Unpooled.buffer(TRIPLET_BYTE_SIZE);
62 MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(this.afiReg, this.safiReg,
63 msg.getAfi(), msg.getSafi(), msgBuf);
65 LOG.trace("RouteRefresh message serialized to: {}", ByteBufUtil.hexDump(msgBuf));
66 MessageUtil.formatMessage(TYPE, msgBuf, bytes);
70 * Parses BGP Route Refresh message to bytes.
72 * @param body ByteBuf to be parsed
73 * @param messageLength the length of the message
74 * @return {@link RouteRefresh} which represents BGP notification message
75 * @throws BGPDocumentedException if parsing goes wrong
78 public RouteRefresh parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
79 Preconditions.checkArgument(body != null, "Body buffer cannot be null.");
80 if (body.readableBytes() < TRIPLET_BYTE_SIZE) {
81 throw BGPDocumentedException.badMessageLength("RouteRefresh message is too small.", messageLength);
83 final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, this.afiReg,
85 if (!parsedAfiSafi.isPresent()) {
86 throw new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.",
87 BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
89 return new RouteRefreshBuilder(parsedAfiSafi.get()).build();