blob: 80f309cfe1cb4a06f30d0b0622c683b5ea50145c [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
28 public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
29 Link[] links = new Link[2];
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070030 final Dpid srcDpidObj = new Dpid(srcDpid);
31 final Dpid dstDpidObj = new Dpid(dstDpid);
32 final PortNumber srcPortNum = new PortNumber(srcPortNo.shortValue());
33 final PortNumber dstPortNum = new PortNumber(dstPortNo.shortValue());
34 links[0] = new LinkImpl(this,
35 getPort(srcDpidObj, srcPortNum),
36 getPort(dstDpidObj, dstPortNum));
37 links[1] = new LinkImpl(this,
38 getPort(dstDpidObj, dstPortNum),
39 getPort(srcDpidObj, srcPortNum));
TeruU5d2c9392014-06-09 20:02:02 -070040
41 putLink(links[0]);
42 putLink(links[1]);
43
44 return links;
45 }
46
47 /**
48 * create sample topology of 4 switches and 5 bidirectional links.
49 * <pre>
50 * [1] --- [2]
51 * | / |
52 * | / |
53 * [4] --- [3]
54 * </pre>
55 */
56 public void createSampleTopology1() {
57 sw1 = (SwitchImpl) addSwitch(1L);
58 sw1.addPort(LOCAL_PORT);
59 sw2 = (SwitchImpl) addSwitch(2L);
60 sw2.addPort(LOCAL_PORT);
61 sw3 = (SwitchImpl) addSwitch(3L);
62 sw3.addPort(LOCAL_PORT);
63 sw4 = (SwitchImpl) addSwitch(4L);
64 sw4.addPort(LOCAL_PORT);
65
66 sw1.addPort(12L); // sw1 -> sw2
67 sw1.addPort(14L); // sw1 -> sw4
68 sw2.addPort(21L); // sw2 -> sw1
69 sw2.addPort(23L); // sw2 -> sw3
70 sw2.addPort(24L); // sw2 -> sw4
71 sw3.addPort(32L); // sw3 -> sw2
72 sw3.addPort(34L); // sw3 -> sw4
73 sw4.addPort(41L); // sw4 -> sw1
74 sw4.addPort(42L); // sw4 -> sw2
75 sw4.addPort(43L); // sw4 -> sw3
76
77 addBidirectionalLinks(1L, 12L, 2L, 21L);
78 addBidirectionalLinks(2L, 23L, 3L, 32L);
79 addBidirectionalLinks(3L, 34L, 4L, 43L);
80 addBidirectionalLinks(4L, 41L, 1L, 14L);
81 addBidirectionalLinks(2L, 24L, 4L, 42L);
82
83 // set capacity of all links to 1000Mbps
84 for (Link link : getLinks()) {
85 ((LinkImpl) link).setCapacity(1000.0);
86 }
87 }
88
89 /**
90 * create sample topology of 4 switches and 5 bidirectional links.
91 * <pre>
92 *
93 *
94 * [H1]-[1] --- [2]
95 * | / |
96 * | / |
97 * [4] --- [3]-[H3]
98 * </pre>
99 */
100 public void createSampleTopology2() {
101 sw1 = (SwitchImpl) addSwitch(1L);
102 sw1.addPort(LOCAL_PORT);
103 sw2 = (SwitchImpl) addSwitch(2L);
104 sw2.addPort(LOCAL_PORT);
105 sw3 = (SwitchImpl) addSwitch(3L);
106 sw3.addPort(LOCAL_PORT);
107 sw4 = (SwitchImpl) addSwitch(4L);
108 sw4.addPort(LOCAL_PORT);
109
110 Port port12 = sw1.addPort(12L); // sw1 -> sw2
111 Port port14 = sw1.addPort(14L); // sw1 -> sw4
112 Port port15 = sw1.addPort(15L); // sw1 -> h1
113 Port port21 = sw2.addPort(21L); // sw2 -> sw1
114 Port port23 = sw2.addPort(23L); // sw2 -> sw3
115 Port port24 = sw2.addPort(24L); // sw2 -> sw4
116 Port port32 = sw3.addPort(32L); // sw3 -> sw2
117 Port port34 = sw3.addPort(34L); // sw3 -> sw4
118 Port port35 = sw3.addPort(35L); // sw3 -> h3
119 Port port41 = sw4.addPort(41L); // sw4 -> sw1
120 Port port42 = sw4.addPort(42L); // sw4 -> sw2
121 Port port43 = sw4.addPort(43L); // sw4 -> sw3
122
123 MACAddress mac1 = MACAddress.valueOf("00:44:33:22:11:00");
124 DeviceImpl dev1 = new DeviceImpl(this, mac1);
125 dev1.addAttachmentPoint(port15);
126 dev1.setLastSeenTime(1L);
127 this.putDevice(dev1);
128
129 MACAddress mac3 = MACAddress.valueOf("00:11:22:33:44:55");
130 DeviceImpl dev3 = new DeviceImpl(this, mac3);
131 dev3.addAttachmentPoint(port35);
132 dev3.setLastSeenTime(1L);
133 this.putDevice(dev3);
134
135 addBidirectionalLinks(1L, 12L, 2L, 21L);
136 addBidirectionalLinks(2L, 23L, 3L, 32L);
137 addBidirectionalLinks(3L, 34L, 4L, 43L);
138 addBidirectionalLinks(4L, 41L, 1L, 14L);
139 addBidirectionalLinks(2L, 24L, 4L, 42L);
140
141 // set capacity of all links to 1000Mbps
142 for (Link link : getLinks()) {
143 ((LinkImpl) link).setCapacity(1000.0);
144 }
145 }
146
147 public void removeLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700148 removeLink(getLink(new Dpid(srcDpid),
149 new PortNumber(srcPortNo.shortValue()),
150 new Dpid(dstDpid),
151 new PortNumber(dstPortNo.shortValue())));
152 }
153
154 public void removeLink(Dpid srcDpid, PortNumber srcPortNo,
155 Dpid dstDpid, PortNumber dstPortNo) {
156 super.removeLink(getLink(srcDpid, srcPortNo, dstDpid, dstPortNo));
TeruU5d2c9392014-06-09 20:02:02 -0700157 }
158}