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 java.util.Optional;
13 import org.opendaylight.protocol.concepts.HandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.ObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.protocol.pcep.spi.UnknownObject;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationObjectRegistry;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
28 public final class SimpleObjectRegistry implements ObjectRegistry {
29 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
31 private static final int MAX_OBJECT_TYPE = 15;
32 private static final int MAX_OBJECT_CLASS = 4;
33 private final VendorInformationObjectRegistry viRegistry;
35 public SimpleObjectRegistry(final VendorInformationObjectRegistry viRegistry) {
36 this.viRegistry = viRegistry;
40 private static int createKey(final int objectClass, final int objectType) {
41 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
42 Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE);
43 return (objectClass << MAX_OBJECT_CLASS) | objectType;
46 public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
47 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illegal object class %s",
49 Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
50 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
53 public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
54 return this.handlers.registerSerializer(objClass, serializer);
58 public Object parseObject(final int objectClass, final int objectType, final ObjectHeader header, final ByteBuf buffer)
59 throws PCEPDeserializerException {
60 Preconditions.checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
61 final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
64 if (!header.isProcessingRule()) {
67 for (int type = 1; type <= MAX_OBJECT_TYPE; type++) {
68 final ObjectParser objParser = this.handlers.getParser(createKey(objectClass, type));
69 if (objParser != null) {
70 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
73 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
75 return parser.parseObject(header, buffer);
79 public void serializeObject(final Object object, final ByteBuf buffer) {
80 final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
81 if (serializer == null) {
84 serializer.serializeObject(object, buffer);
87 public Optional<? extends Object> parseVendorInformationObject(final EnterpriseNumber enterpriseNumber,
88 final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
89 return this.viRegistry.parseVendorInformationObject(enterpriseNumber, header, buffer);
93 public void serializeVendorInformationObject(final VendorInformationObject viObject, final ByteBuf buffer) {
94 this.viRegistry.serializeVendorInformationObject(viObject, buffer);