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 java.util.List;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
23 public final class MessageUtil {
26 public static final int MARKER_LENGTH = 16;
28 public static final int COMMON_HEADER_LENGTH = 19;
29 private static final byte[] MARKER = new byte[MARKER_LENGTH];
32 Arrays.fill(MARKER, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE);
35 private MessageUtil() {
36 throw new UnsupportedOperationException();
40 * Adds header to message value.
42 * @param type of the message
43 * @param body message body
44 * @param buffer ByteBuf where the message will be copied with its header
46 public static void formatMessage(final int type, final ByteBuf body, final ByteBuf buffer) {
47 buffer.writeBytes(MARKER);
48 buffer.writeShort(body.writerIndex() + COMMON_HEADER_LENGTH);
49 buffer.writeByte(type);
50 buffer.writeBytes(body);
54 * Check for NLRI attribute in Update message
56 * @param message Update message
57 * @return true if any prefix or MP-REACH-NLRI attribute is present, false otherwise
59 public static boolean isAnyNlriPresent(final Update message) {
60 if (message == null || message.getAttributes() == null) {
63 final List<Nlri> nlri = message.getNlri();
64 return nlri != null && !nlri.isEmpty()
65 || getMpReachNlri(message.getAttributes()) != null;
69 * Finds MP-REACH-NLRI in Update message attributes
71 * @param attrs Update message attributes
72 * @return MP-REACH-NLRI if present in the attributes, null otherwise
74 public static MpReachNlri getMpReachNlri(final Attributes attrs) {
75 if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) {
76 return attrs.getAugmentation(Attributes1.class).getMpReachNlri();
83 * Finds MP-UNREACH-NLRI in Update message attributes
85 * @param attrs Update message attributes
86 * @return MP-UNREACH-NLRI if present in the attributes, null otherwise
88 public static MpUnreachNlri getMpUnreachNlri(final Attributes attrs) {
89 if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) {
90 return attrs.getAugmentation(Attributes2.class).getMpUnreachNlri();