Remove superfluous constants
[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 package org.opendaylight.protocol.bmp.spi.parser;
9
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;
16
17 public final class TlvUtil {
18
19     private TlvUtil() {
20         throw new UnsupportedOperationException();
21     }
22
23     public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
24         formatTlvHeader(type, value.readableBytes(), out);
25         out.writeBytes(value);
26     }
27
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);
31     }
32
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);
37         }
38     }
39
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);
44         }
45     }
46
47     public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
48         if (value != null) {
49             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
50         }
51     }
52
53     public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
54         if (value != null) {
55             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
56         }
57
58     }
59
60     private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
61         ByteBufWriteUtil.writeUnsignedShort(type, output);
62         ByteBufWriteUtil.writeUnsignedShort(length, output);
63     }
64 }