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