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