2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.aclservice.utils;
11 import static com.google.common.collect.Iterables.filter;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.Iterables;
16 import java.util.Arrays;
17 import java.util.List;
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;
34 public class AclServiceTestUtils {
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);
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));
51 v4builder.setSourceIpv4Network(null);
54 if (dstIpv4Net != null) {
55 v4builder.setDestinationIpv4Network(new Ipv4Prefix(dstIpv4Net));
57 v4builder.setDestinationIpv4Network(null);
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());
70 builder.setProtocol(protocol);
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);
84 public static void verifyMatchValues(MatchInfo match, String... params) {
85 switch (match.getMatchField()) {
89 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
90 Assert.assertArrayEquals(values, match.getMatchValues());
93 case ipv4_destination:
94 Assert.assertArrayEquals(params, match.getStringMatchValues());
97 assertTrue("match type is not supported", true);
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());
112 assertTrue("match type is not supported", true);
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));
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));