blob: 2bf8cb777452e7106bb9b265b978bb0541c9e81e [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;
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080020import org.onlab.packet.ChassisId;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DefaultDevice;
23import org.onosproject.net.DefaultLink;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.provider.ProviderId;
30import org.onosproject.net.topology.ClusterId;
31import org.onosproject.net.topology.DefaultGraphDescription;
Jon Halle7539632016-05-11 15:44:31 -070032import org.onosproject.net.topology.DefaultTopologyVertex;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.topology.GraphDescription;
34import org.onosproject.net.topology.LinkWeight;
35import org.onosproject.net.topology.TopologyCluster;
Jon Halle7539632016-05-11 15:44:31 -070036import org.onosproject.net.topology.TopologyVertex;
tomdc95b8a2014-09-17 08:07:26 -070037
38import java.util.Set;
39
40import static com.google.common.collect.ImmutableSet.of;
41import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import static org.onosproject.net.DeviceId.deviceId;
43import static org.onosproject.net.PortNumber.portNumber;
tomdc95b8a2014-09-17 08:07:26 -070044
45/**
46 * Test of the default topology implementation.
47 */
48public class DefaultTopologyTest {
49
tom7e02cda2014-09-18 12:05:46 -070050 public static final ProviderId PID = new ProviderId("of", "foo.bar");
tomdc95b8a2014-09-17 08:07:26 -070051
52 public static final DeviceId D1 = deviceId("of:1");
53 public static final DeviceId D2 = deviceId("of:2");
54 public static final DeviceId D3 = deviceId("of:3");
55 public static final DeviceId D4 = deviceId("of:4");
56 public static final DeviceId D5 = deviceId("of:5");
57
Jon Halle7539632016-05-11 15:44:31 -070058 public static final TopologyVertex V1 = new DefaultTopologyVertex(D1);
59 public static final TopologyVertex V5 = new DefaultTopologyVertex(D5);
60
tomdc95b8a2014-09-17 08:07:26 -070061 public static final PortNumber P1 = portNumber(1);
62 public static final PortNumber P2 = portNumber(2);
63
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080064 public static final LinkWeight WEIGHT = edge ->
65 edge.src().deviceId().equals(D4) || edge.dst().deviceId().equals(D4)
66 ? 2.0 : 1.0;
tomdc95b8a2014-09-17 08:07:26 -070067
68 private DefaultTopology dt;
69
Jon Halle7539632016-05-11 15:44:31 -070070 public static final ClusterId C0 = ClusterId.clusterId(0);
71 public static final ClusterId C1 = ClusterId.clusterId(1);
72
tomdc95b8a2014-09-17 08:07:26 -070073 @Before
74 public void setUp() {
75 long now = System.currentTimeMillis();
76 Set<Device> devices = of(device("1"), device("2"),
77 device("3"), device("4"),
78 device("5"));
79 Set<Link> links = of(link("1", 1, "2", 1), link("2", 1, "1", 1),
80 link("3", 2, "2", 2), link("2", 2, "3", 2),
81 link("1", 3, "4", 3), link("4", 3, "1", 3),
82 link("3", 4, "4", 4), link("4", 4, "3", 4));
83 GraphDescription graphDescription =
Sho SHIMIZU2581e182015-09-25 10:22:33 -070084 new DefaultGraphDescription(now, System.currentTimeMillis(), devices, links);
tomdc95b8a2014-09-17 08:07:26 -070085
86 dt = new DefaultTopology(PID, graphDescription);
87 assertEquals("incorrect supplier", PID, dt.providerId());
88 assertEquals("incorrect time", now, dt.time());
89 assertEquals("incorrect device count", 5, dt.deviceCount());
90 assertEquals("incorrect link count", 8, dt.linkCount());
91 assertEquals("incorrect cluster count", 2, dt.clusterCount());
Jon Halle7539632016-05-11 15:44:31 -070092 assertEquals("incorrect broadcast set size", 6, dt.broadcastSetSize(C0));
93 assertEquals("incorrect root node", V1, dt.getCluster(C0).root());
94 assertEquals("incorrect root node", V5, dt.getCluster(C1).root());
tomdc95b8a2014-09-17 08:07:26 -070095 }
96
97 @Test
98 public void pathRelated() {
99 Set<Path> paths = dt.getPaths(D1, D2);
100 assertEquals("incorrect path count", 1, paths.size());
101
102 paths = dt.getPaths(D1, D3);
103 assertEquals("incorrect path count", 2, paths.size());
104
105 paths = dt.getPaths(D1, D5);
106 assertTrue("no paths expected", paths.isEmpty());
107
108 paths = dt.getPaths(D1, D3, WEIGHT);
109 assertEquals("incorrect path count", 1, paths.size());
110 }
111
112 @Test
113 public void pointRelated() {
114 assertTrue("should be infrastructure point",
115 dt.isInfrastructure(new ConnectPoint(D1, P1)));
116 assertFalse("should not be infrastructure point",
117 dt.isInfrastructure(new ConnectPoint(D1, P2)));
118 }
119
120 @Test
121 public void clusterRelated() {
122 Set<TopologyCluster> clusters = dt.getClusters();
123 assertEquals("incorrect cluster count", 2, clusters.size());
124
125 TopologyCluster c = dt.getCluster(D1);
126 Set<DeviceId> devs = dt.getClusterDevices(c);
127 assertEquals("incorrect cluster device count", 4, devs.size());
128 assertTrue("cluster should contain D2", devs.contains(D2));
129 assertFalse("cluster should not contain D5", devs.contains(D5));
130 }
131
tombe988312014-09-19 18:38:47 -0700132 // Short-hand for creating a link.
133 public static Link link(String src, int sp, String dst, int dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800134 return DefaultLink.builder().providerId(PID)
135 .src(new ConnectPoint(did(src), portNumber(sp)))
136 .dst(new ConnectPoint(did(dst), portNumber(dp)))
137 .type(Link.Type.DIRECT)
138 .build();
tombe988312014-09-19 18:38:47 -0700139 }
140
141 // Crates a new device with the specified id
142 public static Device device(String id) {
143 return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
alshabib7911a052014-10-16 17:49:37 -0700144 "mfg", "1.0", "1.1", "1234", new ChassisId());
tombe988312014-09-19 18:38:47 -0700145 }
146
147 // Short-hand for producing a device id from a string
148 public static DeviceId did(String id) {
149 return deviceId("of:" + id);
150 }
151
tomdc95b8a2014-09-17 08:07:26 -0700152}