Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.junit.Assert.fail; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import static org.onosproject.net.intent.IntentEvent.Type.FAILED; |
| 23 | import static org.onosproject.net.intent.IntentEvent.Type.INSTALLED; |
| 24 | import static org.onosproject.net.intent.IntentEvent.Type.INSTALL_REQ; |
| 25 | import static org.onosproject.net.intent.IntentEvent.Type.WITHDRAWN; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Arrays; |
| 29 | import java.util.Collections; |
| 30 | import java.util.Iterator; |
| 31 | import java.util.List; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 32 | import java.util.Set; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 33 | |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.core.IdGenerator; |
| 38 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 39 | import org.onosproject.net.resource.LinkResourceAllocations; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 40 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Suite of tests for the intent service contract. |
| 43 | */ |
| 44 | public class IntentServiceTest { |
| 45 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 46 | public static final int IID = 123; |
| 47 | public static final int INSTALLABLE_IID = 234; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 48 | |
| 49 | protected static final int GRACE_MS = 500; // millis |
| 50 | |
| 51 | protected TestableIntentService service; |
| 52 | protected TestListener listener = new TestListener(); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 53 | protected IdGenerator idGenerator = new MockIdGenerator(); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 54 | |
| 55 | @Before |
| 56 | public void setUp() { |
| 57 | service = createIntentService(); |
| 58 | service.addListener(listener); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 59 | Intent.bindIdGenerator(idGenerator); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() { |
| 64 | service.removeListener(listener); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 65 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a service instance appropriately instrumented for testing. |
| 70 | * |
| 71 | * @return testable intent service |
| 72 | */ |
| 73 | protected TestableIntentService createIntentService() { |
| 74 | return new FakeIntentManager(); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void basics() { |
| 79 | // Make sure there are no intents |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 80 | assertEquals("incorrect intent count", 0, service.getIntentCount()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 81 | |
| 82 | // Register a compiler and an installer both setup for success. |
| 83 | service.registerCompiler(TestIntent.class, new TestCompiler(new TestInstallableIntent(INSTALLABLE_IID))); |
| 84 | service.registerInstaller(TestInstallableIntent.class, new TestInstaller(false)); |
| 85 | |
| 86 | final Intent intent = new TestIntent(IID); |
| 87 | service.submit(intent); |
| 88 | |
| 89 | // Allow a small window of time until the intent is in the expected state |
| 90 | TestTools.assertAfter(GRACE_MS, new Runnable() { |
| 91 | @Override |
| 92 | public void run() { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 93 | assertEquals("incorrect intent state", IntentState.INSTALLED, |
| 94 | service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 95 | } |
| 96 | }); |
| 97 | |
| 98 | // Make sure that all expected events have been emitted |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 99 | validateEvents(intent, INSTALL_REQ, INSTALLED); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 100 | |
| 101 | // Make sure there is just one intent (and is ours) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 102 | assertEquals("incorrect intent count", 1, service.getIntentCount()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 103 | |
| 104 | // Reset the listener events |
| 105 | listener.events.clear(); |
| 106 | |
| 107 | // Now withdraw the intent |
| 108 | service.withdraw(intent); |
| 109 | |
| 110 | // Allow a small window of time until the event is in the expected state |
| 111 | TestTools.assertAfter(GRACE_MS, new Runnable() { |
| 112 | @Override |
| 113 | public void run() { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 114 | assertEquals("incorrect intent state", IntentState.WITHDRAWN, |
| 115 | service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 116 | } |
| 117 | }); |
| 118 | |
| 119 | // Make sure that all expected events have been emitted |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 120 | validateEvents(intent, WITHDRAWN); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 121 | |
| 122 | // TODO: discuss what is the fate of intents after they have been withdrawn |
| 123 | // Make sure that the intent is no longer in the system |
| 124 | // assertEquals("incorrect intent count", 0, service.getIntents().size()); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 125 | // assertNull("intent should not be found", service.getIntent(intent.id())); |
| 126 | // assertNull("intent state should not be found", service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void failedCompilation() { |
| 131 | // Register a compiler programmed for success |
| 132 | service.registerCompiler(TestIntent.class, new TestCompiler(true)); |
| 133 | |
| 134 | // Submit an intent |
| 135 | final Intent intent = new TestIntent(IID); |
| 136 | service.submit(intent); |
| 137 | |
| 138 | // Allow a small window of time until the intent is in the expected state |
| 139 | TestTools.assertAfter(GRACE_MS, new Runnable() { |
| 140 | @Override |
| 141 | public void run() { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 142 | assertEquals("incorrect intent state", IntentState.FAILED, |
| 143 | service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 144 | } |
| 145 | }); |
| 146 | |
| 147 | // Make sure that all expected events have been emitted |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 148 | validateEvents(intent, INSTALL_REQ, FAILED); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void failedInstallation() { |
| 153 | // Register a compiler programmed for success and installer for failure |
| 154 | service.registerCompiler(TestIntent.class, new TestCompiler(new TestInstallableIntent(INSTALLABLE_IID))); |
| 155 | service.registerInstaller(TestInstallableIntent.class, new TestInstaller(true)); |
| 156 | |
| 157 | // Submit an intent |
| 158 | final Intent intent = new TestIntent(IID); |
| 159 | service.submit(intent); |
| 160 | |
| 161 | // Allow a small window of time until the intent is in the expected state |
| 162 | TestTools.assertAfter(GRACE_MS, new Runnable() { |
| 163 | @Override |
| 164 | public void run() { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 165 | assertEquals("incorrect intent state", IntentState.FAILED, |
| 166 | service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 167 | } |
| 168 | }); |
| 169 | |
| 170 | // Make sure that all expected events have been emitted |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 171 | validateEvents(intent, INSTALL_REQ, FAILED); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Validates that the test event listener has received the following events |
| 176 | * for the specified intent. Events received for other intents will not be |
| 177 | * considered. |
| 178 | * |
| 179 | * @param intent intent subject |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 180 | * @param types list of event types for which events are expected |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 181 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 182 | protected void validateEvents(Intent intent, IntentEvent.Type... types) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 183 | Iterator<IntentEvent> events = listener.events.iterator(); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 184 | for (IntentEvent.Type type : types) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 185 | IntentEvent event = events.hasNext() ? events.next() : null; |
| 186 | if (event == null) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 187 | fail("expected event not found: " + type); |
| 188 | } else if (intent.equals(event.subject())) { |
| 189 | assertEquals("incorrect state", type, event.type()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | // Remainder of events should not apply to this intent; make sure. |
| 194 | while (events.hasNext()) { |
| 195 | assertFalse("unexpected event for intent", |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 196 | intent.equals(events.next().subject())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void compilerBasics() { |
| 202 | // Make sure there are no compilers |
| 203 | assertEquals("incorrect compiler count", 0, service.getCompilers().size()); |
| 204 | |
| 205 | // Add a compiler and make sure that it appears in the map |
| 206 | IntentCompiler<TestIntent> compiler = new TestCompiler(false); |
| 207 | service.registerCompiler(TestIntent.class, compiler); |
| 208 | assertEquals("incorrect compiler", compiler, |
| 209 | service.getCompilers().get(TestIntent.class)); |
| 210 | |
| 211 | // Remove the same and make sure that it no longer appears in the map |
| 212 | service.unregisterCompiler(TestIntent.class); |
| 213 | assertNull("compiler should not be registered", |
| 214 | service.getCompilers().get(TestIntent.class)); |
| 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void installerBasics() { |
| 219 | // Make sure there are no installers |
| 220 | assertEquals("incorrect installer count", 0, service.getInstallers().size()); |
| 221 | |
| 222 | // Add an installer and make sure that it appears in the map |
| 223 | IntentInstaller<TestInstallableIntent> installer = new TestInstaller(false); |
| 224 | service.registerInstaller(TestInstallableIntent.class, installer); |
| 225 | assertEquals("incorrect installer", installer, |
| 226 | service.getInstallers().get(TestInstallableIntent.class)); |
| 227 | |
| 228 | // Remove the same and make sure that it no longer appears in the map |
| 229 | service.unregisterInstaller(TestInstallableIntent.class); |
| 230 | assertNull("installer should not be registered", |
| 231 | service.getInstallers().get(TestInstallableIntent.class)); |
| 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void implicitRegistration() { |
| 236 | // Add a compiler and make sure that it appears in the map |
| 237 | IntentCompiler<TestIntent> compiler = new TestCompiler(new TestSubclassInstallableIntent(INSTALLABLE_IID)); |
| 238 | service.registerCompiler(TestIntent.class, compiler); |
| 239 | assertEquals("incorrect compiler", compiler, |
| 240 | service.getCompilers().get(TestIntent.class)); |
| 241 | |
| 242 | // Add a installer and make sure that it appears in the map |
| 243 | IntentInstaller<TestInstallableIntent> installer = new TestInstaller(false); |
| 244 | service.registerInstaller(TestInstallableIntent.class, installer); |
| 245 | assertEquals("incorrect installer", installer, |
| 246 | service.getInstallers().get(TestInstallableIntent.class)); |
| 247 | |
| 248 | |
| 249 | // Submit an intent which is a subclass of the one we registered |
| 250 | final Intent intent = new TestSubclassIntent(IID); |
| 251 | service.submit(intent); |
| 252 | |
| 253 | // Allow some time for the intent to be compiled and installed |
| 254 | TestTools.assertAfter(GRACE_MS, new Runnable() { |
| 255 | @Override |
| 256 | public void run() { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 257 | assertEquals("incorrect intent state", IntentState.INSTALLED, |
| 258 | service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 259 | } |
| 260 | }); |
| 261 | |
| 262 | // Make sure that now we have an implicit registration of the compiler |
| 263 | // under the intent subclass |
| 264 | assertEquals("incorrect compiler", compiler, |
| 265 | service.getCompilers().get(TestSubclassIntent.class)); |
| 266 | |
| 267 | // Make sure that now we have an implicit registration of the installer |
| 268 | // under the intent subclass |
| 269 | assertEquals("incorrect installer", installer, |
| 270 | service.getInstallers().get(TestSubclassInstallableIntent.class)); |
| 271 | |
| 272 | // TODO: discuss whether or if implicit registration should require implicit unregistration |
| 273 | // perhaps unregister by compiler or installer itself, rather than by class would be better |
| 274 | } |
| 275 | |
| 276 | |
| 277 | // Fixture to track emitted intent events |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 278 | protected class TestListener implements IntentListener { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 279 | final List<IntentEvent> events = new ArrayList<>(); |
| 280 | |
| 281 | @Override |
| 282 | public void event(IntentEvent event) { |
| 283 | events.add(event); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // Controllable compiler |
| 288 | private class TestCompiler implements IntentCompiler<TestIntent> { |
| 289 | private final boolean fail; |
| 290 | private final List<Intent> result; |
| 291 | |
| 292 | TestCompiler(boolean fail) { |
| 293 | this.fail = fail; |
| 294 | this.result = Collections.emptyList(); |
| 295 | } |
| 296 | |
| 297 | TestCompiler(Intent... result) { |
| 298 | this.fail = false; |
| 299 | this.result = Arrays.asList(result); |
| 300 | } |
| 301 | |
| 302 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 303 | public List<Intent> compile(TestIntent intent, List<Intent> installable, |
| 304 | Set<LinkResourceAllocations> resources) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 305 | if (fail) { |
| 306 | throw new IntentException("compile failed by design"); |
| 307 | } |
| 308 | List<Intent> compiled = new ArrayList<>(result); |
| 309 | return compiled; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Controllable installer |
| 314 | private class TestInstaller implements IntentInstaller<TestInstallableIntent> { |
| 315 | private final boolean fail; |
| 316 | |
| 317 | TestInstaller(boolean fail) { |
| 318 | this.fail = fail; |
| 319 | } |
| 320 | |
| 321 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 322 | public List<FlowRuleBatchOperation> install(TestInstallableIntent intent) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 323 | if (fail) { |
| 324 | throw new IntentException("install failed by design"); |
| 325 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 326 | return null; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 330 | public List<FlowRuleBatchOperation> uninstall(TestInstallableIntent intent) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 331 | if (fail) { |
| 332 | throw new IntentException("remove failed by design"); |
| 333 | } |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 334 | return null; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 335 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 336 | |
| 337 | @Override |
| 338 | public List<FlowRuleBatchOperation> replace(TestInstallableIntent intent, |
| 339 | TestInstallableIntent newIntent) { |
| 340 | return null; |
| 341 | } |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | } |