Enforce pcep-spi checkstyle
[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.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.yang.binding.DataContainer;
29
30 public final class SimpleObjectRegistry implements ObjectRegistry {
31     private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
32
33     private static final int MAX_OBJECT_TYPE = 15;
34     private static final int MAX_OBJECT_CLASS = 4;
35
36     private final VendorInformationObjectRegistry viRegistry;
37
38     public SimpleObjectRegistry(final VendorInformationObjectRegistry viRegistry) {
39         this.viRegistry = viRegistry;
40     }
41
42     private static int createKey(final int objectClass, final int objectType) {
43         Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
44         Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE);
45         return objectClass << MAX_OBJECT_CLASS | objectType;
46     }
47
48     public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
49         checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illegal object class %s",
50                 objectClass);
51         checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
52         return this.handlers.registerParser(createKey(objectClass, objectType), parser);
53     }
54
55     public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass,
56             final ObjectSerializer serializer) {
57         return this.handlers.registerSerializer(objClass, serializer);
58     }
59
60     @Override
61     public Object parseObject(final int objectClass, final int objectType, final ObjectHeader header,
62             final ByteBuf buffer) throws PCEPDeserializerException {
63         checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
64         final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
65
66         if (parser == null) {
67             if (!header.isProcessingRule()) {
68                 return null;
69             }
70             for (int type = 1; type <= MAX_OBJECT_TYPE; type++) {
71                 final ObjectParser objParser = this.handlers.getParser(createKey(objectClass, type));
72                 if (objParser != null) {
73                     return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
74                 }
75             }
76             return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
77         }
78         return parser.parseObject(header, buffer);
79     }
80
81     @Override
82     public void serializeObject(final Object object, final ByteBuf buffer) {
83         final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
84         if (serializer == null) {
85             return;
86         }
87         serializer.serializeObject(object, buffer);
88     }
89
90     @Override
91     public Optional<? extends Object> parseVendorInformationObject(final EnterpriseNumber enterpriseNumber,
92             final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
93         return this.viRegistry.parseVendorInformationObject(enterpriseNumber, header, buffer);
94     }
95
96     @Override
97     public void serializeVendorInformationObject(final VendorInformationObject viObject, final ByteBuf buffer) {
98         this.viRegistry.serializeVendorInformationObject(viObject, buffer);
99     }
100 }