BUG-113: make sure we capture TYPE
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPKeepAliveMessageParser.java
1 package org.opendaylight.protocol.bgp.parser.impl.message;
2
3 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
4 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
5 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
6 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
9 import org.opendaylight.yangtools.yang.binding.Notification;
10
11 import com.google.common.base.Preconditions;
12
13 public class BGPKeepAliveMessageParser implements MessageParser, MessageSerializer {
14         public static final int TYPE = 4;
15         public static final MessageParser PARSER;
16         public static final MessageSerializer SERIALIZER;
17
18         private static final Keepalive msg = new KeepaliveBuilder().build();
19         private static final byte[] bytes = MessageUtil.formatMessage(TYPE, new byte[0]);
20
21
22         static {
23                 final BGPKeepAliveMessageParser p = new BGPKeepAliveMessageParser();
24                 PARSER = p;
25                 SERIALIZER = p;
26         }
27
28         private BGPKeepAliveMessageParser() {
29
30         }
31
32         @Override
33         public Keepalive parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
34                 if (body.length != 0) {
35                         throw  BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
36                 }
37
38                 return msg;
39         }
40
41         @Override
42         public byte[] serializeMessage(final Notification message) {
43                 Preconditions.checkArgument(message instanceof Keepalive);
44                 return bytes;
45         }
46 }