blob: a922c16f1b145279fee82b5808f2628aa8f6b633 [file] [log] [blame]
Nikhil Cheerlad6734f62015-07-21 10:41:44 -07001package org.onosproject.flowanalyzer;
2
Nikhil Cheerlad6734f62015-07-21 10:41:44 -07003import org.onosproject.net.ConnectPoint;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -08004import org.onosproject.net.DefaultLink;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -07005import org.onosproject.net.DeviceId;
6import org.onosproject.net.ElementId;
7import org.onosproject.net.HostId;
8import org.onosproject.net.Link;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -08009import org.onosproject.net.PortNumber;
10import org.onosproject.net.link.LinkServiceAdapter;
11import org.onosproject.net.topology.TopologyEdge;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070012import org.onosproject.net.topology.TopologyVertex;
13
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070014import java.util.ArrayList;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070015import java.util.HashSet;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080016import java.util.List;
17import java.util.Set;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070018
19import static org.onosproject.net.Link.State.ACTIVE;
20
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070021/**
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080022 * Test fixture for the flow analyzer.
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070023 */
24public 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 Vachuska3afbc7f2016-02-01 15:55:38 -080048 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070049 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 Vachuska3afbc7f2016-02-01 15:55:38 -080059 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070060 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 Vachuska3afbc7f2016-02-01 15:55:38 -080071 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070072 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 Vachuska3afbc7f2016-02-01 15:55:38 -080082 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070083 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 Vachuska3afbc7f2016-02-01 15:55:38 -080093 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070094 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 Vachuska3afbc7f2016-02-01 15:55:38 -0800120 curLink = DefaultLink.builder().src(src).dst(dst).state(ACTIVE).build();
Nikhil Cheerlad6734f62015-07-21 10:41:44 -0700121 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}