Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.impl; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import com.google.common.util.concurrent.ListenableFuture; |
| 23 | import com.google.common.util.concurrent.MoreExecutors; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 27 | import org.onlab.junit.TestTools; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.IpAddress; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigAdapter; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.ControllerNode; |
| 33 | import org.onosproject.cluster.NodeId; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 34 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.core.ApplicationId; |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 36 | import org.onosproject.core.CoreServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.core.DefaultApplicationId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 38 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 39 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 40 | import org.onosproject.net.AnnotationKeys; |
| 41 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.DefaultDevice; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.Device.Type; |
| 45 | import org.onosproject.net.DeviceId; |
| 46 | import org.onosproject.net.MastershipRole; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 47 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceServiceAdapter; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 49 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 50 | import org.onosproject.net.driver.DefaultDriver; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 51 | import org.onosproject.net.driver.DriverRegistry; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 52 | import org.onosproject.net.driver.impl.DriverManager; |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 53 | import org.onosproject.net.driver.impl.DriverRegistryManager; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 55 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 56 | import org.onosproject.net.flow.DefaultFlowRule; |
| 57 | import org.onosproject.net.flow.FlowEntry; |
| 58 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 59 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.FlowRuleEvent; |
| 61 | import org.onosproject.net.flow.FlowRuleListener; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 62 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.flow.FlowRuleProvider; |
| 64 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 65 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 66 | import org.onosproject.net.flow.FlowRuleService; |
| 67 | import org.onosproject.net.flow.StoredFlowEntry; |
| 68 | import org.onosproject.net.flow.TrafficSelector; |
| 69 | import org.onosproject.net.flow.TrafficTreatment; |
| 70 | import org.onosproject.net.flow.criteria.Criterion; |
| 71 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 72 | import org.onosproject.net.flow.instructions.Instructions; |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 73 | import org.onosproject.net.flow.instructions.Instructions.MetadataInstruction; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 74 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 75 | import org.onosproject.net.pi.PiPipeconfServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 76 | import org.onosproject.net.provider.AbstractProvider; |
| 77 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 78 | import org.onosproject.store.trivial.SimpleFlowRuleStore; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 79 | |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 80 | import java.util.ArrayList; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 81 | import java.util.Collection; |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 82 | import java.util.Collections; |
| 83 | import java.util.HashMap; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 84 | import java.util.HashSet; |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 85 | import java.util.List; |
| 86 | import java.util.Map; |
| 87 | import java.util.Set; |
| 88 | import java.util.concurrent.ExecutionException; |
| 89 | import java.util.concurrent.Executor; |
| 90 | import java.util.concurrent.TimeUnit; |
| 91 | import java.util.concurrent.TimeoutException; |
| 92 | import java.util.concurrent.atomic.AtomicLong; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 93 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 94 | import static org.easymock.EasyMock.*; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 95 | import static org.junit.Assert.assertEquals; |
| 96 | import static org.junit.Assert.assertFalse; |
| 97 | import static org.junit.Assert.assertNotNull; |
| 98 | import static org.junit.Assert.assertTrue; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 99 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 100 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADDED; |
| 101 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED; |
| 102 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 103 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED; |
| 104 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_UPDATED; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 105 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Test codifying the flow rule service & flow rule provider service contracts. |
| 108 | */ |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 109 | public class FlowRuleManagerTest { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 110 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 111 | |
tom | f6ab215 | 2014-09-18 12:08:29 -0700 | [diff] [blame] | 112 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 113 | private static final ProviderId FOO_PID = new ProviderId("foo", "foo"); |
| 114 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 115 | private static final DeviceId DID = DeviceId.deviceId("of:001"); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 116 | private static final DeviceId FOO_DID = DeviceId.deviceId("foo:002"); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 117 | private static final int TIMEOUT = 10; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 118 | |
| 119 | private static final DefaultAnnotations ANNOTATIONS = |
| 120 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, "foo").build(); |
| 121 | |
| 122 | private static final Device DEV = |
| 123 | new DefaultDevice(PID, DID, Type.SWITCH, "", "", "", "", null); |
| 124 | private static final Device FOO_DEV = |
| 125 | new DefaultDevice(FOO_PID, FOO_DID, Type.SWITCH, "", "", "", "", null, ANNOTATIONS); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 126 | |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 127 | private FlowRuleManager mgr; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 128 | |
| 129 | protected FlowRuleService service; |
| 130 | protected FlowRuleProviderRegistry registry; |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 131 | protected FlowRuleProviderService providerService; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 132 | protected TestProvider provider; |
| 133 | protected TestListener listener = new TestListener(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 134 | private ApplicationId appId; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 135 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 136 | private TestDriverManager driverService; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 137 | private static final String NODE_ID = "1"; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 138 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 139 | @Before |
| 140 | public void setUp() { |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 141 | mgr = new FlowRuleManager(); |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 142 | mgr.store = new SimpleFlowRuleStore(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 143 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 144 | mgr.deviceService = new TestDeviceService(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 145 | mgr.mastershipService = new TestMastershipService(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 146 | mgr.coreService = new TestCoreService(); |
| 147 | mgr.operationsService = MoreExecutors.newDirectExecutorService(); |
| 148 | mgr.deviceInstallers = MoreExecutors.newDirectExecutorService(); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 149 | mgr.cfgService = new ComponentConfigAdapter(); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 150 | |
| 151 | ClusterService mockClusterService = createMock(ClusterService.class); |
| 152 | NodeId nodeId = new NodeId(NODE_ID); |
| 153 | MockControllerNode mockControllerNode = new MockControllerNode(nodeId); |
| 154 | expect(mockClusterService.getLocalNode()) |
| 155 | .andReturn(mockControllerNode).anyTimes(); |
| 156 | replay(mockClusterService); |
| 157 | mgr.clusterService = mockClusterService; |
| 158 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 159 | service = mgr; |
| 160 | registry = mgr; |
| 161 | |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 162 | DriverRegistryManager driverRegistry = new DriverRegistryManager(); |
| 163 | driverService = new TestDriverManager(driverRegistry); |
| 164 | driverRegistry.addDriver(new DefaultDriver("foo", ImmutableList.of(), "", "", "", |
| 165 | ImmutableMap.of(FlowRuleProgrammable.class, |
| 166 | TestFlowRuleProgrammable.class), |
| 167 | ImmutableMap.of())); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 168 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 169 | mgr.activate(null); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 170 | mgr.addListener(listener); |
| 171 | provider = new TestProvider(PID); |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 172 | providerService = this.registry.register(provider); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 173 | appId = new TestApplicationId(0, "FlowRuleManagerTest"); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 174 | assertTrue("provider should be registered", |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 175 | this.registry.getProviders().contains(provider.id())); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @After |
| 179 | public void tearDown() { |
| 180 | registry.unregister(provider); |
| 181 | assertFalse("provider should not be registered", |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 182 | registry.getProviders().contains(provider.id())); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 183 | service.removeListener(listener); |
| 184 | mgr.deactivate(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 185 | injectEventDispatcher(mgr, null); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 186 | mgr.deviceService = null; |
| 187 | } |
| 188 | |
| 189 | private FlowRule flowRule(int tsval, int trval) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 190 | return flowRule(DID, tsval, trval); |
| 191 | } |
| 192 | |
| 193 | private FlowRule flowRule(DeviceId did, int tsval, int trval) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 194 | TestSelector ts = new TestSelector(tsval); |
| 195 | TestTreatment tr = new TestTreatment(trval); |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 196 | return DefaultFlowRule.builder() |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 197 | .forDevice(did) |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 198 | .withSelector(ts) |
| 199 | .withTreatment(tr) |
| 200 | .withPriority(10) |
| 201 | .fromApp(appId) |
| 202 | .makeTemporary(TIMEOUT) |
| 203 | .build(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 204 | } |
| 205 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 206 | private FlowRule addFlowRule(int hval) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 207 | FlowRule rule = flowRule(hval, hval); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 208 | service.applyFlowRules(rule); |
| 209 | |
David Glantz | 0a5779c | 2021-09-22 14:34:14 -0500 | [diff] [blame^] | 210 | assertNotNull("rule should be found", service.getFlowEntry(rule)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 211 | return rule; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 214 | private void validateEvents(FlowRuleEvent.Type... events) { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 215 | if (events == null) { |
| 216 | assertTrue("events generated", listener.events.isEmpty()); |
| 217 | } |
| 218 | |
| 219 | int i = 0; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 220 | System.err.println("events :" + listener.events); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 221 | for (FlowRuleEvent e : listener.events) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 222 | assertEquals("unexpected event", events[i], e.type()); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 223 | i++; |
| 224 | } |
| 225 | |
| 226 | assertEquals("mispredicted number of events", |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 227 | events.length, listener.events.size()); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 228 | |
| 229 | listener.events.clear(); |
| 230 | } |
| 231 | |
| 232 | private int flowCount() { |
| 233 | return Sets.newHashSet(service.getFlowEntries(DID)).size(); |
| 234 | } |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 235 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 236 | @Test |
David Glantz | 0a5779c | 2021-09-22 14:34:14 -0500 | [diff] [blame^] | 237 | public void getFlowEntry() { |
| 238 | assertTrue("store should be empty", |
| 239 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
| 240 | FlowRule f1 = addFlowRule(1); |
| 241 | FlowRule f2 = addFlowRule(2); |
| 242 | |
| 243 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 244 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 245 | assertEquals("2 rules should exist", 2, flowCount()); |
| 246 | |
| 247 | FlowEntry actual1 = service.getFlowEntry(f1); |
| 248 | FlowEntry actual2 = service.getFlowEntry(f2); |
| 249 | |
| 250 | assertEquals(fe1, actual1); |
| 251 | assertEquals(fe2, actual2); |
| 252 | } |
| 253 | |
| 254 | @Test |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 255 | public void getFlowEntries() { |
| 256 | assertTrue("store should be empty", |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 257 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 258 | FlowRule f1 = addFlowRule(1); |
| 259 | FlowRule f2 = addFlowRule(2); |
| 260 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 261 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 262 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 263 | assertEquals("2 rules should exist", 2, flowCount()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 264 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 265 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2)); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 266 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 267 | RULE_ADDED, RULE_ADDED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 268 | |
| 269 | addFlowRule(1); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 270 | System.err.println("events :" + listener.events); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 271 | assertEquals("should still be 2 rules", 2, flowCount()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 272 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 273 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1)); |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 274 | validateEvents(RULE_UPDATED, RULE_UPDATED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 277 | private boolean validateState(Map<FlowRule, FlowEntryState> expected) { |
| 278 | Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 279 | Iterable<FlowEntry> rules = service.getFlowEntries(DID); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 280 | for (FlowEntry f : rules) { |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 281 | assertTrue("Unexpected FlowRule " + f, expectedToCheck.containsKey(f)); |
| 282 | assertEquals("FlowEntry" + f, expectedToCheck.get(f), f.state()); |
| 283 | expectedToCheck.remove(f); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 284 | } |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 285 | assertEquals(Collections.emptySet(), expectedToCheck.entrySet()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 289 | @Test |
| 290 | public void applyFlowRules() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 291 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 292 | FlowRule r1 = flowRule(1, 1); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 293 | FlowRule r2 = flowRule(2, 2); |
| 294 | FlowRule r3 = flowRule(3, 3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 295 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 296 | assertTrue("store should be empty", |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 297 | Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 298 | mgr.applyFlowRules(r1, r2, r3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 299 | assertEquals("3 rules should exist", 3, flowCount()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 300 | assertTrue("Entries should be pending add.", |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 301 | validateState(ImmutableMap.of( |
| 302 | r1, FlowEntryState.PENDING_ADD, |
| 303 | r2, FlowEntryState.PENDING_ADD, |
| 304 | r3, FlowEntryState.PENDING_ADD))); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Test |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 308 | public void purgeFlowRules() { |
| 309 | FlowRule f1 = addFlowRule(1); |
| 310 | FlowRule f2 = addFlowRule(2); |
| 311 | FlowRule f3 = addFlowRule(3); |
| 312 | assertEquals("3 rules should exist", 3, flowCount()); |
| 313 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 314 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 315 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 316 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3)); |
| 317 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 318 | RULE_ADDED, RULE_ADDED, RULE_ADDED); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 319 | mgr.purgeFlowRules(DID); |
| 320 | assertEquals("0 rule should exist", 0, flowCount()); |
| 321 | } |
| 322 | |
| 323 | @Test |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 324 | public void removeFlowRules() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 325 | FlowRule f1 = addFlowRule(1); |
| 326 | FlowRule f2 = addFlowRule(2); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 327 | FlowRule f3 = addFlowRule(3); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 328 | assertEquals("3 rules should exist", 3, flowCount()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 329 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 330 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 331 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 332 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
| 333 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2, fe3)); |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 334 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 335 | RULE_ADDED, RULE_ADDED, RULE_ADDED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 336 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 337 | mgr.removeFlowRules(f1, f2); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 338 | //removing from north, so no events generated |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 339 | validateEvents(RULE_REMOVE_REQUESTED, RULE_REMOVE_REQUESTED); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 340 | assertEquals("3 rule should exist", 3, flowCount()); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 341 | assertTrue("Entries should be pending remove.", |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 342 | validateState(ImmutableMap.of( |
| 343 | f1, FlowEntryState.PENDING_REMOVE, |
| 344 | f2, FlowEntryState.PENDING_REMOVE, |
| 345 | f3, FlowEntryState.ADDED))); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 346 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 347 | mgr.removeFlowRules(f1); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 348 | assertEquals("3 rule should still exist", 3, flowCount()); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Test |
| 352 | public void flowRemoved() { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 353 | FlowRule f1 = addFlowRule(1); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 354 | FlowRule f2 = addFlowRule(2); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 355 | StoredFlowEntry fe1 = new DefaultFlowEntry(f1); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 356 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 357 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 358 | providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 359 | service.removeFlowRules(f1); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 360 | |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 361 | //FIXME modification of "stored" flow entry outside of store |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 362 | fe1.setState(FlowEntryState.REMOVED); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 363 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 364 | providerService.flowRemoved(fe1); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 365 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 366 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 367 | RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 368 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 369 | providerService.flowRemoved(fe1); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 370 | validateEvents(); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 371 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 372 | FlowRule f3 = flowRule(3, 3); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 373 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 374 | service.applyFlowRules(f3); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 375 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 376 | providerService.pushFlowMetrics(DID, Collections.singletonList(fe3)); |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 377 | validateEvents(RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 378 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 379 | providerService.flowRemoved(fe3); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 380 | validateEvents(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 381 | } |
| 382 | |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 383 | @Test |
| 384 | public void flowMetrics() { |
| 385 | FlowRule f1 = flowRule(1, 1); |
| 386 | FlowRule f2 = flowRule(2, 2); |
| 387 | FlowRule f3 = flowRule(3, 3); |
| 388 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 389 | mgr.applyFlowRules(f1, f2, f3); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 390 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 391 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 392 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 393 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 394 | //FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 395 | //FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 396 | |
| 397 | providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 398 | |
| 399 | assertTrue("Entries should be added.", |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 400 | validateState(ImmutableMap.of( |
| 401 | f1, FlowEntryState.ADDED, |
| 402 | f2, FlowEntryState.ADDED, |
| 403 | f3, FlowEntryState.PENDING_ADD))); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 404 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 405 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 406 | RULE_ADDED, RULE_ADDED, RULE_ADD_REQUESTED); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Test |
| 410 | public void extraneousFlow() { |
| 411 | FlowRule f1 = flowRule(1, 1); |
| 412 | FlowRule f2 = flowRule(2, 2); |
| 413 | FlowRule f3 = flowRule(3, 3); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 414 | mgr.applyFlowRules(f1, f2); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 415 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 416 | // FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 417 | // FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 418 | // FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED); |
| 419 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 420 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
| 421 | FlowEntry fe3 = new DefaultFlowEntry(f3); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 422 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 423 | |
| 424 | providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2, fe3)); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 425 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 426 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 427 | |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Tests whether a rule that was marked for removal but no flowRemoved was received |
| 432 | * is indeed removed at the next stats update. |
| 433 | */ |
| 434 | @Test |
| 435 | public void flowMissingRemove() { |
| 436 | FlowRule f1 = flowRule(1, 1); |
| 437 | FlowRule f2 = flowRule(2, 2); |
| 438 | FlowRule f3 = flowRule(3, 3); |
| 439 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 440 | // FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); |
| 441 | // FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); |
| 442 | |
| 443 | FlowEntry fe1 = new DefaultFlowEntry(f1); |
| 444 | FlowEntry fe2 = new DefaultFlowEntry(f2); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 445 | mgr.applyFlowRules(f1, f2, f3); |
| 446 | |
| 447 | mgr.removeFlowRules(f3); |
| 448 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 449 | providerService.pushFlowMetrics(DID, Lists.newArrayList(fe1, fe2)); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 450 | |
alshabib | 3d643ec | 2014-10-22 18:33:00 -0700 | [diff] [blame] | 451 | validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, |
| 452 | RULE_REMOVE_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVED); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 453 | |
| 454 | } |
| 455 | |
| 456 | @Test |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 457 | public void removeByAppId() { |
| 458 | FlowRule f1 = flowRule(1, 1); |
| 459 | FlowRule f2 = flowRule(2, 2); |
| 460 | mgr.applyFlowRules(f1, f2); |
| 461 | |
| 462 | |
| 463 | mgr.removeFlowRulesById(appId); |
| 464 | |
| 465 | //only check that we are in pending remove. Events and actual remove state will |
| 466 | // be set by flowRemoved call. |
Yuta HIGUCHI | 605347c | 2014-10-17 21:05:23 -0700 | [diff] [blame] | 467 | validateState(ImmutableMap.of( |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 468 | f1, FlowEntryState.PENDING_REMOVE, |
| 469 | f2, FlowEntryState.PENDING_REMOVE)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 472 | @Test |
| 473 | public void fallbackBasics() { |
| 474 | FlowRule f1 = flowRule(FOO_DID, 1, 1); |
| 475 | flowRules.clear(); |
| 476 | mgr.applyFlowRules(f1); |
| 477 | assertTrue("flow rule not applied", flowRules.contains(f1)); |
| 478 | |
| 479 | flowRules.clear(); |
| 480 | mgr.removeFlowRules(f1); |
| 481 | assertTrue("flow rule not removed", flowRules.contains(f1)); |
| 482 | } |
| 483 | |
| 484 | @Test |
| 485 | public void fallbackFlowRemoved() { |
| 486 | FlowRule f1 = flowRule(FOO_DID, 1, 1); |
| 487 | mgr.applyFlowRules(f1); |
| 488 | flowRules.clear(); |
| 489 | providerService.flowRemoved(new DefaultFlowEntry(f1)); |
| 490 | assertTrue("flow rule not reapplied", flowRules.contains(f1)); |
| 491 | } |
| 492 | |
| 493 | @Test |
| 494 | public void fallbackExtraFlow() { |
| 495 | FlowRule f1 = flowRule(FOO_DID, 1, 1); |
| 496 | flowRules.clear(); |
| 497 | providerService.pushFlowMetrics(FOO_DID, ImmutableList.of(new DefaultFlowEntry(f1))); |
| 498 | assertTrue("flow rule not removed", flowRules.contains(f1)); |
| 499 | } |
| 500 | |
| 501 | @Test |
| 502 | public void fallbackPoll() { |
| 503 | FlowRuleDriverProvider fallback = (FlowRuleDriverProvider) mgr.defaultProvider(); |
| 504 | FlowRule f1 = flowRule(FOO_DID, 1, 1); |
| 505 | mgr.applyFlowRules(f1); |
| 506 | FlowEntry fe = mgr.getFlowEntries(FOO_DID).iterator().next(); |
| 507 | assertEquals("incorrect state", FlowEntryState.PENDING_ADD, fe.state()); |
| 508 | |
| 509 | fallback.init(fallback.providerService, mgr.deviceService, mgr.mastershipService, 1); |
| 510 | TestTools.assertAfter(2000, () -> { |
| 511 | FlowEntry e = mgr.getFlowEntries(FOO_DID).iterator().next(); |
| 512 | assertEquals("incorrect state", FlowEntryState.ADDED, e.state()); |
| 513 | }); |
| 514 | } |
| 515 | |
| 516 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 517 | private static class TestListener implements FlowRuleListener { |
| 518 | final List<FlowRuleEvent> events = new ArrayList<>(); |
| 519 | |
| 520 | @Override |
| 521 | public void event(FlowRuleEvent event) { |
| 522 | events.add(event); |
| 523 | } |
| 524 | } |
| 525 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 526 | private static class TestDeviceService extends DeviceServiceAdapter { |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 527 | @Override |
| 528 | public int getDeviceCount() { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 529 | return 2; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public Iterable<Device> getDevices() { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 534 | return ImmutableList.of(DEV, FOO_DEV); |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public Iterable<Device> getAvailableDevices() { |
| 539 | return getDevices(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public Device getDevice(DeviceId deviceId) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 544 | return deviceId.equals(FOO_DID) ? FOO_DEV : DEV; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 545 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | private class TestProvider extends AbstractProvider implements FlowRuleProvider { |
| 549 | |
| 550 | protected TestProvider(ProviderId id) { |
| 551 | super(PID); |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public void applyFlowRule(FlowRule... flowRules) { |
| 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public void removeFlowRule(FlowRule... flowRules) { |
| 560 | } |
| 561 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 562 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 563 | public void executeBatch(FlowRuleBatchOperation batch) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 564 | // TODO: need to call batchOperationComplete |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 565 | } |
| 566 | |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 567 | private class TestInstallationFuture |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 568 | implements ListenableFuture<CompletedBatchOperation> { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 569 | |
| 570 | @Override |
| 571 | public boolean cancel(boolean mayInterruptIfRunning) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 572 | return false; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public boolean isCancelled() { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 577 | return false; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public boolean isDone() { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 582 | return true; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public CompletedBatchOperation get() |
| 587 | throws InterruptedException, ExecutionException { |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 588 | return new CompletedBatchOperation(true, Collections.emptySet(), null); |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 593 | throws InterruptedException, |
| 594 | ExecutionException, TimeoutException { |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 595 | return new CompletedBatchOperation(true, Collections.emptySet(), null); |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 596 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 597 | |
| 598 | @Override |
| 599 | public void addListener(Runnable task, Executor executor) { |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 600 | if (isDone()) { |
| 601 | executor.execute(task); |
| 602 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 603 | } |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 604 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 605 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | private class TestSelector implements TrafficSelector { |
| 609 | |
| 610 | //for controlling hashcode uniqueness; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 611 | private final int testval; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 612 | |
| 613 | public TestSelector(int val) { |
| 614 | testval = val; |
| 615 | } |
| 616 | |
| 617 | @Override |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 618 | public Set<Criterion> criteria() { |
Hyunsun Moon | 7bfbc1c | 2016-04-05 16:40:43 -0700 | [diff] [blame] | 619 | return Collections.emptySet(); |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 623 | public Criterion getCriterion( |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 624 | org.onosproject.net.flow.criteria.Criterion.Type type) { |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 625 | return null; |
| 626 | } |
| 627 | |
| 628 | @Override |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 629 | public int hashCode() { |
| 630 | return testval; |
| 631 | } |
| 632 | |
| 633 | @Override |
| 634 | public boolean equals(Object o) { |
| 635 | if (o instanceof TestSelector) { |
| 636 | return this.testval == ((TestSelector) o).testval; |
| 637 | } |
| 638 | return false; |
| 639 | } |
Jonathan Hart | 936c49d | 2014-10-23 16:38:59 -0700 | [diff] [blame] | 640 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | private class TestTreatment implements TrafficTreatment { |
| 644 | |
| 645 | //for controlling hashcode uniqueness; |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 646 | private final int testval; |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 647 | |
| 648 | public TestTreatment(int val) { |
| 649 | testval = val; |
| 650 | } |
| 651 | |
| 652 | @Override |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 653 | public List<Instruction> deferred() { |
| 654 | return null; |
| 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public List<Instruction> immediate() { |
| 659 | return null; |
| 660 | } |
| 661 | |
| 662 | @Override |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 663 | public List<Instruction> allInstructions() { |
| 664 | return null; |
| 665 | } |
| 666 | |
| 667 | @Override |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 668 | public Instructions.TableTypeTransition tableTransition() { |
| 669 | return null; |
| 670 | } |
| 671 | |
| 672 | @Override |
Jonathan Hart | 4a0ba56 | 2015-03-23 17:23:33 -0700 | [diff] [blame] | 673 | public boolean clearedDeferred() { |
| 674 | return false; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | @Override |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 678 | public int hashCode() { |
| 679 | return testval; |
| 680 | } |
| 681 | |
| 682 | @Override |
| 683 | public boolean equals(Object o) { |
| 684 | if (o instanceof TestTreatment) { |
| 685 | return this.testval == ((TestTreatment) o).testval; |
| 686 | } |
| 687 | return false; |
| 688 | } |
| 689 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 690 | @Override |
| 691 | public MetadataInstruction writeMetadata() { |
| 692 | return null; |
| 693 | } |
| 694 | |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 695 | @Override |
Cem Türker | 3baff67 | 2017-10-12 15:09:01 +0300 | [diff] [blame] | 696 | public Instructions.StatTriggerInstruction statTrigger() { |
| 697 | return null; |
| 698 | } |
| 699 | |
| 700 | @Override |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 701 | public Instructions.MeterInstruction metered() { |
| 702 | return null; |
| 703 | } |
| 704 | |
cansu.toprak | 409289d | 2017-10-27 10:04:05 +0300 | [diff] [blame] | 705 | @Override |
| 706 | public Set<Instructions.MeterInstruction> meters() { |
| 707 | return Sets.newHashSet(); |
| 708 | } |
| 709 | |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 710 | } |
| 711 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 712 | public class TestApplicationId extends DefaultApplicationId { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 713 | public TestApplicationId(int id, String name) { |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 714 | super(id, name); |
| 715 | } |
| 716 | } |
| 717 | |
Ray Milkey | cc53abd | 2015-02-19 12:31:33 -0800 | [diff] [blame] | 718 | private class TestCoreService extends CoreServiceAdapter { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 719 | |
| 720 | @Override |
| 721 | public IdGenerator getIdGenerator(String topic) { |
| 722 | return new IdGenerator() { |
| 723 | private AtomicLong counter = new AtomicLong(0); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 724 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 725 | @Override |
| 726 | public long getNewId() { |
| 727 | return counter.getAndIncrement(); |
| 728 | } |
| 729 | }; |
| 730 | } |
| 731 | } |
| 732 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 733 | private class TestMastershipService extends MastershipServiceAdapter { |
| 734 | @Override |
| 735 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 736 | return MastershipRole.MASTER; |
| 737 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 738 | |
| 739 | @Override |
| 740 | public NodeId getMasterFor(DeviceId deviceId) { |
| 741 | return new NodeId(NODE_ID); |
| 742 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | private class TestDriverManager extends DriverManager { |
Thomas Vachuska | 11b99fc | 2017-04-27 12:51:04 -0700 | [diff] [blame] | 746 | TestDriverManager(DriverRegistry registry) { |
| 747 | this.registry = registry; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 748 | this.deviceService = mgr.deviceService; |
Carmelo Cascone | 0761cd3 | 2018-08-29 19:22:50 -0700 | [diff] [blame] | 749 | this.pipeconfService = new PiPipeconfServiceAdapter(); |
| 750 | this.networkConfigService = new NetworkConfigServiceAdapter(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 751 | activate(); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | static Collection<FlowRule> flowRules = new HashSet<>(); |
| 756 | |
| 757 | public static class TestFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable { |
| 758 | |
| 759 | @Override |
| 760 | public Collection<FlowEntry> getFlowEntries() { |
| 761 | ImmutableList.Builder<FlowEntry> builder = ImmutableList.builder(); |
| 762 | flowRules.stream().map(DefaultFlowEntry::new).forEach(builder::add); |
| 763 | return builder.build(); |
| 764 | } |
| 765 | |
| 766 | @Override |
| 767 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
| 768 | flowRules.addAll(rules); |
| 769 | return rules; |
| 770 | } |
| 771 | |
| 772 | @Override |
| 773 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
| 774 | flowRules.addAll(rules); |
| 775 | return rules; |
| 776 | } |
| 777 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 778 | |
| 779 | private static class MockControllerNode implements ControllerNode { |
| 780 | final NodeId id; |
| 781 | |
| 782 | public MockControllerNode(NodeId id) { |
| 783 | this.id = id; |
| 784 | } |
| 785 | |
| 786 | @Override |
| 787 | public NodeId id() { |
| 788 | return this.id; |
| 789 | } |
| 790 | |
| 791 | @Override |
| 792 | public Ip4Address ip() { |
| 793 | return Ip4Address.valueOf("127.0.0.1"); |
| 794 | } |
| 795 | |
| 796 | @Override |
| 797 | public IpAddress ip(boolean resolve) { |
| 798 | return null; |
| 799 | } |
| 800 | |
| 801 | @Override |
| 802 | public String host() { |
| 803 | return null; |
| 804 | } |
| 805 | |
| 806 | @Override |
| 807 | public int tcpPort() { |
| 808 | return 0; |
| 809 | } |
| 810 | } |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 811 | } |