BGPCEP-706: Fix BGP Flowspec NumbericOphrand
[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 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;
20
21 /**
22  * Created by cgasparini on 13.5.2015.
23  */
24 public final class TlvUtil {
25
26     private TlvUtil() {
27         throw new UnsupportedOperationException();
28     }
29
30     public static void formatTlv(final int type, final ByteBuf value, final ByteBuf out) {
31         formatTlvHeader(type, value.readableBytes(), out);
32         out.writeBytes(value);
33     }
34
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);
38     }
39
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);
44         }
45     }
46
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);
51         }
52     }
53
54     public static void formatTlvUtf8(final int type, final String value, final ByteBuf out) {
55         if (value != null) {
56             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.UTF_8), out);
57         }
58     }
59
60     public static void formatTlvAscii(final int type, final String value, final ByteBuf out) {
61         if (value != null) {
62             TlvUtil.formatTlv(type, Unpooled.copiedBuffer(value, StandardCharsets.US_ASCII), out);
63         }
64
65     }
66
67     private static void formatTlvHeader(final int type, final int length, final ByteBuf output) {
68         ByteBufWriteUtil.writeUnsignedShort(type, output);
69         ByteBufWriteUtil.writeUnsignedShort(length, output);
70     }
71 }