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.UnknownObject;
12 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
13 import org.opendaylight.protocol.pcep.spi.ObjectParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
15 import org.opendaylight.protocol.util.Util;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import com.google.common.base.Preconditions;
25 public final class SimpleObjectHandlerRegistry implements ObjectHandlerRegistry {
26 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
28 private static int createKey(final int objectClass, final int objectType) {
29 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Util.UNSIGNED_BYTE_MAX_VALUE);
30 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
31 return (objectClass << 4) | objectType;
34 public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
35 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Util.UNSIGNED_BYTE_MAX_VALUE);
36 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
37 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
40 public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
41 return this.handlers.registerSerializer(objClass, serializer);
45 public ObjectParser getObjectParser(final int objectClass, final int objectType) {
46 final ObjectParser ret = this.handlers.getParser(createKey(objectClass, objectType));
51 boolean foundClass = false;
53 // FIXME: search the parsers, check classes
54 //e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_CLASS || e.getError() == PCEPErrors.UNRECOGNIZED_OBJ_TYPE
57 return new ObjectParser() {
59 public Object parseObject(final ObjectHeader header, final byte[] buffer) {
60 // FIXME: appropriate error (unrecognized object class)
61 return new UnknownObject(null);
66 return new ObjectParser() {
68 public Object parseObject(final ObjectHeader header, final byte[] buffer) {
69 // FIXME: appropriate error (unrecognized object type)
70 return new UnknownObject(null);
77 public ObjectSerializer getObjectSerializer(final Object object) {
78 return this.handlers.getSerializer(object.getImplementedInterface());