2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.impl;
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.MessageRegistry;
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;
18 public final class SimpleMessageRegistry extends AbstractMessageRegistry {
19 private static final class Holder {
20 private static final MessageRegistry INSTANCE = new SimpleMessageRegistry();
23 private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
25 private SimpleMessageRegistry() {
29 public static MessageRegistry getInstance() {
30 return Holder.INSTANCE;
34 protected Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException {
35 final MessageParser parser = handlers.getParser(type);
40 return parser.parseMessageBody(body, messageLength);
44 protected byte[] serializeMessageImpl(final Notification message) {
45 final MessageSerializer serializer = handlers.getSerializer(message.getImplementedInterface());
46 if (serializer == null) {
50 return serializer.serializeMessage(message);
54 public AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
55 return handlers.registerParser(messageType, parser);
59 public AutoCloseable registerMessageSerializer(final Class<? extends Notification> messageClass, final MessageSerializer serializer) {
60 return handlers.registerSerializer(messageClass, serializer);