Merge "BUG-730 : added tests for Encoders/Decoders in PCEP."
[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 org.opendaylight.protocol.concepts.HandlerRegistry;
11 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.util.Values;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.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 SimpleEROSubobjectRegistry implements EROSubobjectRegistry {
23         private final HandlerRegistry<DataContainer, EROSubobjectParser, EROSubobjectSerializer> handlers = new HandlerRegistry<>();
24
25         public AutoCloseable registerSubobjectParser(final int subobjectType, final EROSubobjectParser 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 EROSubobjectSerializer serializer) {
32                 return this.handlers.registerSerializer(subobjectClass, serializer);
33         }
34
35         @Override
36         public Subobject parseSubobject(int type, byte[] buffer, boolean loose) throws PCEPDeserializerException {
37                 Preconditions.checkArgument(type >= 0 && type <= Values.UNSIGNED_SHORT_MAX_VALUE);
38                 final EROSubobjectParser parser = this.handlers.getParser(type);
39                 if (parser == null) {
40                         return null;
41                 }
42                 return parser.parseSubobject(buffer, loose);
43         }
44
45         @Override
46         public byte[] serializeSubobject(Subobject subobject) {
47                 final EROSubobjectSerializer serializer = this.handlers.getSerializer(subobject.getSubobjectType().getImplementedInterface());
48                 if (serializer == null) {
49                         return null;
50                 }
51                 return serializer.serializeSubobject(subobject);
52         }
53 }