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