Action redesign: use the new ActionInfo Genius classes
[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.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.collect.Iterables;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26
27 import org.junit.Assert;
28 import org.opendaylight.genius.mdsalutil.ActionInfo;
29 import org.opendaylight.genius.mdsalutil.MatchFieldType;
30 import org.opendaylight.genius.mdsalutil.MatchInfo;
31 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
32 import org.opendaylight.genius.mdsalutil.NwConstants;
33 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
34 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
35 import org.opendaylight.genius.mdsalutil.actions.ActionLearn;
36 import org.opendaylight.genius.utils.cache.CacheUtil;
37 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
41 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;
42 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;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRangeBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
48
49
50 public class AclServiceTestUtils {
51
52     public static void verifyGeneralFlows(List<MatchInfoBase> srcFlowMatches, String protocol, String srcIpv4Net,
53             String dstIpv4Net, String mask) {
54         verifyMatchInfo(srcFlowMatches, MatchFieldType.eth_type, Integer.toString(NwConstants.ETHTYPE_IPV4));
55         verifyMatchInfo(srcFlowMatches, MatchFieldType.ip_proto, protocol);
56         verifyMatchInfo(srcFlowMatches, MatchFieldType.ipv4_source, srcIpv4Net, mask);
57         verifyMatchInfo(srcFlowMatches, MatchFieldType.ipv4_destination, dstIpv4Net, mask);
58     }
59
60     public static AceIpBuilder prepareAceIpBuilder(String srcIpv4Net, String dstIpv4Net, String lowerPort,
61             String upperPort, short protocol) {
62         AceIpBuilder builder = new AceIpBuilder();
63         AceIpv4Builder v4builder = new AceIpv4Builder();
64         if (srcIpv4Net != null) {
65             v4builder.setSourceIpv4Network(new Ipv4Prefix(srcIpv4Net));
66         } else {
67             v4builder.setSourceIpv4Network(null);
68         }
69
70         if (dstIpv4Net != null) {
71             v4builder.setDestinationIpv4Network(new Ipv4Prefix(dstIpv4Net));
72         } else {
73             v4builder.setDestinationIpv4Network(null);
74         }
75         builder.setAceIpVersion(v4builder.build());
76         if (lowerPort != null && upperPort != null) {
77             SourcePortRangeBuilder srcPortBuilder = new SourcePortRangeBuilder();
78             srcPortBuilder.setLowerPort(PortNumber.getDefaultInstance(lowerPort));
79             srcPortBuilder.setUpperPort(PortNumber.getDefaultInstance(upperPort));
80             builder.setSourcePortRange(srcPortBuilder.build());
81             DestinationPortRangeBuilder dstPortBuilder = new DestinationPortRangeBuilder();
82             dstPortBuilder.setLowerPort(PortNumber.getDefaultInstance(lowerPort));
83             dstPortBuilder.setUpperPort(PortNumber.getDefaultInstance(upperPort));
84             builder.setDestinationPortRange(dstPortBuilder.build());
85         }
86         builder.setProtocol(protocol);
87         return builder;
88     }
89
90     public static void verifyMatchInfo(List<MatchInfoBase> flowMatches, MatchFieldType matchType, String... params) {
91         Iterable<MatchInfoBase> matches = filter(flowMatches,
92             item -> item instanceof MatchInfo && ((MatchInfo) item).getMatchField().equals(matchType)
93                  || item instanceof NxMatchInfo && ((NxMatchInfo) item).getMatchField().equals(matchType));
94         assertFalse(Iterables.isEmpty(matches));
95         for (MatchInfoBase baseMatch : matches) {
96             if (baseMatch instanceof MatchInfo) {
97                 verifyMatchValues((MatchInfo)baseMatch, params);
98             } else {
99                 verifyMatchValues((NxMatchInfo)baseMatch, params);
100             }
101         }
102     }
103
104     public static void verifyMatchInfo(List<MatchInfoBase> flowMatches, NxMatchFieldType matchType, String... params) {
105         Iterable<MatchInfoBase> matches = filter(flowMatches,
106             item -> item instanceof MatchInfo && ((MatchInfo) item).getMatchField().equals(matchType)
107                  || item instanceof NxMatchInfo && ((NxMatchInfo) item).getMatchField().equals(matchType));
108         assertFalse(Iterables.isEmpty(matches));
109         for (MatchInfoBase baseMatch : matches) {
110             if (baseMatch instanceof MatchInfo) {
111                 verifyMatchValues((MatchInfo)baseMatch, params);
112             } else {
113                 verifyMatchValues((NxMatchInfo)baseMatch, params);
114             }
115         }
116     }
117
118     public static void verifyMatchValues(NxMatchInfo match, String... params) {
119         switch (match.getMatchField()) {
120             case nx_tcp_src_with_mask:
121             case nx_tcp_dst_with_mask:
122             case nx_udp_src_with_mask:
123             case nx_udp_dst_with_mask:
124             case ct_state:
125                 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
126                 Assert.assertArrayEquals(values, match.getMatchValues());
127                 break;
128             default:
129                 assertTrue("match type is not supported", false);
130                 break;
131         }
132     }
133
134     public static void verifyMatchValues(MatchInfo match, String... params) {
135         switch (match.getMatchField()) {
136
137             case ip_proto:
138             case eth_type:
139             case tcp_flags:
140             case icmp_v4:
141                 long[] values = Arrays.stream(params).mapToLong(l -> Long.parseLong(l)).toArray();
142                 Assert.assertArrayEquals(values, match.getMatchValues());
143                 break;
144             case ipv4_source:
145             case ipv4_destination:
146             case eth_src:
147             case eth_dst:
148             case arp_sha:
149             case arp_tha:
150                 Assert.assertArrayEquals(params, match.getStringMatchValues());
151                 break;
152             default:
153                 assertTrue("match type is not supported", false);
154                 break;
155         }
156     }
157
158     public static void verifyMatchFieldTypeDontExist(List<MatchInfoBase> flowMatches, MatchFieldType matchType) {
159         Iterable<MatchInfoBase> matches = filter(flowMatches,
160             item -> ((MatchInfo) item).getMatchField().equals(matchType));
161         Assert.assertTrue("unexpected match type " + matchType.name(), Iterables.isEmpty(matches));
162     }
163
164     public static void verifyMatchFieldTypeDontExist(List<MatchInfoBase> flowMatches, NxMatchFieldType matchType) {
165         Iterable<MatchInfoBase> matches = filter(flowMatches,
166             item -> ((MatchInfo) item).getMatchField().equals(matchType));
167         Assert.assertTrue("unexpected match type " + matchType.name(), Iterables.isEmpty(matches));
168     }
169
170     public static void prepareAclDataUtil(AclDataUtil aclDataUtil, AclInterface inter, String... updatedAclNames) {
171         aclDataUtil.addAclInterfaceMap(prapreaAclIds(updatedAclNames), inter);
172     }
173
174     public static Acl prepareAcl(String aclName, String... aces) {
175         AccessListEntries aceEntries = mock(AccessListEntries.class);
176         List<Ace> aceList = prepareAceList(aces);
177         when(aceEntries.getAce()).thenReturn(aceList);
178
179         Acl acl = mock(Acl.class);
180         when(acl.getAccessListEntries()).thenReturn(aceEntries);
181         when(acl.getAclName()).thenReturn(aclName);
182         return acl;
183     }
184
185     public static List<Ace> prepareAceList(String... aces) {
186         List<Ace> aceList = new ArrayList<>();
187         for (String aceName : aces) {
188             Ace aceMock = mock(Ace.class);
189             when(aceMock.getRuleName()).thenReturn(aceName);
190             aceList.add(aceMock);
191         }
192         return aceList;
193     }
194
195     public static List<Uuid> prapreaAclIds(String... names) {
196         return Stream.of(names).map(name -> new Uuid(name)).collect(Collectors.toList());
197     }
198
199     public static void verifyActionTypeExist(List<ActionInfo> flowActions, Class<? extends ActionInfo> actionType) {
200         assertTrue(flowActions.stream().anyMatch(actionInfo -> actionInfo.getClass().equals(actionType)));
201     }
202
203     public static void verifyActionInfo(List<ActionInfo> flowActions, ActionInfo actionInfo) {
204         assertTrue(flowActions.contains(actionInfo));
205     }
206
207     public static void verifyActionLearn(List<ActionInfo> flowActions, ActionLearn actionLearn) {
208         for (ActionInfo actionInfo : flowActions) {
209             if (actionInfo instanceof ActionLearn) {
210                 ActionLearn check = (ActionLearn) actionInfo;
211                 assertEquals(actionLearn.getCookie(), check.getCookie());
212                 assertEquals(actionLearn.getFinHardTimeout(), check.getFinHardTimeout());
213                 assertEquals(actionLearn.getFinIdleTimeout(), check.getFinIdleTimeout());
214                 assertEquals(actionLearn.getFlags(), check.getFlags());
215                 assertEquals(actionLearn.getHardTimeout(), check.getHardTimeout());
216                 assertEquals(actionLearn.getIdleTimeout(), check.getIdleTimeout());
217                 assertEquals(actionLearn.getPriority(), check.getPriority());
218                 assertEquals(actionLearn.getTableId(), check.getTableId());
219             }
220         }
221     }
222
223     public static void prepareAclClusterUtil(String entityName) {
224         if (CacheUtil.getCache("entity.owner.cache") == null) {
225             CacheUtil.createCache("entity.owner.cache");
226         }
227         ConcurrentMap entityOwnerCache = CacheUtil.getCache("entity.owner.cache");
228         if (entityOwnerCache != null) {
229             entityOwnerCache.put(entityName, true);
230         }
231
232     }
233
234 }