blob: d285504abeff238ce7416b03c86d2851f00ed052 [file] [log] [blame]
TeruU5d2c9392014-06-09 20:02:02 -07001package net.onrc.onos.core.topology;
2
3import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07004import net.onrc.onos.core.util.Dpid;
5import net.onrc.onos.core.util.PortNumber;
TeruU5d2c9392014-06-09 20:02:02 -07006
7/**
8 * A mock class of Topology.
9 * This class should be used only by test codes.
10 */
11public class MockTopology extends TopologyImpl {
12 // TODO this class doesn't seem like it should extend TopologyImpl. It
13 // isn't a Topology, it's more of a TopologyBuilder - methods to
14 // create an populate a fake topology that's not based on discovery
15 // data from the driver modules.
16 // We may well need a MockTopology, but that's not what this class is
17 // doing.
18
19 public static final Long LOCAL_PORT = 0xFFFEL;
20 public SwitchImpl sw1, sw2, sw3, sw4;
21
22 public Switch addSwitch(Long switchId) {
23 SwitchImpl sw = new SwitchImpl(this, switchId);
24 this.putSwitch(sw);
25 return sw;
26 }
27
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070028 public Port addPort(Switch sw, Long portNumber) {
29 PortImpl port = new PortImpl(this, sw.getDpid(),
30 new PortNumber(portNumber.shortValue()));
31 ((TopologyImpl) this).putPort(port);
32 return port;
33 }
34
TeruU5d2c9392014-06-09 20:02:02 -070035 public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
36 Link[] links = new Link[2];
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070037 final Dpid srcDpidObj = new Dpid(srcDpid);
38 final Dpid dstDpidObj = new Dpid(dstDpid);
39 final PortNumber srcPortNum = new PortNumber(srcPortNo.shortValue());
40 final PortNumber dstPortNum = new PortNumber(dstPortNo.shortValue());
41 links[0] = new LinkImpl(this,
42 getPort(srcDpidObj, srcPortNum),
43 getPort(dstDpidObj, dstPortNum));
44 links[1] = new LinkImpl(this,
45 getPort(dstDpidObj, dstPortNum),
46 getPort(srcDpidObj, srcPortNum));
TeruU5d2c9392014-06-09 20:02:02 -070047
48 putLink(links[0]);
49 putLink(links[1]);
50
51 return links;
52 }
53
54 /**
55 * create sample topology of 4 switches and 5 bidirectional links.
56 * <pre>
57 * [1] --- [2]
58 * | / |
59 * | / |
60 * [4] --- [3]
61 * </pre>
62 */
63 public void createSampleTopology1() {
64 sw1 = (SwitchImpl) addSwitch(1L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070065 addPort(sw1, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -070066 sw2 = (SwitchImpl) addSwitch(2L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070067 addPort(sw2, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -070068 sw3 = (SwitchImpl) addSwitch(3L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070069 addPort(sw3, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -070070 sw4 = (SwitchImpl) addSwitch(4L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070071 addPort(sw4, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -070072
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070073 addPort(sw1, 12L); // sw1 -> sw2
74 addPort(sw1, 14L); // sw1 -> sw4
75 addPort(sw2, 21L); // sw2 -> sw1
76 addPort(sw2, 23L); // sw2 -> sw3
77 addPort(sw2, 24L); // sw2 -> sw4
78 addPort(sw3, 32L); // sw3 -> sw2
79 addPort(sw3, 34L); // sw3 -> sw4
80 addPort(sw4, 41L); // sw4 -> sw1
81 addPort(sw4, 42L); // sw4 -> sw2
82 addPort(sw4, 43L); // sw4 -> sw3
TeruU5d2c9392014-06-09 20:02:02 -070083
84 addBidirectionalLinks(1L, 12L, 2L, 21L);
85 addBidirectionalLinks(2L, 23L, 3L, 32L);
86 addBidirectionalLinks(3L, 34L, 4L, 43L);
87 addBidirectionalLinks(4L, 41L, 1L, 14L);
88 addBidirectionalLinks(2L, 24L, 4L, 42L);
89
90 // set capacity of all links to 1000Mbps
91 for (Link link : getLinks()) {
92 ((LinkImpl) link).setCapacity(1000.0);
93 }
94 }
95
96 /**
97 * create sample topology of 4 switches and 5 bidirectional links.
98 * <pre>
99 *
100 *
101 * [H1]-[1] --- [2]
102 * | / |
103 * | / |
104 * [4] --- [3]-[H3]
105 * </pre>
106 */
107 public void createSampleTopology2() {
108 sw1 = (SwitchImpl) addSwitch(1L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700109 addPort(sw1, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -0700110 sw2 = (SwitchImpl) addSwitch(2L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700111 addPort(sw2, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -0700112 sw3 = (SwitchImpl) addSwitch(3L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700113 addPort(sw3, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -0700114 sw4 = (SwitchImpl) addSwitch(4L);
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700115 addPort(sw4, LOCAL_PORT);
TeruU5d2c9392014-06-09 20:02:02 -0700116
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -0700117 Port port12 = addPort(sw1, 12L); // sw1 -> sw2
118 Port port14 = addPort(sw1, 14L); // sw1 -> sw4
119 Port port15 = addPort(sw1, 15L); // sw1 -> h1
120 Port port21 = addPort(sw2, 21L); // sw2 -> sw1
121 Port port23 = addPort(sw2, 23L); // sw2 -> sw3
122 Port port24 = addPort(sw2, 24L); // sw2 -> sw4
123 Port port32 = addPort(sw3, 32L); // sw3 -> sw2
124 Port port34 = addPort(sw3, 34L); // sw3 -> sw4
125 Port port35 = addPort(sw3, 35L); // sw3 -> h3
126 Port port41 = addPort(sw4, 41L); // sw4 -> sw1
127 Port port42 = addPort(sw4, 42L); // sw4 -> sw2
128 Port port43 = addPort(sw4, 43L); // sw4 -> sw3
TeruU5d2c9392014-06-09 20:02:02 -0700129
130 MACAddress mac1 = MACAddress.valueOf("00:44:33:22:11:00");
131 DeviceImpl dev1 = new DeviceImpl(this, mac1);
132 dev1.addAttachmentPoint(port15);
133 dev1.setLastSeenTime(1L);
134 this.putDevice(dev1);
135
136 MACAddress mac3 = MACAddress.valueOf("00:11:22:33:44:55");
137 DeviceImpl dev3 = new DeviceImpl(this, mac3);
138 dev3.addAttachmentPoint(port35);
139 dev3.setLastSeenTime(1L);
140 this.putDevice(dev3);
141
142 addBidirectionalLinks(1L, 12L, 2L, 21L);
143 addBidirectionalLinks(2L, 23L, 3L, 32L);
144 addBidirectionalLinks(3L, 34L, 4L, 43L);
145 addBidirectionalLinks(4L, 41L, 1L, 14L);
146 addBidirectionalLinks(2L, 24L, 4L, 42L);
147
148 // set capacity of all links to 1000Mbps
149 for (Link link : getLinks()) {
150 ((LinkImpl) link).setCapacity(1000.0);
151 }
152 }
153
154 public void removeLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700155 removeLink(getLink(new Dpid(srcDpid),
156 new PortNumber(srcPortNo.shortValue()),
157 new Dpid(dstDpid),
158 new PortNumber(dstPortNo.shortValue())));
159 }
160
161 public void removeLink(Dpid srcDpid, PortNumber srcPortNo,
162 Dpid dstDpid, PortNumber dstPortNo) {
163 super.removeLink(getLink(srcDpid, srcPortNo, dstDpid, dstPortNo));
TeruU5d2c9392014-06-09 20:02:02 -0700164 }
165}