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 org.opendaylight.protocol.concepts.HandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.SubobjectType;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
24 public final class SimpleRROSubobjectRegistry implements RROSubobjectRegistry {
25 private final HandlerRegistry<DataContainer, RROSubobjectParser, RROSubobjectSerializer> handlers = new HandlerRegistry<>();
27 public AutoCloseable registerSubobjectParser(final int subobjectType, final RROSubobjectParser parser) {
28 Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
29 return this.handlers.registerParser(subobjectType, parser);
32 public AutoCloseable registerSubobjectSerializer(final Class<? extends SubobjectType> subobjectClass,
33 final RROSubobjectSerializer serializer) {
34 return this.handlers.registerSerializer(subobjectClass, serializer);
38 public Subobject parseSubobject(final int type, final ByteBuf buffer) throws PCEPDeserializerException {
39 Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
40 final RROSubobjectParser parser = this.handlers.getParser(type);
44 return parser.parseSubobject(buffer);
48 public byte[] serializeSubobject(Subobject subobject) {
49 final RROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
50 if (serializer == null) {
53 return serializer.serializeSubobject(subobject);