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