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