5f1cf6f510475ebabfeb140cb68e7d8f6b057e1f
[netvirt.git] / vpnservice / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / utils / AclServiceTestUtils.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.aclservice.utils;
10
11 import static com.google.common.collect.Iterables.filter;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Iterables;
15
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.junit.Assert;
20 import org.opendaylight.genius.mdsalutil.MatchFieldType;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRangeBuilder;
30
31
32 public class AclServiceTestUtils {
33
34     public static void verifyGeneralFlows(List<MatchInfoBase> srcFlowMatches, String protocol, String srcIpv4Net,
35             String dstIpv4Net, String mask) {
36         verifyMatchInfo(srcFlowMatches, MatchFieldType.eth_type, Integer.toString(NwConstants.ETHTYPE_IPV4));
37         verifyMatchInfo(srcFlowMatches, MatchFieldType.ip_proto, protocol);
38         verifyMatchInfo(srcFlowMatches, MatchFieldType.ipv4_source, srcIpv4Net, mask);
39         verifyMatchInfo(srcFlowMatches, MatchFieldType.ipv4_destination, dstIpv4Net, mask);
40     }
41
42     public static AceIpBuilder prepareAceIpBuilder(String srcIpv4Net, String dstIpv4Net, String lowerPort,
43             String upperPort, short protocol) {
44         AceIpBuilder builder = new AceIpBuilder();
45         AceIpv4Builder v4builder = new AceIpv4Builder();
46         if (srcIpv4Net != null) {
47             v4builder.setSourceIpv4Network(new Ipv4Prefix(srcIpv4Net));
48         } else {
49             v4builder.setSourceIpv4Network(null);
50         }
51
52         if (dstIpv4Net != null) {
53             v4builder.setDestinationIpv4Network(new Ipv4Prefix(dstIpv4Net));
54         } else {
55             v4builder.setDestinationIpv4Network(null);
56         }
57         builder.setAceIpVersion(v4builder.build());
58         if (lowerPort != null && upperPort != null) {
59             SourcePortRangeBuilder srcPortBuilder = new SourcePortRangeBuilder();
60             srcPortBuilder.setLowerPort(PortNumber.getDefaultInstance(lowerPort));
61             srcPortBuilder.setUpperPort(PortNumber.getDefaultInstance(upperPort));
62             builder.setSourcePortRange(srcPortBuilder.build());
63             DestinationPortRangeBuilder dstPortBuilder = new DestinationPortRangeBuilder();
64             dstPortBuilder.setLowerPort(PortNumber.getDefaultInstance(lowerPort));
65             dstPortBuilder.setUpperPort(PortNumber.getDefaultInstance(upperPort));
66             builder.setDestinationPortRange(dstPortBuilder.build());
67         }
68         builder.setProtocol(protocol);
69         return builder;
70     }
71
72     public static void verifyMatchInfo(List<MatchInfoBase> flowMatches, MatchFieldType matchType, String... params) {
73         Iterable<MatchInfoBase> matches = filter(flowMatches,
74                 (item -> ((MatchInfo) item).getMatchField().equals(matchType)));
75         for (MatchInfoBase baseMatch : matches) {
76             verifyMatchValues((MatchInfo) baseMatch, params);
77         }
78     }
79
80     public static void verifyMatchValues(MatchInfo match, String... params) {
81         switch (match.getMatchField()) {
82             case tcp_src:
83             case tcp_dst:
84             case ip_proto:
85             case udp_src:
86             case udp_dst:
87             case eth_type:
88                 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
89                 Assert.assertArrayEquals(values, match.getMatchValues());
90                 break;
91             case ipv4_source:
92             case ipv4_destination:
93                 Assert.assertArrayEquals(params, match.getStringMatchValues());
94                 break;
95             default:
96                 assertTrue("match type is not supported", true);
97                 break;
98         }
99     }
100
101     public static void verifyMatchFieldTypeDontExist(List<MatchInfoBase> flowMatches, MatchFieldType matchType) {
102         Iterable<MatchInfoBase> matches = filter(flowMatches,
103                 (item -> ((MatchInfo) item).getMatchField().equals(matchType)));
104         Assert.assertTrue("unexpected match type " + matchType.name(), Iterables.isEmpty(matches));
105     }
106 }