blob: 73414ce6c3ef63db2593cd618ffe5fb0c24440bf [file] [log] [blame]
Ray Milkey7c44c052014-12-05 10:34:54 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Ray Milkey7c44c052014-12-05 10:34:54 -08003 *
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 */
16package org.onosproject.net.intent.impl;
17
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
Ray Milkey7c44c052014-12-05 10:34:54 -080020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
Ray Milkey2da272b2015-02-03 19:52:08 -080024import org.onlab.junit.TestUtils.TestUtilsException;
Ray Milkey7c44c052014-12-05 10:34:54 -080025import org.onosproject.event.Event;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070026import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080027import org.onosproject.net.DeviceId;
Ray Milkey7c44c052014-12-05 10:34:54 -080028import org.onosproject.net.Link;
Ray Milkey7c44c052014-12-05 10:34:54 -080029import org.onosproject.net.NetworkResource;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080030import org.onosproject.net.PortNumber;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070031import org.onosproject.net.device.DeviceEvent;
32import org.onosproject.net.device.DeviceListener;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070033import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080034import org.onosproject.net.intent.Key;
Yi Tseng24d9be72017-05-12 11:28:13 -070035import org.onosproject.net.intent.TopologyChangeDelegate;
Ray Milkey7c44c052014-12-05 10:34:54 -080036import org.onosproject.net.link.LinkEvent;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080037import org.onosproject.net.resource.ResourceEvent;
38import org.onosproject.net.resource.ResourceListener;
39import org.onosproject.net.resource.Resources;
Ray Milkey7c44c052014-12-05 10:34:54 -080040import org.onosproject.net.topology.Topology;
41import org.onosproject.net.topology.TopologyEvent;
42import org.onosproject.net.topology.TopologyListener;
43
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070044import java.util.Collection;
45import java.util.LinkedList;
46import java.util.List;
47import java.util.concurrent.CountDownLatch;
48import java.util.concurrent.TimeUnit;
Ray Milkey7c44c052014-12-05 10:34:54 -080049
50import static org.easymock.EasyMock.createMock;
Ray Milkey7c44c052014-12-05 10:34:54 -080051import static org.hamcrest.MatcherAssert.assertThat;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070052import static org.hamcrest.Matchers.*;
53import static org.onosproject.net.NetTestTools.*;
54import static org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED;
Ray Milkey7c44c052014-12-05 10:34:54 -080055
56/**
57 * Tests for the objective tracker.
58 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070059public class ObjectiveTrackerTest extends AbstractIntentTest {
Ray Milkey7c44c052014-12-05 10:34:54 -080060 private static final int WAIT_TIMEOUT_SECONDS = 2;
61 private Topology topology;
62 private ObjectiveTracker tracker;
63 private TestTopologyChangeDelegate delegate;
64 private List<Event> reasons;
65 private TopologyListener listener;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070066 private DeviceListener deviceListener;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080067 private ResourceListener resourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080068
69 /**
70 * Initialization shared by all test cases.
71 *
72 * @throws TestUtilsException if any filed look ups fail
73 */
74 @Before
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070075 public void setUp() {
Ray Milkey7c44c052014-12-05 10:34:54 -080076 topology = createMock(Topology.class);
77 tracker = new ObjectiveTracker();
78 delegate = new TestTopologyChangeDelegate();
79 tracker.setDelegate(delegate);
80 reasons = new LinkedList<>();
81 listener = TestUtils.getField(tracker, "listener");
Brian O'Connorb5dcc512015-03-24 17:28:00 -070082 deviceListener = TestUtils.getField(tracker, "deviceListener");
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080083 resourceListener = TestUtils.getField(tracker, "resourceListener");
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070084 super.setUp();
Ray Milkey7c44c052014-12-05 10:34:54 -080085 }
86
87 /**
88 * Code to clean up shared by all test case.
89 */
90 @After
91 public void tearDown() {
92 tracker.unsetDelegate(delegate);
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070093 super.tearDown();
Ray Milkey7c44c052014-12-05 10:34:54 -080094 }
95
96 /**
97 * Topology change delegate mock that tracks the events coming into it
98 * and saves them. It provides a latch so that tests can wait for events
99 * to be generated.
100 */
101 static class TestTopologyChangeDelegate implements TopologyChangeDelegate {
102
103 CountDownLatch latch = new CountDownLatch(1);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800104 List<Key> intentIdsFromEvent;
Ray Milkey7c44c052014-12-05 10:34:54 -0800105 boolean compileAllFailedFromEvent;
106
107 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800108 public void triggerCompile(Iterable<Key> intentKeys,
Ray Milkey7c44c052014-12-05 10:34:54 -0800109 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800110 intentIdsFromEvent = Lists.newArrayList(intentKeys);
Ray Milkey7c44c052014-12-05 10:34:54 -0800111 compileAllFailedFromEvent = compileAllFailed;
112 latch.countDown();
113 }
114 }
115
116 /**
Ray Milkey7c44c052014-12-05 10:34:54 -0800117 * Tests an event with no associated reasons.
118 *
119 * @throws InterruptedException if the latch wait fails.
120 */
121 @Test
122 public void testEventNoReasons() throws InterruptedException {
123 final TopologyEvent event = new TopologyEvent(
124 TopologyEvent.Type.TOPOLOGY_CHANGED,
125 topology,
126 null);
127
128 listener.event(event);
129 assertThat(
130 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
131 is(true));
132
133 assertThat(delegate.intentIdsFromEvent, hasSize(0));
134 assertThat(delegate.compileAllFailedFromEvent, is(true));
135 }
136
137 /**
138 * Tests an event for a link down where none of the reasons match
139 * currently installed intents.
140 *
141 * @throws InterruptedException if the latch wait fails.
142 */
143 @Test
144 public void testEventLinkDownNoMatches() throws InterruptedException {
145 final Link link = link("src", 1, "dst", 2);
146 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
147 reasons.add(linkEvent);
148
149 final TopologyEvent event = new TopologyEvent(
150 TopologyEvent.Type.TOPOLOGY_CHANGED,
151 topology,
152 reasons);
153
154 listener.event(event);
155 assertThat(
156 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
157 is(true));
158
159 assertThat(delegate.intentIdsFromEvent, hasSize(0));
160 assertThat(delegate.compileAllFailedFromEvent, is(false));
161 }
162
163 /**
164 * Tests an event for a link being added.
165 *
166 * @throws InterruptedException if the latch wait fails.
167 */
168 @Test
169 public void testEventLinkAdded() throws InterruptedException {
170 final Link link = link("src", 1, "dst", 2);
171 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_ADDED, link);
172 reasons.add(linkEvent);
173
174 final TopologyEvent event = new TopologyEvent(
175 TopologyEvent.Type.TOPOLOGY_CHANGED,
176 topology,
177 reasons);
178
179 listener.event(event);
180 assertThat(
181 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
182 is(true));
183
184 assertThat(delegate.intentIdsFromEvent, hasSize(0));
185 assertThat(delegate.compileAllFailedFromEvent, is(true));
186 }
187
188 /**
189 * Tests an event for a link down where the link matches existing intents.
190 *
191 * @throws InterruptedException if the latch wait fails.
192 */
193 @Test
194 public void testEventLinkDownMatch() throws Exception {
195 final Link link = link("src", 1, "dst", 2);
196 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
197 reasons.add(linkEvent);
198
199 final TopologyEvent event = new TopologyEvent(
200 TopologyEvent.Type.TOPOLOGY_CHANGED,
201 topology,
202 reasons);
203
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800204 final Key key = Key.of(0x333L, APP_ID);
Ray Milkey7c44c052014-12-05 10:34:54 -0800205 Collection<NetworkResource> resources = ImmutableSet.of(link);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800206 tracker.addTrackedResources(key, resources);
Ray Milkey7c44c052014-12-05 10:34:54 -0800207
208 listener.event(event);
209 assertThat(
210 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
211 is(true));
212
213 assertThat(delegate.intentIdsFromEvent, hasSize(1));
214 assertThat(delegate.compileAllFailedFromEvent, is(false));
215 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
216 equalTo("0x333"));
217 }
218
219 /**
220 * Tests a resource available event.
221 *
222 * @throws InterruptedException if the latch wait fails.
223 */
224 @Test
225 public void testResourceEvent() throws Exception {
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800226 ResourceEvent event = new ResourceEvent(RESOURCE_ADDED,
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800227 Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource());
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800228 resourceListener.event(event);
Ray Milkey7c44c052014-12-05 10:34:54 -0800229
230 assertThat(
231 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
232 is(true));
233
234 assertThat(delegate.intentIdsFromEvent, hasSize(0));
235 assertThat(delegate.compileAllFailedFromEvent, is(true));
236 }
237
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700238 /**
239 * Tests an event for a host becoming available that matches an intent.
240 *
241 * @throws InterruptedException if the latch wait fails.
242 */
243
244 @Test
245 public void testEventHostAvailableMatch() throws Exception {
246 final Device host = device("host1");
247
248 final DeviceEvent deviceEvent =
249 new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host);
250 reasons.add(deviceEvent);
251
252 final Key key = Key.of(0x333L, APP_ID);
253 Collection<NetworkResource> resources = ImmutableSet.of(host.id());
254 tracker.addTrackedResources(key, resources);
255
256 deviceListener.event(deviceEvent);
257 assertThat(
258 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
259 is(true));
260
261 assertThat(delegate.intentIdsFromEvent, hasSize(1));
262 assertThat(delegate.compileAllFailedFromEvent, is(true));
263 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
264 equalTo("0x333"));
265 }
266
267 /**
268 * Tests an event for a host becoming unavailable that matches an intent.
269 *
270 * @throws InterruptedException if the latch wait fails.
271 */
272
273 @Test
274 public void testEventHostUnavailableMatch() throws Exception {
275 final Device host = device("host1");
276
277 final DeviceEvent deviceEvent =
278 new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, host);
279 reasons.add(deviceEvent);
280
281 final Key key = Key.of(0x333L, APP_ID);
282 Collection<NetworkResource> resources = ImmutableSet.of(host.id());
283 tracker.addTrackedResources(key, resources);
284
285 deviceListener.event(deviceEvent);
286 assertThat(
287 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
288 is(true));
289
290 assertThat(delegate.intentIdsFromEvent, hasSize(1));
291 assertThat(delegate.compileAllFailedFromEvent, is(false));
292 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
293 equalTo("0x333"));
294 }
295
296 /**
297 * Tests an event for a host becoming available that matches an intent.
298 *
299 * @throws InterruptedException if the latch wait fails.
300 */
301
302 @Test
303 public void testEventHostAvailableNoMatch() throws Exception {
304 final Device host = device("host1");
305
306 final DeviceEvent deviceEvent =
307 new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host);
308 reasons.add(deviceEvent);
309
310 deviceListener.event(deviceEvent);
311 assertThat(
312 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
313 is(true));
314
315 assertThat(delegate.intentIdsFromEvent, hasSize(0));
316 assertThat(delegate.compileAllFailedFromEvent, is(true));
317 }
318
319
Ray Milkey7c44c052014-12-05 10:34:54 -0800320}