Remove useless UnsupportedOperationExceptions
[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     private TlvUtil() {
19         // Hidden on purpose
20     }
21
22     public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
23         formatTlvHeader(type, value.readableBytes(), out);
24         out.writeBytes(value);
25     }
26
27     public static void formatTlvShort16(final int type, final int value, final ByteBuf out) {
28         formatTlvHeader(type, Short.BYTES, out);
29         ByteBufWriteUtil.writeUnsignedShort(value, out);
30     }
31
32     public static void formatTlvCounter32(final int type, final Counter32 value, final ByteBuf out) {
33         if (value != null && value.getValue() != null) {
34             formatTlvHeader(type, Integer.BYTES, out);
35             ByteBufWriteUtil.writeUnsignedInt(value.getValue(), out);
36         }
37     }
38
39     public static void formatTlvGauge64(final int type, final Gauge64 value, final ByteBuf out) {
40         if (value != null && value.getValue() != null) {
41             formatTlvHeader(type, Long.BYTES, out);
42             ByteBufWriteUtil.writeUnsignedLong(value.getValue(), out);
43         }
44     }
45
46     public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
47         if (value != null) {
48             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
49         }
50     }
51
52     public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
53         if (value != null) {
54             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
55         }
56
57     }
58
59     private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
60         ByteBufWriteUtil.writeUnsignedShort(type, output);
61         ByteBufWriteUtil.writeUnsignedShort(length, output);
62     }
63 }