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