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