Ray Milkey | ec253f8 | 2017-09-20 16:29:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.topology; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | import java.util.stream.Collectors; |
| 24 | |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.graph.ScalarWeight; |
| 28 | import org.onlab.graph.Weight; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DefaultDisjointPath; |
| 31 | import org.onosproject.net.DefaultLink; |
| 32 | import org.onosproject.net.DefaultPath; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.DisjointPath; |
| 35 | import org.onosproject.net.ElementId; |
| 36 | import org.onosproject.net.HostId; |
| 37 | import org.onosproject.net.Link; |
| 38 | import org.onosproject.net.NetTestTools; |
| 39 | import org.onosproject.net.Path; |
| 40 | import org.onosproject.net.host.HostServiceAdapter; |
| 41 | import org.onosproject.net.provider.ProviderId; |
| 42 | |
| 43 | import com.google.common.collect.ImmutableList; |
| 44 | import com.google.common.collect.ImmutableMap; |
| 45 | import com.google.common.collect.ImmutableSet; |
| 46 | |
| 47 | import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | import static org.hamcrest.Matchers.empty; |
| 49 | import static org.hamcrest.Matchers.hasSize; |
| 50 | import static org.hamcrest.Matchers.is; |
| 51 | import static org.hamcrest.Matchers.not; |
| 52 | import static org.hamcrest.Matchers.notNullValue; |
| 53 | import static org.hamcrest.Matchers.nullValue; |
| 54 | import static org.onosproject.net.NetTestTools.did; |
| 55 | import static org.onosproject.net.NetTestTools.hid; |
| 56 | |
| 57 | public class AbstractPathServiceTest { |
| 58 | |
| 59 | private TestPathService service; |
| 60 | private FakeTopoMgr topoMgr; |
| 61 | |
| 62 | private ConnectPoint cpA = NetTestTools.connectPoint("A", 1); |
| 63 | private ConnectPoint cpB = NetTestTools.connectPoint("B", 2); |
| 64 | private ConnectPoint cpC = NetTestTools.connectPoint("C", 3); |
| 65 | private ProviderId pid = ProviderId.NONE; |
| 66 | private Link link1 = DefaultLink.builder() |
| 67 | .providerId(pid) |
| 68 | .src(cpA) |
| 69 | .dst(cpB) |
| 70 | .type(Link.Type.DIRECT) |
| 71 | .state(Link.State.ACTIVE) |
| 72 | .build(); |
| 73 | private Link link2 = DefaultLink.builder() |
| 74 | .providerId(pid) |
| 75 | .src(cpB) |
| 76 | .dst(cpC) |
| 77 | .type(Link.Type.DIRECT) |
| 78 | .state(Link.State.ACTIVE) |
| 79 | .build(); |
| 80 | private List<Link> links1 = ImmutableList.of(link1, link2); |
| 81 | private Path path1 = new DefaultPath(pid, links1, new ScalarWeight(1.0)); |
| 82 | |
| 83 | |
| 84 | |
| 85 | private class TestPathService extends AbstractPathService { |
| 86 | Set<Path> paths = null; |
| 87 | Set<DisjointPath> disjointPaths = null; |
| 88 | |
| 89 | @Override |
| 90 | public Set<Path> getPaths(ElementId src, ElementId dst) { |
| 91 | return paths; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst) { |
| 96 | return disjointPaths; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, Map<Link, Object> riskProfile) { |
| 101 | return disjointPaths; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | class TestWeigher implements LinkWeigher { |
| 106 | @Override |
| 107 | public Weight weight(TopologyEdge edge) { |
| 108 | return new ScalarWeight(1.0); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Weight getInitialWeight() { |
| 113 | return new ScalarWeight(1.0); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public Weight getNonViableWeight() { |
| 118 | return new ScalarWeight(0.0); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Fake entity to give out paths. |
| 123 | private class FakeTopoMgr extends TopologyServiceAdapter { |
| 124 | |
| 125 | Set<Path> paths = new HashSet<>(); |
| 126 | Set<DisjointPath> disjointPaths = new HashSet<>(); |
| 127 | |
| 128 | void definePaths(Set<Path> paths) { |
| 129 | this.paths = paths; |
| 130 | this.disjointPaths = paths.stream() |
| 131 | .map(path -> |
| 132 | new DefaultDisjointPath(path.providerId(), |
| 133 | (DefaultPath) path)) |
| 134 | .collect(Collectors.toSet()); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Set<Path> getPaths(Topology topology, DeviceId src, |
| 139 | DeviceId dst) { |
| 140 | return paths; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Set<Path> getPaths(Topology topology, DeviceId src, |
| 145 | DeviceId dst, LinkWeigher weight) { |
| 146 | return paths; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 151 | DeviceId dst, |
| 152 | LinkWeigher weigher) { |
| 153 | return disjointPaths; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 158 | DeviceId dst, |
| 159 | LinkWeigher weigher, |
| 160 | Map<Link, Object> riskProfile) { |
| 161 | return disjointPaths; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Before |
| 166 | public void setUp() { |
| 167 | service = new TestPathService(); |
| 168 | topoMgr = new FakeTopoMgr(); |
| 169 | service.topologyService = topoMgr; |
| 170 | service.hostService = new HostServiceAdapter(); |
| 171 | } |
| 172 | |
| 173 | private void checkPathValues(Path path) { |
| 174 | assertThat(path, notNullValue()); |
| 175 | assertThat(path.links(), hasSize(2)); |
| 176 | assertThat(path.links().get(0).src(), is(cpA)); |
| 177 | assertThat(path.links().get(0).dst(), is(cpB)); |
| 178 | assertThat(path.links().get(1).src(), is(cpB)); |
| 179 | assertThat(path.links().get(1).dst(), is(cpC)); |
| 180 | } |
| 181 | |
| 182 | private void checkDisjointPaths(Set<DisjointPath> paths) { |
| 183 | assertThat(paths, notNullValue()); |
| 184 | assertThat(paths, hasSize(1)); |
| 185 | Path path = paths.iterator().next(); |
| 186 | checkPathValues(path); |
| 187 | } |
| 188 | |
| 189 | private void checkPaths(Collection<Path> paths) { |
| 190 | assertThat(paths, notNullValue()); |
| 191 | assertThat(paths, hasSize(1)); |
| 192 | Path path = paths.iterator().next(); |
| 193 | checkPathValues(path); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tests no paths being set up. |
| 198 | */ |
| 199 | @Test |
| 200 | public void testNoPaths() { |
| 201 | Set<Path> noPaths = service.getPaths(did("A"), did("B"), new TestWeigher()); |
| 202 | assertThat(noPaths, empty()); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Tests paths from a host. |
| 207 | */ |
| 208 | @Test |
| 209 | public void testSelfPaths() { |
| 210 | HostId host = hid("12:34:56:78:90:ab/1"); |
| 211 | Set<Path> paths = service.getPaths(host, host, new TestWeigher()); |
| 212 | assertThat(paths, hasSize(1)); |
| 213 | Path path = paths.iterator().next(); |
| 214 | assertThat(path, not(nullValue())); |
| 215 | assertThat(path.links(), hasSize(2)); |
| 216 | Link link1 = path.links().get(0); |
| 217 | Link link2 = path.links().get(1); |
| 218 | assertThat(link1.src(), is(link2.dst())); |
| 219 | assertThat(link2.src(), is(link1.dst())); |
| 220 | assertThat(link1.src().hostId(), is(host)); |
| 221 | assertThat(link2.dst().hostId(), is(host)); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Tests paths from a device to a device. |
| 226 | */ |
| 227 | @Test |
| 228 | public void testDevicePaths() { |
| 229 | topoMgr.definePaths(ImmutableSet.of(path1)); |
| 230 | Set<Path> pathsAC = service.getPaths(did("A"), did("C"), new TestWeigher()); |
| 231 | checkPaths(pathsAC); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Tests K Shortest Path computation. |
| 236 | */ |
| 237 | @Test |
| 238 | public void testKShortestPath() { |
| 239 | topoMgr.definePaths(ImmutableSet.of(path1)); |
| 240 | List<Path> paths = service.getKShortestPaths(did("A"), did("C"), new TestWeigher()) |
| 241 | .collect(Collectors.toList()); |
| 242 | checkPaths(paths); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Tests disjoint paths. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testDisjointPaths() { |
| 250 | topoMgr.definePaths(ImmutableSet.of(path1)); |
| 251 | Set<DisjointPath> paths = service.getDisjointPaths(did("A"), did("C"), new TestWeigher()); |
| 252 | checkDisjointPaths(paths); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Tests disjoint paths with a risk profile. |
| 257 | */ |
| 258 | @Test |
| 259 | public void testDisjointPathsWithRiskProfile() { |
| 260 | topoMgr.definePaths(ImmutableSet.of(path1)); |
| 261 | Map<Link, Object> riskProfile = ImmutableMap.of(); |
| 262 | |
| 263 | Set<DisjointPath> paths = |
| 264 | service.getDisjointPaths(did("A"), did("C"), new TestWeigher(), |
| 265 | riskProfile); |
| 266 | |
| 267 | checkDisjointPaths(paths); |
| 268 | } |
| 269 | } |