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; |
| 5 | import net.onrc.onos.core.util.PortNumber; |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 6 | |
| 7 | /** |
| 8 | * A mock class of Topology. |
| 9 | * This class should be used only by test codes. |
| 10 | */ |
| 11 | public 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) { |
Yuta HIGUCHI | 5f34c1d | 2014-07-15 15:15:08 -0700 | [diff] [blame] | 23 | SwitchImpl sw = new SwitchImpl(this, new Dpid(switchId)); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 24 | this.putSwitch(sw); |
| 25 | return sw; |
| 26 | } |
| 27 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 28 | 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 | |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 35 | public Link[] addBidirectionalLinks(Long srcDpid, Long srcPortNo, Long dstDpid, Long dstPortNo) { |
| 36 | Link[] links = new Link[2]; |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 37 | 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)); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 47 | |
| 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 HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 65 | addPort(sw1, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 66 | sw2 = (SwitchImpl) addSwitch(2L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 67 | addPort(sw2, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 68 | sw3 = (SwitchImpl) addSwitch(3L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 69 | addPort(sw3, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 70 | sw4 = (SwitchImpl) addSwitch(4L); |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 71 | addPort(sw4, LOCAL_PORT); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 72 | |
Yuta HIGUCHI | e2a4e17 | 2014-07-03 10:50:39 -0700 | [diff] [blame] | 73 | 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 |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 83 | |
| 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 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 | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 131 | HostImpl dev1 = new HostImpl(this, mac1); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 132 | dev1.addAttachmentPoint(port15); |
| 133 | dev1.setLastSeenTime(1L); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 134 | this.putHost(dev1); |
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 | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 137 | HostImpl dev3 = new HostImpl(this, mac3); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 138 | dev3.addAttachmentPoint(port35); |
| 139 | dev3.setLastSeenTime(1L); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 140 | this.putHost(dev3); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 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 HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 155 | 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)); |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 164 | } |
| 165 | } |