Enforce pcep-spi checkstyle
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleXROSubobjectRegistry.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.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
17 import org.opendaylight.protocol.util.Values;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21
22 public final class SimpleXROSubobjectRegistry implements XROSubobjectRegistry {
23     private final HandlerRegistry<DataContainer, XROSubobjectParser, XROSubobjectSerializer> handlers =
24             new HandlerRegistry<>();
25
26     public AutoCloseable registerSubobjectParser(final int subobjectType, final XROSubobjectParser parser) {
27         Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
28         return this.handlers.registerParser(subobjectType, parser);
29     }
30
31     public AutoCloseable registerSubobjectSerializer(final Class<? extends SubobjectType> subobjectClass,
32             final XROSubobjectSerializer serializer) {
33         return this.handlers.registerSerializer(subobjectClass, serializer);
34     }
35
36     @Override
37     public Subobject parseSubobject(final int type, final ByteBuf buffer, final boolean mandatory)
38             throws PCEPDeserializerException {
39         Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
40         final XROSubobjectParser parser = this.handlers.getParser(type);
41         if (parser == null) {
42             return null;
43         }
44         return parser.parseSubobject(buffer, mandatory);
45     }
46
47     @Override
48     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
49         final XROSubobjectSerializer serializer = this.handlers.getSerializer(
50             subobject.getSubobjectType().getImplementedInterface());
51         if (serializer == null) {
52             return;
53         }
54         serializer.serializeSubobject(subobject, buffer);
55     }
56 }