Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 1 | package org.onosproject.flowanalyzer; |
| 2 | |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 3 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 4 | import org.onosproject.net.DefaultLink; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 5 | import org.onosproject.net.DeviceId; |
| 6 | import org.onosproject.net.ElementId; |
| 7 | import org.onosproject.net.HostId; |
| 8 | import org.onosproject.net.Link; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 9 | import org.onosproject.net.PortNumber; |
| 10 | import org.onosproject.net.link.LinkServiceAdapter; |
| 11 | import org.onosproject.net.topology.TopologyEdge; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 12 | import org.onosproject.net.topology.TopologyVertex; |
| 13 | |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 14 | import java.util.ArrayList; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 15 | import java.util.HashSet; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 16 | import java.util.List; |
| 17 | import java.util.Set; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 18 | |
| 19 | import static org.onosproject.net.Link.State.ACTIVE; |
| 20 | |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 21 | /** |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 22 | * Test fixture for the flow analyzer. |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 23 | */ |
| 24 | public class MockLinkService extends LinkServiceAdapter { |
| 25 | DefaultMutableTopologyGraph createdGraph = new DefaultMutableTopologyGraph(new HashSet<>(), new HashSet<>()); |
| 26 | List<Link> links = new ArrayList<>(); |
| 27 | |
| 28 | @Override |
| 29 | public int getLinkCount() { |
| 30 | return links.size(); |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public Iterable<Link> getLinks() { |
| 35 | return links; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public Set<Link> getDeviceLinks(DeviceId deviceId) { |
| 40 | Set<Link> egress = getDeviceEgressLinks(deviceId); |
| 41 | egress.addAll(getDeviceIngressLinks(deviceId)); |
| 42 | return egress; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
| 47 | Set<Link> setL = new HashSet<>(); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 48 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 49 | if (l.src().elementId() instanceof DeviceId && l.src().deviceId().equals(deviceId)) { |
| 50 | setL.add(l); |
| 51 | } |
| 52 | } |
| 53 | return setL; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
| 58 | Set<Link> setL = new HashSet<>(); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 59 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 60 | if (l.dst().elementId() instanceof DeviceId && l.dst().deviceId().equals(deviceId)) { |
| 61 | setL.add(l); |
| 62 | } |
| 63 | } |
| 64 | return setL; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | @Override |
| 69 | public Set<Link> getEgressLinks(ConnectPoint pt) { |
| 70 | Set<Link> setL = new HashSet<>(); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 71 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 72 | if (l.src().equals(pt)) { |
| 73 | setL.add(l); |
| 74 | } |
| 75 | } |
| 76 | return setL; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Set<Link> getIngressLinks(ConnectPoint pt) { |
| 81 | Set<Link> setL = new HashSet<>(); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 82 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 83 | if (l.dst().equals(pt)) { |
| 84 | setL.add(l); |
| 85 | } |
| 86 | } |
| 87 | return setL; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Set<Link> getLinks(ConnectPoint pt) { |
| 92 | Set<Link> setL = new HashSet<>(); |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 93 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 94 | if (l.src().equals(pt) || l.dst().equals(pt)) { |
| 95 | setL.add(l); |
| 96 | } |
| 97 | } |
| 98 | return setL; |
| 99 | } |
| 100 | |
| 101 | public void addLink(String device, long port, String device2, long port2) { |
| 102 | ElementId d1; |
| 103 | if (device.charAt(0) == 'H') { |
| 104 | device = device.substring(1, device.length()); |
| 105 | d1 = HostId.hostId(device); |
| 106 | } else { |
| 107 | d1 = DeviceId.deviceId(device); |
| 108 | } |
| 109 | |
| 110 | ElementId d2; |
| 111 | if (device2.charAt(0) == 'H') { |
| 112 | d2 = HostId.hostId(device2.substring(1, device2.length())); |
| 113 | } else { |
| 114 | d2 = DeviceId.deviceId(device2); |
| 115 | } |
| 116 | |
| 117 | ConnectPoint src = new ConnectPoint(d1, PortNumber.portNumber(port)); |
| 118 | ConnectPoint dst = new ConnectPoint(d2, PortNumber.portNumber(port2)); |
| 119 | Link curLink; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 120 | curLink = DefaultLink.builder().src(src).dst(dst).state(ACTIVE).build(); |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 121 | links.add(curLink); |
| 122 | if (d1 instanceof DeviceId && d2 instanceof DeviceId) { |
| 123 | TopologyVertex v1 = () -> (DeviceId) d1, v2 = () -> (DeviceId) d2; |
| 124 | createdGraph.addVertex(v1); |
| 125 | createdGraph.addVertex(v2); |
| 126 | createdGraph.addEdge(new TopologyEdge() { |
| 127 | @Override |
| 128 | public Link link() { |
| 129 | return curLink; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public TopologyVertex src() { |
| 134 | return v1; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public TopologyVertex dst() { |
| 139 | return v2; |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | } |