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;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.concepts.HandlerRegistry;
13 import org.opendaylight.protocol.pcep.spi.ObjectParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
15 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
18 import org.opendaylight.protocol.pcep.spi.UnknownObject;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
27 public final class SimpleObjectRegistry implements ObjectRegistry {
28 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
30 private static final int MAX_OBJECT_TYPE = 15;
31 private static final int MAX_OBJECT_CLASS = 4;
33 private static int createKey(final int objectClass, final int objectType) {
34 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
35 Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE);
36 return (objectClass << MAX_OBJECT_CLASS) | objectType;
39 public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
40 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illegal object class %s",
42 Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
43 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
46 public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
47 return this.handlers.registerSerializer(objClass, serializer);
51 public Object parseObject(final int objectClass, final int objectType, final ObjectHeader header, final ByteBuf buffer)
52 throws PCEPDeserializerException {
53 Preconditions.checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
54 final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
57 if (!header.isProcessingRule()) {
60 for (int type = 1; type <= MAX_OBJECT_TYPE; type++) {
61 final ObjectParser objParser = this.handlers.getParser(createKey(objectClass, type));
62 if (objParser != null) {
63 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
66 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
68 return parser.parseObject(header, buffer);
72 public void serializeObject(final Object object, final ByteBuf buffer) {
73 final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
74 if (serializer == null) {
77 serializer.serializeObject(object, buffer);