TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
| 3 | import net.floodlightcontroller.util.MACAddress; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.LinkTuple; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.util.SwitchPort; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 8 | |
| 9 | /** |
| 10 | * A mock class of Topology. |
| 11 | * This class should be used only by test codes. |
| 12 | */ |
| 13 | public class MockTopology extends TopologyImpl { |
| 14 | // TODO this class doesn't seem like it should extend TopologyImpl. It |
| 15 | // isn't a Topology, it's more of a TopologyBuilder - methods to |
| 16 | // create an populate a fake topology that's not based on discovery |
| 17 | // data from the driver modules. |
| 18 | // We may well need a MockTopology, but that's not what this class is |
| 19 | // doing. |
| 20 | |
| 21 | public static final Long LOCAL_PORT = 0xFFFEL; |
| 22 | public SwitchImpl sw1, sw2, sw3, sw4; |
| 23 | |
| 24 | public Switch addSwitch(Long switchId) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 25 | SwitchEvent sw = new SwitchEvent(new Dpid(switchId)); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 26 | this.putSwitch(sw); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 27 | return this.getSwitch(sw.getDpid()); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 30 | public Port addPort(Switch sw, Long portNumber) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 31 | PortEvent port = new PortEvent(sw.getDpid(), |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 32 | new PortNumber(portNumber.shortValue())); |
| 33 | ((TopologyImpl) this).putPort(port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 34 | return this.getPort(port.getSwitchPort()); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 37 | public void addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
| 38 | addBidirectionalLinks(srcDpid, srcPortNo, dstDpid, dstPortNo, null); |
| 39 | } |
| 40 | |
| 41 | public void addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo, Double capacity) { |
| 42 | LinkEvent[] links = new LinkEvent[2]; |
| 43 | final SwitchPort src = new SwitchPort(srcDpid, srcPortNo); |
| 44 | final SwitchPort dst = new SwitchPort(dstDpid, dstPortNo); |
| 45 | links[0] = new LinkEvent(src, dst); |
| 46 | links[1] = new LinkEvent(dst, src); |
| 47 | if (capacity != null) { |
| 48 | links[0].setCapacity(capacity); |
| 49 | links[1].setCapacity(capacity); |
| 50 | } |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 51 | |
| 52 | putLink(links[0]); |
| 53 | putLink(links[1]); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * create sample topology of 4 switches and 5 bidirectional links. |
| 58 | * <pre> |
| 59 | * [1] --- [2] |
| 60 | * | / | |
| 61 | * | / | |
| 62 | * [4] --- [3] |
| 63 | * </pre> |
| 64 | */ |
| 65 | public void createSampleTopology1() { |
| 66 | sw1 = (SwitchImpl) addSwitch(1L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 67 | addPort(sw1, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 68 | sw2 = (SwitchImpl) addSwitch(2L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 69 | addPort(sw2, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 70 | sw3 = (SwitchImpl) addSwitch(3L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 71 | addPort(sw3, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 72 | sw4 = (SwitchImpl) addSwitch(4L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 73 | addPort(sw4, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 74 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 75 | addPort(sw1, 12L); // sw1 -> sw2 |
| 76 | addPort(sw1, 14L); // sw1 -> sw4 |
| 77 | addPort(sw2, 21L); // sw2 -> sw1 |
| 78 | addPort(sw2, 23L); // sw2 -> sw3 |
| 79 | addPort(sw2, 24L); // sw2 -> sw4 |
| 80 | addPort(sw3, 32L); // sw3 -> sw2 |
| 81 | addPort(sw3, 34L); // sw3 -> sw4 |
| 82 | addPort(sw4, 41L); // sw4 -> sw1 |
| 83 | addPort(sw4, 42L); // sw4 -> sw2 |
| 84 | addPort(sw4, 43L); // sw4 -> sw3 |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 85 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 86 | addBidirectionalLinks(1L, 12L, 2L, 21L, 1000.0); |
| 87 | addBidirectionalLinks(2L, 23L, 3L, 32L, 1000.0); |
| 88 | addBidirectionalLinks(3L, 34L, 4L, 43L, 1000.0); |
| 89 | addBidirectionalLinks(4L, 41L, 1L, 14L, 1000.0); |
| 90 | addBidirectionalLinks(2L, 24L, 4L, 42L, 1000.0); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * create sample topology of 4 switches and 5 bidirectional links. |
| 95 | * <pre> |
| 96 | * |
| 97 | * |
| 98 | * [H1]-[1] --- [2] |
| 99 | * | / | |
| 100 | * | / | |
| 101 | * [4] --- [3]-[H3] |
| 102 | * </pre> |
| 103 | */ |
| 104 | public void createSampleTopology2() { |
| 105 | sw1 = (SwitchImpl) addSwitch(1L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 106 | addPort(sw1, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 107 | sw2 = (SwitchImpl) addSwitch(2L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 108 | addPort(sw2, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 109 | sw3 = (SwitchImpl) addSwitch(3L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 110 | addPort(sw3, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 111 | sw4 = (SwitchImpl) addSwitch(4L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 112 | addPort(sw4, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 113 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 114 | Port port12 = addPort(sw1, 12L); // sw1 -> sw2 |
| 115 | Port port14 = addPort(sw1, 14L); // sw1 -> sw4 |
| 116 | Port port15 = addPort(sw1, 15L); // sw1 -> h1 |
| 117 | Port port21 = addPort(sw2, 21L); // sw2 -> sw1 |
| 118 | Port port23 = addPort(sw2, 23L); // sw2 -> sw3 |
| 119 | Port port24 = addPort(sw2, 24L); // sw2 -> sw4 |
| 120 | Port port32 = addPort(sw3, 32L); // sw3 -> sw2 |
| 121 | Port port34 = addPort(sw3, 34L); // sw3 -> sw4 |
| 122 | Port port35 = addPort(sw3, 35L); // sw3 -> h3 |
| 123 | Port port41 = addPort(sw4, 41L); // sw4 -> sw1 |
| 124 | Port port42 = addPort(sw4, 42L); // sw4 -> sw2 |
| 125 | Port port43 = addPort(sw4, 43L); // sw4 -> sw3 |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 126 | |
| 127 | MACAddress mac1 = MACAddress.valueOf("00:44:33:22:11:00"); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 128 | HostEvent host1 = new HostEvent(mac1); |
| 129 | host1.addAttachmentPoint(port15.asSwitchPort()); |
| 130 | host1.setLastSeenTime(1L); |
| 131 | this.putHost(host1); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 132 | |
| 133 | MACAddress mac3 = MACAddress.valueOf("00:11:22:33:44:55"); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 134 | HostEvent host3 = new HostEvent(mac3); |
| 135 | host3.addAttachmentPoint(port35.asSwitchPort()); |
| 136 | host3.setLastSeenTime(1L); |
| 137 | this.putHost(host3); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 138 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 139 | addBidirectionalLinks(1L, 12L, 2L, 21L, 1000.0); |
| 140 | addBidirectionalLinks(2L, 23L, 3L, 32L, 1000.0); |
| 141 | addBidirectionalLinks(3L, 34L, 4L, 43L, 1000.0); |
| 142 | addBidirectionalLinks(4L, 41L, 1L, 14L, 1000.0); |
| 143 | addBidirectionalLinks(2L, 24L, 4L, 42L, 1000.0); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | public void removeLink(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 147 | this.removeLink(new Dpid(srcDpid), |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 148 | new PortNumber(srcPortNo.shortValue()), |
| 149 | new Dpid(dstDpid), |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 150 | new PortNumber(dstPortNo.shortValue())); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public void removeLink(Dpid srcDpid, PortNumber srcPortNo, |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 154 | Dpid dstDpid, PortNumber dstPortNo) { |
| 155 | this.removeLink(srcDpid, srcPortNo, dstDpid, dstPortNo, |
| 156 | TopologyElement.TYPE_PACKET_LAYER); |
| 157 | } |
| 158 | public void removeLink(Dpid srcDpid, PortNumber srcPortNo, |
| 159 | Dpid dstDpid, PortNumber dstPortNo, String type) { |
| 160 | super.removeLink(new LinkTuple(srcDpid, srcPortNo, dstDpid, dstPortNo), |
| 161 | type); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 162 | } |
| 163 | } |