Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 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 | */ |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 16 | package org.onosproject.ui.impl; |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.ConnectPoint; |
| 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.Host; |
| 22 | import org.onosproject.net.HostId; |
| 23 | import org.onosproject.net.Link; |
| 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.host.HostService; |
| 26 | import org.onosproject.net.intent.HostToHostIntent; |
| 27 | import org.onosproject.net.intent.Intent; |
| 28 | import org.onosproject.net.intent.IntentService; |
| 29 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 30 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 31 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 32 | import org.onosproject.net.intent.PathIntent; |
| 33 | import org.onosproject.net.intent.PointToPointIntent; |
| 34 | import org.onosproject.net.link.LinkService; |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 35 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 36 | import java.util.ArrayList; |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 37 | import java.util.HashSet; |
| 38 | import java.util.List; |
| 39 | import java.util.Set; |
| 40 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import static org.onosproject.net.intent.IntentState.INSTALLED; |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Auxiliary facility to query the intent service based on the specified |
| 45 | * set of end-station hosts, edge points or infrastructure devices. |
| 46 | */ |
| 47 | public class TopologyViewIntentFilter { |
| 48 | |
| 49 | private final IntentService intentService; |
| 50 | private final DeviceService deviceService; |
| 51 | private final HostService hostService; |
| 52 | private final LinkService linkService; |
| 53 | |
| 54 | /** |
| 55 | * Crreates an intent filter. |
| 56 | * |
| 57 | * @param intentService intent service reference |
| 58 | * @param deviceService device service reference |
| 59 | * @param hostService host service reference |
| 60 | * @param linkService link service reference |
| 61 | */ |
| 62 | TopologyViewIntentFilter(IntentService intentService, |
| 63 | DeviceService deviceService, |
| 64 | HostService hostService, LinkService linkService) { |
| 65 | this.intentService = intentService; |
| 66 | this.deviceService = deviceService; |
| 67 | this.hostService = hostService; |
| 68 | this.linkService = linkService; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Finds all path (host-to-host or point-to-point) intents that pertains |
| 73 | * to the given hosts. |
| 74 | * |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 75 | * @param hosts set of hosts to query by |
| 76 | * @param devices set of devices to query by |
| 77 | * @param sourceIntents collection of intents to search |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 78 | * @return set of intents that 'match' all hosts and devices given |
| 79 | */ |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 80 | List<Intent> findPathIntents(Set<Host> hosts, Set<Device> devices, |
| 81 | Iterable<Intent> sourceIntents) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 82 | // Derive from this the set of edge connect points. |
| 83 | Set<ConnectPoint> edgePoints = getEdgePoints(hosts); |
| 84 | |
| 85 | // Iterate over all intents and produce a set that contains only those |
| 86 | // intents that target all selected hosts or derived edge connect points. |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 87 | return getIntents(hosts, devices, edgePoints, sourceIntents); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | // Produces a set of edge points from the specified set of hosts. |
| 92 | private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) { |
| 93 | Set<ConnectPoint> edgePoints = new HashSet<>(); |
| 94 | for (Host host : hosts) { |
| 95 | edgePoints.add(host.location()); |
| 96 | } |
| 97 | return edgePoints; |
| 98 | } |
| 99 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 100 | // Produces a list of intents that target all selected hosts, devices or connect points. |
| 101 | private List<Intent> getIntents(Set<Host> hosts, Set<Device> devices, |
| 102 | Set<ConnectPoint> edgePoints, |
| 103 | Iterable<Intent> sourceIntents) { |
| 104 | List<Intent> intents = new ArrayList<>(); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 105 | if (hosts.isEmpty() && devices.isEmpty()) { |
| 106 | return intents; |
| 107 | } |
| 108 | |
| 109 | Set<OpticalConnectivityIntent> opticalIntents = new HashSet<>(); |
| 110 | |
| 111 | // Search through all intents and see if they are relevant to our search. |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 112 | for (Intent intent : sourceIntents) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 113 | if (intentService.getIntentState(intent.key()) == INSTALLED) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 114 | boolean isRelevant = false; |
| 115 | if (intent instanceof HostToHostIntent) { |
| 116 | isRelevant = isIntentRelevantToHosts((HostToHostIntent) intent, hosts) && |
| 117 | isIntentRelevantToDevices(intent, devices); |
| 118 | } else if (intent instanceof PointToPointIntent) { |
| 119 | isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints) && |
| 120 | isIntentRelevantToDevices(intent, devices); |
| 121 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 122 | isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints) && |
| 123 | isIntentRelevantToDevices(intent, devices); |
| 124 | } else if (intent instanceof OpticalConnectivityIntent) { |
| 125 | opticalIntents.add((OpticalConnectivityIntent) intent); |
| 126 | } |
| 127 | // TODO: add other intents, e.g. SinglePointToMultiPointIntent |
| 128 | |
| 129 | if (isRelevant) { |
| 130 | intents.add(intent); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // As a second pass, try to link up any optical intents with the |
| 136 | // packet-level ones. |
| 137 | for (OpticalConnectivityIntent intent : opticalIntents) { |
| 138 | if (isIntentRelevant(intent, intents) && |
| 139 | isIntentRelevantToDevices(intent, devices)) { |
| 140 | intents.add(intent); |
| 141 | } |
| 142 | } |
| 143 | return intents; |
| 144 | } |
| 145 | |
| 146 | // Indicates whether the specified intent involves all of the given hosts. |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 147 | private boolean isIntentRelevantToHosts(HostToHostIntent intent, Iterable<Host> hosts) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 148 | for (Host host : hosts) { |
| 149 | HostId id = host.id(); |
| 150 | // Bail if intent does not involve this host. |
| 151 | if (!id.equals(intent.one()) && !id.equals(intent.two())) { |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | // Indicates whether the specified intent involves all of the given devices. |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 159 | private boolean isIntentRelevantToDevices(Intent intent, Iterable<Device> devices) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 160 | List<Intent> installables = intentService.getInstallableIntents(intent.key()); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 161 | for (Device device : devices) { |
| 162 | if (!isIntentRelevantToDevice(installables, device)) { |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | // Indicates whether the specified intent involves the given device. |
| 170 | private boolean isIntentRelevantToDevice(List<Intent> installables, Device device) { |
Thomas Vachuska | 4731f12 | 2014-11-20 04:56:19 -0800 | [diff] [blame] | 171 | if (installables != null) { |
| 172 | for (Intent installable : installables) { |
| 173 | if (installable instanceof PathIntent) { |
| 174 | PathIntent pathIntent = (PathIntent) installable; |
| 175 | if (pathContainsDevice(pathIntent.path().links(), device.id())) { |
| 176 | return true; |
| 177 | } |
| 178 | } else if (installable instanceof LinkCollectionIntent) { |
| 179 | LinkCollectionIntent linksIntent = (LinkCollectionIntent) installable; |
| 180 | if (pathContainsDevice(linksIntent.links(), device.id())) { |
| 181 | return true; |
| 182 | } |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Indicates whether the specified intent involves the given device. |
| 190 | private boolean pathContainsDevice(Iterable<Link> links, DeviceId id) { |
| 191 | for (Link link : links) { |
| 192 | if (link.src().elementId().equals(id) || link.dst().elementId().equals(id)) { |
| 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 199 | private boolean isIntentRelevant(PointToPointIntent intent, |
| 200 | Iterable<ConnectPoint> edgePoints) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 201 | for (ConnectPoint point : edgePoints) { |
| 202 | // Bail if intent does not involve this edge point. |
| 203 | if (!point.equals(intent.egressPoint()) && |
| 204 | !point.equals(intent.ingressPoint())) { |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | // Indicates whether the specified intent involves all of the given edge points. |
| 212 | private boolean isIntentRelevant(MultiPointToSinglePointIntent intent, |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 213 | Iterable<ConnectPoint> edgePoints) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 214 | for (ConnectPoint point : edgePoints) { |
| 215 | // Bail if intent does not involve this edge point. |
| 216 | if (!point.equals(intent.egressPoint()) && |
| 217 | !intent.ingressPoints().contains(point)) { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | // Indicates whether the specified intent involves all of the given edge points. |
| 225 | private boolean isIntentRelevant(OpticalConnectivityIntent opticalIntent, |
Thomas Vachuska | 164fa5c | 2014-12-02 21:59:41 -0800 | [diff] [blame] | 226 | Iterable<Intent> intents) { |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 227 | Link ccSrc = getFirstLink(opticalIntent.getSrc(), false); |
| 228 | Link ccDst = getFirstLink(opticalIntent.getDst(), true); |
| 229 | |
| 230 | for (Intent intent : intents) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 231 | List<Intent> installables = intentService.getInstallableIntents(intent.key()); |
Thomas Vachuska | 5fedb7a | 2014-11-20 00:55:08 -0800 | [diff] [blame] | 232 | for (Intent installable : installables) { |
| 233 | if (installable instanceof PathIntent) { |
| 234 | List<Link> links = ((PathIntent) installable).path().links(); |
| 235 | if (links.size() == 3) { |
| 236 | Link tunnel = links.get(1); |
| 237 | if (tunnel.src().equals(ccSrc.src()) && |
| 238 | tunnel.dst().equals(ccDst.dst())) { |
| 239 | return true; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | private Link getFirstLink(ConnectPoint point, boolean ingress) { |
| 249 | for (Link link : linkService.getLinks(point)) { |
| 250 | if (point.equals(ingress ? link.src() : link.dst())) { |
| 251 | return link; |
| 252 | } |
| 253 | } |
| 254 | return null; |
| 255 | } |
| 256 | |
| 257 | } |