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