BUG-731: utility classes should be final
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / LabelUtil.java
1 /*
2  * Copyright (c) 2014 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.pcep.spi;
9
10 import java.util.BitSet;
11
12 import org.opendaylight.protocol.util.ByteArray;
13
14 import com.google.common.primitives.UnsignedBytes;
15
16 public final class LabelUtil {
17
18         private static final int RES_F_LENGTH = 1;
19
20         private static final int C_TYPE_F_LENGTH = 1;
21
22         private static final int RES_F_OFFSET = 0;
23
24         private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
25
26         private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
27
28         private static final int U_FLAG_OFFSET = 0;
29
30         private static final int G_FLAG_OFFSET = 7;
31
32         private LabelUtil() {
33                 throw new UnsupportedOperationException("Utility class should not be instantiated");
34         }
35
36         public static byte[] formatLabel(final int type, final boolean unidirectional, final boolean global, final byte[] labelbytes) {
37
38                 final byte[] retBytes = new byte[labelbytes.length + HEADER_LENGTH];
39
40                 System.arraycopy(labelbytes, 0, retBytes, HEADER_LENGTH, labelbytes.length);
41
42                 final BitSet reserved = new BitSet();
43                 reserved.set(U_FLAG_OFFSET, unidirectional);
44                 reserved.set(G_FLAG_OFFSET, global);
45                 System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
46                 retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(type);
47                 return retBytes;
48         }
49 }