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