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
9 package org.opendaylight.protocol.bmp.spi.parser;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.INT_BYTES_LENGTH;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.LONG_BYTES_LENGTH;
13 import static org.opendaylight.protocol.util.ByteBufWriteUtil.SHORT_BYTES_LENGTH;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.nio.charset.StandardCharsets;
18 import org.opendaylight.protocol.util.ByteBufWriteUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
22 public final class TlvUtil {
25 throw new UnsupportedOperationException();
28 public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
29 formatTlvHeader(type, value.readableBytes(), out);
30 out.writeBytes(value);
33 public static void formatTlvShort16(final int type, final int value, final ByteBuf out) {
34 formatTlvHeader(type, SHORT_BYTES_LENGTH, out);
35 ByteBufWriteUtil.writeUnsignedShort(value, out);
38 public static void formatTlvCounter32(final int type, final Counter32 value, final ByteBuf out) {
39 if (value != null && value.getValue() != null) {
40 formatTlvHeader(type, INT_BYTES_LENGTH, out);
41 ByteBufWriteUtil.writeUnsignedInt(value.getValue(), out);
45 public static void formatTlvGauge64(final int type, final Gauge64 value, final ByteBuf out) {
46 if (value != null && value.getValue() != null) {
47 formatTlvHeader(type, LONG_BYTES_LENGTH, out);
48 ByteBufWriteUtil.writeUnsignedLong(value.getValue(), out);
52 public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
54 TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
58 public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
60 TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
65 private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
66 ByteBufWriteUtil.writeUnsignedShort(type, output);
67 ByteBufWriteUtil.writeUnsignedShort(length, output);