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