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. |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 11 | * This class should be used only by test code. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 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 | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 32 | PortNumber.uint16(portNumber.shortValue())); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 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 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 37 | public void addBidirectionalLinks(Long srcDpid, Long srcPortNo, |
| 38 | Long dstDpid, Long dstPortNo) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 39 | addBidirectionalLinks(srcDpid, srcPortNo, dstDpid, dstPortNo, null); |
| 40 | } |
| 41 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 42 | public void addBidirectionalLinks(Long srcDpid, Long srcPortNo, |
| 43 | Long dstDpid, Long dstPortNo, |
| 44 | Double capacity) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 45 | LinkEvent[] links = new LinkEvent[2]; |
| 46 | final SwitchPort src = new SwitchPort(srcDpid, srcPortNo); |
| 47 | final SwitchPort dst = new SwitchPort(dstDpid, dstPortNo); |
| 48 | links[0] = new LinkEvent(src, dst); |
| 49 | links[1] = new LinkEvent(dst, src); |
| 50 | if (capacity != null) { |
| 51 | links[0].setCapacity(capacity); |
| 52 | links[1].setCapacity(capacity); |
| 53 | } |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 54 | |
| 55 | putLink(links[0]); |
| 56 | putLink(links[1]); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 60 | * Creates a sample topology of 4 switches and 5 bidirectional links. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 61 | * <pre> |
| 62 | * [1] --- [2] |
| 63 | * | / | |
| 64 | * | / | |
| 65 | * [4] --- [3] |
| 66 | * </pre> |
| 67 | */ |
| 68 | public void createSampleTopology1() { |
| 69 | sw1 = (SwitchImpl) addSwitch(1L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 70 | addPort(sw1, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 71 | sw2 = (SwitchImpl) addSwitch(2L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 72 | addPort(sw2, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 73 | sw3 = (SwitchImpl) addSwitch(3L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 74 | addPort(sw3, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 75 | sw4 = (SwitchImpl) addSwitch(4L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 76 | addPort(sw4, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 77 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 78 | addPort(sw1, 12L); // sw1 -> sw2 |
| 79 | addPort(sw1, 14L); // sw1 -> sw4 |
| 80 | addPort(sw2, 21L); // sw2 -> sw1 |
| 81 | addPort(sw2, 23L); // sw2 -> sw3 |
| 82 | addPort(sw2, 24L); // sw2 -> sw4 |
| 83 | addPort(sw3, 32L); // sw3 -> sw2 |
| 84 | addPort(sw3, 34L); // sw3 -> sw4 |
| 85 | addPort(sw4, 41L); // sw4 -> sw1 |
| 86 | addPort(sw4, 42L); // sw4 -> sw2 |
| 87 | addPort(sw4, 43L); // sw4 -> sw3 |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 88 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 89 | addBidirectionalLinks(1L, 12L, 2L, 21L, 1000.0); |
| 90 | addBidirectionalLinks(2L, 23L, 3L, 32L, 1000.0); |
| 91 | addBidirectionalLinks(3L, 34L, 4L, 43L, 1000.0); |
| 92 | addBidirectionalLinks(4L, 41L, 1L, 14L, 1000.0); |
| 93 | addBidirectionalLinks(2L, 24L, 4L, 42L, 1000.0); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 97 | * Creates a sample topology of 4 switches and 5 bidirectional links. |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 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 HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 109 | addPort(sw1, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 110 | sw2 = (SwitchImpl) addSwitch(2L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 111 | addPort(sw2, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 112 | sw3 = (SwitchImpl) addSwitch(3L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 113 | addPort(sw3, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 114 | sw4 = (SwitchImpl) addSwitch(4L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 115 | addPort(sw4, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 116 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 117 | 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 |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 129 | |
| 130 | MACAddress mac1 = MACAddress.valueOf("00:44:33:22:11:00"); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 131 | HostEvent host1 = new HostEvent(mac1); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 132 | host1.addAttachmentPoint(port15.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 133 | host1.setLastSeenTime(1L); |
| 134 | this.putHost(host1); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 135 | |
| 136 | MACAddress mac3 = MACAddress.valueOf("00:11:22:33:44:55"); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 137 | HostEvent host3 = new HostEvent(mac3); |
Yuta HIGUCHI | cd14dda | 2014-07-24 09:57:22 -0700 | [diff] [blame] | 138 | host3.addAttachmentPoint(port35.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 139 | host3.setLastSeenTime(1L); |
| 140 | this.putHost(host3); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 141 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 142 | addBidirectionalLinks(1L, 12L, 2L, 21L, 1000.0); |
| 143 | addBidirectionalLinks(2L, 23L, 3L, 32L, 1000.0); |
| 144 | addBidirectionalLinks(3L, 34L, 4L, 43L, 1000.0); |
| 145 | addBidirectionalLinks(4L, 41L, 1L, 14L, 1000.0); |
| 146 | addBidirectionalLinks(2L, 24L, 4L, 42L, 1000.0); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 149 | public void removeLink(Long srcDpid, Long srcPortNo, Long dstDpid, |
| 150 | Long dstPortNo) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 151 | this.removeLink(new Dpid(srcDpid), |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 152 | PortNumber.uint16(srcPortNo.shortValue()), |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 153 | new Dpid(dstDpid), |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 154 | PortNumber.uint16(dstPortNo.shortValue())); |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public void removeLink(Dpid srcDpid, PortNumber srcPortNo, |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 158 | Dpid dstDpid, PortNumber dstPortNo) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 159 | this.removeLink(srcDpid, srcPortNo, dstDpid, dstPortNo, |
| 160 | TopologyElement.TYPE_PACKET_LAYER); |
| 161 | } |
| 162 | public void removeLink(Dpid srcDpid, PortNumber srcPortNo, |
| 163 | Dpid dstDpid, PortNumber dstPortNo, String type) { |
| 164 | super.removeLink(new LinkTuple(srcDpid, srcPortNo, dstDpid, dstPortNo), |
| 165 | type); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 166 | } |
| 167 | } |