Add ByteBufUtils
[bgpcep.git] / bmp / bmp-parser-impl / src / main / java / org / opendaylight / protocol / bmp / parser / tlv / StatType005TlvHandler.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 com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
14 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
15 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
16 import org.opendaylight.protocol.bmp.spi.parser.TlvUtil;
17 import org.opendaylight.protocol.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.stat.tlvs.InvalidatedOriginatorIdTlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.stat.tlvs.InvalidatedOriginatorIdTlvBuilder;
22
23 public class StatType005TlvHandler implements BmpTlvParser, BmpTlvSerializer {
24
25     public static final int TYPE = 5;
26
27     @Override
28     public void serializeTlv(final Tlv tlv, final ByteBuf output) {
29         Preconditions.checkArgument(tlv instanceof InvalidatedOriginatorIdTlv,
30                 "InvalidatedOriginatorIdTlv is mandatory.");
31         TlvUtil.formatTlvCounter32(TYPE, ((InvalidatedOriginatorIdTlv) tlv).getCount(), output);
32     }
33
34     @Override
35     public Tlv parseTlv(final ByteBuf buffer) throws BmpDeserializationException {
36         if (buffer == null) {
37             return null;
38         }
39         return new InvalidatedOriginatorIdTlvBuilder().setCount(new Counter32(ByteBufUtils.readUint32(buffer))).build();
40     }
41
42 }