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