2 * Copyright (c) 2013 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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import java.util.Arrays;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
22 public final class MessageUtil {
25 public static final int MARKER_LENGTH = 16;
27 public static final int COMMON_HEADER_LENGTH = 19;
28 private static final byte[] MARKER = new byte[MARKER_LENGTH];
31 Arrays.fill(MARKER, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE);
34 private MessageUtil() {
35 throw new UnsupportedOperationException();
39 * Adds header to message value.
41 * @param type of the message
42 * @param body message body
43 * @param buffer ByteBuf where the message will be copied with its header
45 public static void formatMessage(final int type, final ByteBuf body, final ByteBuf buffer) {
46 buffer.writeBytes(MARKER);
47 buffer.writeShort(body.writerIndex() + COMMON_HEADER_LENGTH);
48 buffer.writeByte(type);
49 buffer.writeBytes(body);
53 * Check for NLRI attribute in Update message
55 * @param message Update message
56 * @return true if any prefix or MP-REACH-NLRI attribute is present, false otherwise
58 public static boolean isAnyNlriPresent(final Update message) {
59 if (message == null || message.getAttributes() == null) {
62 final Nlri nlri = message.getNlri();
63 return (nlri != null && nlri.getNlri() != null && !nlri.getNlri().isEmpty())
64 || (getMpReachNlri(message.getAttributes()) != null);
68 * Finds MP-REACH-NLRI in Update message attributes
70 * @param attrs Update message attributes
71 * @return MP-REACH-NLRI if present in the attributes, null otherwise
73 public static MpReachNlri getMpReachNlri(final Attributes attrs) {
74 if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) {
75 return attrs.getAugmentation(Attributes1.class).getMpReachNlri();
82 * Finds MP-UNREACH-NLRI in Update message attributes
84 * @param attrs Update message attributes
85 * @return MP-UNREACH-NLRI if present in the attributes, null otherwise
87 public static MpUnreachNlri getMpUnreachNlri(final Attributes attrs) {
88 if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) {
89 return attrs.getAugmentation(Attributes2.class).getMpUnreachNlri();