package org.opendaylight.protocol.bgp.flowspec;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
abstract @NonNull DestinationType createWithdrawnDestinationType(List<Flowspec> flowspecList,
@Nullable PathId pathId);
+
+ protected final void serializeNlri(final List<Flowspec> flowspecList, final @Nullable PathId pathId,
+ final @NonNull ByteBuf buffer) {
+ final var nlri = Unpooled.buffer();
+ serializeNlri(flowspecList, nlri);
+ appendNlri(pathId, nlri, buffer);
+ }
}
*/
package org.opendaylight.protocol.bgp.flowspec;
+import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
-import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
}
}
- protected void serializeNlri(final Object @NonNull [] nlriFields, final @NonNull ByteBuf buffer) {
- final List<Flowspec> flowspecList = (List<Flowspec>) nlriFields[0];
- serializeNlri(flowspecList, buffer);
- }
-
protected final void serializeNlri(final List<Flowspec> flowspecList, final @NonNull ByteBuf buffer) {
if (flowspecList != null) {
for (final Flowspec flow : flowspecList) {
}
/**
- * Serializes Flowspec NLRI to ByteBuf.
+ * Appends a Flowspec NLRI buffer to an output ByteBuf.
*
- * @param nlriFields NLRI fields to be serialized
* @param pathId path ID
- * @param buffer where flowspec NLRI will be serialized
+ * @param nlri NLRI to be appended
+ * @param output where flowspec NLRI will be appended
*/
- protected final void serializeNlri(final Object @NonNull[] nlriFields, final @Nullable PathId pathId,
- final @NonNull ByteBuf buffer) {
- final ByteBuf nlriByteBuf = Unpooled.buffer();
- PathIdUtil.writePathId(pathId, buffer);
-
- serializeNlri(nlriFields, nlriByteBuf);
+ protected static final void appendNlri(final @Nullable PathId pathId, final @NonNull ByteBuf nlri,
+ final @NonNull ByteBuf output) {
+ checkState(nlri.readableBytes() <= MAX_NLRI_LENGTH, "Maximum length of Flowspec NLRI reached.");
+ PathIdUtil.writePathId(pathId, output);
- Preconditions.checkState(nlriByteBuf.readableBytes() <= MAX_NLRI_LENGTH,
- "Maximum length of Flowspec NLRI reached.");
- if (nlriByteBuf.readableBytes() <= MAX_NLRI_LENGTH_ONE_BYTE) {
- buffer.writeByte(nlriByteBuf.readableBytes());
+ if (nlri.readableBytes() <= MAX_NLRI_LENGTH_ONE_BYTE) {
+ output.writeByte(nlri.readableBytes());
} else {
- buffer.writeShort(nlriByteBuf.readableBytes() + LENGTH_MAGIC);
+ output.writeShort(nlri.readableBytes() + LENGTH_MAGIC);
}
- buffer.writeBytes(nlriByteBuf);
+ output.writeBytes(nlri);
}
public String stringNlri(final DataContainerNode flowspec) {
public static int readNlriLength(final @NonNull ByteBuf nlri) {
requireNonNull(nlri, "NLRI information cannot be null");
- Preconditions.checkState(nlri.isReadable(), "NLRI Byte buffer is not readable.");
+ checkState(nlri.isReadable(), "NLRI Byte buffer is not readable.");
int length = nlri.readUnsignedByte();
if (length >= MAX_NLRI_LENGTH_ONE_BYTE) {
length = (length << Byte.SIZE | nlri.readUnsignedByte()) & MAX_NLRI_LENGTH;
}
- Preconditions.checkState(length > 0 && length <= nlri.readableBytes(),
- "Invalid flowspec NLRI length %s", length);
+ checkState(length > 0 && length <= nlri.readableBytes(), "Invalid flowspec NLRI length %s", length);
return length;
}
while (nlri.isReadable()) {
int nlriLength = readNlriLength(nlri);
- Preconditions.checkState(nlriLength > 0 && nlriLength <= nlri.readableBytes(),
- "Invalid flowspec NLRI length %s", nlriLength);
+ checkState(nlriLength > 0 && nlriLength <= nlri.readableBytes(),
+ "Invalid flowspec NLRI length %s", nlriLength);
LOG.trace("Flowspec NLRI length is {}", nlriLength);
while (nlriLength > 0) {
final int flowspecTypeLength = readableLength - nlri.readableBytes();
nlriLength -= flowspecTypeLength;
}
- Preconditions.checkState(nlriLength == 0,
- "Remain NLRI length should be 0 instead of %s", nlriLength);
+ checkState(nlriLength == 0, "Remain NLRI length should be 0 instead of %s", nlriLength);
}
return fss;
final DestinationFlowspecIpv4 destFlowspec = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type
.DestinationFlowspecCase) dstType).getDestinationFlowspecIpv4();
- serializeNlri(new Object[] {destFlowspec.getFlowspec()}, destFlowspec.getPathId(), byteAggregator);
+ serializeNlri(destFlowspec.getFlowspec(), destFlowspec.getPathId(), byteAggregator);
}
}
final DestinationFlowspecIpv4 destFlowspec = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
.yang.bgp.flowspec.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
.DestinationFlowspecCase) dstType).getDestinationFlowspecIpv4();
- serializeNlri(new Object[] {destFlowspec.getFlowspec()}, destFlowspec.getPathId(), byteAggregator);
+ serializeNlri(destFlowspec.getFlowspec(), destFlowspec.getPathId(), byteAggregator);
}
}
}
final DestinationFlowspecIpv6 destFlowspec = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type
.DestinationFlowspecIpv6Case) dstType).getDestinationFlowspecIpv6();
- serializeNlri(new Object[] {destFlowspec.getFlowspec()}, destFlowspec.getPathId(), byteAggregator);
+ serializeNlri(destFlowspec.getFlowspec(), destFlowspec.getPathId(), byteAggregator);
}
}
final DestinationFlowspecIpv6 destFlowspec = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
.yang.bgp.flowspec.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
.DestinationFlowspecIpv6Case) dstType).getDestinationFlowspecIpv6();
- serializeNlri(new Object[] {destFlowspec.getFlowspec()}, destFlowspec.getPathId(), byteAggregator);
+ serializeNlri(destFlowspec.getFlowspec(), destFlowspec.getPathId(), byteAggregator);
}
}
}
import static java.util.Objects.requireNonNull;
import static org.opendaylight.bgp.concepts.RouteDistinguisherUtil.extractRouteDistinguisher;
+import com.google.common.annotations.VisibleForTesting;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
return rd;
}
- @Override
- protected final void serializeNlri(final Object[] nlriFields, final ByteBuf buffer) {
- final RouteDistinguisher rd = requireNonNull((RouteDistinguisher) nlriFields[0]);
- RouteDistinguisherUtil.serializeRouteDistinquisher(rd, buffer);
- final List<Flowspec> flowspecList = (List<Flowspec>) nlriFields[1];
- serializeNlri(flowspecList, buffer);
+ @VisibleForTesting
+ public final void serializeNlri(final @NonNull RouteDistinguisher rd, final List<Flowspec> flowspecList,
+ final @Nullable PathId pathId, final @NonNull ByteBuf buffer) {
+ final var nlri = Unpooled.buffer();
+ RouteDistinguisherUtil.serializeRouteDistinquisher(rd, nlri);
+ serializeNlri(flowspecList, nlri);
+ appendNlri(pathId, nlri, buffer);
}
@Override
.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type
.DestinationFlowspecL3vpnIpv4Case) dstType).getDestinationFlowspecL3vpnIpv4();
serializeNlri(
- new Object[] {
- destFlowspec.getRouteDistinguisher(),
- destFlowspec.getFlowspec()
- },
+ destFlowspec.getRouteDistinguisher(),
+ destFlowspec.getFlowspec(),
destFlowspec.getPathId(),
byteAggregator
);
.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
.DestinationFlowspecL3vpnIpv4Case) dstType).getDestinationFlowspecL3vpnIpv4();
serializeNlri(
- new Object[] {
- destFlowspec.getRouteDistinguisher(),
- destFlowspec.getFlowspec()
- },
+ destFlowspec.getRouteDistinguisher(),
+ destFlowspec.getFlowspec(),
destFlowspec.getPathId(),
byteAggregator
);
.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type
.DestinationFlowspecL3vpnIpv6Case) dstType).getDestinationFlowspecL3vpnIpv6();
serializeNlri(
- new Object[] {
- destFlowspec.getRouteDistinguisher(),
- destFlowspec.getFlowspec()
- },
+ destFlowspec.getRouteDistinguisher(),
+ destFlowspec.getFlowspec(),
destFlowspec.getPathId(),
byteAggregator
);
.ns.yang.bgp.flowspec.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
.DestinationFlowspecL3vpnIpv6Case) dstType).getDestinationFlowspecL3vpnIpv6();
serializeNlri(
- new Object[] {
- destFlowspec.getRouteDistinguisher(),
- destFlowspec.getFlowspec()
- },
+ destFlowspec.getRouteDistinguisher(),
+ destFlowspec.getFlowspec(),
destFlowspec.getPathId(),
byteAggregator
);
);
final ByteBuf buffer = Unpooled.buffer();
- parser.serializeNlri(new Object[] {rd, flows}, null, buffer);
+ parser.serializeNlri(rd, flows, null, buffer);
assertArrayEquals(UNREACHED_NLRI, ByteArray.readAllBytes(buffer));
parser.serializeAttribute(new AttributesBuilder()
);
final ByteBuf buffer = Unpooled.buffer();
- parser.serializeNlri(new Object[] {rd, flows}, PATH_ID, buffer);
+ parser.serializeNlri(rd, flows, PATH_ID, buffer);
assertArrayEquals(UNREACHED_NLRI_ADD_PATH, ByteArray.readAllBytes(buffer));
parser.serializeAttribute(new AttributesBuilder()
checkUnreachFlows(flows, icmpType, icmpCode, tcp, packet, dscp, fragment);
final ByteBuf buffer = Unpooled.buffer();
- parser.serializeNlri(new Object[] {flows}, null, buffer);
+ parser.serializeNlri(flows, null, buffer);
assertArrayEquals(UNREACHED_NLRI, ByteArray.readAllBytes(buffer));
parser.serializeAttribute(new AttributesBuilder()
final ByteBuf buffer = Unpooled.buffer();
- parser.serializeNlri(new Object[] {flows}, PATH_ID, buffer);
+ parser.serializeNlri(flows, PATH_ID, buffer);
assertArrayEquals(UNREACHED_NLRI_ADD_PATH, ByteArray.readAllBytes(buffer));
parser.serializeAttribute(new AttributesBuilder()