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 static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import java.util.Optional;
15 import org.opendaylight.protocol.concepts.HandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.ObjectParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.protocol.pcep.spi.UnknownObject;
22 import org.opendaylight.protocol.pcep.spi.VendorInformationObjectRegistry;
23 import org.opendaylight.protocol.util.Values;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
31 public final class SimpleObjectRegistry implements ObjectRegistry {
32 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
34 private static final int MAX_OBJECT_TYPE = 15;
35 private static final int MAX_OBJECT_CLASS = 4;
37 private final VendorInformationObjectRegistry viRegistry;
39 public SimpleObjectRegistry(final VendorInformationObjectRegistry viRegistry) {
40 this.viRegistry = viRegistry;
43 private static int createKey(final int objectClass, final int objectType) {
44 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
45 Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE);
46 return objectClass << MAX_OBJECT_CLASS | objectType;
49 public Registration registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
50 checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illegal object class %s",
52 checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
53 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
56 public Registration registerObjectSerializer(final Class<? extends Object> objClass,
57 final ObjectSerializer serializer) {
58 return this.handlers.registerSerializer(objClass, serializer);
62 public Object parseObject(final int objectClass, final int objectType, final ObjectHeader header,
63 final ByteBuf buffer) throws PCEPDeserializerException {
64 checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
65 final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
68 if (!header.isProcessingRule()) {
71 for (int type = 1; type <= MAX_OBJECT_TYPE; type++) {
72 final ObjectParser objParser = this.handlers.getParser(createKey(objectClass, type));
73 if (objParser != null) {
74 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
77 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
79 return parser.parseObject(header, buffer);
83 public void serializeObject(final Object object, final ByteBuf buffer) {
84 final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
85 if (serializer == null) {
88 serializer.serializeObject(object, buffer);
92 public Optional<? extends Object> parseVendorInformationObject(final EnterpriseNumber enterpriseNumber,
93 final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
94 return this.viRegistry.parseVendorInformationObject(enterpriseNumber, header, buffer);
98 public void serializeVendorInformationObject(final VendorInformationObject viObject, final ByteBuf buffer) {
99 this.viRegistry.serializeVendorInformationObject(viObject, buffer);