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