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;
12 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.concepts.HandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.ObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
17 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.UnknownObject;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 public final class SimpleObjectRegistry implements ObjectRegistry {
30 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
32 private static int createKey(final int objectClass, final int objectType) {
33 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
34 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
35 return (objectClass << 4) | objectType;
38 public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
39 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illagal object class %s",
41 Preconditions.checkArgument(objectType >= 0 && objectType <= 15, "Illegal object type %s", objectType);
42 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
45 public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
46 return this.handlers.registerSerializer(objClass, serializer);
50 public Object parseObject(final int objectClass, final int objectType, final ObjectHeader header, final ByteBuf buffer)
51 throws PCEPDeserializerException {
52 Preconditions.checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
53 final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
56 if (!header.isProcessingRule()) {
59 for (int type = 1; type <= 15; type++) {
60 final ObjectParser objParser = this.handlers.getParser(createKey(objectClass, type));
61 if (objParser != null) {
62 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
65 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
67 return parser.parseObject(header, buffer);
71 public byte[] serializeObject(Object object) {
72 final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
73 if (serializer == null) {
76 return serializer.serializeObject(object);