Removed checkstyle warnings.
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleTlvRegistry.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
12 import io.netty.buffer.ByteBuf;
13
14 import org.opendaylight.protocol.concepts.HandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvParser;
17 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
18 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21 import org.opendaylight.yangtools.yang.binding.DataContainer;
22
23 /**
24  *
25  */
26 public final class SimpleTlvRegistry implements TlvRegistry {
27
28     private final HandlerRegistry<DataContainer, TlvParser, TlvSerializer> handlers = new HandlerRegistry<>();
29
30     public AutoCloseable registerTlvParser(final int tlvType, final TlvParser parser) {
31         Preconditions.checkArgument(tlvType >= 0 && tlvType < Values.UNSIGNED_SHORT_MAX_VALUE);
32         return this.handlers.registerParser(tlvType, parser);
33     }
34
35     public AutoCloseable registerTlvSerializer(final Class<? extends Tlv> tlvClass, final TlvSerializer serializer) {
36         return this.handlers.registerSerializer(tlvClass, serializer);
37     }
38
39     @Override
40     public Tlv parseTlv(final int type, final ByteBuf buffer) throws PCEPDeserializerException {
41         Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
42         final TlvParser parser = this.handlers.getParser(type);
43         if (parser == null) {
44             return null;
45         }
46         return parser.parseTlv(buffer);
47     }
48
49     @Override
50     public byte[] serializeTlv(final Tlv tlv) {
51         final TlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
52         if (serializer == null) {
53             return null;
54         }
55         return serializer.serializeTlv(tlv);
56     }
57 }