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