blob: 58fa1292fa6e396d58dd535ea3e7b96ae5d7d415 [file] [log] [blame]
Ray Milkey7c44c052014-12-05 10:34:54 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
18import java.util.Collection;
19import java.util.HashSet;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.concurrent.CountDownLatch;
23import java.util.concurrent.TimeUnit;
24
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.onlab.junit.TestUtils;
Ray Milkey2da272b2015-02-03 19:52:08 -080029import org.onlab.junit.TestUtils.TestUtilsException;
Ray Milkey7c44c052014-12-05 10:34:54 -080030import org.onosproject.core.IdGenerator;
31import org.onosproject.event.Event;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070032import org.onosproject.net.Device;
Ray Milkey7c44c052014-12-05 10:34:54 -080033import org.onosproject.net.Link;
Ray Milkey7c44c052014-12-05 10:34:54 -080034import org.onosproject.net.NetworkResource;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070035import org.onosproject.net.device.DeviceEvent;
36import org.onosproject.net.device.DeviceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080037import org.onosproject.net.intent.Intent;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080038import org.onosproject.net.intent.Key;
Ray Milkey7c44c052014-12-05 10:34:54 -080039import org.onosproject.net.intent.MockIdGenerator;
40import org.onosproject.net.link.LinkEvent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070041import org.onosproject.net.resource.link.LinkResourceEvent;
42import org.onosproject.net.resource.link.LinkResourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080043import org.onosproject.net.topology.Topology;
44import org.onosproject.net.topology.TopologyEvent;
45import org.onosproject.net.topology.TopologyListener;
46
Ray Milkey7c44c052014-12-05 10:34:54 -080047import com.google.common.collect.ImmutableSet;
48import com.google.common.collect.Lists;
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;
52import static org.hamcrest.Matchers.equalTo;
53import static org.hamcrest.Matchers.hasSize;
54import static org.hamcrest.Matchers.is;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080055import static org.onosproject.net.NetTestTools.APP_ID;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070056import static org.onosproject.net.NetTestTools.device;
Ray Milkey7c44c052014-12-05 10:34:54 -080057import static org.onosproject.net.NetTestTools.link;
Ray Milkey7c44c052014-12-05 10:34:54 -080058
59/**
60 * Tests for the objective tracker.
61 */
62public 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'Connorb5dcc512015-03-24 17:28:00 -070069 private DeviceListener deviceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080070 private LinkResourceListener linkResourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080071 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'Connorb5dcc512015-03-24 17:28:00 -070086 deviceListener = TestUtils.getField(tracker, "deviceListener");
Ray Milkey7c44c052014-12-05 10:34:54 -080087 linkResourceListener = TestUtils.getField(tracker, "linkResourceListener");
Ray Milkey7c44c052014-12-05 10:34:54 -080088 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 Milkeyf9af43c2015-02-09 16:45:48 -0800109 List<Key> intentIdsFromEvent;
Ray Milkey7c44c052014-12-05 10:34:54 -0800110 boolean compileAllFailedFromEvent;
111
112 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800113 public void triggerCompile(Iterable<Key> intentKeys,
Ray Milkey7c44c052014-12-05 10:34:54 -0800114 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800115 intentIdsFromEvent = Lists.newArrayList(intentKeys);
Ray Milkey7c44c052014-12-05 10:34:54 -0800116 compileAllFailedFromEvent = compileAllFailed;
117 latch.countDown();
118 }
119 }
120
121 /**
Ray Milkey7c44c052014-12-05 10:34:54 -0800122 * 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 Milkeyf9af43c2015-02-09 16:45:48 -0800209 final Key key = Key.of(0x333L, APP_ID);
Ray Milkey7c44c052014-12-05 10:34:54 -0800210 Collection<NetworkResource> resources = ImmutableSet.of(link);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800211 tracker.addTrackedResources(key, resources);
Ray Milkey7c44c052014-12-05 10:34:54 -0800212
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'Connorb5dcc512015-03-24 17:28:00 -0700244 /**
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 Milkey7c44c052014-12-05 10:34:54 -0800326}