Create common parent for extensions families
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / Util.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.bgp.flowspec.handlers;
9
10 import com.google.common.primitives.UnsignedBytes;
11 import com.google.common.primitives.UnsignedInts;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.util.ByteBufWriteUtil;
14 import org.opendaylight.protocol.util.Values;
15
16 public final class Util {
17
18     private Util() {
19         throw new UnsupportedOperationException();
20     }
21
22     /**
23      * Given the integer values, this method instead of writing the value
24      * in 4B field, compresses the value to lowest required byte field
25      * depending on the value.
26      *
27      * @param value integer to be written
28      * @param buffer ByteBuf where the value will be written
29      */
30     public static void writeShortest(final int value, final ByteBuf buffer) {
31         if (value <= Values.UNSIGNED_BYTE_MAX_VALUE) {
32             buffer.writeByte(UnsignedBytes.checkedCast(value));
33         } else if (value <= Values.UNSIGNED_SHORT_MAX_VALUE) {
34             ByteBufWriteUtil.writeUnsignedShort(value, buffer);
35         } else {
36             //value <= Values.UNSIGNED_INT_MAX_VALUE
37             ByteBufWriteUtil.writeUnsignedInt(UnsignedInts.toLong(value), buffer);
38         }
39     }
40 }