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