Migrate DTO builders
[bgpcep.git] / util / src / main / java / org / opendaylight / protocol / util / MplsLabelUtil.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.util;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
13 import org.opendaylight.yangtools.yang.common.Uint32;
14
15 /**
16  * Util class for encoding/decoding 20bit leftmost value.
17  */
18 public final class MplsLabelUtil {
19
20     private static final int LABEL_OFFSET = 4;
21     private static final byte BOTTOM_LABEL_BIT = 0x1;
22
23     private MplsLabelUtil() {
24         throw new UnsupportedOperationException();
25     }
26
27     /**
28      * Creates and returns MplsLabel object from 20 leftmost bits in the incoming buffer.
29      * Moves reader index by 3.
30      *
31      * @param buffer expecting 3 bytes with 20 leftmost bits as label
32      * @return MplsLabel object
33      */
34     public static MplsLabel mplsLabelForByteBuf(final ByteBuf buffer) {
35         return new MplsLabel(Uint32.valueOf(buffer.readUnsignedMedium() >> LABEL_OFFSET));
36     }
37
38     /**
39      * Serializes incoming MplsLabel without bottom bit.
40      *
41      * @param label MplsLabel object
42      * @return ByteBuf
43      */
44     public static ByteBuf byteBufForMplsLabel(final MplsLabel label) {
45         return Unpooled.copyMedium(intForMplsLabel(label));
46     }
47
48     /**
49      * Serializes incoming MplsLabel with bottom bit.
50      *
51      * @param label MplsLabel object
52      * @return ByteBuf
53      */
54     public static ByteBuf byteBufForMplsLabelWithBottomBit(final MplsLabel label) {
55         return Unpooled.copyMedium(intForMplsLabelWithBottomBit(label));
56     }
57
58     /**
59      * Makes a value of incoming label 20 leftmost bits in 24bit number.
60      *
61      * @param label object
62      * @return shifted value
63      */
64     public static int intForMplsLabel(final MplsLabel label) {
65         return label.getValue().intValue() << LABEL_OFFSET;
66     }
67
68     /**
69      * Makes a value of incoming label 20 leftmost bits in 24bit number and sets bottom bit.
70      *
71      * @param label object
72      * @return value with bottom bit
73      */
74     private static int intForMplsLabelWithBottomBit(final MplsLabel label) {
75         final int value = intForMplsLabel(label);
76         return setBottomBit(value);
77     }
78
79     /**
80      * Sets bottom bit of 3 byte value.
81      *
82      * @param value where 20 leftmost bits are label
83      * @return value with set bottom bit
84      */
85     private static int setBottomBit(final int value) {
86         return value | BOTTOM_LABEL_BIT;
87     }
88
89     /**
90      * Gets Bottom Bit.
91      *
92      * @param slice with 20 leftmost bits as label
93      * @return value of bottom bit
94      */
95     public static boolean getBottomBit(final ByteBuf slice) {
96         return (slice.getUnsignedMedium(slice.readerIndex()) & BOTTOM_LABEL_BIT) == 1;
97     }
98 }