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 | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.topology.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.onos.net.DeviceId; |
| 22 | import org.onlab.onos.net.ElementId; |
| 23 | import org.onlab.onos.net.Host; |
| 24 | import org.onlab.onos.net.HostId; |
| 25 | import org.onlab.onos.net.Path; |
| 26 | import org.onlab.onos.net.host.HostService; |
| 27 | import org.onlab.onos.net.host.HostServiceAdapter; |
| 28 | import org.onlab.onos.net.provider.ProviderId; |
| 29 | import org.onlab.onos.net.topology.LinkWeight; |
| 30 | import org.onlab.onos.net.topology.PathService; |
| 31 | import org.onlab.onos.net.topology.Topology; |
| 32 | import org.onlab.onos.net.topology.TopologyService; |
| 33 | import org.onlab.onos.net.topology.TopologyServiceAdapter; |
| 34 | |
| 35 | import java.util.HashMap; |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Map; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | import static org.junit.Assert.assertEquals; |
| 41 | import static org.junit.Assert.assertTrue; |
| 42 | import static org.onlab.onos.net.NetTestTools.*; |
| 43 | |
| 44 | /** |
| 45 | * Test of the path selection subsystem. |
| 46 | */ |
| 47 | public class PathManagerTest { |
| 48 | |
| 49 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 50 | |
| 51 | private PathManager mgr; |
| 52 | private PathService service; |
| 53 | |
| 54 | private FakeTopoMgr fakeTopoMgr = new FakeTopoMgr(); |
| 55 | private FakeHostMgr fakeHostMgr = new FakeHostMgr(); |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | mgr = new PathManager(); |
| 60 | service = mgr; |
| 61 | mgr.topologyService = fakeTopoMgr; |
| 62 | mgr.hostService = fakeHostMgr; |
| 63 | mgr.activate(); |
| 64 | } |
| 65 | |
| 66 | @After |
| 67 | public void tearDown() { |
| 68 | mgr.deactivate(); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void infraToInfra() { |
| 73 | DeviceId src = did("src"); |
| 74 | DeviceId dst = did("dst"); |
| 75 | fakeTopoMgr.paths.add(createPath("src", "middle", "dst")); |
| 76 | Set<Path> paths = service.getPaths(src, dst); |
| 77 | validatePaths(paths, 1, 2, src, dst); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void infraToEdge() { |
| 82 | DeviceId src = did("src"); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 83 | HostId dst = hid("12:34:56:78:90:ab/1"); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 84 | fakeTopoMgr.paths.add(createPath("src", "middle", "edge")); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 85 | fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ab/1", "edge")); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 86 | Set<Path> paths = service.getPaths(src, dst); |
| 87 | validatePaths(paths, 1, 3, src, dst); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void edgeToInfra() { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 92 | HostId src = hid("12:34:56:78:90:ab/1"); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 93 | DeviceId dst = did("dst"); |
| 94 | fakeTopoMgr.paths.add(createPath("edge", "middle", "dst")); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 95 | fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge")); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 96 | Set<Path> paths = service.getPaths(src, dst); |
| 97 | validatePaths(paths, 1, 3, src, dst); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void edgeToEdge() { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 102 | HostId src = hid("12:34:56:78:90:ab/1"); |
| 103 | HostId dst = hid("12:34:56:78:90:ef/1"); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 104 | fakeTopoMgr.paths.add(createPath("srcEdge", "middle", "dstEdge")); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 105 | fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "srcEdge")); |
| 106 | fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ef/1", "dstEdge")); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 107 | Set<Path> paths = service.getPaths(src, dst); |
| 108 | validatePaths(paths, 1, 4, src, dst); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void edgeToEdgeDirect() { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 113 | HostId src = hid("12:34:56:78:90:ab/1"); |
| 114 | HostId dst = hid("12:34:56:78:90:ef/1"); |
| 115 | fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge")); |
| 116 | fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ef/1", "edge")); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 117 | Set<Path> paths = service.getPaths(src, dst); |
| 118 | validatePaths(paths, 1, 2, src, dst); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void noEdge() { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 123 | Set<Path> paths = service.getPaths(hid("12:34:56:78:90:ab/1"), |
| 124 | hid("12:34:56:78:90:ef/1")); |
tom | ca90c46 | 2014-09-22 11:40:58 -0700 | [diff] [blame] | 125 | assertTrue("there should be no paths", paths.isEmpty()); |
| 126 | } |
| 127 | |
| 128 | // Makes sure the set of paths meets basic expectations. |
| 129 | private void validatePaths(Set<Path> paths, int count, int length, |
| 130 | ElementId src, ElementId dst) { |
| 131 | assertEquals("incorrect path count", count, paths.size()); |
| 132 | for (Path path : paths) { |
| 133 | assertEquals("incorrect length", length, path.links().size()); |
| 134 | assertEquals("incorrect source", src, path.src().elementId()); |
| 135 | assertEquals("incorrect destination", dst, path.dst().elementId()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Fake entity to give out paths. |
| 140 | private class FakeTopoMgr extends TopologyServiceAdapter implements TopologyService { |
| 141 | Set<Path> paths = new HashSet<>(); |
| 142 | |
| 143 | @Override |
| 144 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 145 | return paths; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) { |
| 150 | return paths; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Fake entity to give out hosts. |
| 155 | private class FakeHostMgr extends HostServiceAdapter implements HostService { |
| 156 | private Map<HostId, Host> hosts = new HashMap<>(); |
| 157 | |
| 158 | @Override |
| 159 | public Host getHost(HostId hostId) { |
| 160 | return hosts.get(hostId); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | } |