/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.protocol.pcep.sync.optimizations;

import static com.google.common.base.Preconditions.checkArgument;
import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedLong;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.math.BigInteger;
import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
import org.opendaylight.protocol.pcep.spi.TlvParser;
import org.opendaylight.protocol.pcep.spi.TlvSerializer;
import org.opendaylight.protocol.pcep.spi.TlvUtil;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.lsp.db.version.tlv.LspDbVersion;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.lsp.db.version.tlv.LspDbVersionBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;

public class LspDbVersionTlvParser implements TlvParser, TlvSerializer {

    public static final int TYPE = 23;

    @Override
    public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
        checkArgument(tlv instanceof LspDbVersion, "Tlv object is not instance of LspDbVersion.");
        final ByteBuf body = Unpooled.buffer();
        writeUnsignedLong(((LspDbVersion) tlv).getLspDbVersionValue(), body);
        TlvUtil.formatTlv(TYPE, body, buffer);
    }

    @Override
    public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
        if (buffer == null) {
            return null;
        }
        return new LspDbVersionBuilder().setLspDbVersionValue(new BigInteger(ByteArray.readAllBytes(buffer))).build();
    }

}

@@ -483,35 +483,95 @@
 eBuf
-WriteUtil.writeUnsignedLong
+Utils.readUint64;%0Aimport static org.opendaylight.protocol.util.ByteBufUtils.writeOrZero
 ;%0A%0Ai
@@ -638,37 +638,8 @@
 ed;%0A
-import java.math.BigInteger;%0A
 impo
@@ -868,57 +868,8 @@
 il;%0A
-import org.opendaylight.protocol.util.ByteArray;%0A
 impo
@@ -1343,17 +1343,16 @@
 lizer %7B%0A
-%0A
     publ
@@ -1629,21 +1629,21 @@
 rite
-UnsignedLong(
+OrZero(body, 
 ((Ls
@@ -1681,22 +1681,16 @@
 nValue()
-, body
 );%0A     
@@ -1964,45 +1964,18 @@
 lue(
-new BigInteger(ByteArray.readAllBytes
+readUint64
 (buf
@@ -1979,17 +1979,16 @@
 buffer))
-)
 .build()
@@ -1995,11 +1995,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Migrate LspDbVersion parsing to ByteBufUtils