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;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.concepts.HandlerRegistry;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.TlvParser;
15 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
16 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
17 import org.opendaylight.protocol.util.Values;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
21 public final class SimpleTlvRegistry implements TlvRegistry {
23 private final HandlerRegistry<DataContainer, TlvParser, TlvSerializer> handlers = new HandlerRegistry<>();
25 public AutoCloseable registerTlvParser(final int tlvType, final TlvParser parser) {
26 Preconditions.checkArgument(tlvType >= 0 && tlvType < Values.UNSIGNED_SHORT_MAX_VALUE);
27 return this.handlers.registerParser(tlvType, parser);
30 public AutoCloseable registerTlvSerializer(final Class<? extends Tlv> tlvClass, final TlvSerializer serializer) {
31 return this.handlers.registerSerializer(tlvClass, serializer);
35 public Tlv parseTlv(final int type, final ByteBuf buffer) throws PCEPDeserializerException {
36 Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
37 final TlvParser parser = this.handlers.getParser(type);
41 return parser.parseTlv(buffer);
45 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
46 final TlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
47 if (serializer == null) {
50 serializer.serializeTlv(tlv, buffer);