BUG-612 : switched ERO to ByteBuf.
[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 io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.protocol.concepts.HandlerRegistry;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.util.Values;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.SubobjectType;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21
22 import com.google.common.base.Preconditions;
23
24 public final class SimpleEROSubobjectRegistry implements EROSubobjectRegistry {
25         private final HandlerRegistry<DataContainer, EROSubobjectParser, EROSubobjectSerializer> handlers = new HandlerRegistry<>();
26
27         public AutoCloseable registerSubobjectParser(final int subobjectType, final EROSubobjectParser parser) {
28                 Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
29                 return this.handlers.registerParser(subobjectType, parser);
30         }
31
32         public AutoCloseable registerSubobjectSerializer(final Class<? extends SubobjectType> subobjectClass,
33                         final EROSubobjectSerializer serializer) {
34                 return this.handlers.registerSerializer(subobjectClass, serializer);
35         }
36
37         @Override
38         public Subobject parseSubobject(final int type, final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
39                 Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
40                 final EROSubobjectParser parser = this.handlers.getParser(type);
41                 if (parser == null) {
42                         return null;
43                 }
44                 return parser.parseSubobject(buffer, loose);
45         }
46
47         @Override
48         public byte[] serializeSubobject(Subobject subobject) {
49                 final EROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
50                 if (serializer == null) {
51                         return null;
52                 }
53                 return serializer.serializeSubobject(subobject);
54         }
55 }