Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPKeepAliveMessageParser.java
1 /*
2  * Copyright (c) 2013 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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
15 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
17 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Keepalive;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.KeepaliveBuilder;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21
22 public final class BGPKeepAliveMessageParser implements MessageParser, MessageSerializer {
23     public static final int TYPE = 4;
24     private static final Keepalive KEEPALIVE_MSG = new KeepaliveBuilder().build();
25     private static final ByteBuf KEEPALIVE_BYTES = Unpooled.buffer();
26
27     static {
28         MessageUtil.formatMessage(TYPE, Unpooled.EMPTY_BUFFER,KEEPALIVE_BYTES);
29     }
30
31     @Override
32     public Keepalive parseMessageBody(final ByteBuf body, final int messageLength,
33             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
34         if (body.isReadable()) {
35             throw BGPDocumentedException.badMessageLength("Message length field not within valid range.",
36                     messageLength);
37         }
38         return KEEPALIVE_MSG;
39     }
40
41     @Override
42     public void serializeMessage(final Notification<?> message, final ByteBuf bytes) {
43         Preconditions.checkArgument(message instanceof Keepalive);
44         bytes.writeBytes(KEEPALIVE_BYTES.slice());
45     }
46 }