2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bmp.spi.parser;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.nio.charset.StandardCharsets;
13 import org.opendaylight.protocol.util.ByteBufWriteUtil;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
17 public final class TlvUtil {
20 throw new UnsupportedOperationException();
23 public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
24 formatTlvHeader(type, value.readableBytes(), out);
25 out.writeBytes(value);
28 public static void formatTlvShort16(final int type, final int value, final ByteBuf out) {
29 formatTlvHeader(type, Short.BYTES, out);
30 ByteBufWriteUtil.writeUnsignedShort(value, out);
33 public static void formatTlvCounter32(final int type, final Counter32 value, final ByteBuf out) {
34 if (value != null && value.getValue() != null) {
35 formatTlvHeader(type, Integer.BYTES, out);
36 ByteBufWriteUtil.writeUnsignedInt(value.getValue(), out);
40 public static void formatTlvGauge64(final int type, final Gauge64 value, final ByteBuf out) {
41 if (value != null && value.getValue() != null) {
42 formatTlvHeader(type, Long.BYTES, out);
43 ByteBufWriteUtil.writeUnsignedLong(value.getValue(), out);
47 public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
49 TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
53 public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
55 TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
60 private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
61 ByteBufWriteUtil.writeUnsignedShort(type, output);
62 ByteBufWriteUtil.writeUnsignedShort(length, output);