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