Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -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 | */ |
| 16 | package org.onosproject.net.intent.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | import java.util.concurrent.CountDownLatch; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.junit.TestUtils; |
Ray Milkey | 2da272b | 2015-02-03 19:52:08 -0800 | [diff] [blame] | 29 | import org.onlab.junit.TestUtils.TestUtilsException; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 30 | import org.onosproject.core.IdGenerator; |
| 31 | import org.onosproject.event.Event; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 32 | import org.onosproject.net.Device; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 33 | import org.onosproject.net.Link; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 34 | import org.onosproject.net.NetworkResource; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceEvent; |
| 36 | import org.onosproject.net.device.DeviceListener; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.Intent; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.Key; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.MockIdGenerator; |
| 40 | import org.onosproject.net.link.LinkEvent; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame^] | 41 | import org.onosproject.net.resource.link.LinkResourceEvent; |
| 42 | import org.onosproject.net.resource.link.LinkResourceListener; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 43 | import org.onosproject.net.topology.Topology; |
| 44 | import org.onosproject.net.topology.TopologyEvent; |
| 45 | import org.onosproject.net.topology.TopologyListener; |
| 46 | |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 47 | import com.google.common.collect.ImmutableSet; |
| 48 | import com.google.common.collect.Lists; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 49 | |
| 50 | import static org.easymock.EasyMock.createMock; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 51 | import static org.hamcrest.MatcherAssert.assertThat; |
| 52 | import static org.hamcrest.Matchers.equalTo; |
| 53 | import static org.hamcrest.Matchers.hasSize; |
| 54 | import static org.hamcrest.Matchers.is; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 55 | import static org.onosproject.net.NetTestTools.APP_ID; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 56 | import static org.onosproject.net.NetTestTools.device; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 57 | import static org.onosproject.net.NetTestTools.link; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Tests for the objective tracker. |
| 61 | */ |
| 62 | public class ObjectiveTrackerTest { |
| 63 | private static final int WAIT_TIMEOUT_SECONDS = 2; |
| 64 | private Topology topology; |
| 65 | private ObjectiveTracker tracker; |
| 66 | private TestTopologyChangeDelegate delegate; |
| 67 | private List<Event> reasons; |
| 68 | private TopologyListener listener; |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 69 | private DeviceListener deviceListener; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 70 | private LinkResourceListener linkResourceListener; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 71 | private IdGenerator mockGenerator; |
| 72 | |
| 73 | /** |
| 74 | * Initialization shared by all test cases. |
| 75 | * |
| 76 | * @throws TestUtilsException if any filed look ups fail |
| 77 | */ |
| 78 | @Before |
| 79 | public void setUp() throws TestUtilsException { |
| 80 | topology = createMock(Topology.class); |
| 81 | tracker = new ObjectiveTracker(); |
| 82 | delegate = new TestTopologyChangeDelegate(); |
| 83 | tracker.setDelegate(delegate); |
| 84 | reasons = new LinkedList<>(); |
| 85 | listener = TestUtils.getField(tracker, "listener"); |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 86 | deviceListener = TestUtils.getField(tracker, "deviceListener"); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 87 | linkResourceListener = TestUtils.getField(tracker, "linkResourceListener"); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 88 | mockGenerator = new MockIdGenerator(); |
| 89 | Intent.bindIdGenerator(mockGenerator); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Code to clean up shared by all test case. |
| 94 | */ |
| 95 | @After |
| 96 | public void tearDown() { |
| 97 | tracker.unsetDelegate(delegate); |
| 98 | Intent.unbindIdGenerator(mockGenerator); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Topology change delegate mock that tracks the events coming into it |
| 103 | * and saves them. It provides a latch so that tests can wait for events |
| 104 | * to be generated. |
| 105 | */ |
| 106 | static class TestTopologyChangeDelegate implements TopologyChangeDelegate { |
| 107 | |
| 108 | CountDownLatch latch = new CountDownLatch(1); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 109 | List<Key> intentIdsFromEvent; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 110 | boolean compileAllFailedFromEvent; |
| 111 | |
| 112 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 113 | public void triggerCompile(Iterable<Key> intentKeys, |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 114 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 115 | intentIdsFromEvent = Lists.newArrayList(intentKeys); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 116 | compileAllFailedFromEvent = compileAllFailed; |
| 117 | latch.countDown(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 122 | * Tests an event with no associated reasons. |
| 123 | * |
| 124 | * @throws InterruptedException if the latch wait fails. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testEventNoReasons() throws InterruptedException { |
| 128 | final TopologyEvent event = new TopologyEvent( |
| 129 | TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 130 | topology, |
| 131 | null); |
| 132 | |
| 133 | listener.event(event); |
| 134 | assertThat( |
| 135 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 136 | is(true)); |
| 137 | |
| 138 | assertThat(delegate.intentIdsFromEvent, hasSize(0)); |
| 139 | assertThat(delegate.compileAllFailedFromEvent, is(true)); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tests an event for a link down where none of the reasons match |
| 144 | * currently installed intents. |
| 145 | * |
| 146 | * @throws InterruptedException if the latch wait fails. |
| 147 | */ |
| 148 | @Test |
| 149 | public void testEventLinkDownNoMatches() throws InterruptedException { |
| 150 | final Link link = link("src", 1, "dst", 2); |
| 151 | final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link); |
| 152 | reasons.add(linkEvent); |
| 153 | |
| 154 | final TopologyEvent event = new TopologyEvent( |
| 155 | TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 156 | topology, |
| 157 | reasons); |
| 158 | |
| 159 | listener.event(event); |
| 160 | assertThat( |
| 161 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 162 | is(true)); |
| 163 | |
| 164 | assertThat(delegate.intentIdsFromEvent, hasSize(0)); |
| 165 | assertThat(delegate.compileAllFailedFromEvent, is(false)); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests an event for a link being added. |
| 170 | * |
| 171 | * @throws InterruptedException if the latch wait fails. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testEventLinkAdded() throws InterruptedException { |
| 175 | final Link link = link("src", 1, "dst", 2); |
| 176 | final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_ADDED, link); |
| 177 | reasons.add(linkEvent); |
| 178 | |
| 179 | final TopologyEvent event = new TopologyEvent( |
| 180 | TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 181 | topology, |
| 182 | reasons); |
| 183 | |
| 184 | listener.event(event); |
| 185 | assertThat( |
| 186 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 187 | is(true)); |
| 188 | |
| 189 | assertThat(delegate.intentIdsFromEvent, hasSize(0)); |
| 190 | assertThat(delegate.compileAllFailedFromEvent, is(true)); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Tests an event for a link down where the link matches existing intents. |
| 195 | * |
| 196 | * @throws InterruptedException if the latch wait fails. |
| 197 | */ |
| 198 | @Test |
| 199 | public void testEventLinkDownMatch() throws Exception { |
| 200 | final Link link = link("src", 1, "dst", 2); |
| 201 | final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link); |
| 202 | reasons.add(linkEvent); |
| 203 | |
| 204 | final TopologyEvent event = new TopologyEvent( |
| 205 | TopologyEvent.Type.TOPOLOGY_CHANGED, |
| 206 | topology, |
| 207 | reasons); |
| 208 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 209 | final Key key = Key.of(0x333L, APP_ID); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 210 | Collection<NetworkResource> resources = ImmutableSet.of(link); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 211 | tracker.addTrackedResources(key, resources); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 212 | |
| 213 | listener.event(event); |
| 214 | assertThat( |
| 215 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 216 | is(true)); |
| 217 | |
| 218 | assertThat(delegate.intentIdsFromEvent, hasSize(1)); |
| 219 | assertThat(delegate.compileAllFailedFromEvent, is(false)); |
| 220 | assertThat(delegate.intentIdsFromEvent.get(0).toString(), |
| 221 | equalTo("0x333")); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Tests a resource available event. |
| 226 | * |
| 227 | * @throws InterruptedException if the latch wait fails. |
| 228 | */ |
| 229 | @Test |
| 230 | public void testResourceEvent() throws Exception { |
| 231 | LinkResourceEvent event = new LinkResourceEvent( |
| 232 | LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE, |
| 233 | new HashSet<>()); |
| 234 | linkResourceListener.event(event); |
| 235 | |
| 236 | assertThat( |
| 237 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 238 | is(true)); |
| 239 | |
| 240 | assertThat(delegate.intentIdsFromEvent, hasSize(0)); |
| 241 | assertThat(delegate.compileAllFailedFromEvent, is(true)); |
| 242 | } |
| 243 | |
Brian O'Connor | b5dcc51 | 2015-03-24 17:28:00 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Tests an event for a host becoming available that matches an intent. |
| 246 | * |
| 247 | * @throws InterruptedException if the latch wait fails. |
| 248 | */ |
| 249 | |
| 250 | @Test |
| 251 | public void testEventHostAvailableMatch() throws Exception { |
| 252 | final Device host = device("host1"); |
| 253 | |
| 254 | final DeviceEvent deviceEvent = |
| 255 | new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host); |
| 256 | reasons.add(deviceEvent); |
| 257 | |
| 258 | final Key key = Key.of(0x333L, APP_ID); |
| 259 | Collection<NetworkResource> resources = ImmutableSet.of(host.id()); |
| 260 | tracker.addTrackedResources(key, resources); |
| 261 | |
| 262 | deviceListener.event(deviceEvent); |
| 263 | assertThat( |
| 264 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 265 | is(true)); |
| 266 | |
| 267 | assertThat(delegate.intentIdsFromEvent, hasSize(1)); |
| 268 | assertThat(delegate.compileAllFailedFromEvent, is(true)); |
| 269 | assertThat(delegate.intentIdsFromEvent.get(0).toString(), |
| 270 | equalTo("0x333")); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Tests an event for a host becoming unavailable that matches an intent. |
| 275 | * |
| 276 | * @throws InterruptedException if the latch wait fails. |
| 277 | */ |
| 278 | |
| 279 | @Test |
| 280 | public void testEventHostUnavailableMatch() throws Exception { |
| 281 | final Device host = device("host1"); |
| 282 | |
| 283 | final DeviceEvent deviceEvent = |
| 284 | new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, host); |
| 285 | reasons.add(deviceEvent); |
| 286 | |
| 287 | final Key key = Key.of(0x333L, APP_ID); |
| 288 | Collection<NetworkResource> resources = ImmutableSet.of(host.id()); |
| 289 | tracker.addTrackedResources(key, resources); |
| 290 | |
| 291 | deviceListener.event(deviceEvent); |
| 292 | assertThat( |
| 293 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 294 | is(true)); |
| 295 | |
| 296 | assertThat(delegate.intentIdsFromEvent, hasSize(1)); |
| 297 | assertThat(delegate.compileAllFailedFromEvent, is(false)); |
| 298 | assertThat(delegate.intentIdsFromEvent.get(0).toString(), |
| 299 | equalTo("0x333")); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Tests an event for a host becoming available that matches an intent. |
| 304 | * |
| 305 | * @throws InterruptedException if the latch wait fails. |
| 306 | */ |
| 307 | |
| 308 | @Test |
| 309 | public void testEventHostAvailableNoMatch() throws Exception { |
| 310 | final Device host = device("host1"); |
| 311 | |
| 312 | final DeviceEvent deviceEvent = |
| 313 | new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host); |
| 314 | reasons.add(deviceEvent); |
| 315 | |
| 316 | deviceListener.event(deviceEvent); |
| 317 | assertThat( |
| 318 | delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), |
| 319 | is(true)); |
| 320 | |
| 321 | assertThat(delegate.intentIdsFromEvent, hasSize(0)); |
| 322 | assertThat(delegate.compileAllFailedFromEvent, is(true)); |
| 323 | } |
| 324 | |
| 325 | |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 326 | } |