Bump MDSAL to 4.0.0
[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 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
12 import org.opendaylight.protocol.bgp.parser.spi.AbstractMessageRegistry;
13 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
14 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
15 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
16 import org.opendaylight.protocol.concepts.HandlerRegistry;
17 import org.opendaylight.yangtools.concepts.Registration;
18 import org.opendaylight.yangtools.yang.binding.DataContainer;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20
21 final class SimpleMessageRegistry extends AbstractMessageRegistry {
22
23     private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
24
25     @Override
26     protected Notification parseBody(final int type, final ByteBuf body, final int messageLength,
27             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
28         final MessageParser parser = this.handlers.getParser(type);
29         if (parser == null) {
30             return null;
31         }
32         return parser.parseMessageBody(body, messageLength, constraint);
33     }
34
35     @Override
36     protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
37         final MessageSerializer serializer = this.handlers.getSerializer(message.implementedInterface());
38         if (serializer == null) {
39             return;
40         }
41         serializer.serializeMessage(message, buffer);
42     }
43
44     Registration registerMessageParser(final int messageType, final MessageParser parser) {
45         return this.handlers.registerParser(messageType, parser);
46     }
47
48     Registration registerMessageSerializer(final Class<? extends Notification> messageClass,
49             final MessageSerializer serializer) {
50         return this.handlers.registerSerializer(messageClass, serializer);
51     }
52 }