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.ObjectParser;
12 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
13 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
16 import org.opendaylight.protocol.pcep.spi.UnknownObject;
17 import org.opendaylight.protocol.util.Values;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
22 import com.google.common.base.Preconditions;
27 public final class SimpleObjectRegistry implements ObjectRegistry {
28 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
30 private static int createKey(final int objectClass, final int objectType) {
31 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
32 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
33 return (objectClass << 4) | objectType;
36 public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
37 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illagal object class %s", objectClass);
38 Preconditions.checkArgument(objectType >= 0 && objectType <= 15, "Illegal object type %s", objectType);
39 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
42 public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
43 return this.handlers.registerSerializer(objClass, serializer);
47 public Object parseObject(int objectClass, int objectType, ObjectHeader header, byte[] buffer) throws PCEPDeserializerException {
48 Preconditions.checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
49 final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
52 if(!header.isProcessingRule()) {
56 final boolean foundClass = false;
58 // FIXME: BUG-187: search the parsers, check classes
61 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
63 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
66 return parser.parseObject(header, buffer);
70 public byte[] serializeObject(Object object) {
71 final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
72 if (serializer == null) {
75 return serializer.serializeObject(object);