Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 20 | import org.onlab.packet.ChassisId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DefaultDevice; |
| 23 | import org.onosproject.net.DefaultLink; |
| 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.Path; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.provider.ProviderId; |
| 30 | import org.onosproject.net.topology.ClusterId; |
| 31 | import org.onosproject.net.topology.DefaultGraphDescription; |
| 32 | import org.onosproject.net.topology.GraphDescription; |
| 33 | import org.onosproject.net.topology.LinkWeight; |
| 34 | import org.onosproject.net.topology.TopologyCluster; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.Set; |
| 37 | |
| 38 | import static com.google.common.collect.ImmutableSet.of; |
| 39 | import static org.junit.Assert.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import static org.onosproject.net.DeviceId.deviceId; |
| 41 | import static org.onosproject.net.PortNumber.portNumber; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Test of the default topology implementation. |
| 45 | */ |
| 46 | public class DefaultTopologyTest { |
| 47 | |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 48 | public static final ProviderId PID = new ProviderId("of", "foo.bar"); |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 49 | |
| 50 | public static final DeviceId D1 = deviceId("of:1"); |
| 51 | public static final DeviceId D2 = deviceId("of:2"); |
| 52 | public static final DeviceId D3 = deviceId("of:3"); |
| 53 | public static final DeviceId D4 = deviceId("of:4"); |
| 54 | public static final DeviceId D5 = deviceId("of:5"); |
| 55 | |
| 56 | public static final PortNumber P1 = portNumber(1); |
| 57 | public static final PortNumber P2 = portNumber(2); |
| 58 | |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 59 | public static final LinkWeight WEIGHT = edge -> |
| 60 | edge.src().deviceId().equals(D4) || edge.dst().deviceId().equals(D4) |
| 61 | ? 2.0 : 1.0; |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 62 | |
| 63 | private DefaultTopology dt; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | long now = System.currentTimeMillis(); |
| 68 | Set<Device> devices = of(device("1"), device("2"), |
| 69 | device("3"), device("4"), |
| 70 | device("5")); |
| 71 | Set<Link> links = of(link("1", 1, "2", 1), link("2", 1, "1", 1), |
| 72 | link("3", 2, "2", 2), link("2", 2, "3", 2), |
| 73 | link("1", 3, "4", 3), link("4", 3, "1", 3), |
| 74 | link("3", 4, "4", 4), link("4", 4, "3", 4)); |
| 75 | GraphDescription graphDescription = |
| 76 | new DefaultGraphDescription(now, devices, links); |
| 77 | |
| 78 | dt = new DefaultTopology(PID, graphDescription); |
| 79 | assertEquals("incorrect supplier", PID, dt.providerId()); |
| 80 | assertEquals("incorrect time", now, dt.time()); |
| 81 | assertEquals("incorrect device count", 5, dt.deviceCount()); |
| 82 | assertEquals("incorrect link count", 8, dt.linkCount()); |
| 83 | assertEquals("incorrect cluster count", 2, dt.clusterCount()); |
| 84 | assertEquals("incorrect broadcast set size", 6, |
| 85 | dt.broadcastSetSize(ClusterId.clusterId(0))); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void pathRelated() { |
| 90 | Set<Path> paths = dt.getPaths(D1, D2); |
| 91 | assertEquals("incorrect path count", 1, paths.size()); |
| 92 | |
| 93 | paths = dt.getPaths(D1, D3); |
| 94 | assertEquals("incorrect path count", 2, paths.size()); |
| 95 | |
| 96 | paths = dt.getPaths(D1, D5); |
| 97 | assertTrue("no paths expected", paths.isEmpty()); |
| 98 | |
| 99 | paths = dt.getPaths(D1, D3, WEIGHT); |
| 100 | assertEquals("incorrect path count", 1, paths.size()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void pointRelated() { |
| 105 | assertTrue("should be infrastructure point", |
| 106 | dt.isInfrastructure(new ConnectPoint(D1, P1))); |
| 107 | assertFalse("should not be infrastructure point", |
| 108 | dt.isInfrastructure(new ConnectPoint(D1, P2))); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void clusterRelated() { |
| 113 | Set<TopologyCluster> clusters = dt.getClusters(); |
| 114 | assertEquals("incorrect cluster count", 2, clusters.size()); |
| 115 | |
| 116 | TopologyCluster c = dt.getCluster(D1); |
| 117 | Set<DeviceId> devs = dt.getClusterDevices(c); |
| 118 | assertEquals("incorrect cluster device count", 4, devs.size()); |
| 119 | assertTrue("cluster should contain D2", devs.contains(D2)); |
| 120 | assertFalse("cluster should not contain D5", devs.contains(D5)); |
| 121 | } |
| 122 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 123 | // Short-hand for creating a link. |
| 124 | public static Link link(String src, int sp, String dst, int dp) { |
| 125 | return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)), |
| 126 | new ConnectPoint(did(dst), portNumber(dp)), |
| 127 | Link.Type.DIRECT); |
| 128 | } |
| 129 | |
| 130 | // Crates a new device with the specified id |
| 131 | public static Device device(String id) { |
| 132 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 133 | "mfg", "1.0", "1.1", "1234", new ChassisId()); |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Short-hand for producing a device id from a string |
| 137 | public static DeviceId did(String id) { |
| 138 | return deviceId("of:" + id); |
| 139 | } |
| 140 | |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 141 | } |