blob: 48403d54c87bfa7b6b37b30befe4033581aa0058 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070016package org.onosproject.common;
tomdc95b8a2014-09-17 08:07:26 -070017
18import org.junit.Before;
19import org.junit.Test;
Andrey Komarov2398d962016-09-26 15:11:23 +030020import org.onlab.graph.DefaultEdgeWeigher;
21import org.onlab.graph.ScalarWeight;
22import org.onlab.graph.Weight;
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080023import org.onlab.packet.ChassisId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DefaultDevice;
26import org.onosproject.net.DefaultLink;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link;
30import org.onosproject.net.Path;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.provider.ProviderId;
33import org.onosproject.net.topology.ClusterId;
34import org.onosproject.net.topology.DefaultGraphDescription;
Jon Halle7539632016-05-11 15:44:31 -070035import org.onosproject.net.topology.DefaultTopologyVertex;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.topology.GraphDescription;
Andrey Komarov2398d962016-09-26 15:11:23 +030037import org.onosproject.net.topology.LinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.topology.TopologyCluster;
Andrey Komarov2398d962016-09-26 15:11:23 +030039import org.onosproject.net.topology.TopologyEdge;
Jon Halle7539632016-05-11 15:44:31 -070040import org.onosproject.net.topology.TopologyVertex;
tomdc95b8a2014-09-17 08:07:26 -070041
42import java.util.Set;
43
44import static com.google.common.collect.ImmutableSet.of;
45import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.net.PortNumber.portNumber;
tomdc95b8a2014-09-17 08:07:26 -070048
49/**
50 * Test of the default topology implementation.
51 */
52public class DefaultTopologyTest {
53
tom7e02cda2014-09-18 12:05:46 -070054 public static final ProviderId PID = new ProviderId("of", "foo.bar");
tomdc95b8a2014-09-17 08:07:26 -070055
56 public static final DeviceId D1 = deviceId("of:1");
57 public static final DeviceId D2 = deviceId("of:2");
58 public static final DeviceId D3 = deviceId("of:3");
59 public static final DeviceId D4 = deviceId("of:4");
60 public static final DeviceId D5 = deviceId("of:5");
61
Jon Halle7539632016-05-11 15:44:31 -070062 public static final TopologyVertex V1 = new DefaultTopologyVertex(D1);
63 public static final TopologyVertex V5 = new DefaultTopologyVertex(D5);
64
tomdc95b8a2014-09-17 08:07:26 -070065 public static final PortNumber P1 = portNumber(1);
66 public static final PortNumber P2 = portNumber(2);
67
Andrey Komarov2398d962016-09-26 15:11:23 +030068 public static final class TestLinkWeigher
69 extends DefaultEdgeWeigher<TopologyVertex, TopologyEdge>
70 implements LinkWeigher {
71 @Override
72 public Weight weight(TopologyEdge edge) {
73 double value = edge.src().deviceId().equals(D4) ||
74 edge.dst().deviceId().equals(D4)
75 ? 2.0 : HOP_WEIGHT_VALUE;
76 return new ScalarWeight(value);
77 }
78 }
79 public static final LinkWeigher WEIGHER = new TestLinkWeigher();
80
tomdc95b8a2014-09-17 08:07:26 -070081
82 private DefaultTopology dt;
83
Jon Halle7539632016-05-11 15:44:31 -070084 public static final ClusterId C0 = ClusterId.clusterId(0);
85 public static final ClusterId C1 = ClusterId.clusterId(1);
86
tomdc95b8a2014-09-17 08:07:26 -070087 @Before
88 public void setUp() {
89 long now = System.currentTimeMillis();
90 Set<Device> devices = of(device("1"), device("2"),
91 device("3"), device("4"),
92 device("5"));
93 Set<Link> links = of(link("1", 1, "2", 1), link("2", 1, "1", 1),
94 link("3", 2, "2", 2), link("2", 2, "3", 2),
95 link("1", 3, "4", 3), link("4", 3, "1", 3),
96 link("3", 4, "4", 4), link("4", 4, "3", 4));
97 GraphDescription graphDescription =
Sho SHIMIZU2581e182015-09-25 10:22:33 -070098 new DefaultGraphDescription(now, System.currentTimeMillis(), devices, links);
tomdc95b8a2014-09-17 08:07:26 -070099
100 dt = new DefaultTopology(PID, graphDescription);
101 assertEquals("incorrect supplier", PID, dt.providerId());
102 assertEquals("incorrect time", now, dt.time());
103 assertEquals("incorrect device count", 5, dt.deviceCount());
104 assertEquals("incorrect link count", 8, dt.linkCount());
105 assertEquals("incorrect cluster count", 2, dt.clusterCount());
Jon Halle7539632016-05-11 15:44:31 -0700106 assertEquals("incorrect broadcast set size", 6, dt.broadcastSetSize(C0));
107 assertEquals("incorrect root node", V1, dt.getCluster(C0).root());
108 assertEquals("incorrect root node", V5, dt.getCluster(C1).root());
tomdc95b8a2014-09-17 08:07:26 -0700109 }
110
111 @Test
112 public void pathRelated() {
113 Set<Path> paths = dt.getPaths(D1, D2);
114 assertEquals("incorrect path count", 1, paths.size());
115
116 paths = dt.getPaths(D1, D3);
117 assertEquals("incorrect path count", 2, paths.size());
118
119 paths = dt.getPaths(D1, D5);
120 assertTrue("no paths expected", paths.isEmpty());
121
Andrey Komarov2398d962016-09-26 15:11:23 +0300122 paths = dt.getPaths(D1, D3, WEIGHER);
tomdc95b8a2014-09-17 08:07:26 -0700123 assertEquals("incorrect path count", 1, paths.size());
124 }
125
126 @Test
127 public void pointRelated() {
128 assertTrue("should be infrastructure point",
129 dt.isInfrastructure(new ConnectPoint(D1, P1)));
130 assertFalse("should not be infrastructure point",
131 dt.isInfrastructure(new ConnectPoint(D1, P2)));
132 }
133
134 @Test
135 public void clusterRelated() {
136 Set<TopologyCluster> clusters = dt.getClusters();
137 assertEquals("incorrect cluster count", 2, clusters.size());
138
139 TopologyCluster c = dt.getCluster(D1);
140 Set<DeviceId> devs = dt.getClusterDevices(c);
141 assertEquals("incorrect cluster device count", 4, devs.size());
142 assertTrue("cluster should contain D2", devs.contains(D2));
143 assertFalse("cluster should not contain D5", devs.contains(D5));
144 }
145
tombe988312014-09-19 18:38:47 -0700146 // Short-hand for creating a link.
147 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800148 return DefaultLink.builder().providerId(PID)
149 .src(new ConnectPoint(did(src), portNumber(sp)))
150 .dst(new ConnectPoint(did(dst), portNumber(dp)))
151 .type(Link.Type.DIRECT)
152 .build();
tombe988312014-09-19 18:38:47 -0700153 }
154
155 // Crates a new device with the specified id
156 public static Device device(String id) {
157 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -0700158 "mfg", "1.0", "1.1", "1234", new ChassisId());
tombe988312014-09-19 18:38:47 -0700159 }
160
161 // Short-hand for producing a device id from a string
162 public static DeviceId did(String id) {
163 return deviceId("of:" + id);
164 }
165
tomdc95b8a2014-09-17 08:07:26 -0700166}