blob: bd12884a3ad1b19c18fb22604cefec20e254c67b [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;
Ray Milkey7c44c052014-12-05 10:34:54 -080035import org.onosproject.net.link.LinkEvent;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080036import org.onosproject.net.resource.ResourceEvent;
37import org.onosproject.net.resource.ResourceListener;
38import org.onosproject.net.resource.Resources;
Ray Milkey7c44c052014-12-05 10:34:54 -080039import org.onosproject.net.topology.Topology;
40import org.onosproject.net.topology.TopologyEvent;
41import org.onosproject.net.topology.TopologyListener;
42
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070043import java.util.Collection;
44import java.util.LinkedList;
45import java.util.List;
46import java.util.concurrent.CountDownLatch;
47import java.util.concurrent.TimeUnit;
Ray Milkey7c44c052014-12-05 10:34:54 -080048
49import static org.easymock.EasyMock.createMock;
Ray Milkey7c44c052014-12-05 10:34:54 -080050import static org.hamcrest.MatcherAssert.assertThat;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070051import static org.hamcrest.Matchers.*;
52import static org.onosproject.net.NetTestTools.*;
53import static org.onosproject.net.resource.ResourceEvent.Type.RESOURCE_ADDED;
Ray Milkey7c44c052014-12-05 10:34:54 -080054
55/**
56 * Tests for the objective tracker.
57 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070058public class ObjectiveTrackerTest extends AbstractIntentTest {
Ray Milkey7c44c052014-12-05 10:34:54 -080059 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'Connorb5dcc512015-03-24 17:28:00 -070065 private DeviceListener deviceListener;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080066 private ResourceListener resourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080067
68 /**
69 * Initialization shared by all test cases.
70 *
71 * @throws TestUtilsException if any filed look ups fail
72 */
73 @Before
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070074 public void setUp() {
Ray Milkey7c44c052014-12-05 10:34:54 -080075 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'Connorb5dcc512015-03-24 17:28:00 -070081 deviceListener = TestUtils.getField(tracker, "deviceListener");
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080082 resourceListener = TestUtils.getField(tracker, "resourceListener");
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070083 super.setUp();
Ray Milkey7c44c052014-12-05 10:34:54 -080084 }
85
86 /**
87 * Code to clean up shared by all test case.
88 */
89 @After
90 public void tearDown() {
91 tracker.unsetDelegate(delegate);
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070092 super.tearDown();
Ray Milkey7c44c052014-12-05 10:34:54 -080093 }
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 Milkeyf9af43c2015-02-09 16:45:48 -0800103 List<Key> intentIdsFromEvent;
Ray Milkey7c44c052014-12-05 10:34:54 -0800104 boolean compileAllFailedFromEvent;
105
106 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800107 public void triggerCompile(Iterable<Key> intentKeys,
Ray Milkey7c44c052014-12-05 10:34:54 -0800108 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800109 intentIdsFromEvent = Lists.newArrayList(intentKeys);
Ray Milkey7c44c052014-12-05 10:34:54 -0800110 compileAllFailedFromEvent = compileAllFailed;
111 latch.countDown();
112 }
113 }
114
115 /**
Ray Milkey7c44c052014-12-05 10:34:54 -0800116 * 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 Milkeyf9af43c2015-02-09 16:45:48 -0800203 final Key key = Key.of(0x333L, APP_ID);
Ray Milkey7c44c052014-12-05 10:34:54 -0800204 Collection<NetworkResource> resources = ImmutableSet.of(link);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800205 tracker.addTrackedResources(key, resources);
Ray Milkey7c44c052014-12-05 10:34:54 -0800206
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 SHIMIZU0b9c4682015-11-02 18:30:34 -0800225 ResourceEvent event = new ResourceEvent(RESOURCE_ADDED,
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800226 Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource());
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800227 resourceListener.event(event);
Ray Milkey7c44c052014-12-05 10:34:54 -0800228
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'Connorb5dcc512015-03-24 17:28:00 -0700237 /**
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 Milkey7c44c052014-12-05 10:34:54 -0800319}