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