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