Enforce pcep-spi checkstyle
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / 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.pcep.spi.pojo;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import org.opendaylight.protocol.concepts.HandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.MessageParser;
15 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
16 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class SimpleMessageRegistry implements MessageRegistry {
25
26     private static final Logger LOG = LoggerFactory.getLogger(SimpleMessageRegistry.class);
27
28     private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
29
30     public AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
31         Preconditions.checkArgument(messageType >= 0 && messageType <= Values.UNSIGNED_BYTE_MAX_VALUE);
32         return this.handlers.registerParser(messageType, parser);
33     }
34
35     public AutoCloseable registerMessageSerializer(final Class<? extends Message> msgClass,
36             final MessageSerializer serializer) {
37         return this.handlers.registerSerializer(msgClass, serializer);
38     }
39
40     @Override
41     public Message parseMessage(final int messageType, final ByteBuf buffer, final List<Message> errors)
42             throws PCEPDeserializerException {
43         Preconditions.checkArgument(messageType >= 0 && messageType <= Values.UNSIGNED_BYTE_MAX_VALUE);
44         final MessageParser parser = this.handlers.getParser(messageType);
45         if (parser == null) {
46             LOG.warn("PCEP parser for message type {} is not registered.", messageType);
47             return null;
48         }
49         return parser.parseMessage(buffer, errors);
50     }
51
52     @Override
53     public void serializeMessage(final Message message, final ByteBuf buffer) {
54         final MessageSerializer serializer = this.handlers.getSerializer(message.getImplementedInterface());
55         if (serializer == null) {
56             LOG.warn("PCEP serializer for message type {} is not registered.", message.getClass());
57             return;
58         }
59         serializer.serializeMessage(message, buffer);
60     }
61 }