Merge "Fixed incorrect parsing of Linklocal/remote link identifier."
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / ObjectUtil.java
1 /*
2  * Copyright (c) 2013 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 io.netty.buffer.ByteBuf;
11 import java.util.BitSet;
12 import org.opendaylight.protocol.util.ByteArray;
13
14 public final class ObjectUtil {
15
16     private static final int HEADER_SIZE = 4;
17
18     private static final int OT_SF_LENGTH = 4;
19     /*
20      * flags offsets inside multi-field
21      */
22     private static final int P_FLAG_OFFSET = 6;
23     private static final int I_FLAG_OFFSET = 7;
24
25     private ObjectUtil() {
26     }
27
28     public static void formatSubobject(final int objectType, final int objectClass, final Boolean processingRule, final Boolean ignore,
29         final ByteBuf body, final ByteBuf out) {
30         out.writeByte(objectClass);
31         BitSet flags = new BitSet(Byte.SIZE);
32         if (ignore != null) {
33             flags.set(I_FLAG_OFFSET, ignore);
34         }
35         if (processingRule != null) {
36             flags.set(P_FLAG_OFFSET, processingRule);
37         }
38         byte[] flagB = ByteArray.bitSetToBytes(flags, 1);
39         int typeByte = objectType << OT_SF_LENGTH | flagB[0];
40         out.writeByte(typeByte);
41         out.writeShort(body.writerIndex() + HEADER_SIZE);
42         out.writeBytes(body);
43     }
44 }