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.pcep.spi.pojo;
10 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
14 import java.util.List;
16 import org.opendaylight.protocol.concepts.HandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.MessageParser;
18 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
19 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
23 import org.opendaylight.yangtools.yang.binding.DataContainer;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class SimpleMessageRegistry implements MessageRegistry {
29 private static final Logger LOG = LoggerFactory.getLogger(SimpleMessageRegistry.class);
31 private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
33 public AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
34 Preconditions.checkArgument(messageType >= 0 && messageType <= Values.UNSIGNED_BYTE_MAX_VALUE);
35 return this.handlers.registerParser(messageType, parser);
38 public AutoCloseable registerMessageSerializer(final Class<? extends Message> msgClass, final MessageSerializer serializer) {
39 return this.handlers.registerSerializer(msgClass, serializer);
43 public Message parseMessage(final int messageType, final ByteBuf buffer, final List<Message> errors) throws PCEPDeserializerException {
44 Preconditions.checkArgument(messageType >= 0 && messageType <= Values.UNSIGNED_BYTE_MAX_VALUE);
45 final MessageParser parser = this.handlers.getParser(messageType);
47 LOG.warn("PCEP parser for message type {} is not registered.", messageType);
50 return parser.parseMessage(buffer, errors);
54 public void serializeMessage(Message message, ByteBuf buffer) {
55 final MessageSerializer serializer = this.handlers.getSerializer(message.getImplementedInterface());
56 if (serializer == null) {
57 LOG.warn("PCEP serializer for message type {} is not registered.", message.getClass());
60 serializer.serializeMessage(message, buffer);