Fix most bgp-parser-spi checkstyle violations
[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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
13 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
14 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
15 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
16 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
17 import org.opendaylight.protocol.concepts.HandlerRegistry;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21
22 final class SimpleParameterRegistry implements ParameterRegistry {
23     private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers =
24             new HandlerRegistry<>();
25
26     AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
27         Preconditions.checkArgument(messageType >= 0 && messageType <= Values.UNSIGNED_BYTE_MAX_VALUE);
28         return this.handlers.registerParser(messageType, parser);
29     }
30
31     AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass,
32             final ParameterSerializer serializer) {
33         return this.handlers.registerSerializer(paramClass, serializer);
34     }
35
36     @Override
37     public BgpParameters parseParameter(final int parameterType, final ByteBuf buffer) throws BGPParsingException,
38             BGPDocumentedException {
39         final ParameterParser parser = this.handlers.getParser(parameterType);
40         if (parser == null) {
41             return null;
42         }
43         return parser.parseParameter(buffer);
44     }
45
46     @Override
47     public void serializeParameter(final BgpParameters parameter, final ByteBuf bytes) {
48         final ParameterSerializer serializer = this.handlers.getSerializer(parameter.getImplementedInterface());
49         if (serializer == null) {
50             return;
51         }
52         serializer.serializeParameter(parameter,bytes);
53     }
54 }