BUG-731 : fixed pcep sonar warnings
[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 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;
23
24 /**
25  *
26  */
27 public final class SimpleObjectRegistry implements ObjectRegistry {
28     private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
29
30     private static final int MAX_OBJECT_TYPE = 15;
31     private static final int MAX_OBJECT_CLASS = 4;
32
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;
37     }
38
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",
41                 objectClass);
42         Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
43         return this.handlers.registerParser(createKey(objectClass, objectType), parser);
44     }
45
46     public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
47         return this.handlers.registerSerializer(objClass, serializer);
48     }
49
50     @Override
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));
55
56         if (parser == null) {
57             if (!header.isProcessingRule()) {
58                 return null;
59             }
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);
64                 }
65             }
66             return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
67         }
68         return parser.parseObject(header, buffer);
69     }
70
71     @Override
72     public void serializeObject(final Object object, final ByteBuf buffer) {
73         final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
74         if (serializer == null) {
75             return;
76         }
77         serializer.serializeObject(object, buffer);
78     }
79 }