Cleanup boolean literals
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / AttributeUtil.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.bgp.parser.spi;
9
10 import io.netty.buffer.ByteBuf;
11
12 public final class AttributeUtil {
13     public static final int OPTIONAL = 128;
14     public static final int TRANSITIVE = 64;
15     public static final int PARTIAL = 32;
16     private static final int EXTENDED = 16;
17
18     private AttributeUtil() {
19         // Hidden on purpose
20     }
21
22     /**
23      * Adds header to attribute value. If the length of the attribute value exceeds one-byte length field,
24      * set EXTENDED bit and write length as 2B field.
25      *
26      * @param flags attribute flags
27      * @param type of the attribute
28      * @param value attribute value
29      * @param buffer ByteBuf where the attribute will be copied with its header
30      */
31     public static void formatAttribute(final int flags, final int type, final ByteBuf value, final ByteBuf buffer) {
32         final int length = value.writerIndex();
33         final boolean extended = length > 255;
34         buffer.writeByte(extended ? flags | EXTENDED : flags);
35         buffer.writeByte(type);
36         if (extended) {
37             buffer.writeShort(length);
38         } else {
39             buffer.writeByte(length);
40         }
41         buffer.writeBytes(value);
42     }
43 }