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 org.opendaylight.protocol.concepts.HandlerRegistry;
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
13 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
14 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
15 import org.opendaylight.protocol.util.Values;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.SubobjectType;
18 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import com.google.common.base.Preconditions;
22 public final class SimpleXROSubobjectRegistry implements XROSubobjectRegistry {
23 private final HandlerRegistry<DataContainer, XROSubobjectParser, XROSubobjectSerializer> handlers = new HandlerRegistry<>();
25 public AutoCloseable registerSubobjectParser(final int subobjectType, final XROSubobjectParser parser) {
26 Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
27 return this.handlers.registerParser(subobjectType, parser);
30 public AutoCloseable registerSubobjectSerializer(final Class<? extends SubobjectType> subobjectClass,
31 final XROSubobjectSerializer serializer) {
32 return this.handlers.registerSerializer(subobjectClass, serializer);
36 public Subobject parseSubobject(int type, byte[] buffer, boolean mandatory) throws PCEPDeserializerException {
37 Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
38 final XROSubobjectParser parser = this.handlers.getParser(type);
42 return parser.parseSubobject(buffer, mandatory);
46 public byte[] serializeSubobject(Subobject subobject) {
47 final XROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
48 if (serializer == null) {
51 return serializer.serializeSubobject(subobject);