e841ed6213274bee0bf658075ccff897112d33e1
[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
12 import io.netty.buffer.ByteBuf;
13
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
16 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21
22 public class BGPKeepAliveMessageParser implements MessageParser, MessageSerializer {
23     public static final int TYPE = 4;
24
25     private final Keepalive msg = new KeepaliveBuilder().build();
26     private final byte[] bytes = MessageUtil.formatMessage(TYPE, new byte[0]);
27
28     @Override
29     public Keepalive parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
30         if (body.readableBytes() != 0) {
31             throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
32         }
33         return this.msg;
34     }
35
36     @Override
37     public byte[] serializeMessage(final Notification message) {
38         Preconditions.checkArgument(message instanceof Keepalive);
39         return this.bytes;
40     }
41 }