blob: bb899d24215ab93ab69a8317ec39f1c170806d36 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.topology.impl;
tomca90c462014-09-22 11:40:58 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Claudine Chiu45920dd2016-07-28 19:19:46 +000021import org.onlab.junit.TestUtils;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.DeviceId;
23import org.onosproject.net.ElementId;
24import org.onosproject.net.Host;
25import org.onosproject.net.HostId;
26import org.onosproject.net.Path;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.host.HostServiceAdapter;
28import org.onosproject.net.provider.ProviderId;
Andrey Komarov2398d962016-09-26 15:11:23 +030029import org.onosproject.net.topology.LinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.topology.PathService;
31import org.onosproject.net.topology.Topology;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.topology.TopologyServiceAdapter;
tomca90c462014-09-22 11:40:58 -070033
34import java.util.HashMap;
35import java.util.HashSet;
36import java.util.Map;
37import java.util.Set;
Yuta HIGUCHIa684b6e2017-05-18 22:29:22 -070038import java.util.stream.Collectors;
tomca90c462014-09-22 11:40:58 -070039
40import static org.junit.Assert.assertEquals;
41import static org.junit.Assert.assertTrue;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import static org.onosproject.net.NetTestTools.*;
tomca90c462014-09-22 11:40:58 -070043
44/**
45 * Test of the path selection subsystem.
46 */
47public 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
Claudine Chiu45920dd2016-07-28 19:19:46 +000058 public void setUp() throws Exception {
tomca90c462014-09-22 11:40:58 -070059 mgr = new PathManager();
60 service = mgr;
Claudine Chiu45920dd2016-07-28 19:19:46 +000061 TestUtils.setField(mgr, "topologyService", fakeTopoMgr);
62 TestUtils.setField(mgr, "hostService", fakeHostMgr);
tomca90c462014-09-22 11:40:58 -070063 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);
Yuta HIGUCHIa684b6e2017-05-18 22:29:22 -070078
79 validatePaths(service.getKShortestPaths(src, dst)
80 .collect(Collectors.toSet()),
81 1, 2, src, dst);
tomca90c462014-09-22 11:40:58 -070082 }
83
84 @Test
85 public void infraToEdge() {
86 DeviceId src = did("src");
tom545708e2014-10-09 17:10:02 -070087 HostId dst = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070088 fakeTopoMgr.paths.add(createPath("src", "middle", "edge"));
tom545708e2014-10-09 17:10:02 -070089 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070090 Set<Path> paths = service.getPaths(src, dst);
91 validatePaths(paths, 1, 3, src, dst);
92 }
93
94 @Test
95 public void edgeToInfra() {
tom545708e2014-10-09 17:10:02 -070096 HostId src = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070097 DeviceId dst = did("dst");
98 fakeTopoMgr.paths.add(createPath("edge", "middle", "dst"));
tom545708e2014-10-09 17:10:02 -070099 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -0700100 Set<Path> paths = service.getPaths(src, dst);
101 validatePaths(paths, 1, 3, src, dst);
102 }
103
104 @Test
105 public void edgeToEdge() {
tom545708e2014-10-09 17:10:02 -0700106 HostId src = hid("12:34:56:78:90:ab/1");
107 HostId dst = hid("12:34:56:78:90:ef/1");
tomca90c462014-09-22 11:40:58 -0700108 fakeTopoMgr.paths.add(createPath("srcEdge", "middle", "dstEdge"));
tom545708e2014-10-09 17:10:02 -0700109 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "srcEdge"));
110 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ef/1", "dstEdge"));
tomca90c462014-09-22 11:40:58 -0700111 Set<Path> paths = service.getPaths(src, dst);
112 validatePaths(paths, 1, 4, src, dst);
Yuta HIGUCHIa684b6e2017-05-18 22:29:22 -0700113
114 validatePaths(service.getKShortestPaths(src, dst)
115 .collect(Collectors.toSet()),
116 1, 4, src, dst);
tomca90c462014-09-22 11:40:58 -0700117 }
118
119 @Test
120 public void edgeToEdgeDirect() {
tom545708e2014-10-09 17:10:02 -0700121 HostId src = hid("12:34:56:78:90:ab/1");
122 HostId dst = hid("12:34:56:78:90:ef/1");
123 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge"));
124 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ef/1", "edge"));
tomca90c462014-09-22 11:40:58 -0700125 Set<Path> paths = service.getPaths(src, dst);
126 validatePaths(paths, 1, 2, src, dst);
Yuta HIGUCHIa684b6e2017-05-18 22:29:22 -0700127
128 validatePaths(service.getKShortestPaths(src, dst)
129 .collect(Collectors.toSet()),
130 1, 2, src, dst);
tomca90c462014-09-22 11:40:58 -0700131 }
132
133 @Test
134 public void noEdge() {
tom545708e2014-10-09 17:10:02 -0700135 Set<Path> paths = service.getPaths(hid("12:34:56:78:90:ab/1"),
136 hid("12:34:56:78:90:ef/1"));
tomca90c462014-09-22 11:40:58 -0700137 assertTrue("there should be no paths", paths.isEmpty());
138 }
139
140 // Makes sure the set of paths meets basic expectations.
141 private void validatePaths(Set<Path> paths, int count, int length,
142 ElementId src, ElementId dst) {
143 assertEquals("incorrect path count", count, paths.size());
144 for (Path path : paths) {
145 assertEquals("incorrect length", length, path.links().size());
146 assertEquals("incorrect source", src, path.src().elementId());
147 assertEquals("incorrect destination", dst, path.dst().elementId());
148 }
149 }
150
151 // Fake entity to give out paths.
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700152 private class FakeTopoMgr extends TopologyServiceAdapter {
tomca90c462014-09-22 11:40:58 -0700153 Set<Path> paths = new HashSet<>();
154
155 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300156 public Set<Path> getPaths(Topology topology, DeviceId src,
157 DeviceId dst) {
tomca90c462014-09-22 11:40:58 -0700158 return paths;
159 }
160
161 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300162 public Set<Path> getPaths(Topology topology, DeviceId src,
163 DeviceId dst, LinkWeigher weight) {
tomca90c462014-09-22 11:40:58 -0700164 return paths;
165 }
tomca90c462014-09-22 11:40:58 -0700166 }
167
168 // Fake entity to give out hosts.
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700169 private class FakeHostMgr extends HostServiceAdapter {
tomca90c462014-09-22 11:40:58 -0700170 private Map<HostId, Host> hosts = new HashMap<>();
171
172 @Override
173 public Host getHost(HostId hostId) {
174 return hosts.get(hostId);
175 }
176 }
177
178}