blob: b324ae8623577a559233c5e588217a2be38d6d42 [file] [log] [blame]
Ray Milkey7c44c052014-12-05 10:34:54 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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;
32import org.onosproject.net.Link;
Ray Milkey7c44c052014-12-05 10:34:54 -080033import org.onosproject.net.NetworkResource;
34import org.onosproject.net.intent.Intent;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080035import org.onosproject.net.intent.Key;
Ray Milkey7c44c052014-12-05 10:34:54 -080036import org.onosproject.net.intent.MockIdGenerator;
37import org.onosproject.net.link.LinkEvent;
38import org.onosproject.net.resource.LinkResourceEvent;
39import org.onosproject.net.resource.LinkResourceListener;
40import org.onosproject.net.topology.Topology;
41import org.onosproject.net.topology.TopologyEvent;
42import org.onosproject.net.topology.TopologyListener;
43
Ray Milkey7c44c052014-12-05 10:34:54 -080044import com.google.common.collect.ImmutableSet;
45import com.google.common.collect.Lists;
Ray Milkey7c44c052014-12-05 10:34:54 -080046
47import static org.easymock.EasyMock.createMock;
Ray Milkey7c44c052014-12-05 10:34:54 -080048import static org.hamcrest.MatcherAssert.assertThat;
49import static org.hamcrest.Matchers.equalTo;
50import static org.hamcrest.Matchers.hasSize;
51import static org.hamcrest.Matchers.is;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080052import static org.onosproject.net.NetTestTools.APP_ID;
Ray Milkey7c44c052014-12-05 10:34:54 -080053import static org.onosproject.net.NetTestTools.link;
Ray Milkey7c44c052014-12-05 10:34:54 -080054
55/**
56 * Tests for the objective tracker.
57 */
58public class ObjectiveTrackerTest {
59 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;
65 private LinkResourceListener linkResourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080066 private IdGenerator mockGenerator;
67
68 /**
69 * Initialization shared by all test cases.
70 *
71 * @throws TestUtilsException if any filed look ups fail
72 */
73 @Before
74 public void setUp() throws TestUtilsException {
75 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");
81 linkResourceListener = TestUtils.getField(tracker, "linkResourceListener");
Ray Milkey7c44c052014-12-05 10:34:54 -080082 mockGenerator = new MockIdGenerator();
83 Intent.bindIdGenerator(mockGenerator);
84 }
85
86 /**
87 * Code to clean up shared by all test case.
88 */
89 @After
90 public void tearDown() {
91 tracker.unsetDelegate(delegate);
92 Intent.unbindIdGenerator(mockGenerator);
93 }
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 {
225 LinkResourceEvent event = new LinkResourceEvent(
226 LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
227 new HashSet<>());
228 linkResourceListener.event(event);
229
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
Ray Milkey7c44c052014-12-05 10:34:54 -0800238}