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