Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleEROSubobjectRegistry.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.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.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 SimpleEROSubobjectRegistry implements EROSubobjectRegistry {
25     private final HandlerRegistry<DataContainer, EROSubobjectParser, EROSubobjectSerializer> handlers =
26             new HandlerRegistry<>();
27
28     public Registration registerSubobjectParser(final int subobjectType, final EROSubobjectParser 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 EROSubobjectSerializer serializer) {
35         return this.handlers.registerSerializer(subobjectClass, serializer);
36     }
37
38     @Override
39     public Subobject parseSubobject(final int type, final ByteBuf buffer, final boolean loose)
40             throws PCEPDeserializerException {
41         checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
42         final EROSubobjectParser parser = this.handlers.getParser(type);
43         if (parser == null) {
44             return null;
45         }
46         return parser.parseSubobject(buffer, loose);
47     }
48
49     @Override
50     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
51         final EROSubobjectSerializer serializer = this.handlers.getSerializer(
52             subobject.getSubobjectType().implementedInterface());
53         if (serializer == null) {
54             return;
55         }
56         serializer.serializeSubobject(subobject, buffer);
57     }
58 }