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.ObjectHandlerRegistry;
12 import org.opendaylight.protocol.pcep.spi.ObjectParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
15 import org.opendaylight.protocol.pcep.spi.UnknownObject;
16 import org.opendaylight.protocol.util.Values;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
21 import com.google.common.base.Preconditions;
26 public final class SimpleObjectHandlerRegistry implements ObjectHandlerRegistry {
27 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
29 private static int createKey(final int objectClass, final int objectType) {
30 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
31 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
32 return (objectClass << 4) | objectType;
35 public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
36 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
37 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
38 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
41 public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
42 return this.handlers.registerSerializer(objClass, serializer);
46 public ObjectParser getObjectParser(final int objectClass, final int objectType) {
47 final ObjectParser ret = this.handlers.getParser(createKey(objectClass, objectType));
52 final boolean foundClass = false;
54 // FIXME: BUG-187: search the parsers, check classes
57 return new ObjectParser() {
59 public Object parseObject(final ObjectHeader header, final byte[] buffer) {
60 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
64 return new ObjectParser() {
66 public Object parseObject(final ObjectHeader header, final byte[] buffer) {
67 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
74 public ObjectSerializer getObjectSerializer(final Object object) {
75 return this.handlers.getSerializer(object.getImplementedInterface());