BUG-64 : refactor BGP parser to use ByteBuf
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleMessageRegistry.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.spi.pojo;
9
10 import io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
13 import org.opendaylight.protocol.bgp.parser.spi.AbstractMessageRegistry;
14 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
15 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
16 import org.opendaylight.protocol.concepts.HandlerRegistry;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18 import org.opendaylight.yangtools.yang.binding.Notification;
19
20 final class SimpleMessageRegistry extends AbstractMessageRegistry {
21         private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
22
23         @Override
24         protected Notification parseBody(final int type, final ByteBuf body, final int messageLength) throws BGPDocumentedException {
25                 final MessageParser parser = this.handlers.getParser(type);
26                 if (parser == null) {
27                         return null;
28                 }
29
30                 return parser.parseMessageBody(body, messageLength);
31         }
32
33         @Override
34         protected byte[] serializeMessageImpl(final Notification message) {
35                 final MessageSerializer serializer = this.handlers.getSerializer(message.getImplementedInterface());
36                 if (serializer == null) {
37                         return null;
38                 }
39
40                 return serializer.serializeMessage(message);
41         }
42
43         AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
44                 return this.handlers.registerParser(messageType, parser);
45         }
46
47         AutoCloseable registerMessageSerializer(final Class<? extends Notification> messageClass, final MessageSerializer serializer) {
48                 return this.handlers.registerSerializer(messageClass, serializer);
49         }
50 }