alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [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.intent.impl; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
| 22 | import com.google.common.collect.Sets; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 23 | import org.hamcrest.Description; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 24 | import org.hamcrest.TypeSafeMatcher; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 27 | import org.junit.Ignore; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 28 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.TestApplicationId; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.impl.TestCoreManager; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 33 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.NetworkResource; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.FlowRuleIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentCompiler; |
| 38 | import org.onosproject.net.intent.IntentEvent; |
| 39 | import org.onosproject.net.intent.IntentEvent.Type; |
| 40 | import org.onosproject.net.intent.IntentExtensionService; |
| 41 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentListener; |
| 43 | import org.onosproject.net.intent.IntentService; |
| 44 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.Key; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame^] | 46 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
Jonathan Hart | 6a8fd1d | 2015-02-25 15:44:37 -0800 | [diff] [blame] | 47 | import org.onosproject.store.trivial.impl.SimpleIntentStore; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 48 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 49 | import java.util.Collection; |
| 50 | import java.util.Collections; |
| 51 | import java.util.List; |
| 52 | import java.util.Map; |
| 53 | import java.util.Set; |
| 54 | import java.util.concurrent.CountDownLatch; |
| 55 | import java.util.concurrent.TimeUnit; |
| 56 | import java.util.stream.Collectors; |
| 57 | import java.util.stream.IntStream; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 58 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 59 | import static org.hamcrest.MatcherAssert.assertThat; |
| 60 | import static org.hamcrest.Matchers.hasSize; |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 61 | import static org.hamcrest.Matchers.is; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 62 | import static org.junit.Assert.*; |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 63 | import static org.onlab.junit.TestTools.assertAfter; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 64 | import static org.onlab.util.Tools.delay; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 65 | import static org.onosproject.net.intent.IntentState.*; |
Ray Milkey | 43a2822 | 2015-02-23 13:57:58 -0800 | [diff] [blame] | 66 | import static org.onosproject.net.intent.IntentTestsMocks.MockFlowRule; |
| 67 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Test intent manager and transitions. |
| 71 | * |
| 72 | * TODO implement the following tests: |
| 73 | * - {submit, withdraw, update, replace} intent |
Sho SHIMIZU | 3466096 | 2015-01-22 17:58:44 -0800 | [diff] [blame] | 74 | * - {submit, update, recompiling} intent with failed compilation |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 75 | * - failed reservation |
| 76 | * - push timeout recovery |
| 77 | * - failed items recovery |
| 78 | * |
| 79 | * in general, verify intents store, flow store, and work queue |
| 80 | */ |
Ray Milkey | 1c166dc | 2015-03-02 13:32:39 -0800 | [diff] [blame] | 81 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 82 | public class IntentManagerTest { |
| 83 | |
Pavlin Radoslavov | 35b4ecb | 2015-03-03 15:06:04 -0800 | [diff] [blame] | 84 | private static final int SUBMIT_TIMEOUT_MS = 1000; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 85 | private static final ApplicationId APPID = new TestApplicationId("manager-test"); |
| 86 | |
| 87 | private IntentManager manager; |
| 88 | private MockFlowRuleService flowRuleService; |
| 89 | |
| 90 | protected IntentService service; |
| 91 | protected IntentExtensionService extensionService; |
| 92 | protected TestListener listener = new TestListener(); |
| 93 | protected TestIntentCompiler compiler = new TestIntentCompiler(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 94 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 95 | private static class TestListener implements IntentListener { |
| 96 | final Multimap<IntentEvent.Type, IntentEvent> events = HashMultimap.create(); |
| 97 | Map<IntentEvent.Type, CountDownLatch> latchMap = Maps.newHashMap(); |
| 98 | |
| 99 | @Override |
| 100 | public void event(IntentEvent event) { |
| 101 | events.put(event.type(), event); |
| 102 | if (latchMap.containsKey(event.type())) { |
| 103 | latchMap.get(event.type()).countDown(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public int getCounts(IntentEvent.Type type) { |
| 108 | return events.get(type).size(); |
| 109 | } |
| 110 | |
| 111 | public void setLatch(int count, IntentEvent.Type type) { |
| 112 | latchMap.put(type, new CountDownLatch(count)); |
| 113 | } |
| 114 | |
| 115 | public void await(IntentEvent.Type type) { |
| 116 | try { |
| 117 | assertTrue("Timed out waiting for: " + type, |
| 118 | latchMap.get(type).await(5, TimeUnit.SECONDS)); |
| 119 | } catch (InterruptedException e) { |
| 120 | e.printStackTrace(); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private static class TestIntentTracker implements ObjectiveTrackerService { |
| 126 | private TopologyChangeDelegate delegate; |
| 127 | @Override |
| 128 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 129 | this.delegate = delegate; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 134 | if (delegate.equals(this.delegate)) { |
| 135 | this.delegate = null; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 140 | public void addTrackedResources(Key key, Collection<NetworkResource> resources) { |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 141 | //TODO |
| 142 | } |
| 143 | |
| 144 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 145 | public void removeTrackedResources(Key key, Collection<NetworkResource> resources) { |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 146 | //TODO |
| 147 | } |
| 148 | } |
| 149 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 150 | private static class MockInstallableIntent extends FlowRuleIntent { |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 151 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 152 | public MockInstallableIntent() { |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 153 | super(APPID, Collections.singletonList(new MockFlowRule(100))); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | private static class TestIntentCompiler implements IntentCompiler<MockIntent> { |
| 158 | @Override |
| 159 | public List<Intent> compile(MockIntent intent, List<Intent> installable, |
| 160 | Set<LinkResourceAllocations> resources) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 161 | return Lists.newArrayList(new MockInstallableIntent()); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 165 | private static class TestIntentCompilerMultipleFlows implements IntentCompiler<MockIntent> { |
| 166 | @Override |
| 167 | public List<Intent> compile(MockIntent intent, List<Intent> installable, |
| 168 | Set<LinkResourceAllocations> resources) { |
| 169 | |
| 170 | return IntStream.rangeClosed(1, 5) |
| 171 | .mapToObj(mock -> (new MockInstallableIntent())) |
| 172 | .collect(Collectors.toList()); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 177 | private static class TestIntentCompilerError implements IntentCompiler<MockIntent> { |
| 178 | @Override |
| 179 | public List<Intent> compile(MockIntent intent, List<Intent> installable, |
| 180 | Set<LinkResourceAllocations> resources) { |
| 181 | throw new IntentCompilationException("Compilation always fails"); |
| 182 | } |
| 183 | } |
| 184 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 185 | /** |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 186 | * Hamcrest matcher to check that a collection of Intents contains an |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 187 | * Intent with the specified Intent Id. |
| 188 | */ |
| 189 | public static class EntryForIntentMatcher extends TypeSafeMatcher<Collection<Intent>> { |
| 190 | private final IntentId id; |
| 191 | |
| 192 | public EntryForIntentMatcher(IntentId idValue) { |
| 193 | id = idValue; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public boolean matchesSafely(Collection<Intent> intents) { |
| 198 | for (Intent intent : intents) { |
| 199 | if (intent.id().equals(id)) { |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void describeTo(Description description) { |
| 208 | description.appendText("an intent with id \" "). |
| 209 | appendText(id.toString()). |
| 210 | appendText("\""); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private static EntryForIntentMatcher hasIntentWithId(IntentId id) { |
| 215 | return new EntryForIntentMatcher(id); |
| 216 | } |
| 217 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 218 | @Before |
| 219 | public void setUp() { |
| 220 | manager = new IntentManager(); |
| 221 | flowRuleService = new MockFlowRuleService(); |
| 222 | manager.store = new SimpleIntentStore(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 223 | manager.eventDispatcher = new TestEventDispatcher(); |
| 224 | manager.trackerService = new TestIntentTracker(); |
| 225 | manager.flowRuleService = flowRuleService; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 226 | manager.coreService = new TestCoreManager(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 227 | service = manager; |
| 228 | extensionService = manager; |
| 229 | |
| 230 | manager.activate(); |
| 231 | service.addListener(listener); |
| 232 | extensionService.registerCompiler(MockIntent.class, compiler); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 233 | |
| 234 | assertTrue("store should be empty", |
| 235 | Sets.newHashSet(service.getIntents()).isEmpty()); |
| 236 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
| 237 | } |
| 238 | |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 239 | public void verifyState() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 240 | // verify that all intents are parked and the batch operation is unblocked |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 241 | Set<IntentState> parked = Sets.newHashSet(INSTALLED, WITHDRAWN, FAILED, CORRUPT); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 242 | for (Intent i : service.getIntents()) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 243 | IntentState state = service.getIntentState(i.key()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 244 | assertTrue("Intent " + i.id() + " is in invalid state " + state, |
| 245 | parked.contains(state)); |
| 246 | } |
| 247 | //the batch has not yet been removed when we receive the last event |
| 248 | // FIXME: this doesn't guarantee to avoid the race |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 249 | |
| 250 | //FIXME |
| 251 | // for (int tries = 0; tries < 10; tries++) { |
| 252 | // if (manager.batchService.getPendingOperations().isEmpty()) { |
| 253 | // break; |
| 254 | // } |
| 255 | // delay(10); |
| 256 | // } |
| 257 | // assertTrue("There are still pending batch operations.", |
| 258 | // manager.batchService.getPendingOperations().isEmpty()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 259 | |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @After |
| 263 | public void tearDown() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 264 | extensionService.unregisterCompiler(MockIntent.class); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 265 | service.removeListener(listener); |
| 266 | manager.deactivate(); |
| 267 | // TODO null the other refs? |
| 268 | } |
| 269 | |
| 270 | @Test |
| 271 | public void submitIntent() { |
| 272 | flowRuleService.setFuture(true); |
| 273 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 274 | listener.setLatch(1, Type.INSTALL_REQ); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 275 | listener.setLatch(1, Type.INSTALLED); |
| 276 | Intent intent = new MockIntent(MockIntent.nextId()); |
| 277 | service.submit(intent); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 278 | listener.await(Type.INSTALL_REQ); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 279 | listener.await(Type.INSTALLED); |
| 280 | assertEquals(1L, service.getIntentCount()); |
| 281 | assertEquals(1L, flowRuleService.getFlowRuleCount()); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 282 | verifyState(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Test |
| 286 | public void withdrawIntent() { |
| 287 | flowRuleService.setFuture(true); |
| 288 | |
| 289 | listener.setLatch(1, Type.INSTALLED); |
| 290 | Intent intent = new MockIntent(MockIntent.nextId()); |
| 291 | service.submit(intent); |
| 292 | listener.await(Type.INSTALLED); |
| 293 | assertEquals(1L, service.getIntentCount()); |
| 294 | assertEquals(1L, flowRuleService.getFlowRuleCount()); |
| 295 | |
| 296 | listener.setLatch(1, Type.WITHDRAWN); |
| 297 | service.withdraw(intent); |
| 298 | listener.await(Type.WITHDRAWN); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 299 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 300 | verifyState(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Test |
Ray Milkey | 0811bdd | 2015-03-11 10:21:55 -0700 | [diff] [blame] | 304 | @Ignore("This is disabled because we are seeing intermittent failures on Jenkins") |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 305 | public void stressSubmitWithdrawUnique() { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 306 | flowRuleService.setFuture(true); |
| 307 | |
| 308 | int count = 500; |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 309 | Intent[] intents = new Intent[count]; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 310 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 311 | listener.setLatch(count, Type.WITHDRAWN); |
| 312 | |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 313 | for (int i = 0; i < count; i++) { |
| 314 | intents[i] = new MockIntent(MockIntent.nextId()); |
| 315 | service.submit(intents[i]); |
| 316 | } |
| 317 | |
| 318 | for (int i = 0; i < count; i++) { |
| 319 | service.withdraw(intents[i]); |
| 320 | } |
| 321 | |
| 322 | listener.await(Type.WITHDRAWN); |
| 323 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
| 324 | verifyState(); |
| 325 | } |
| 326 | |
| 327 | @Test |
| 328 | public void stressSubmitWithdrawSame() { |
| 329 | flowRuleService.setFuture(true); |
| 330 | |
| 331 | int count = 50; |
| 332 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 333 | Intent intent = new MockIntent(MockIntent.nextId()); |
| 334 | for (int i = 0; i < count; i++) { |
| 335 | service.submit(intent); |
| 336 | service.withdraw(intent); |
| 337 | } |
| 338 | |
Pavlin Radoslavov | 35b4ecb | 2015-03-03 15:06:04 -0800 | [diff] [blame] | 339 | assertAfter(SUBMIT_TIMEOUT_MS, () -> { |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 340 | assertEquals(1L, service.getIntentCount()); |
| 341 | assertEquals(0L, flowRuleService.getFlowRuleCount()); |
| 342 | }); |
| 343 | verifyState(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 346 | |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 347 | /** |
| 348 | * Tests for proper behavior of installation of an intent that triggers |
| 349 | * a compilation error. |
| 350 | */ |
| 351 | @Test |
| 352 | public void errorIntentCompile() { |
| 353 | final TestIntentCompilerError errorCompiler = new TestIntentCompilerError(); |
| 354 | extensionService.registerCompiler(MockIntent.class, errorCompiler); |
| 355 | MockIntent intent = new MockIntent(MockIntent.nextId()); |
| 356 | listener.setLatch(1, Type.INSTALL_REQ); |
| 357 | listener.setLatch(1, Type.FAILED); |
| 358 | service.submit(intent); |
| 359 | listener.await(Type.INSTALL_REQ); |
| 360 | listener.await(Type.FAILED); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 361 | verifyState(); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Tests handling a future that contains an error as a result of |
| 366 | * installing an intent. |
| 367 | */ |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 368 | @Ignore("skipping until we fix update ordering problem") |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 369 | @Test |
| 370 | public void errorIntentInstallFromFlows() { |
| 371 | final Long id = MockIntent.nextId(); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 372 | flowRuleService.setFuture(false); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 373 | MockIntent intent = new MockIntent(id); |
| 374 | listener.setLatch(1, Type.FAILED); |
| 375 | listener.setLatch(1, Type.INSTALL_REQ); |
| 376 | service.submit(intent); |
| 377 | listener.await(Type.INSTALL_REQ); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 378 | listener.await(Type.FAILED); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 379 | // FIXME the intent will be moved into INSTALLED immediately which overrides FAILED |
| 380 | // ... the updates come out of order |
| 381 | verifyState(); |
Ray Milkey | 93508c2 | 2014-12-02 11:35:56 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 385 | * Tests handling a future that contains an unresolvable error as a result of |
| 386 | * installing an intent. |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 387 | */ |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 388 | @Test |
| 389 | public void errorIntentInstallNeverTrue() { |
| 390 | final Long id = MockIntent.nextId(); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 391 | flowRuleService.setFuture(false); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 392 | MockIntent intent = new MockIntent(id); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 393 | listener.setLatch(1, Type.CORRUPT); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 394 | listener.setLatch(1, Type.INSTALL_REQ); |
| 395 | service.submit(intent); |
| 396 | listener.await(Type.INSTALL_REQ); |
| 397 | // The delay here forces the retry loop in the intent manager to time out |
| 398 | delay(100); |
Brian O'Connor | 5811ac2 | 2015-02-09 19:17:07 -0800 | [diff] [blame] | 399 | flowRuleService.setFuture(false); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 400 | service.withdraw(intent); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 401 | listener.await(Type.CORRUPT); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 402 | verifyState(); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 403 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 404 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 405 | /** |
| 406 | * Tests that a compiler for a subclass of an intent that already has a |
| 407 | * compiler is automatically added. |
| 408 | */ |
| 409 | @Test |
| 410 | public void intentSubclassCompile() { |
| 411 | class MockIntentSubclass extends MockIntent { |
| 412 | public MockIntentSubclass(Long number) { |
| 413 | super(number); |
| 414 | } |
| 415 | } |
| 416 | flowRuleService.setFuture(true); |
| 417 | |
| 418 | listener.setLatch(1, Type.INSTALL_REQ); |
| 419 | listener.setLatch(1, Type.INSTALLED); |
| 420 | Intent intent = new MockIntentSubclass(MockIntent.nextId()); |
| 421 | service.submit(intent); |
| 422 | listener.await(Type.INSTALL_REQ); |
| 423 | listener.await(Type.INSTALLED); |
| 424 | assertEquals(1L, service.getIntentCount()); |
| 425 | assertEquals(1L, flowRuleService.getFlowRuleCount()); |
| 426 | |
| 427 | final Map<Class<? extends Intent>, IntentCompiler<? extends Intent>> compilers = |
| 428 | extensionService.getCompilers(); |
| 429 | assertEquals(2, compilers.size()); |
| 430 | assertNotNull(compilers.get(MockIntentSubclass.class)); |
| 431 | assertNotNull(compilers.get(MockIntent.class)); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 432 | verifyState(); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Tests an intent with no compiler. |
| 437 | */ |
| 438 | @Test |
| 439 | public void intentWithoutCompiler() { |
| 440 | class IntentNoCompiler extends Intent { |
| 441 | IntentNoCompiler() { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 442 | super(APPID, null, Collections.emptyList(), |
| 443 | Intent.DEFAULT_INTENT_PRIORITY); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 444 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 447 | Intent intent = new IntentNoCompiler(); |
| 448 | listener.setLatch(1, Type.INSTALL_REQ); |
| 449 | listener.setLatch(1, Type.FAILED); |
| 450 | service.submit(intent); |
| 451 | listener.await(Type.INSTALL_REQ); |
| 452 | listener.await(Type.FAILED); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 453 | verifyState(); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 454 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 455 | |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 456 | /** |
| 457 | * Tests an intent with no installer. |
| 458 | */ |
| 459 | @Test |
| 460 | public void intentWithoutInstaller() { |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 461 | MockIntent intent = new MockIntent(MockIntent.nextId()); |
| 462 | listener.setLatch(1, Type.INSTALL_REQ); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 463 | listener.setLatch(1, Type.CORRUPT); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 464 | service.submit(intent); |
| 465 | listener.await(Type.INSTALL_REQ); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 466 | listener.await(Type.CORRUPT); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 467 | verifyState(); |
Ray Milkey | e9a3e22 | 2014-12-03 16:46:06 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Tests that the intent fetching methods are correct. |
| 472 | */ |
| 473 | @Test |
| 474 | public void testIntentFetching() { |
| 475 | List<Intent> intents; |
| 476 | |
| 477 | flowRuleService.setFuture(true); |
| 478 | |
| 479 | intents = Lists.newArrayList(service.getIntents()); |
| 480 | assertThat(intents, hasSize(0)); |
| 481 | |
| 482 | final MockIntent intent1 = new MockIntent(MockIntent.nextId()); |
| 483 | final MockIntent intent2 = new MockIntent(MockIntent.nextId()); |
| 484 | |
| 485 | listener.setLatch(2, Type.INSTALL_REQ); |
| 486 | listener.setLatch(2, Type.INSTALLED); |
| 487 | service.submit(intent1); |
| 488 | service.submit(intent2); |
| 489 | listener.await(Type.INSTALL_REQ); |
| 490 | listener.await(Type.INSTALL_REQ); |
| 491 | listener.await(Type.INSTALLED); |
| 492 | listener.await(Type.INSTALLED); |
| 493 | |
| 494 | intents = Lists.newArrayList(service.getIntents()); |
| 495 | assertThat(intents, hasSize(2)); |
| 496 | |
| 497 | assertThat(intents, hasIntentWithId(intent1.id())); |
| 498 | assertThat(intents, hasIntentWithId(intent2.id())); |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 499 | verifyState(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 500 | } |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * Tests that removing all intents results in no flows remaining. |
| 504 | */ |
| 505 | @Test |
| 506 | public void testFlowRemoval() { |
| 507 | List<Intent> intents; |
| 508 | |
| 509 | flowRuleService.setFuture(true); |
| 510 | |
| 511 | intents = Lists.newArrayList(service.getIntents()); |
| 512 | assertThat(intents, hasSize(0)); |
| 513 | |
| 514 | final MockIntent intent1 = new MockIntent(MockIntent.nextId()); |
| 515 | final MockIntent intent2 = new MockIntent(MockIntent.nextId()); |
| 516 | |
| 517 | listener.setLatch(1, Type.INSTALL_REQ); |
| 518 | listener.setLatch(1, Type.INSTALLED); |
| 519 | |
| 520 | service.submit(intent1); |
| 521 | listener.await(Type.INSTALL_REQ); |
| 522 | listener.await(Type.INSTALLED); |
| 523 | |
| 524 | |
| 525 | listener.setLatch(1, Type.INSTALL_REQ); |
| 526 | listener.setLatch(1, Type.INSTALLED); |
| 527 | |
| 528 | service.submit(intent2); |
| 529 | listener.await(Type.INSTALL_REQ); |
| 530 | listener.await(Type.INSTALLED); |
| 531 | |
| 532 | assertThat(listener.getCounts(Type.INSTALLED), is(2)); |
| 533 | assertThat(flowRuleService.getFlowRuleCount(), is(2)); |
| 534 | |
| 535 | listener.setLatch(1, Type.WITHDRAWN); |
| 536 | service.withdraw(intent1); |
| 537 | listener.await(Type.WITHDRAWN); |
| 538 | |
| 539 | listener.setLatch(1, Type.WITHDRAWN); |
| 540 | service.withdraw(intent2); |
| 541 | listener.await(Type.WITHDRAWN); |
| 542 | |
| 543 | assertThat(listener.getCounts(Type.WITHDRAWN), is(2)); |
| 544 | assertThat(flowRuleService.getFlowRuleCount(), is(0)); |
| 545 | } |
| 546 | |
| 547 | /** |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 548 | * Test failure to install an intent, then succeed on retry via IntentCleanup. |
| 549 | */ |
| 550 | @Test |
| 551 | public void testCorruptCleanup() { |
| 552 | IntentCleanup cleanup = new IntentCleanup(); |
| 553 | cleanup.service = manager; |
| 554 | cleanup.store = manager.store; |
| 555 | cleanup.cfgService = new ComponentConfigAdapter(); |
| 556 | |
| 557 | try { |
| 558 | cleanup.activate(); |
| 559 | |
| 560 | final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows(); |
| 561 | extensionService.registerCompiler(MockIntent.class, errorCompiler); |
| 562 | List<Intent> intents; |
| 563 | |
| 564 | flowRuleService.setFuture(false); |
| 565 | |
| 566 | intents = Lists.newArrayList(service.getIntents()); |
| 567 | assertThat(intents, hasSize(0)); |
| 568 | |
| 569 | final MockIntent intent1 = new MockIntent(MockIntent.nextId()); |
| 570 | |
| 571 | listener.setLatch(1, Type.INSTALL_REQ); |
| 572 | listener.setLatch(1, Type.CORRUPT); |
| 573 | listener.setLatch(1, Type.INSTALLED); |
| 574 | |
| 575 | service.submit(intent1); |
| 576 | |
| 577 | listener.await(Type.INSTALL_REQ); |
| 578 | listener.await(Type.CORRUPT); |
| 579 | |
| 580 | flowRuleService.setFuture(true); |
| 581 | |
| 582 | listener.await(Type.INSTALLED); |
| 583 | |
| 584 | assertThat(listener.getCounts(Type.CORRUPT), is(1)); |
| 585 | assertThat(listener.getCounts(Type.INSTALLED), is(1)); |
| 586 | assertEquals(INSTALLED, manager.getIntentState(intent1.key())); |
| 587 | assertThat(flowRuleService.getFlowRuleCount(), is(5)); |
| 588 | } finally { |
| 589 | cleanup.deactivate(); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /** |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 594 | * Test failure to install an intent, and verify retries. |
| 595 | */ |
| 596 | @Test |
| 597 | public void testCorruptRetry() { |
| 598 | IntentCleanup cleanup = new IntentCleanup(); |
| 599 | cleanup.service = manager; |
| 600 | cleanup.store = manager.store; |
| 601 | cleanup.cfgService = new ComponentConfigAdapter(); |
| 602 | cleanup.period = 1_000_000; |
| 603 | cleanup.retryThreshold = 3; |
| 604 | |
| 605 | try { |
| 606 | cleanup.activate(); |
| 607 | |
| 608 | final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows(); |
| 609 | extensionService.registerCompiler(MockIntent.class, errorCompiler); |
| 610 | List<Intent> intents; |
| 611 | |
| 612 | flowRuleService.setFuture(false); |
| 613 | |
| 614 | intents = Lists.newArrayList(service.getIntents()); |
| 615 | assertThat(intents, hasSize(0)); |
| 616 | |
| 617 | final MockIntent intent1 = new MockIntent(MockIntent.nextId()); |
| 618 | |
| 619 | listener.setLatch(1, Type.INSTALL_REQ); |
| 620 | listener.setLatch(cleanup.retryThreshold, Type.CORRUPT); |
| 621 | listener.setLatch(1, Type.INSTALLED); |
| 622 | |
| 623 | service.submit(intent1); |
| 624 | |
| 625 | listener.await(Type.INSTALL_REQ); |
| 626 | listener.await(Type.CORRUPT); |
| 627 | assertEquals(CORRUPT, manager.getIntentState(intent1.key())); |
| 628 | assertThat(listener.getCounts(Type.CORRUPT), is(cleanup.retryThreshold)); |
| 629 | |
| 630 | } finally { |
| 631 | cleanup.deactivate(); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /** |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 636 | * Tests that an intent that fails installation results in no flows remaining. |
| 637 | */ |
| 638 | @Test |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 639 | @Ignore("MockFlowRule numbering issue") //test works if run independently |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 640 | public void testFlowRemovalInstallError() { |
| 641 | final TestIntentCompilerMultipleFlows errorCompiler = new TestIntentCompilerMultipleFlows(); |
| 642 | extensionService.registerCompiler(MockIntent.class, errorCompiler); |
| 643 | List<Intent> intents; |
| 644 | |
| 645 | flowRuleService.setFuture(true); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 646 | //FIXME relying on "3" is brittle |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 647 | flowRuleService.setErrorFlow(3); |
| 648 | |
| 649 | intents = Lists.newArrayList(service.getIntents()); |
| 650 | assertThat(intents, hasSize(0)); |
| 651 | |
| 652 | final MockIntent intent1 = new MockIntent(MockIntent.nextId()); |
| 653 | |
| 654 | listener.setLatch(1, Type.INSTALL_REQ); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 655 | listener.setLatch(1, Type.CORRUPT); |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 656 | |
| 657 | service.submit(intent1); |
| 658 | listener.await(Type.INSTALL_REQ); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 659 | listener.await(Type.CORRUPT); |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 660 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 661 | assertThat(listener.getCounts(Type.CORRUPT), is(1)); |
| 662 | // in this test, there will still be flows abandoned on the data plane |
| 663 | //assertThat(flowRuleService.getFlowRuleCount(), is(0)); |
Ray Milkey | 77a455f | 2015-03-27 10:08:17 -0700 | [diff] [blame] | 664 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 665 | } |