Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.topology.impl; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 17 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 19 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.onos.event.Event; |
| 24 | import org.onlab.onos.event.impl.TestEventDispatcher; |
| 25 | import org.onlab.onos.net.Device; |
| 26 | import org.onlab.onos.net.Link; |
| 27 | import org.onlab.onos.net.device.DeviceEvent; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.device.impl.DeviceManager; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.link.LinkEvent; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.link.impl.LinkManager; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 32 | import org.onlab.onos.net.provider.ProviderId; |
| 33 | import org.onlab.onos.net.topology.GraphDescription; |
| 34 | import org.onlab.onos.net.topology.TopologyProvider; |
| 35 | import org.onlab.onos.net.topology.TopologyProviderRegistry; |
| 36 | import org.onlab.onos.net.topology.TopologyProviderService; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 37 | |
| 38 | import java.util.List; |
| 39 | import java.util.Set; |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 40 | import java.util.concurrent.Phaser; |
| 41 | import java.util.concurrent.TimeUnit; |
| 42 | import java.util.concurrent.TimeoutException; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 43 | |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 44 | import static org.junit.Assert.*; |
| 45 | import static org.hamcrest.Matchers.*; |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 46 | import static org.onlab.onos.net.NetTestTools.device; |
| 47 | import static org.onlab.onos.net.NetTestTools.link; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 48 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 49 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Test of the default topology provider implementation. |
| 53 | */ |
| 54 | public class DefaultTopologyProviderTest { |
| 55 | |
| 56 | private DefaultTopologyProvider provider = new DefaultTopologyProvider(); |
| 57 | private TestTopoRegistry topologyService = new TestTopoRegistry(); |
| 58 | private TestDeviceService deviceService = new TestDeviceService(); |
| 59 | private TestLinkService linkService = new TestLinkService(); |
| 60 | private TestTopoProviderService providerService; |
| 61 | |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 62 | // phase corresponds to number of topologyChanged called |
| 63 | private Phaser topologyChangedCounts = new Phaser(1); |
| 64 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 65 | @Before |
| 66 | public void setUp() { |
| 67 | provider.deviceService = deviceService; |
| 68 | provider.linkService = linkService; |
| 69 | provider.providerRegistry = topologyService; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 70 | provider.activate(null); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @After |
| 74 | public void tearDown() { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 75 | provider.deactivate(null); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 76 | provider.providerRegistry = null; |
| 77 | provider.deviceService = null; |
| 78 | provider.linkService = null; |
| 79 | } |
| 80 | |
| 81 | private void validateSubmission() { |
| 82 | assertNotNull("registration expected", providerService); |
Ayaka Koshibe | cc4c4c8 | 2014-09-16 11:23:37 -0700 | [diff] [blame] | 83 | assertEquals("incorrect provider", provider, providerService.provider()); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 84 | assertNotNull("topo change should be submitted", providerService.graphDesc); |
| 85 | assertEquals("incorrect vertex count", 6, providerService.graphDesc.vertexes().size()); |
| 86 | assertEquals("incorrect edge count", 10, providerService.graphDesc.edges().size()); |
| 87 | } |
| 88 | |
| 89 | @Test |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 90 | public void basics() throws InterruptedException, TimeoutException { |
| 91 | assertEquals(1, topologyChangedCounts.awaitAdvanceInterruptibly(0, 1, TimeUnit.SECONDS)); |
| 92 | validateSubmission(); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 96 | public void eventDriven() throws InterruptedException, TimeoutException { |
| 97 | assertEquals(1, topologyChangedCounts.awaitAdvanceInterruptibly(0, 1, TimeUnit.SECONDS)); |
| 98 | validateSubmission(); |
| 99 | |
| 100 | deviceService.post(new DeviceEvent(DEVICE_ADDED, device("z"), null)); |
| 101 | linkService.post(new LinkEvent(LINK_ADDED, link("z", 1, "a", 4))); |
| 102 | assertThat(topologyChangedCounts.awaitAdvanceInterruptibly(1, 1, TimeUnit.SECONDS), |
| 103 | is(greaterThanOrEqualTo(2))); |
| 104 | // Note: posting event, to trigger topologyChanged call, |
| 105 | // but dummy topology will not change. |
| 106 | validateSubmission(); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | private class TestTopoRegistry implements TopologyProviderRegistry { |
| 111 | |
| 112 | @Override |
| 113 | public TopologyProviderService register(TopologyProvider provider) { |
| 114 | providerService = new TestTopoProviderService(provider); |
| 115 | return providerService; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void unregister(TopologyProvider provider) { |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Set<ProviderId> getProviders() { |
| 124 | return null; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private class TestTopoProviderService |
| 129 | extends AbstractProviderService<TopologyProvider> |
| 130 | implements TopologyProviderService { |
| 131 | GraphDescription graphDesc; |
| 132 | |
| 133 | protected TestTopoProviderService(TopologyProvider provider) { |
| 134 | super(provider); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void topologyChanged(GraphDescription graphDescription, List<Event> reasons) { |
| 139 | graphDesc = graphDescription; |
Yuta HIGUCHI | 8bca235 | 2014-10-14 11:17:49 -0700 | [diff] [blame] | 140 | topologyChangedCounts.arrive(); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
tom | 41a2c5f | 2014-09-19 09:20:35 -0700 | [diff] [blame] | 144 | private class TestDeviceService extends DeviceManager { |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 145 | TestDeviceService() { |
| 146 | eventDispatcher = new TestEventDispatcher(); |
| 147 | eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Iterable<Device> getDevices() { |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 152 | return ImmutableSet.of(device("a"), device("b"), |
| 153 | device("c"), device("d"), |
| 154 | device("e"), device("f")); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame^] | 157 | @Override |
| 158 | public Iterable<Device> getAvailableDevices() { |
| 159 | return getDevices(); |
| 160 | } |
| 161 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 162 | void post(DeviceEvent event) { |
| 163 | eventDispatcher.post(event); |
| 164 | } |
| 165 | } |
| 166 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 167 | private class TestLinkService extends LinkManager { |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 168 | TestLinkService() { |
| 169 | eventDispatcher = new TestEventDispatcher(); |
| 170 | eventDispatcher.addSink(LinkEvent.class, listenerRegistry); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Iterable<Link> getLinks() { |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 175 | return ImmutableSet.of(link("a", 1, "b", 1), link("b", 1, "a", 1), |
| 176 | link("b", 2, "c", 1), link("c", 1, "b", 2), |
| 177 | link("c", 2, "d", 1), link("d", 1, "c", 2), |
| 178 | link("d", 2, "a", 2), link("a", 2, "d", 2), |
| 179 | link("e", 1, "f", 1), link("f", 1, "e", 1)); |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame^] | 182 | @Override |
| 183 | public Iterable<Link> getActiveLinks() { |
| 184 | return getLinks(); |
| 185 | } |
| 186 | |
tom | b86066b | 2014-09-15 09:52:24 -0700 | [diff] [blame] | 187 | void post(LinkEvent event) { |
| 188 | eventDispatcher.post(event); |
| 189 | } |
| 190 | } |
Yuta HIGUCHI | 4e9c37c | 2014-09-21 14:34:33 -0700 | [diff] [blame] | 191 | } |