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.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;
32 public class AclServiceTestUtils {
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);
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));
49 v4builder.setSourceIpv4Network(null);
52 if (dstIpv4Net != null) {
53 v4builder.setDestinationIpv4Network(new Ipv4Prefix(dstIpv4Net));
55 v4builder.setDestinationIpv4Network(null);
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());
68 builder.setProtocol(protocol);
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);
80 public static void verifyMatchValues(MatchInfo match, String... params) {
81 switch (match.getMatchField()) {
88 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
89 Assert.assertArrayEquals(values, match.getMatchValues());
92 case ipv4_destination:
93 Assert.assertArrayEquals(params, match.getStringMatchValues());
96 assertTrue("match type is not supported", true);
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));