Reduce noise from BGPSessionImpl
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleObjectRegistry.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.spi.pojo;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.ObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
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;
30
31 public final class SimpleObjectRegistry implements ObjectRegistry {
32     private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
33
34     private static final int MAX_OBJECT_TYPE = 15;
35     private static final int MAX_OBJECT_CLASS = 4;
36
37     private final VendorInformationObjectRegistry viRegistry;
38
39     public SimpleObjectRegistry(final VendorInformationObjectRegistry viRegistry) {
40         this.viRegistry = viRegistry;
41     }
42
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;
47     }
48
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",
51                 objectClass);
52         checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
53         return this.handlers.registerParser(createKey(objectClass, objectType), parser);
54     }
55
56     public Registration registerObjectSerializer(final Class<? extends Object> objClass,
57             final ObjectSerializer serializer) {
58         return this.handlers.registerSerializer(objClass, serializer);
59     }
60
61     @Override
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));
66
67         if (parser == null) {
68             if (!header.getProcessingRule()) {
69                 return null;
70             }
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);
75                 }
76             }
77             return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
78         }
79         return parser.parseObject(header, buffer);
80     }
81
82     @Override
83     public void serializeObject(final Object object, final ByteBuf buffer) {
84         final ObjectSerializer serializer = this.handlers.getSerializer(object.implementedInterface());
85         if (serializer == null) {
86             return;
87         }
88         serializer.serializeObject(object, buffer);
89     }
90
91     @Override
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);
95     }
96
97     @Override
98     public void serializeVendorInformationObject(final VendorInformationObject viObject, final ByteBuf buffer) {
99         this.viRegistry.serializeVendorInformationObject(viObject, buffer);
100     }
101 }