Clean up ShortestPathFirst
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleRROSubobjectRegistry.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.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectRegistry;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.SubobjectType;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
23
24 public final class SimpleRROSubobjectRegistry implements RROSubobjectRegistry {
25     private final HandlerRegistry<DataContainer, RROSubobjectParser, RROSubobjectSerializer> handlers =
26             new HandlerRegistry<>();
27
28     public Registration registerSubobjectParser(final int subobjectType, final RROSubobjectParser 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 RROSubobjectSerializer serializer) {
35         return this.handlers.registerSerializer(subobjectClass, serializer);
36     }
37
38     @Override
39     public Subobject parseSubobject(final int type, final ByteBuf buffer) throws PCEPDeserializerException {
40         checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
41         final RROSubobjectParser parser = this.handlers.getParser(type);
42         return parser == null ? null : parser.parseSubobject(buffer);
43     }
44
45     @Override
46     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
47         final RROSubobjectSerializer serializer = this.handlers.getSerializer(
48             subobject.getSubobjectType().implementedInterface());
49         if (serializer != null) {
50             serializer.serializeSubobject(subobject, buffer);
51         }
52     }
53 }