Fix most bgp-parser-spi checkstyle violations
[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
14     private static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 255;
15
16     public static final int OPTIONAL = 128;
17     public static final int TRANSITIVE = 64;
18     public static final int PARTIAL = 32;
19     private static final int EXTENDED = 16;
20
21     private AttributeUtil() {
22         throw new UnsupportedOperationException();
23     }
24
25     /**
26      * Adds header to attribute value. If the length of the attribute value exceeds one-byte length field,
27      * set EXTENDED bit and write length as 2B field.
28      *
29      * @param flags attribute flags
30      * @param type of the attribute
31      * @param value attribute value
32      * @param buffer ByteBuf where the attribute will be copied with its header
33      */
34     public static void formatAttribute(final int flags, final int type, final ByteBuf value, final ByteBuf buffer) {
35         final int length = value.writerIndex();
36         final boolean extended = length > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE ? true : false;
37         buffer.writeByte(extended ? flags | EXTENDED : flags);
38         buffer.writeByte(type);
39         if (extended) {
40             buffer.writeShort(length);
41         } else {
42             buffer.writeByte(length);
43         }
44         buffer.writeBytes(value);
45     }
46 }