Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 16 | package org.onosproject.common; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 20 | import org.onlab.graph.DefaultEdgeWeigher; |
| 21 | import org.onlab.graph.ScalarWeight; |
| 22 | import org.onlab.graph.Weight; |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 23 | import org.onlab.packet.ChassisId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultDevice; |
| 26 | import org.onosproject.net.DefaultLink; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.Path; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.provider.ProviderId; |
| 33 | import org.onosproject.net.topology.ClusterId; |
| 34 | import org.onosproject.net.topology.DefaultGraphDescription; |
Jon Hall | e753963 | 2016-05-11 15:44:31 -0700 | [diff] [blame] | 35 | import org.onosproject.net.topology.DefaultTopologyVertex; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.topology.GraphDescription; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 37 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.topology.TopologyCluster; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 39 | import org.onosproject.net.topology.TopologyEdge; |
Jon Hall | e753963 | 2016-05-11 15:44:31 -0700 | [diff] [blame] | 40 | import org.onosproject.net.topology.TopologyVertex; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.Set; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 43 | import static com.google.common.collect.ImmutableSet.of; |
| 44 | import static org.junit.Assert.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import static org.onosproject.net.DeviceId.deviceId; |
| 46 | import static org.onosproject.net.PortNumber.portNumber; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Test of the default topology implementation. |
| 50 | */ |
| 51 | public class DefaultTopologyTest { |
| 52 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 53 | public static final ProviderId PID = new ProviderId("of", "foo.bar"); |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 54 | |
| 55 | public static final DeviceId D1 = deviceId("of:1"); |
| 56 | public static final DeviceId D2 = deviceId("of:2"); |
| 57 | public static final DeviceId D3 = deviceId("of:3"); |
| 58 | public static final DeviceId D4 = deviceId("of:4"); |
| 59 | public static final DeviceId D5 = deviceId("of:5"); |
| 60 | |
Jon Hall | e753963 | 2016-05-11 15:44:31 -0700 | [diff] [blame] | 61 | public static final TopologyVertex V1 = new DefaultTopologyVertex(D1); |
| 62 | public static final TopologyVertex V5 = new DefaultTopologyVertex(D5); |
| 63 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 64 | public static final PortNumber P1 = portNumber(1); |
| 65 | public static final PortNumber P2 = portNumber(2); |
| 66 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 67 | public static final class TestLinkWeigher |
| 68 | extends DefaultEdgeWeigher<TopologyVertex, TopologyEdge> |
| 69 | implements LinkWeigher { |
| 70 | @Override |
| 71 | public Weight weight(TopologyEdge edge) { |
| 72 | double value = edge.src().deviceId().equals(D4) || |
| 73 | edge.dst().deviceId().equals(D4) |
| 74 | ? 2.0 : HOP_WEIGHT_VALUE; |
| 75 | return new ScalarWeight(value); |
| 76 | } |
| 77 | } |
| 78 | public static final LinkWeigher WEIGHER = new TestLinkWeigher(); |
| 79 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 80 | |
| 81 | private DefaultTopology dt; |
| 82 | |
Jon Hall | e753963 | 2016-05-11 15:44:31 -0700 | [diff] [blame] | 83 | public static final ClusterId C0 = ClusterId.clusterId(0); |
| 84 | public static final ClusterId C1 = ClusterId.clusterId(1); |
| 85 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 86 | @Before |
| 87 | public void setUp() { |
| 88 | long now = System.currentTimeMillis(); |
| 89 | Set<Device> devices = of(device("1"), device("2"), |
| 90 | device("3"), device("4"), |
| 91 | device("5")); |
| 92 | Set<Link> links = of(link("1", 1, "2", 1), link("2", 1, "1", 1), |
| 93 | link("3", 2, "2", 2), link("2", 2, "3", 2), |
| 94 | link("1", 3, "4", 3), link("4", 3, "1", 3), |
| 95 | link("3", 4, "4", 4), link("4", 4, "3", 4)); |
| 96 | GraphDescription graphDescription = |
Sho SHIMIZU | 2581e18 | 2015-09-25 10:22:33 -0700 | [diff] [blame] | 97 | new DefaultGraphDescription(now, System.currentTimeMillis(), devices, links); |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 98 | |
| 99 | dt = new DefaultTopology(PID, graphDescription); |
| 100 | assertEquals("incorrect supplier", PID, dt.providerId()); |
| 101 | assertEquals("incorrect time", now, dt.time()); |
| 102 | assertEquals("incorrect device count", 5, dt.deviceCount()); |
| 103 | assertEquals("incorrect link count", 8, dt.linkCount()); |
| 104 | assertEquals("incorrect cluster count", 2, dt.clusterCount()); |
Jon Hall | e753963 | 2016-05-11 15:44:31 -0700 | [diff] [blame] | 105 | assertEquals("incorrect broadcast set size", 6, dt.broadcastSetSize(C0)); |
| 106 | assertEquals("incorrect root node", V1, dt.getCluster(C0).root()); |
| 107 | assertEquals("incorrect root node", V5, dt.getCluster(C1).root()); |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void pathRelated() { |
| 112 | Set<Path> paths = dt.getPaths(D1, D2); |
| 113 | assertEquals("incorrect path count", 1, paths.size()); |
| 114 | |
| 115 | paths = dt.getPaths(D1, D3); |
| 116 | assertEquals("incorrect path count", 2, paths.size()); |
| 117 | |
| 118 | paths = dt.getPaths(D1, D5); |
| 119 | assertTrue("no paths expected", paths.isEmpty()); |
| 120 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 121 | paths = dt.getPaths(D1, D3, WEIGHER); |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 122 | assertEquals("incorrect path count", 1, paths.size()); |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame^] | 123 | |
| 124 | paths = dt.getKShortestPaths(D1, D2, 42); |
| 125 | assertEquals("incorrect path count", 2, paths.size()); |
| 126 | |
| 127 | assertEquals("incorrect path count", 2, dt.getKShortestPaths(D1, D2).limit(42).count()); |
| 128 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void pointRelated() { |
| 133 | assertTrue("should be infrastructure point", |
| 134 | dt.isInfrastructure(new ConnectPoint(D1, P1))); |
| 135 | assertFalse("should not be infrastructure point", |
| 136 | dt.isInfrastructure(new ConnectPoint(D1, P2))); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void clusterRelated() { |
| 141 | Set<TopologyCluster> clusters = dt.getClusters(); |
| 142 | assertEquals("incorrect cluster count", 2, clusters.size()); |
| 143 | |
| 144 | TopologyCluster c = dt.getCluster(D1); |
| 145 | Set<DeviceId> devs = dt.getClusterDevices(c); |
| 146 | assertEquals("incorrect cluster device count", 4, devs.size()); |
| 147 | assertTrue("cluster should contain D2", devs.contains(D2)); |
| 148 | assertFalse("cluster should not contain D5", devs.contains(D5)); |
| 149 | } |
| 150 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 151 | // Short-hand for creating a link. |
| 152 | public static Link link(String src, int sp, String dst, int dp) { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 153 | return DefaultLink.builder().providerId(PID) |
| 154 | .src(new ConnectPoint(did(src), portNumber(sp))) |
| 155 | .dst(new ConnectPoint(did(dst), portNumber(dp))) |
| 156 | .type(Link.Type.DIRECT) |
| 157 | .build(); |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Crates a new device with the specified id |
| 161 | public static Device device(String id) { |
| 162 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 163 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Short-hand for producing a device id from a string |
| 167 | public static DeviceId did(String id) { |
| 168 | return deviceId("of:" + id); |
| 169 | } |
| 170 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 171 | } |