YANG revision dates mass-update
[bgpcep.git] / bmp / 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 package org.opendaylight.protocol.bmp.parser.tlv;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.stat.tlvs.PerAfiSafiAdjRibInTlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.stat.tlvs.PerAfiSafiAdjRibInTlvBuilder;
25 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26
27 public class StatType009TlvHandler implements BmpTlvParser, BmpTlvSerializer {
28     public static final int TYPE = 9;
29
30     private final AddressFamilyRegistry afiRegistry;
31     private final SubsequentAddressFamilyRegistry safiRegistry;
32
33     public StatType009TlvHandler(final AddressFamilyRegistry afiReg, final SubsequentAddressFamilyRegistry safiReg) {
34         this.afiRegistry = requireNonNull(afiReg, "AddressFamily cannot be null");
35         this.safiRegistry = requireNonNull(safiReg, "SubsequentAddressFamily cannot be null");
36     }
37
38     @Override
39     public void serializeTlv(final Tlv tlv, final ByteBuf output) {
40         checkArgument(tlv instanceof PerAfiSafiAdjRibInTlv, "PerAfiSafiAdjRibInTlv is mandatory.");
41         final PerAfiSafiAdjRibInTlv perAfiSafi = (PerAfiSafiAdjRibInTlv) tlv;
42
43         final ByteBuf buffer = Unpooled.buffer();
44         final Integer afiInt = this.afiRegistry.numberForClass(perAfiSafi.getAfi());
45         buffer.writeShort(afiInt != null ? afiInt : 0);
46         buffer.writeByte(this.safiRegistry.numberForClass(perAfiSafi.getSafi()));
47         ByteBufUtils.write(buffer, perAfiSafi.getCount().getValue());
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(ByteBufUtils.readUint64(buffer))).build();
60     }
61 }