f3cd28dfb5ff4539c82bfc45bcc2074ef5a521a8
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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  */
16 package org.onosproject.net.topology.impl;
17
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.onosproject.event.Event;
22 import org.onosproject.common.event.impl.TestEventDispatcher;
23 import org.onosproject.net.ConnectPoint;
24 import org.onosproject.net.Device;
25 import org.onosproject.net.Link;
26 import org.onosproject.net.Path;
27 import org.onosproject.net.provider.AbstractProvider;
28 import org.onosproject.net.provider.ProviderId;
29 import org.onosproject.net.topology.DefaultGraphDescription;
30 import org.onosproject.net.topology.GraphDescription;
31 import org.onosproject.net.topology.LinkWeight;
32 import org.onosproject.net.topology.Topology;
33 import org.onosproject.net.topology.TopologyCluster;
34 import org.onosproject.net.topology.TopologyEvent;
35 import org.onosproject.net.topology.TopologyGraph;
36 import org.onosproject.net.topology.TopologyListener;
37 import org.onosproject.net.topology.TopologyProvider;
38 import org.onosproject.net.topology.TopologyProviderRegistry;
39 import org.onosproject.net.topology.TopologyProviderService;
40 import org.onosproject.net.topology.TopologyService;
41 import org.onosproject.store.trivial.SimpleTopologyStore;
42
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Set;
46
47 import static com.google.common.collect.ImmutableSet.of;
48 import static org.junit.Assert.*;
49 import static org.onosproject.net.NetTestTools.*;
50 import static org.onosproject.net.PortNumber.portNumber;
51 import static org.onosproject.net.topology.ClusterId.clusterId;
52 import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
53
54 /**
55  * Test of the topology subsystem.
56  */
57 public class TopologyManagerTest {
58
59     private static final ProviderId PID = new ProviderId("of", "foo");
60
61     private TopologyManager mgr;
62
63     protected TopologyService service;
64     protected TopologyProviderRegistry registry;
65     protected TopologyProviderService providerService;
66     protected TestProvider provider;
67     protected TestListener listener = new TestListener();
68
69     @Before
70     public void setUp() {
71         mgr = new TopologyManager();
72         service = mgr;
73         registry = mgr;
74
75         mgr.store = new SimpleTopologyStore();
76         injectEventDispatcher(mgr, new TestEventDispatcher());
77         mgr.activate();
78
79         service.addListener(listener);
80
81         provider = new TestProvider();
82         providerService = registry.register(provider);
83
84         assertTrue("provider should be registered",
85                    registry.getProviders().contains(provider.id()));
86     }
87
88     @After
89     public void tearDown() {
90         mgr.deactivate();
91         service.removeListener(listener);
92     }
93
94     @Test
95     public void basics() {
96         Topology topology = service.currentTopology();
97         assertNull("no topo expected", topology);
98         submitTopologyGraph();
99         validateEvents(TOPOLOGY_CHANGED);
100         topology = service.currentTopology();
101         assertTrue("should be latest", service.isLatest(topology));
102
103         submitTopologyGraph();
104         validateEvents(TOPOLOGY_CHANGED);
105         assertFalse("should be latest", service.isLatest(topology));
106     }
107
108     private void submitTopologyGraph() {
109         Set<Device> devices = of(device("a"), device("b"),
110                                  device("c"), device("d"),
111                                  device("e"), device("f"));
112         Set<Link> links = of(link("a", 1, "b", 1), link("b", 1, "a", 1),
113                              link("b", 2, "c", 1), link("c", 1, "b", 2),
114                              link("c", 2, "d", 1), link("d", 1, "c", 2),
115                              link("d", 2, "a", 2), link("a", 2, "d", 2),
116                              link("e", 1, "f", 1), link("f", 1, "e", 1));
117         GraphDescription data = new DefaultGraphDescription(4321L, devices, links);
118         providerService.topologyChanged(data, null);
119     }
120
121     @Test
122     public void clusters() {
123         submitTopologyGraph();
124         Topology topology = service.currentTopology();
125         assertNotNull("topo expected", topology);
126         assertEquals("wrong cluster count", 2, topology.clusterCount());
127         assertEquals("wrong device count", 6, topology.deviceCount());
128         assertEquals("wrong link count", 10, topology.linkCount());
129
130         assertEquals("wrong cluster count", 2, service.getClusters(topology).size());
131
132         TopologyCluster cluster = service.getCluster(topology, clusterId(0));
133         assertEquals("wrong device count", 4, cluster.deviceCount());
134         assertEquals("wrong device count", 4, service.getClusterDevices(topology, cluster).size());
135         assertEquals("wrong link count", 8, cluster.linkCount());
136         assertEquals("wrong link count", 8, service.getClusterLinks(topology, cluster).size());
137     }
138
139     @Test
140     public void structure() {
141         submitTopologyGraph();
142         Topology topology = service.currentTopology();
143
144         assertTrue("should be infrastructure point",
145                    service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(1))));
146         assertFalse("should not be infrastructure point",
147                     service.isInfrastructure(topology, new ConnectPoint(did("a"), portNumber(3))));
148
149         assertTrue("should be broadcast point",
150                    service.isBroadcastPoint(topology, new ConnectPoint(did("a"), portNumber(3))));
151     }
152
153     @Test
154     public void graph() {
155         submitTopologyGraph();
156         Topology topology = service.currentTopology();
157         TopologyGraph graph = service.getGraph(topology);
158         assertEquals("wrong vertex count", 6, graph.getVertexes().size());
159         assertEquals("wrong edge count", 10, graph.getEdges().size());
160     }
161
162     @Test
163     public void precomputedPath() {
164         submitTopologyGraph();
165         Topology topology = service.currentTopology();
166         Set<Path> paths = service.getPaths(topology, did("a"), did("c"));
167         assertEquals("wrong path count", 2, paths.size());
168         Path path = paths.iterator().next();
169         assertEquals("wrong path length", 2, path.links().size());
170         assertEquals("wrong path cost", 2, path.cost(), 0.01);
171     }
172
173     @Test
174     public void onDemandPath() {
175         submitTopologyGraph();
176         Topology topology = service.currentTopology();
177         LinkWeight weight = edge -> 3.3;
178
179         Set<Path> paths = service.getPaths(topology, did("a"), did("c"), weight);
180         assertEquals("wrong path count", 2, paths.size());
181         Path path = paths.iterator().next();
182         assertEquals("wrong path length", 2, path.links().size());
183         assertEquals("wrong path cost", 6.6, path.cost(), 0.01);
184     }
185
186     protected void validateEvents(Enum... types) {
187         int i = 0;
188         assertEquals("wrong events received", types.length, listener.events.size());
189         for (Event event : listener.events) {
190             assertEquals("incorrect event type", types[i], event.type());
191             i++;
192         }
193         listener.events.clear();
194     }
195
196     private class TestProvider extends AbstractProvider implements TopologyProvider {
197         public TestProvider() {
198             super(PID);
199         }
200
201         @Override
202         public void triggerRecompute() {
203         }
204     }
205
206     private static class TestListener implements TopologyListener {
207         final List<TopologyEvent> events = new ArrayList<>();
208
209         @Override
210         public void event(TopologyEvent event) {
211             events.add(event);
212         }
213     }
214
215 }