summaryrefslogtreecommitdiffstats
path: root/java/jvpp-acl/io/fd/vpp/jvpp/acl/examples/AclExpectedDumpData.java
blob: 4806052f5b73ef2edd571c091f9a4a9adf7036bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.vpp.jvpp.acl.examples;


import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_ADDRESS_2_AS_ARRAY;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_ADDRESS_AS_ARRAY;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_DST_ICMP_TYPE_END;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_DST_ICMP_TYPE_START;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_MAC;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_MAC_MASK;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_PREFIX;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_PREFIX_2;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_SRC_ICMP_TYPE_END;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.FIRST_RULE_SRC_ICMP_TYPE_START;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.ICMP_PROTOCOL;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_ADDRESS_2_AS_ARRAY;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_ADDRESS_AS_ARRAY;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_DST_PORT_RANGE_END;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_DST_PORT_RANGE_START;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_MAC;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_MAC_MASK;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_PREFIX;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_PREFIX_2;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_SRC_PORT_RANGE_END;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.SECOND_RULE_SRC_PORT_RANGE_START;
import static io.fd.vpp.jvpp.acl.examples.AclTestData.UDP_PROTOCOL;

import io.fd.vpp.jvpp.acl.dto.AclDetails;
import io.fd.vpp.jvpp.acl.dto.AclInterfaceListDetails;
import io.fd.vpp.jvpp.acl.dto.MacipAclDetails;
import io.fd.vpp.jvpp.acl.types.AclRule;
import io.fd.vpp.jvpp.acl.types.MacipAclRule;
import java.util.Arrays;

class AclExpectedDumpData {

    static void verifyMacIpDump(final MacipAclDetails macipAclDetails) {
        // asserting data create by previous call
        assertEquals(0, macipAclDetails.aclIndex);
        assertEquals(2, macipAclDetails.count);

        final MacipAclRule currentIpv4Rule = macipAclDetails.r[0];
        final MacipAclRule currentIpv6Rule = macipAclDetails.r[1];

        // Comparing one property at the time to better pointer if something is wrong
        //Ipv4 rule
        assertEquals(0, currentIpv4Rule.isIpv6);
        assertEquals(1, currentIpv4Rule.isPermit);

        // cutting expected ipv4 to 4 bytes,vpp sends it as 16 always
        assertArrays(FIRST_RULE_ADDRESS_AS_ARRAY, Arrays.copyOfRange(currentIpv4Rule.srcIpAddr, 0, 4));
        assertEquals(FIRST_RULE_PREFIX, currentIpv4Rule.srcIpPrefixLen);
        assertArrays(FIRST_RULE_MAC, currentIpv4Rule.srcMac);
        assertArrays(FIRST_RULE_MAC_MASK, currentIpv4Rule.srcMacMask);

        //Ipv6 rule
        assertEquals(1, currentIpv6Rule.isIpv6);
        assertEquals(0, currentIpv6Rule.isPermit);
        assertArrays(SECOND_RULE_ADDRESS_AS_ARRAY, currentIpv6Rule.srcIpAddr);
        assertEquals(SECOND_RULE_PREFIX, currentIpv6Rule.srcIpPrefixLen);
        assertArrays(SECOND_RULE_MAC, currentIpv6Rule.srcMac);
        assertArrays(SECOND_RULE_MAC_MASK, currentIpv6Rule.srcMacMask);
    }

    static void verifyAclDump(final AclDetails aclDetails) {
        assertEquals(0, aclDetails.aclIndex);
        assertEquals(2, aclDetails.count);

        final AclRule currentIpv4Rule = aclDetails.r[0];
        final AclRule currentIpv6Rule = aclDetails.r[1];

        // Comparing one property at the time to better pointer if something is wrong
        //Ipv4 rule
        assertEquals(0, currentIpv4Rule.isIpv6);
        assertEquals(1, currentIpv4Rule.isPermit);

        // cutting expected ipv4 to 4 bytes,vpp sends it as 16 always
        assertArrays(FIRST_RULE_ADDRESS_AS_ARRAY, Arrays.copyOfRange(currentIpv4Rule.srcIpAddr, 0, 4));
        assertEquals(FIRST_RULE_PREFIX, currentIpv4Rule.srcIpPrefixLen);
        assertArrays(FIRST_RULE_ADDRESS_2_AS_ARRAY, Arrays.copyOfRange(currentIpv4Rule.dstIpAddr, 0, 4));
        assertEquals(FIRST_RULE_PREFIX_2, currentIpv4Rule.dstIpPrefixLen);

        assertEquals(ICMP_PROTOCOL, currentIpv4Rule.proto);
        assertEquals(FIRST_RULE_SRC_ICMP_TYPE_START, currentIpv4Rule.srcportOrIcmptypeFirst);
        assertEquals(FIRST_RULE_SRC_ICMP_TYPE_END, currentIpv4Rule.srcportOrIcmptypeLast);
        assertEquals(FIRST_RULE_DST_ICMP_TYPE_START, currentIpv4Rule.dstportOrIcmpcodeFirst);
        assertEquals(FIRST_RULE_DST_ICMP_TYPE_END, currentIpv4Rule.dstportOrIcmpcodeLast);

        assertArrays(SECOND_RULE_ADDRESS_AS_ARRAY, currentIpv6Rule.srcIpAddr);
        assertEquals(SECOND_RULE_PREFIX, currentIpv6Rule.srcIpPrefixLen);
        assertArrays(SECOND_RULE_ADDRESS_2_AS_ARRAY, currentIpv6Rule.dstIpAddr);
        assertEquals(SECOND_RULE_PREFIX_2, currentIpv6Rule.dstIpPrefixLen);

        assertEquals(UDP_PROTOCOL, currentIpv6Rule.proto);
        assertEquals(SECOND_RULE_SRC_PORT_RANGE_START, currentIpv6Rule.srcportOrIcmptypeFirst);
        assertEquals(SECOND_RULE_SRC_PORT_RANGE_END, currentIpv6Rule.srcportOrIcmptypeLast);
        assertEquals(SECOND_RULE_DST_PORT_RANGE_START, currentIpv6Rule.dstportOrIcmpcodeFirst);
        assertEquals(SECOND_RULE_DST_PORT_RANGE_END, currentIpv6Rule.dstportOrIcmpcodeLast);
    }

    static void verifyAclInterfaceList(final AclInterfaceListDetails aclInterfaceListDetails) {
        assertEquals(1, aclInterfaceListDetails.count);
        assertEquals(1, aclInterfaceListDetails.acls[0]);
        assertEquals(0, aclInterfaceListDetails.nInput);
        assertEquals(0, aclInterfaceListDetails.swIfIndex);
    }

    private static void assertArrays(final byte[] expected, final byte[] actual) {
        if (!Arrays.equals(expected, actual)) {
            throw new IllegalArgumentException(
                    String.format("Expected[%s]/Actual[%s]", Arrays.toString(expected), Arrays.toString(actual)));
        }
    }

    private static void assertEquals(final int expected, final int actual) {
        if (expected != actual) {
            throw new IllegalArgumentException(String.format("Expected[%s]/Actual[%s]", expected, actual));
        }
    }
}