Bug 611 - BGP Update message serialization
[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
22     private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
23
24     @Override
25     protected Notification parseBody(final int type, final ByteBuf body, final int messageLength) throws BGPDocumentedException {
26         final MessageParser parser = this.handlers.getParser(type);
27         if (parser == null) {
28             return null;
29         }
30
31         return parser.parseMessageBody(body, messageLength);
32     }
33
34     @Override
35     protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
36         final MessageSerializer serializer = this.handlers.getSerializer(message.getImplementedInterface());
37         if (serializer == null) {
38             return;
39         }
40         serializer.serializeMessage(message, buffer);
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 }