Testtools should not be bundles
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleParameterRegistry.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.bgp.parser.spi.pojo;
9
10 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
11 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
12 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
13 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
14 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
15 import org.opendaylight.protocol.concepts.HandlerRegistry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18
19 import com.google.common.base.Preconditions;
20
21 final class SimpleParameterRegistry implements ParameterRegistry {
22         private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers = new HandlerRegistry<>();
23
24         AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
25                 Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
26                 return handlers.registerParser(messageType, parser);
27         }
28
29         AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
30                 return handlers.registerSerializer(paramClass, serializer);
31         }
32
33         @Override
34         public BgpParameters parseParameter(final int parameterType, final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
35                 final ParameterParser parser = handlers.getParser(parameterType);
36                 if (parser == null) {
37                         return null;
38                 }
39
40                 return parser.parseParameter(bytes);
41         }
42
43         @Override
44         public byte[] serializeParameter(final BgpParameters parameter) {
45                 final ParameterSerializer serializer = handlers.getSerializer(parameter.getImplementedInterface());
46                 if (serializer == null) {
47                         return null;
48                 }
49
50                 return serializer.serializeParameter(parameter);
51         }
52 }