Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPRouteRefreshMessageParser.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
30
31 public final class BGPRouteRefreshMessageParser implements MessageParser, MessageSerializer {
32     // https://tools.ietf.org/html/rfc2918#section-3
33
34     private static final Logger LOG = LoggerFactory.getLogger(BGPRouteRefreshMessageParser.class);
35
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.";
39
40     private final AddressFamilyRegistry afiReg;
41     private final SubsequentAddressFamilyRegistry safiReg;
42
43     public BGPRouteRefreshMessageParser(final AddressFamilyRegistry afiReg,
44             final SubsequentAddressFamilyRegistry safiReg) {
45         this.afiReg = requireNonNull(afiReg);
46         this.safiReg = requireNonNull(safiReg);
47     }
48
49     /**
50      * Serializes BGP Route Refresh message.
51      *
52      * @param message to be serialized
53      * @param bytes ByteBuf where the message will be serialized
54      */
55     @Override
56     public void serializeMessage(final Notification<?> message, final ByteBuf bytes) {
57         checkArgument(message instanceof RouteRefresh, ARGUMENT_ERROR);
58         final RouteRefresh msg = (RouteRefresh) message;
59
60         final ByteBuf msgBuf = Unpooled.buffer(TRIPLET_BYTE_SIZE);
61         MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(afiReg, safiReg,
62                 msg.getAfi(), msg.getSafi(), msgBuf);
63
64         if (LOG.isTraceEnabled()) {
65             LOG.trace("RouteRefresh message serialized to: {}", ByteBufUtil.hexDump(msgBuf));
66         }
67         MessageUtil.formatMessage(TYPE, msgBuf, bytes);
68     }
69
70     /**
71      * Parses BGP Route Refresh message to bytes.
72      *
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
77      */
78     @Override
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);
84         }
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))
88             ).build();
89     }
90 }