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