Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Laboratory |
| 3 | * |
| 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 Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 16 | package org.onosproject.flowanalyzer; |
| 17 | |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 18 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 19 | import org.onosproject.net.DefaultLink; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.ElementId; |
| 22 | import org.onosproject.net.HostId; |
| 23 | import org.onosproject.net.Link; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.link.LinkServiceAdapter; |
| 26 | import org.onosproject.net.topology.TopologyEdge; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 27 | import org.onosproject.net.topology.TopologyVertex; |
| 28 | |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 30 | import java.util.HashSet; |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 31 | import java.util.List; |
| 32 | import java.util.Set; |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 33 | |
| 34 | import static org.onosproject.net.Link.State.ACTIVE; |
| 35 | |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 36 | /** |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 37 | * Test fixture for the flow analyzer. |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 38 | */ |
| 39 | public 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 63 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 64 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 74 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 75 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 86 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 87 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 97 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 98 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 108 | for (Link l : links) { |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 109 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 135 | curLink = DefaultLink.builder().src(src).dst(dst).state(ACTIVE).build(); |
Nikhil Cheerla | d6734f6 | 2015-07-21 10:41:44 -0700 | [diff] [blame] | 136 | 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 | } |