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;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.nio.charset.StandardCharsets;
17 import org.opendaylight.protocol.util.ByteBufWriteUtil;
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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Gauge64;
22 * Created by cgasparini on 13.5.2015.
24 public final class TlvUtil {
27 throw new UnsupportedOperationException();
30 public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
31 formatTlvHeader(type, value.readableBytes(), out);
32 out.writeBytes(value);
35 public static void formatTlvShort16(final int type, final int value, final ByteBuf out) {
36 formatTlvHeader(type, SHORT_BYTES_LENGTH, out);
37 ByteBufWriteUtil.writeUnsignedShort(value, out);
40 public static void formatTlvCounter32(final int type, final Counter32 value, final ByteBuf out) {
41 if (value != null && value.getValue() != null) {
42 formatTlvHeader(type, INT_BYTES_LENGTH, out);
43 ByteBufWriteUtil.writeUnsignedInt(value.getValue(), out);
47 public static void formatTlvGauge64(final int type, final Gauge64 value, final ByteBuf out) {
48 if (value != null && value.getValue() != null) {
49 formatTlvHeader(type, LONG_BYTES_LENGTH, out);
50 ByteBufWriteUtil.writeUnsignedLong(value.getValue(), out);
54 public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
56 TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
60 public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
62 TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
67 private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
68 ByteBufWriteUtil.writeUnsignedShort(type, output);
69 ByteBufWriteUtil.writeUnsignedShort(length, output);