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