blob: 8d7452b39caf3c11c3d8adbeb59cc000b663a4ea [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;
Ray Milkey7c44c052014-12-05 10:34:54 -080019import java.util.LinkedList;
20import java.util.List;
21import java.util.concurrent.CountDownLatch;
22import java.util.concurrent.TimeUnit;
23
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.junit.TestUtils;
Ray Milkey2da272b2015-02-03 19:52:08 -080028import org.onlab.junit.TestUtils.TestUtilsException;
Ray Milkey7c44c052014-12-05 10:34:54 -080029import org.onosproject.core.IdGenerator;
30import org.onosproject.event.Event;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070031import org.onosproject.net.Device;
Ray Milkey7c44c052014-12-05 10:34:54 -080032import org.onosproject.net.Link;
Ray Milkey7c44c052014-12-05 10:34:54 -080033import org.onosproject.net.NetworkResource;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070034import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080036import org.onosproject.net.intent.Intent;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080037import org.onosproject.net.intent.Key;
Ray Milkey7c44c052014-12-05 10:34:54 -080038import org.onosproject.net.intent.MockIdGenerator;
39import org.onosproject.net.link.LinkEvent;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080040import org.onosproject.net.newresource.ResourceEvent;
41import org.onosproject.net.newresource.ResourceListener;
42import org.onosproject.net.newresource.ResourcePath;
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;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080055import static org.onosproject.net.LinkKey.linkKey;
56import static org.onosproject.net.newresource.ResourceEvent.Type.*;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080057import static org.onosproject.net.NetTestTools.APP_ID;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070058import static org.onosproject.net.NetTestTools.device;
Ray Milkey7c44c052014-12-05 10:34:54 -080059import static org.onosproject.net.NetTestTools.link;
Ray Milkey7c44c052014-12-05 10:34:54 -080060
61/**
62 * Tests for the objective tracker.
63 */
64public class ObjectiveTrackerTest {
65 private static final int WAIT_TIMEOUT_SECONDS = 2;
66 private Topology topology;
67 private ObjectiveTracker tracker;
68 private TestTopologyChangeDelegate delegate;
69 private List<Event> reasons;
70 private TopologyListener listener;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070071 private DeviceListener deviceListener;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080072 private ResourceListener resourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080073 private IdGenerator mockGenerator;
74
75 /**
76 * Initialization shared by all test cases.
77 *
78 * @throws TestUtilsException if any filed look ups fail
79 */
80 @Before
81 public void setUp() throws TestUtilsException {
82 topology = createMock(Topology.class);
83 tracker = new ObjectiveTracker();
84 delegate = new TestTopologyChangeDelegate();
85 tracker.setDelegate(delegate);
86 reasons = new LinkedList<>();
87 listener = TestUtils.getField(tracker, "listener");
Brian O'Connorb5dcc512015-03-24 17:28:00 -070088 deviceListener = TestUtils.getField(tracker, "deviceListener");
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080089 resourceListener = TestUtils.getField(tracker, "resourceListener");
Ray Milkey7c44c052014-12-05 10:34:54 -080090 mockGenerator = new MockIdGenerator();
91 Intent.bindIdGenerator(mockGenerator);
92 }
93
94 /**
95 * Code to clean up shared by all test case.
96 */
97 @After
98 public void tearDown() {
99 tracker.unsetDelegate(delegate);
100 Intent.unbindIdGenerator(mockGenerator);
101 }
102
103 /**
104 * Topology change delegate mock that tracks the events coming into it
105 * and saves them. It provides a latch so that tests can wait for events
106 * to be generated.
107 */
108 static class TestTopologyChangeDelegate implements TopologyChangeDelegate {
109
110 CountDownLatch latch = new CountDownLatch(1);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800111 List<Key> intentIdsFromEvent;
Ray Milkey7c44c052014-12-05 10:34:54 -0800112 boolean compileAllFailedFromEvent;
113
114 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800115 public void triggerCompile(Iterable<Key> intentKeys,
Ray Milkey7c44c052014-12-05 10:34:54 -0800116 boolean compileAllFailed) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800117 intentIdsFromEvent = Lists.newArrayList(intentKeys);
Ray Milkey7c44c052014-12-05 10:34:54 -0800118 compileAllFailedFromEvent = compileAllFailed;
119 latch.countDown();
120 }
121 }
122
123 /**
Ray Milkey7c44c052014-12-05 10:34:54 -0800124 * Tests an event with no associated reasons.
125 *
126 * @throws InterruptedException if the latch wait fails.
127 */
128 @Test
129 public void testEventNoReasons() throws InterruptedException {
130 final TopologyEvent event = new TopologyEvent(
131 TopologyEvent.Type.TOPOLOGY_CHANGED,
132 topology,
133 null);
134
135 listener.event(event);
136 assertThat(
137 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
138 is(true));
139
140 assertThat(delegate.intentIdsFromEvent, hasSize(0));
141 assertThat(delegate.compileAllFailedFromEvent, is(true));
142 }
143
144 /**
145 * Tests an event for a link down where none of the reasons match
146 * currently installed intents.
147 *
148 * @throws InterruptedException if the latch wait fails.
149 */
150 @Test
151 public void testEventLinkDownNoMatches() throws InterruptedException {
152 final Link link = link("src", 1, "dst", 2);
153 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
154 reasons.add(linkEvent);
155
156 final TopologyEvent event = new TopologyEvent(
157 TopologyEvent.Type.TOPOLOGY_CHANGED,
158 topology,
159 reasons);
160
161 listener.event(event);
162 assertThat(
163 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
164 is(true));
165
166 assertThat(delegate.intentIdsFromEvent, hasSize(0));
167 assertThat(delegate.compileAllFailedFromEvent, is(false));
168 }
169
170 /**
171 * Tests an event for a link being added.
172 *
173 * @throws InterruptedException if the latch wait fails.
174 */
175 @Test
176 public void testEventLinkAdded() throws InterruptedException {
177 final Link link = link("src", 1, "dst", 2);
178 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_ADDED, link);
179 reasons.add(linkEvent);
180
181 final TopologyEvent event = new TopologyEvent(
182 TopologyEvent.Type.TOPOLOGY_CHANGED,
183 topology,
184 reasons);
185
186 listener.event(event);
187 assertThat(
188 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
189 is(true));
190
191 assertThat(delegate.intentIdsFromEvent, hasSize(0));
192 assertThat(delegate.compileAllFailedFromEvent, is(true));
193 }
194
195 /**
196 * Tests an event for a link down where the link matches existing intents.
197 *
198 * @throws InterruptedException if the latch wait fails.
199 */
200 @Test
201 public void testEventLinkDownMatch() throws Exception {
202 final Link link = link("src", 1, "dst", 2);
203 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
204 reasons.add(linkEvent);
205
206 final TopologyEvent event = new TopologyEvent(
207 TopologyEvent.Type.TOPOLOGY_CHANGED,
208 topology,
209 reasons);
210
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800211 final Key key = Key.of(0x333L, APP_ID);
Ray Milkey7c44c052014-12-05 10:34:54 -0800212 Collection<NetworkResource> resources = ImmutableSet.of(link);
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800213 tracker.addTrackedResources(key, resources);
Ray Milkey7c44c052014-12-05 10:34:54 -0800214
215 listener.event(event);
216 assertThat(
217 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
218 is(true));
219
220 assertThat(delegate.intentIdsFromEvent, hasSize(1));
221 assertThat(delegate.compileAllFailedFromEvent, is(false));
222 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
223 equalTo("0x333"));
224 }
225
226 /**
227 * Tests a resource available event.
228 *
229 * @throws InterruptedException if the latch wait fails.
230 */
231 @Test
232 public void testResourceEvent() throws Exception {
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800233 ResourceEvent event = new ResourceEvent(RESOURCE_ADDED,
234 new ResourcePath(linkKey(link("a", 1, "b", 1))));
235 resourceListener.event(event);
Ray Milkey7c44c052014-12-05 10:34:54 -0800236
237 assertThat(
238 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
239 is(true));
240
241 assertThat(delegate.intentIdsFromEvent, hasSize(0));
242 assertThat(delegate.compileAllFailedFromEvent, is(true));
243 }
244
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700245 /**
246 * Tests an event for a host becoming available that matches an intent.
247 *
248 * @throws InterruptedException if the latch wait fails.
249 */
250
251 @Test
252 public void testEventHostAvailableMatch() throws Exception {
253 final Device host = device("host1");
254
255 final DeviceEvent deviceEvent =
256 new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host);
257 reasons.add(deviceEvent);
258
259 final Key key = Key.of(0x333L, APP_ID);
260 Collection<NetworkResource> resources = ImmutableSet.of(host.id());
261 tracker.addTrackedResources(key, resources);
262
263 deviceListener.event(deviceEvent);
264 assertThat(
265 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
266 is(true));
267
268 assertThat(delegate.intentIdsFromEvent, hasSize(1));
269 assertThat(delegate.compileAllFailedFromEvent, is(true));
270 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
271 equalTo("0x333"));
272 }
273
274 /**
275 * Tests an event for a host becoming unavailable that matches an intent.
276 *
277 * @throws InterruptedException if the latch wait fails.
278 */
279
280 @Test
281 public void testEventHostUnavailableMatch() throws Exception {
282 final Device host = device("host1");
283
284 final DeviceEvent deviceEvent =
285 new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, host);
286 reasons.add(deviceEvent);
287
288 final Key key = Key.of(0x333L, APP_ID);
289 Collection<NetworkResource> resources = ImmutableSet.of(host.id());
290 tracker.addTrackedResources(key, resources);
291
292 deviceListener.event(deviceEvent);
293 assertThat(
294 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
295 is(true));
296
297 assertThat(delegate.intentIdsFromEvent, hasSize(1));
298 assertThat(delegate.compileAllFailedFromEvent, is(false));
299 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
300 equalTo("0x333"));
301 }
302
303 /**
304 * Tests an event for a host becoming available that matches an intent.
305 *
306 * @throws InterruptedException if the latch wait fails.
307 */
308
309 @Test
310 public void testEventHostAvailableNoMatch() throws Exception {
311 final Device host = device("host1");
312
313 final DeviceEvent deviceEvent =
314 new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host);
315 reasons.add(deviceEvent);
316
317 deviceListener.event(deviceEvent);
318 assertThat(
319 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
320 is(true));
321
322 assertThat(delegate.intentIdsFromEvent, hasSize(0));
323 assertThat(delegate.compileAllFailedFromEvent, is(true));
324 }
325
326
Ray Milkey7c44c052014-12-05 10:34:54 -0800327}