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