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