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