Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | import java.util.concurrent.CountDownLatch; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.junit.TestUtils; |
Ray Milkey | 2da272b | 2015-02-03 19:52:08 -0800 | [diff] [blame] | 29 | import org.onlab.junit.TestUtils.TestUtilsException; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 30 | import org.onosproject.core.IdGenerator; |
| 31 | import org.onosproject.event.Event; |
| 32 | import org.onosproject.net.Link; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 33 | import org.onosproject.net.NetworkResource; |
| 34 | import org.onosproject.net.intent.Intent; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 35 | import org.onosproject.net.intent.Key; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.MockIdGenerator; |
| 37 | import org.onosproject.net.link.LinkEvent; |
| 38 | import org.onosproject.net.resource.LinkResourceEvent; |
| 39 | import org.onosproject.net.resource.LinkResourceListener; |
| 40 | import org.onosproject.net.topology.Topology; |
| 41 | import org.onosproject.net.topology.TopologyEvent; |
| 42 | import org.onosproject.net.topology.TopologyListener; |
| 43 | |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 44 | import com.google.common.collect.ImmutableSet; |
| 45 | import com.google.common.collect.Lists; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 46 | |
| 47 | import static org.easymock.EasyMock.createMock; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 48 | import static org.hamcrest.MatcherAssert.assertThat; |
| 49 | import static org.hamcrest.Matchers.equalTo; |
| 50 | import static org.hamcrest.Matchers.hasSize; |
| 51 | import static org.hamcrest.Matchers.is; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 52 | import static org.onosproject.net.NetTestTools.APP_ID; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 53 | import static org.onosproject.net.NetTestTools.link; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Tests for the objective tracker. |
| 57 | */ |
| 58 | public 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 Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 66 | 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 Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 82 | 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 Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 103 | List<Key> intentIdsFromEvent; |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 104 | boolean compileAllFailedFromEvent; |
| 105 | |
| 106 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 107 | public void triggerCompile(Iterable<Key> intentKeys, |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 108 | boolean compileAllFailed) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 109 | intentIdsFromEvent = Lists.newArrayList(intentKeys); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 110 | compileAllFailedFromEvent = compileAllFailed; |
| 111 | latch.countDown(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 116 | * 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 Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 203 | final Key key = Key.of(0x333L, APP_ID); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 204 | Collection<NetworkResource> resources = ImmutableSet.of(link); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame^] | 205 | tracker.addTrackedResources(key, resources); |
Ray Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 206 | |
| 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 Milkey | 7c44c05 | 2014-12-05 10:34:54 -0800 | [diff] [blame] | 238 | } |