Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / bmp-parser-impl / src / main / java / org / opendaylight / protocol / bmp / parser / tlv / StatType009TlvHandler.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bmp.parser.tlv;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.math.BigInteger;
17 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
18 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
19 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
20 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
21 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
22 import org.opendaylight.protocol.bmp.spi.parser.TlvUtil;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.ByteBufWriteUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.Tlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.stat.tlvs.PerAfiSafiAdjRibInTlv;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.stat.tlvs.PerAfiSafiAdjRibInTlvBuilder;
29
30 public class StatType009TlvHandler implements BmpTlvParser, BmpTlvSerializer {
31
32     public static final int TYPE = 9;
33     private final AddressFamilyRegistry afiRegistry;
34     private final SubsequentAddressFamilyRegistry safiRegistry;
35
36     public StatType009TlvHandler(final AddressFamilyRegistry afiReg, SubsequentAddressFamilyRegistry safiReg) {
37         this.afiRegistry = requireNonNull(afiReg, "AddressFamily cannot be null");
38         this.safiRegistry = requireNonNull(safiReg, "SubsequentAddressFamily cannot be null");
39     }
40
41     @Override
42     public void serializeTlv(final Tlv tlv, final ByteBuf output) {
43         Preconditions.checkArgument(tlv instanceof PerAfiSafiAdjRibInTlv, "PerAfiSafiAdjRibInTlv is mandatory.");
44         final ByteBuf buffer = Unpooled.buffer();
45         ByteBufWriteUtil.writeUnsignedShort(this.afiRegistry.numberForClass(((PerAfiSafiAdjRibInTlv) tlv).getAfi()), buffer);
46         ByteBufWriteUtil.writeUnsignedByte(this.safiRegistry.numberForClass(((PerAfiSafiAdjRibInTlv) tlv).getSafi()).shortValue(), buffer);
47         ByteBufWriteUtil.writeUnsignedLong(((PerAfiSafiAdjRibInTlv) tlv).getCount().getValue(), buffer);
48         TlvUtil.formatTlv(TYPE, buffer, output);
49     }
50
51     @Override
52     public Tlv parseTlv(final ByteBuf buffer) throws BmpDeserializationException {
53         if (buffer == null) {
54             return null;
55         }
56         return new PerAfiSafiAdjRibInTlvBuilder()
57                        .setAfi(this.afiRegistry.classForFamily(buffer.readUnsignedShort()))
58                        .setSafi(this.safiRegistry.classForFamily(buffer.readUnsignedByte()))
59                        .setCount(new Gauge64(new BigInteger(ByteArray.readAllBytes(buffer)))).build();
60     }
61 }