blob: 6779c5e28b464fab773bd9f3871a539a3b6d2742 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tomca90c462014-09-22 11:40:58 -070016package org.onlab.onos.net.topology.impl;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.onos.net.DeviceId;
22import org.onlab.onos.net.ElementId;
23import org.onlab.onos.net.Host;
24import org.onlab.onos.net.HostId;
25import org.onlab.onos.net.Path;
26import org.onlab.onos.net.host.HostService;
27import org.onlab.onos.net.host.HostServiceAdapter;
28import org.onlab.onos.net.provider.ProviderId;
29import org.onlab.onos.net.topology.LinkWeight;
30import org.onlab.onos.net.topology.PathService;
31import org.onlab.onos.net.topology.Topology;
32import org.onlab.onos.net.topology.TopologyService;
33import org.onlab.onos.net.topology.TopologyServiceAdapter;
34
35import java.util.HashMap;
36import java.util.HashSet;
37import java.util.Map;
38import java.util.Set;
39
40import static org.junit.Assert.assertEquals;
41import static org.junit.Assert.assertTrue;
42import static org.onlab.onos.net.NetTestTools.*;
43
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
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");
tom545708e2014-10-09 17:10:02 -070083 HostId dst = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070084 fakeTopoMgr.paths.add(createPath("src", "middle", "edge"));
tom545708e2014-10-09 17:10:02 -070085 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070086 Set<Path> paths = service.getPaths(src, dst);
87 validatePaths(paths, 1, 3, src, dst);
88 }
89
90 @Test
91 public void edgeToInfra() {
tom545708e2014-10-09 17:10:02 -070092 HostId src = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070093 DeviceId dst = did("dst");
94 fakeTopoMgr.paths.add(createPath("edge", "middle", "dst"));
tom545708e2014-10-09 17:10:02 -070095 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070096 Set<Path> paths = service.getPaths(src, dst);
97 validatePaths(paths, 1, 3, src, dst);
98 }
99
100 @Test
101 public void edgeToEdge() {
tom545708e2014-10-09 17:10:02 -0700102 HostId src = hid("12:34:56:78:90:ab/1");
103 HostId dst = hid("12:34:56:78:90:ef/1");
tomca90c462014-09-22 11:40:58 -0700104 fakeTopoMgr.paths.add(createPath("srcEdge", "middle", "dstEdge"));
tom545708e2014-10-09 17:10:02 -0700105 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"));
tomca90c462014-09-22 11:40:58 -0700107 Set<Path> paths = service.getPaths(src, dst);
108 validatePaths(paths, 1, 4, src, dst);
109 }
110
111 @Test
112 public void edgeToEdgeDirect() {
tom545708e2014-10-09 17:10:02 -0700113 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"));
tomca90c462014-09-22 11:40:58 -0700117 Set<Path> paths = service.getPaths(src, dst);
118 validatePaths(paths, 1, 2, src, dst);
119 }
120
121 @Test
122 public void noEdge() {
tom545708e2014-10-09 17:10:02 -0700123 Set<Path> paths = service.getPaths(hid("12:34:56:78:90:ab/1"),
124 hid("12:34:56:78:90:ef/1"));
tomca90c462014-09-22 11:40:58 -0700125 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}