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