471888649f651ac30a402d8402b7960a955c3600
[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.genius.mdsalutil.NxMatchFieldType;
25 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
26 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;
27 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;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRangeBuilder;
32
33
34 public class AclServiceTestUtils {
35
36     public static void verifyGeneralFlows(List<MatchInfoBase> srcFlowMatches, String protocol, String srcIpv4Net,
37             String dstIpv4Net, String mask) {
38         verifyMatchInfo(srcFlowMatches, MatchFieldType.eth_type, Integer.toString(NwConstants.ETHTYPE_IPV4));
39         verifyMatchInfo(srcFlowMatches, MatchFieldType.ip_proto, protocol);
40         verifyMatchInfo(srcFlowMatches, MatchFieldType.ipv4_source, srcIpv4Net, mask);
41         verifyMatchInfo(srcFlowMatches, MatchFieldType.ipv4_destination, dstIpv4Net, mask);
42     }
43
44     public static AceIpBuilder prepareAceIpBuilder(String srcIpv4Net, String dstIpv4Net, String lowerPort,
45             String upperPort, short protocol) {
46         AceIpBuilder builder = new AceIpBuilder();
47         AceIpv4Builder v4builder = new AceIpv4Builder();
48         if (srcIpv4Net != null) {
49             v4builder.setSourceIpv4Network(new Ipv4Prefix(srcIpv4Net));
50         } else {
51             v4builder.setSourceIpv4Network(null);
52         }
53
54         if (dstIpv4Net != null) {
55             v4builder.setDestinationIpv4Network(new Ipv4Prefix(dstIpv4Net));
56         } else {
57             v4builder.setDestinationIpv4Network(null);
58         }
59         builder.setAceIpVersion(v4builder.build());
60         if (lowerPort != null && upperPort != null) {
61             SourcePortRangeBuilder srcPortBuilder = new SourcePortRangeBuilder();
62             srcPortBuilder.setLowerPort(PortNumber.getDefaultInstance(lowerPort));
63             srcPortBuilder.setUpperPort(PortNumber.getDefaultInstance(upperPort));
64             builder.setSourcePortRange(srcPortBuilder.build());
65             DestinationPortRangeBuilder dstPortBuilder = new DestinationPortRangeBuilder();
66             dstPortBuilder.setLowerPort(PortNumber.getDefaultInstance(lowerPort));
67             dstPortBuilder.setUpperPort(PortNumber.getDefaultInstance(upperPort));
68             builder.setDestinationPortRange(dstPortBuilder.build());
69         }
70         builder.setProtocol(protocol);
71         return builder;
72     }
73
74     public static void verifyMatchInfo(List<MatchInfoBase> flowMatches, MatchFieldType matchType, String... params) {
75         Iterable<MatchInfoBase> matches = filter(flowMatches,
76             (item -> item instanceof MatchInfo) );
77         matches = filter(matches,
78                 (item -> ((MatchInfo) item).getMatchField().equals(matchType)));
79         for (MatchInfoBase baseMatch : matches) {
80             verifyMatchValues((MatchInfo) baseMatch, params);
81         }
82     }
83
84     public static void verifyMatchValues(MatchInfo match, String... params) {
85         switch (match.getMatchField()) {
86
87             case ip_proto:
88             case eth_type:
89                 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
90                 Assert.assertArrayEquals(values, match.getMatchValues());
91                 break;
92             case ipv4_source:
93             case ipv4_destination:
94                 Assert.assertArrayEquals(params, match.getStringMatchValues());
95                 break;
96             default:
97                 assertTrue("match type is not supported", true);
98                 break;
99         }
100     }
101
102     public static void verifyMatchValues(NxMatchInfo match, String... params) {
103         switch (match.getMatchField()) {
104             case nx_tcp_src_with_mask:
105             case nx_tcp_dst_with_mask:
106             case nx_udp_src_with_mask:
107             case nx_udp_dst_with_mask:
108                 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
109                 Assert.assertArrayEquals(values, match.getMatchValues());
110                 break;
111             default:
112                 assertTrue("match type is not supported", true);
113                 break;
114         }
115     }
116
117     public static void verifyMatchFieldTypeDontExist(List<MatchInfoBase> flowMatches, MatchFieldType matchType) {
118         Iterable<MatchInfoBase> matches = filter(flowMatches,
119                 (item -> ((MatchInfo) item).getMatchField().equals(matchType)));
120         Assert.assertTrue("unexpected match type " + matchType.name(), Iterables.isEmpty(matches));
121     }
122
123     public static void verifyMatchFieldTypeDontExist(List<MatchInfoBase> flowMatches, NxMatchFieldType matchType) {
124         Iterable<MatchInfoBase> matches = filter(flowMatches,
125                 (item -> ((MatchInfo) item).getMatchField().equals(matchType)));
126         Assert.assertTrue("unexpected match type " + matchType.name(), Iterables.isEmpty(matches));
127     }
128 }