Fix most bgp-parser-spi checkstyle violations
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleBgpPrefixSidTlvRegistry.java
1 /*
2  * Copyright (c) 2016 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 io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvParser;
14 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
15 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvUtil;
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.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21
22 public final class SimpleBgpPrefixSidTlvRegistry implements BgpPrefixSidTlvRegistry {
23
24     private final HandlerRegistry<DataContainer, BgpPrefixSidTlvParser, BgpPrefixSidTlvSerializer> handlers =
25             new HandlerRegistry<>();
26
27     AutoCloseable registerBgpPrefixSidTlvParser(final int tlvType, final BgpPrefixSidTlvParser parser) {
28         Preconditions.checkArgument(tlvType >= 0 && tlvType <= Values.UNSIGNED_BYTE_MAX_VALUE);
29         return this.handlers.registerParser(tlvType, parser);
30     }
31
32     AutoCloseable registerBgpPrefixSidTlvSerializer(final Class<? extends BgpPrefixSidTlv> tlvClass,
33             final BgpPrefixSidTlvSerializer serializer) {
34         return this.handlers.registerSerializer(tlvClass, serializer);
35     }
36
37     @Override
38     public BgpPrefixSidTlv parseBgpPrefixSidTlv(final int type, final ByteBuf buffer) {
39         final BgpPrefixSidTlvParser parser = this.handlers.getParser(type);
40         if (parser == null) {
41             return null;
42         }
43         final int length = buffer.readUnsignedShort();
44         Preconditions.checkState(length <= buffer.readableBytes(),
45                 "Length of BGP prefix SID TLV exceeds readable bytes of income.");
46         return parser.parseBgpPrefixSidTlv(buffer.readBytes(length));
47     }
48
49     @Override
50     public void serializeBgpPrefixSidTlv(final BgpPrefixSidTlv tlv, final ByteBuf bytes) {
51         final BgpPrefixSidTlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
52         if (serializer == null) {
53             return;
54         }
55         final ByteBuf valueBuf = Unpooled.buffer();
56         serializer.serializeBgpPrefixSidTlv(tlv, valueBuf);
57         BgpPrefixSidTlvUtil.formatBgpPrefixSidTlv(serializer.getType(), valueBuf, bytes);
58     }
59 }