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() {
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 type of the attribute
30 * @param value attribute value
31 * @param buffer ByteBuf where the attribute will be copied with its header
33 public static void formatAttribute(final int flags, final int type, final ByteBuf value, final ByteBuf buffer) {
34 final int length = value.writerIndex();
35 final boolean extended = (length > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE) ? true : false;
36 buffer.writeByte((extended) ? (flags | EXTENDED) : flags);
37 buffer.writeByte(type);
39 buffer.writeShort(length);
41 buffer.writeByte(length);
43 buffer.writeBytes(value);