blob: 02d206a9356957015c0473813c46424f790bf182 [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 Milkey7c44c052014-12-05 10:34:54 -080035import org.onosproject.net.intent.IntentId;
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;
52import static org.onosproject.net.NetTestTools.link;
Ray Milkey7c44c052014-12-05 10:34:54 -080053
54/**
55 * Tests for the objective tracker.
56 */
57public class ObjectiveTrackerTest {
58 private static final int WAIT_TIMEOUT_SECONDS = 2;
59 private Topology topology;
60 private ObjectiveTracker tracker;
61 private TestTopologyChangeDelegate delegate;
62 private List<Event> reasons;
63 private TopologyListener listener;
64 private LinkResourceListener linkResourceListener;
Ray Milkey7c44c052014-12-05 10:34:54 -080065 private IdGenerator mockGenerator;
66
67 /**
68 * Initialization shared by all test cases.
69 *
70 * @throws TestUtilsException if any filed look ups fail
71 */
72 @Before
73 public void setUp() throws TestUtilsException {
74 topology = createMock(Topology.class);
75 tracker = new ObjectiveTracker();
76 delegate = new TestTopologyChangeDelegate();
77 tracker.setDelegate(delegate);
78 reasons = new LinkedList<>();
79 listener = TestUtils.getField(tracker, "listener");
80 linkResourceListener = TestUtils.getField(tracker, "linkResourceListener");
Ray Milkey7c44c052014-12-05 10:34:54 -080081 mockGenerator = new MockIdGenerator();
82 Intent.bindIdGenerator(mockGenerator);
83 }
84
85 /**
86 * Code to clean up shared by all test case.
87 */
88 @After
89 public void tearDown() {
90 tracker.unsetDelegate(delegate);
91 Intent.unbindIdGenerator(mockGenerator);
92 }
93
94 /**
95 * Topology change delegate mock that tracks the events coming into it
96 * and saves them. It provides a latch so that tests can wait for events
97 * to be generated.
98 */
99 static class TestTopologyChangeDelegate implements TopologyChangeDelegate {
100
101 CountDownLatch latch = new CountDownLatch(1);
102 List<IntentId> intentIdsFromEvent;
103 boolean compileAllFailedFromEvent;
104
105 @Override
106 public void triggerCompile(Iterable<IntentId> intentIds,
107 boolean compileAllFailed) {
108 intentIdsFromEvent = Lists.newArrayList(intentIds);
109 compileAllFailedFromEvent = compileAllFailed;
110 latch.countDown();
111 }
112 }
113
114 /**
Ray Milkey7c44c052014-12-05 10:34:54 -0800115 * Tests an event with no associated reasons.
116 *
117 * @throws InterruptedException if the latch wait fails.
118 */
119 @Test
120 public void testEventNoReasons() throws InterruptedException {
121 final TopologyEvent event = new TopologyEvent(
122 TopologyEvent.Type.TOPOLOGY_CHANGED,
123 topology,
124 null);
125
126 listener.event(event);
127 assertThat(
128 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
129 is(true));
130
131 assertThat(delegate.intentIdsFromEvent, hasSize(0));
132 assertThat(delegate.compileAllFailedFromEvent, is(true));
133 }
134
135 /**
136 * Tests an event for a link down where none of the reasons match
137 * currently installed intents.
138 *
139 * @throws InterruptedException if the latch wait fails.
140 */
141 @Test
142 public void testEventLinkDownNoMatches() throws InterruptedException {
143 final Link link = link("src", 1, "dst", 2);
144 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
145 reasons.add(linkEvent);
146
147 final TopologyEvent event = new TopologyEvent(
148 TopologyEvent.Type.TOPOLOGY_CHANGED,
149 topology,
150 reasons);
151
152 listener.event(event);
153 assertThat(
154 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
155 is(true));
156
157 assertThat(delegate.intentIdsFromEvent, hasSize(0));
158 assertThat(delegate.compileAllFailedFromEvent, is(false));
159 }
160
161 /**
162 * Tests an event for a link being added.
163 *
164 * @throws InterruptedException if the latch wait fails.
165 */
166 @Test
167 public void testEventLinkAdded() throws InterruptedException {
168 final Link link = link("src", 1, "dst", 2);
169 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_ADDED, link);
170 reasons.add(linkEvent);
171
172 final TopologyEvent event = new TopologyEvent(
173 TopologyEvent.Type.TOPOLOGY_CHANGED,
174 topology,
175 reasons);
176
177 listener.event(event);
178 assertThat(
179 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
180 is(true));
181
182 assertThat(delegate.intentIdsFromEvent, hasSize(0));
183 assertThat(delegate.compileAllFailedFromEvent, is(true));
184 }
185
186 /**
187 * Tests an event for a link down where the link matches existing intents.
188 *
189 * @throws InterruptedException if the latch wait fails.
190 */
191 @Test
192 public void testEventLinkDownMatch() throws Exception {
193 final Link link = link("src", 1, "dst", 2);
194 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
195 reasons.add(linkEvent);
196
197 final TopologyEvent event = new TopologyEvent(
198 TopologyEvent.Type.TOPOLOGY_CHANGED,
199 topology,
200 reasons);
201
202 final IntentId intentId = IntentId.valueOf(0x333L);
203 Collection<NetworkResource> resources = ImmutableSet.of(link);
204 tracker.addTrackedResources(intentId, resources);
205
206 listener.event(event);
207 assertThat(
208 delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS),
209 is(true));
210
211 assertThat(delegate.intentIdsFromEvent, hasSize(1));
212 assertThat(delegate.compileAllFailedFromEvent, is(false));
213 assertThat(delegate.intentIdsFromEvent.get(0).toString(),
214 equalTo("0x333"));
215 }
216
217 /**
218 * Tests a resource available event.
219 *
220 * @throws InterruptedException if the latch wait fails.
221 */
222 @Test
223 public void testResourceEvent() throws Exception {
224 LinkResourceEvent event = new LinkResourceEvent(
225 LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
226 new HashSet<>());
227 linkResourceListener.event(event);
228
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
Ray Milkey7c44c052014-12-05 10:34:54 -0800237}