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