blob: c1a4715059e016ee18ae34f08a93b9a6a19379af [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.DeviceId;
22import org.onosproject.net.ElementId;
23import org.onosproject.net.Host;
24import org.onosproject.net.HostId;
25import org.onosproject.net.Path;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.host.HostServiceAdapter;
27import org.onosproject.net.provider.ProviderId;
28import org.onosproject.net.topology.LinkWeight;
29import org.onosproject.net.topology.PathService;
30import org.onosproject.net.topology.Topology;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.topology.TopologyServiceAdapter;
tomca90c462014-09-22 11:40:58 -070032
33import java.util.HashMap;
34import java.util.HashSet;
35import java.util.Map;
36import java.util.Set;
37
38import static org.junit.Assert.assertEquals;
39import static org.junit.Assert.assertTrue;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import static org.onosproject.net.NetTestTools.*;
tomca90c462014-09-22 11:40:58 -070041
42/**
43 * Test of the path selection subsystem.
44 */
45public class PathManagerTest {
46
47 private static final ProviderId PID = new ProviderId("of", "foo");
48
49 private PathManager mgr;
50 private PathService service;
51
52 private FakeTopoMgr fakeTopoMgr = new FakeTopoMgr();
53 private FakeHostMgr fakeHostMgr = new FakeHostMgr();
54
55 @Before
56 public void setUp() {
57 mgr = new PathManager();
58 service = mgr;
59 mgr.topologyService = fakeTopoMgr;
60 mgr.hostService = fakeHostMgr;
61 mgr.activate();
62 }
63
64 @After
65 public void tearDown() {
66 mgr.deactivate();
67 }
68
69 @Test
70 public void infraToInfra() {
71 DeviceId src = did("src");
72 DeviceId dst = did("dst");
73 fakeTopoMgr.paths.add(createPath("src", "middle", "dst"));
74 Set<Path> paths = service.getPaths(src, dst);
75 validatePaths(paths, 1, 2, src, dst);
76 }
77
78 @Test
79 public void infraToEdge() {
80 DeviceId src = did("src");
tom545708e2014-10-09 17:10:02 -070081 HostId dst = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070082 fakeTopoMgr.paths.add(createPath("src", "middle", "edge"));
tom545708e2014-10-09 17:10:02 -070083 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070084 Set<Path> paths = service.getPaths(src, dst);
85 validatePaths(paths, 1, 3, src, dst);
86 }
87
88 @Test
89 public void edgeToInfra() {
tom545708e2014-10-09 17:10:02 -070090 HostId src = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070091 DeviceId dst = did("dst");
92 fakeTopoMgr.paths.add(createPath("edge", "middle", "dst"));
tom545708e2014-10-09 17:10:02 -070093 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070094 Set<Path> paths = service.getPaths(src, dst);
95 validatePaths(paths, 1, 3, src, dst);
96 }
97
98 @Test
99 public void edgeToEdge() {
tom545708e2014-10-09 17:10:02 -0700100 HostId src = hid("12:34:56:78:90:ab/1");
101 HostId dst = hid("12:34:56:78:90:ef/1");
tomca90c462014-09-22 11:40:58 -0700102 fakeTopoMgr.paths.add(createPath("srcEdge", "middle", "dstEdge"));
tom545708e2014-10-09 17:10:02 -0700103 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "srcEdge"));
104 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ef/1", "dstEdge"));
tomca90c462014-09-22 11:40:58 -0700105 Set<Path> paths = service.getPaths(src, dst);
106 validatePaths(paths, 1, 4, src, dst);
107 }
108
109 @Test
110 public void edgeToEdgeDirect() {
tom545708e2014-10-09 17:10:02 -0700111 HostId src = hid("12:34:56:78:90:ab/1");
112 HostId dst = hid("12:34:56:78:90:ef/1");
113 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge"));
114 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ef/1", "edge"));
tomca90c462014-09-22 11:40:58 -0700115 Set<Path> paths = service.getPaths(src, dst);
116 validatePaths(paths, 1, 2, src, dst);
117 }
118
119 @Test
120 public void noEdge() {
tom545708e2014-10-09 17:10:02 -0700121 Set<Path> paths = service.getPaths(hid("12:34:56:78:90:ab/1"),
122 hid("12:34:56:78:90:ef/1"));
tomca90c462014-09-22 11:40:58 -0700123 assertTrue("there should be no paths", paths.isEmpty());
124 }
125
126 // Makes sure the set of paths meets basic expectations.
127 private void validatePaths(Set<Path> paths, int count, int length,
128 ElementId src, ElementId dst) {
129 assertEquals("incorrect path count", count, paths.size());
130 for (Path path : paths) {
131 assertEquals("incorrect length", length, path.links().size());
132 assertEquals("incorrect source", src, path.src().elementId());
133 assertEquals("incorrect destination", dst, path.dst().elementId());
134 }
135 }
136
137 // Fake entity to give out paths.
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700138 private class FakeTopoMgr extends TopologyServiceAdapter {
tomca90c462014-09-22 11:40:58 -0700139 Set<Path> paths = new HashSet<>();
140
141 @Override
142 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
143 return paths;
144 }
145
146 @Override
147 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
148 return paths;
149 }
tomca90c462014-09-22 11:40:58 -0700150 }
151
152 // Fake entity to give out hosts.
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700153 private class FakeHostMgr extends HostServiceAdapter {
tomca90c462014-09-22 11:40:58 -0700154 private Map<HostId, Host> hosts = new HashMap<>();
155
156 @Override
157 public Host getHost(HostId hostId) {
158 return hosts.get(hostId);
159 }
160 }
161
162}