2 * Copyright (c) 2017 Lumina Networks, 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.flowspec.handlers;
10 import static org.junit.Assert.assertArrayEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.NumericOperand;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.flowspec.flowspec.type.icmp.code._case.Codes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.flowspec.flowspec.type.icmp.code._case.CodesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.flowspec.flowspec.type.port._case.Ports;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.flowspec.flowspec.type.port._case.PortsBuilder;
24 public class NumericOperandParserTest {
25 private static final byte[] ONE_BYTE_CODE_LIST = new byte[]{
28 (byte) 0x81, 0x66, // last port in the list should have end-of-list set
32 public void testSerializeTwoByte() {
33 final ByteBuf nlriByteBuf = Unpooled.buffer();
34 final List<Ports> ports = new ArrayList<>();
35 // create 3 ports without end-of-list bit set
36 for (int i = 0; i < 3; i++) {
39 .setOp(new NumericOperand(false, false, true, false, false))
44 NumericTwoByteOperandParser.INSTANCE.serialize(ports, nlriByteBuf);
45 assertArrayEquals(ONE_BYTE_CODE_LIST, ByteArray.readAllBytes(nlriByteBuf));
49 public void testSerializeOneByte() {
50 final ByteBuf nlriByteBuf = Unpooled.buffer();
51 final List<Codes> codes = new ArrayList<>();
52 // create 3 ports without end-of-list bit set
53 for (int i = 0; i < 3; i++) {
56 .setOp(new NumericOperand(false, false, true, false, false))
57 .setValue((short) (100 + i))
61 NumericOneByteOperandParser.INSTANCE.serialize(codes, nlriByteBuf);
62 assertArrayEquals(ONE_BYTE_CODE_LIST, ByteArray.readAllBytes(nlriByteBuf));
66 public void testSerializeVariableByte() {
67 final ByteBuf nlriByteBuf = Unpooled.buffer();
68 // test with a operand with endOfList set to true, but override with false
69 NumericOneByteOperandParser.INSTANCE.serialize(
70 new NumericOperand(false, true, true, false, false),
74 assertArrayEquals(new byte[]{(byte) 0x01}, ByteArray.readAllBytes(nlriByteBuf));
76 // test with a operand with endOfList set to false, but override with true
78 NumericOneByteOperandParser.INSTANCE.serialize(
79 new NumericOperand(false, true, true, false, false),
83 assertArrayEquals(new byte[]{(byte) 0x81}, ByteArray.readAllBytes(nlriByteBuf));