fa147583c24b0333320a40cdcd3b178152a56b12
[bgpcep.git] / bmp / bmp-spi / src / main / java / org / opendaylight / protocol / bmp / spi / parser / TlvUtil.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
9 package org.opendaylight.protocol.bmp.spi.parser;
10
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
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;
21
22 public final class TlvUtil {
23
24     private TlvUtil() {
25         throw new UnsupportedOperationException();
26     }
27
28     public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
29         formatTlvHeader(type, value.readableBytes(), out);
30         out.writeBytes(value);
31     }
32
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);
36     }
37
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);
42         }
43     }
44
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);
49         }
50     }
51
52     public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
53         if (value != null) {
54             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
55         }
56     }
57
58     public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
59         if (value != null) {
60             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
61         }
62
63     }
64
65     private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
66         ByteBufWriteUtil.writeUnsignedShort(type, output);
67         ByteBufWriteUtil.writeUnsignedShort(length, output);
68     }
69 }