2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.spi;
10 import io.netty.buffer.ByteBuf;
12 public final class AttributeUtil {
14 private static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 255;
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;
21 private AttributeUtil() {
22 throw new UnsupportedOperationException();
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.
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
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);
40 buffer.writeShort(length);
42 buffer.writeByte(length);
44 buffer.writeBytes(value);