Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / handlers / NumericOperandParserTest.java
1 /*
2  * Copyright (c) 2017 Lumina Networks, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.flowspec.handlers;
9
10 import static org.junit.Assert.assertArrayEquals;
11
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.rev180329.NumericOperand;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.code._case.Codes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.code._case.CodesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.port._case.Ports;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.port._case.PortsBuilder;
23
24 public class NumericOperandParserTest {
25     private static final byte[] ONE_BYTE_CODE_LIST = new byte[]{
26             0x01, 0x64,
27             0x01, 0x65,
28             (byte) 0x81, 0x66,    // last port in the list should have end-of-list set
29     };
30
31     @Test
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++) {
37             ports.add(
38                     new PortsBuilder()
39                             .setOp(new NumericOperand(false, false, true, false, false))
40                             .setValue(100 + i)
41                             .build()
42             );
43         }
44         NumericTwoByteOperandParser.INSTANCE.serialize(ports, nlriByteBuf);
45         assertArrayEquals(ONE_BYTE_CODE_LIST, ByteArray.readAllBytes(nlriByteBuf));
46     }
47
48     @Test
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++) {
54             codes.add(
55                     new CodesBuilder()
56                             .setOp(new NumericOperand(false, false, true, false, false))
57                             .setValue((short) (100 + i))
58                             .build()
59             );
60         }
61         NumericOneByteOperandParser.INSTANCE.serialize(codes, nlriByteBuf);
62         assertArrayEquals(ONE_BYTE_CODE_LIST, ByteArray.readAllBytes(nlriByteBuf));
63     }
64
65     @Test
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),
71                 1,
72                 false,
73                 nlriByteBuf);
74         assertArrayEquals(new byte[]{(byte) 0x01}, ByteArray.readAllBytes(nlriByteBuf));
75
76         // test with a operand with endOfList set to false, but override with true
77         nlriByteBuf.clear();
78         NumericOneByteOperandParser.INSTANCE.serialize(
79                 new NumericOperand(false, true, true, false, false),
80                 1,
81                 true,
82                 nlriByteBuf);
83         assertArrayEquals(new byte[]{(byte) 0x81}, ByteArray.readAllBytes(nlriByteBuf));
84     }
85 }