Removed checkstyle 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
12 import io.netty.buffer.ByteBuf;
13
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;
25
26 /**
27  *
28  */
29 public final class SimpleObjectRegistry implements ObjectRegistry {
30     private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
31
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;
36     }
37
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",
40                 objectClass);
41         Preconditions.checkArgument(objectType >= 0 && objectType <= 15, "Illegal object type %s", objectType);
42         return this.handlers.registerParser(createKey(objectClass, objectType), parser);
43     }
44
45     public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
46         return this.handlers.registerSerializer(objClass, serializer);
47     }
48
49     @Override
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));
54
55         if (parser == null) {
56             if (!header.isProcessingRule()) {
57                 return null;
58             }
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);
63                 }
64             }
65             return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
66         }
67         return parser.parseObject(header, buffer);
68     }
69
70     @Override
71     public byte[] serializeObject(Object object) {
72         final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
73         if (serializer == null) {
74             return null;
75         }
76         return serializer.serializeObject(object);
77     }
78 }