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 {
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;
18 private AttributeUtil() {
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.
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
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);
37 buffer.writeShort(length);
39 buffer.writeByte(length);
41 buffer.writeBytes(value);