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 |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 82 | TestTools.assertAfter(GRACE_MS, () -> |
| 83 | assertEquals("incorrect intent state", IntentState.INSTALLED, service.getIntentState(intent.key()))); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 84 | |
| 85 | // Make sure that all expected events have been emitted |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 86 | validateEvents(intent, INSTALL_REQ, INSTALLED); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 87 | |
| 88 | // Make sure there is just one intent (and is ours) |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 89 | assertEquals("incorrect intent count", 1, service.getIntentCount()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 90 | |
| 91 | // Reset the listener events |
| 92 | listener.events.clear(); |
| 93 | |
| 94 | // Now withdraw the intent |
| 95 | service.withdraw(intent); |
| 96 | |
| 97 | // Allow a small window of time until the event is in the expected state |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 98 | TestTools.assertAfter(GRACE_MS, () -> |
| 99 | assertEquals("incorrect intent state", IntentState.WITHDRAWN, service.getIntentState(intent.key()))); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 100 | |
| 101 | // Make sure that all expected events have been emitted |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 102 | validateEvents(intent, WITHDRAWN); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 103 | |
| 104 | // TODO: discuss what is the fate of intents after they have been withdrawn |
| 105 | // Make sure that the intent is no longer in the system |
| 106 | // assertEquals("incorrect intent count", 0, service.getIntents().size()); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 107 | // assertNull("intent should not be found", service.getIntent(intent.id())); |
| 108 | // assertNull("intent state should not be found", service.getIntentState(intent.id())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void failedCompilation() { |
| 113 | // Register a compiler programmed for success |
| 114 | service.registerCompiler(TestIntent.class, new TestCompiler(true)); |
| 115 | |
| 116 | // Submit an intent |
| 117 | final Intent intent = new TestIntent(IID); |
| 118 | service.submit(intent); |
| 119 | |
| 120 | // Allow a small window of time until the intent is in the expected state |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 121 | TestTools.assertAfter(GRACE_MS, () -> |
| 122 | assertEquals("incorrect intent state", IntentState.FAILED, service.getIntentState(intent.key()))); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 123 | |
| 124 | // Make sure that all expected events have been emitted |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 125 | validateEvents(intent, INSTALL_REQ, FAILED); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Validates that the test event listener has received the following events |
| 130 | * for the specified intent. Events received for other intents will not be |
| 131 | * considered. |
| 132 | * |
| 133 | * @param intent intent subject |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 134 | * @param types list of event types for which events are expected |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 135 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 136 | protected void validateEvents(Intent intent, IntentEvent.Type... types) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 137 | Iterator<IntentEvent> events = listener.events.iterator(); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 138 | for (IntentEvent.Type type : types) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 139 | IntentEvent event = events.hasNext() ? events.next() : null; |
| 140 | if (event == null) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 141 | fail("expected event not found: " + type); |
| 142 | } else if (intent.equals(event.subject())) { |
| 143 | assertEquals("incorrect state", type, event.type()); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | // Remainder of events should not apply to this intent; make sure. |
| 148 | while (events.hasNext()) { |
| 149 | assertFalse("unexpected event for intent", |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 150 | intent.equals(events.next().subject())); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void compilerBasics() { |
| 156 | // Make sure there are no compilers |
| 157 | assertEquals("incorrect compiler count", 0, service.getCompilers().size()); |
| 158 | |
| 159 | // Add a compiler and make sure that it appears in the map |
| 160 | IntentCompiler<TestIntent> compiler = new TestCompiler(false); |
| 161 | service.registerCompiler(TestIntent.class, compiler); |
| 162 | assertEquals("incorrect compiler", compiler, |
| 163 | service.getCompilers().get(TestIntent.class)); |
| 164 | |
| 165 | // Remove the same and make sure that it no longer appears in the map |
| 166 | service.unregisterCompiler(TestIntent.class); |
| 167 | assertNull("compiler should not be registered", |
| 168 | service.getCompilers().get(TestIntent.class)); |
| 169 | } |
| 170 | |
| 171 | @Test |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 172 | public void implicitRegistration() { |
| 173 | // Add a compiler and make sure that it appears in the map |
| 174 | IntentCompiler<TestIntent> compiler = new TestCompiler(new TestSubclassInstallableIntent(INSTALLABLE_IID)); |
| 175 | service.registerCompiler(TestIntent.class, compiler); |
| 176 | assertEquals("incorrect compiler", compiler, |
| 177 | service.getCompilers().get(TestIntent.class)); |
| 178 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 179 | // Submit an intent which is a subclass of the one we registered |
| 180 | final Intent intent = new TestSubclassIntent(IID); |
| 181 | service.submit(intent); |
| 182 | |
| 183 | // Allow some time for the intent to be compiled and installed |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 184 | TestTools.assertAfter(GRACE_MS, () -> |
| 185 | assertEquals("incorrect intent state", IntentState.INSTALLED, service.getIntentState(intent.key()))); |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 186 | |
| 187 | // Make sure that now we have an implicit registration of the compiler |
| 188 | // under the intent subclass |
| 189 | assertEquals("incorrect compiler", compiler, |
| 190 | service.getCompilers().get(TestSubclassIntent.class)); |
| 191 | |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 192 | // TODO: discuss whether or if implicit registration should require implicit unregistration |
| 193 | // perhaps unregister by compiler or installer itself, rather than by class would be better |
| 194 | } |
| 195 | |
| 196 | |
| 197 | // Fixture to track emitted intent events |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 198 | protected class TestListener implements IntentListener { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 199 | final List<IntentEvent> events = new ArrayList<>(); |
| 200 | |
| 201 | @Override |
| 202 | public void event(IntentEvent event) { |
| 203 | events.add(event); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Controllable compiler |
| 208 | private class TestCompiler implements IntentCompiler<TestIntent> { |
| 209 | private final boolean fail; |
| 210 | private final List<Intent> result; |
| 211 | |
| 212 | TestCompiler(boolean fail) { |
| 213 | this.fail = fail; |
| 214 | this.result = Collections.emptyList(); |
| 215 | } |
| 216 | |
| 217 | TestCompiler(Intent... result) { |
| 218 | this.fail = false; |
| 219 | this.result = Arrays.asList(result); |
| 220 | } |
| 221 | |
| 222 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 223 | public List<Intent> compile(TestIntent intent, List<Intent> installable, |
| 224 | Set<LinkResourceAllocations> resources) { |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 225 | if (fail) { |
| 226 | throw new IntentException("compile failed by design"); |
| 227 | } |
| 228 | List<Intent> compiled = new ArrayList<>(result); |
| 229 | return compiled; |
| 230 | } |
| 231 | } |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame] | 232 | } |