blob: 67381e47ab27003243da7783d8088fe465ba1f46 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070016package org.onosproject.flowanalyzer;
17
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070018import org.onosproject.net.ConnectPoint;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080019import org.onosproject.net.DefaultLink;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070020import org.onosproject.net.DeviceId;
21import org.onosproject.net.ElementId;
22import org.onosproject.net.HostId;
23import org.onosproject.net.Link;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080024import org.onosproject.net.PortNumber;
25import org.onosproject.net.link.LinkServiceAdapter;
26import org.onosproject.net.topology.TopologyEdge;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070027import org.onosproject.net.topology.TopologyVertex;
28
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070029import java.util.ArrayList;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070030import java.util.HashSet;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080031import java.util.List;
32import java.util.Set;
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070033
34import static org.onosproject.net.Link.State.ACTIVE;
35
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070036/**
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080037 * Test fixture for the flow analyzer.
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070038 */
39public class MockLinkService extends LinkServiceAdapter {
40 DefaultMutableTopologyGraph createdGraph = new DefaultMutableTopologyGraph(new HashSet<>(), new HashSet<>());
41 List<Link> links = new ArrayList<>();
42
43 @Override
44 public int getLinkCount() {
45 return links.size();
46 }
47
48 @Override
49 public Iterable<Link> getLinks() {
50 return links;
51 }
52
53 @Override
54 public Set<Link> getDeviceLinks(DeviceId deviceId) {
55 Set<Link> egress = getDeviceEgressLinks(deviceId);
56 egress.addAll(getDeviceIngressLinks(deviceId));
57 return egress;
58 }
59
60 @Override
61 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
62 Set<Link> setL = new HashSet<>();
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080063 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070064 if (l.src().elementId() instanceof DeviceId && l.src().deviceId().equals(deviceId)) {
65 setL.add(l);
66 }
67 }
68 return setL;
69 }
70
71 @Override
72 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
73 Set<Link> setL = new HashSet<>();
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080074 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070075 if (l.dst().elementId() instanceof DeviceId && l.dst().deviceId().equals(deviceId)) {
76 setL.add(l);
77 }
78 }
79 return setL;
80 }
81
82
83 @Override
84 public Set<Link> getEgressLinks(ConnectPoint pt) {
85 Set<Link> setL = new HashSet<>();
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080086 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070087 if (l.src().equals(pt)) {
88 setL.add(l);
89 }
90 }
91 return setL;
92 }
93
94 @Override
95 public Set<Link> getIngressLinks(ConnectPoint pt) {
96 Set<Link> setL = new HashSet<>();
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080097 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -070098 if (l.dst().equals(pt)) {
99 setL.add(l);
100 }
101 }
102 return setL;
103 }
104
105 @Override
106 public Set<Link> getLinks(ConnectPoint pt) {
107 Set<Link> setL = new HashSet<>();
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800108 for (Link l : links) {
Nikhil Cheerlad6734f62015-07-21 10:41:44 -0700109 if (l.src().equals(pt) || l.dst().equals(pt)) {
110 setL.add(l);
111 }
112 }
113 return setL;
114 }
115
116 public void addLink(String device, long port, String device2, long port2) {
117 ElementId d1;
118 if (device.charAt(0) == 'H') {
119 device = device.substring(1, device.length());
120 d1 = HostId.hostId(device);
121 } else {
122 d1 = DeviceId.deviceId(device);
123 }
124
125 ElementId d2;
126 if (device2.charAt(0) == 'H') {
127 d2 = HostId.hostId(device2.substring(1, device2.length()));
128 } else {
129 d2 = DeviceId.deviceId(device2);
130 }
131
132 ConnectPoint src = new ConnectPoint(d1, PortNumber.portNumber(port));
133 ConnectPoint dst = new ConnectPoint(d2, PortNumber.portNumber(port2));
134 Link curLink;
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -0800135 curLink = DefaultLink.builder().src(src).dst(dst).state(ACTIVE).build();
Nikhil Cheerlad6734f62015-07-21 10:41:44 -0700136 links.add(curLink);
137 if (d1 instanceof DeviceId && d2 instanceof DeviceId) {
138 TopologyVertex v1 = () -> (DeviceId) d1, v2 = () -> (DeviceId) d2;
139 createdGraph.addVertex(v1);
140 createdGraph.addVertex(v2);
141 createdGraph.addEdge(new TopologyEdge() {
142 @Override
143 public Link link() {
144 return curLink;
145 }
146
147 @Override
148 public TopologyVertex src() {
149 return v1;
150 }
151
152 @Override
153 public TopologyVertex dst() {
154 return v2;
155 }
156 });
157 }
158 }
159
160
161}