blob: fc46c487f3f439e002130f2938243f07fa31ad2d [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 */
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;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070022import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ElementId;
24import org.onosproject.net.Host;
25import org.onosproject.net.HostId;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070026import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Path;
28import org.onosproject.net.host.HostService;
29import org.onosproject.net.host.HostServiceAdapter;
30import org.onosproject.net.provider.ProviderId;
31import org.onosproject.net.topology.LinkWeight;
32import org.onosproject.net.topology.PathService;
33import org.onosproject.net.topology.Topology;
34import org.onosproject.net.topology.TopologyService;
35import org.onosproject.net.topology.TopologyServiceAdapter;
tomca90c462014-09-22 11:40:58 -070036
37import java.util.HashMap;
38import java.util.HashSet;
39import java.util.Map;
40import java.util.Set;
41
42import static org.junit.Assert.assertEquals;
43import static org.junit.Assert.assertTrue;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import static org.onosproject.net.NetTestTools.*;
tomca90c462014-09-22 11:40:58 -070045
46/**
47 * Test of the path selection subsystem.
48 */
49public 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");
tom545708e2014-10-09 17:10:02 -070085 HostId dst = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070086 fakeTopoMgr.paths.add(createPath("src", "middle", "edge"));
tom545708e2014-10-09 17:10:02 -070087 fakeHostMgr.hosts.put(dst, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070088 Set<Path> paths = service.getPaths(src, dst);
89 validatePaths(paths, 1, 3, src, dst);
90 }
91
92 @Test
93 public void edgeToInfra() {
tom545708e2014-10-09 17:10:02 -070094 HostId src = hid("12:34:56:78:90:ab/1");
tomca90c462014-09-22 11:40:58 -070095 DeviceId dst = did("dst");
96 fakeTopoMgr.paths.add(createPath("edge", "middle", "dst"));
tom545708e2014-10-09 17:10:02 -070097 fakeHostMgr.hosts.put(src, host("12:34:56:78:90:ab/1", "edge"));
tomca90c462014-09-22 11:40:58 -070098 Set<Path> paths = service.getPaths(src, dst);
99 validatePaths(paths, 1, 3, src, dst);
100 }
101
102 @Test
103 public void edgeToEdge() {
tom545708e2014-10-09 17:10:02 -0700104 HostId src = hid("12:34:56:78:90:ab/1");
105 HostId dst = hid("12:34:56:78:90:ef/1");
tomca90c462014-09-22 11:40:58 -0700106 fakeTopoMgr.paths.add(createPath("srcEdge", "middle", "dstEdge"));
tom545708e2014-10-09 17:10:02 -0700107 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"));
tomca90c462014-09-22 11:40:58 -0700109 Set<Path> paths = service.getPaths(src, dst);
110 validatePaths(paths, 1, 4, src, dst);
111 }
112
113 @Test
114 public void edgeToEdgeDirect() {
tom545708e2014-10-09 17:10:02 -0700115 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"));
tomca90c462014-09-22 11:40:58 -0700119 Set<Path> paths = service.getPaths(src, dst);
120 validatePaths(paths, 1, 2, src, dst);
121 }
122
123 @Test
124 public void noEdge() {
tom545708e2014-10-09 17:10:02 -0700125 Set<Path> paths = service.getPaths(hid("12:34:56:78:90:ab/1"),
126 hid("12:34:56:78:90:ef/1"));
tomca90c462014-09-22 11:40:58 -0700127 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 Cheerla2ec191f2015-07-09 12:34:54 -0700154
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 }
tomca90c462014-09-22 11:40:58 -0700176 }
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}