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