blob: e43fbf2553853aba863fcc615562921343139d9d [file] [log] [blame]
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08003 *
Simon Hunted804d52016-03-30 09:51:40 -07004 * 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 Vachuska5fedb7a2014-11-20 00:55:08 -08007 *
Simon Hunted804d52016-03-30 09:51:40 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08009 *
Simon Hunted804d52016-03-30 09:51:40 -070010 * 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 Vachuska5fedb7a2014-11-20 00:55:08 -080015 */
Simon Hunted804d52016-03-30 09:51:40 -070016package org.onosproject.ui.impl.topo.util;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.ConnectPoint;
19import org.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.Host;
22import org.onosproject.net.HostId;
23import org.onosproject.net.Link;
24import org.onosproject.net.device.DeviceService;
Thomas Vachuska48958b12015-06-10 10:54:53 -070025import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.host.HostService;
Thomas Vachuskada0665b2016-03-02 19:06:17 -080027import org.onosproject.net.intent.FlowObjectiveIntent;
Thomas Vachuska48958b12015-06-10 10:54:53 -070028import org.onosproject.net.intent.FlowRuleIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.intent.HostToHostIntent;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentService;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.MultiPointToSinglePointIntent;
34import org.onosproject.net.intent.OpticalConnectivityIntent;
35import org.onosproject.net.intent.PathIntent;
36import org.onosproject.net.intent.PointToPointIntent;
37import org.onosproject.net.link.LinkService;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080038
Thomas Vachuska164fa5c2014-12-02 21:59:41 -080039import java.util.ArrayList;
Thomas Vachuska48958b12015-06-10 10:54:53 -070040import java.util.Collection;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080041import java.util.HashSet;
42import java.util.List;
Thomas Vachuska48958b12015-06-10 10:54:53 -070043import java.util.Objects;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080044import java.util.Set;
45
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.intent.IntentState.INSTALLED;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080047
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 Hunt4fc86852015-08-20 17:57:52 -070052public class TopoIntentFilter {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080053
54 private final IntentService intentService;
55 private final DeviceService deviceService;
56 private final HostService hostService;
57 private final LinkService linkService;
58
59 /**
Simon Hunta17fa672015-08-19 18:42:22 -070060 * Creates an intent filter.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080061 *
Simon Hunta17fa672015-08-19 18:42:22 -070062 * @param services service references bundle
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080063 */
Simon Hunt4fc86852015-08-20 17:57:52 -070064 public TopoIntentFilter(ServicesBundle services) {
Simon Hunta17fa672015-08-19 18:42:22 -070065 this.intentService = services.intentService();
66 this.deviceService = services.deviceService();
67 this.hostService = services.hostService();
68 this.linkService = services.linkService();
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080069 }
70
71 /**
Simon Hunta17fa672015-08-19 18:42:22 -070072 * Finds all path (host-to-host or point-to-point) intents that pertain
73 * to the given hosts and devices.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080074 *
Thomas Vachuska164fa5c2014-12-02 21:59:41 -080075 * @param hosts set of hosts to query by
76 * @param devices set of devices to query by
Prince Pereira46c82d42016-09-19 13:30:50 +053077 * @param links set of links to query by
78 * @return set of intents that 'match' all hosts, devices and links given
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080079 */
Prince Pereira46c82d42016-09-19 13:30:50 +053080 public List<Intent> findPathIntents(Set<Host> hosts,
81 Set<Device> devices,
82 Set<Link> links) {
Simon Hunta17fa672015-08-19 18:42:22 -070083 // start with all intents
84 Iterable<Intent> sourceIntents = intentService.getIntents();
85
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080086 // 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 Pereira46c82d42016-09-19 13:30:50 +053091 return getIntents(hosts, devices, links, edgePoints, sourceIntents);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080092 }
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 Pereira46c82d42016-09-19 13:30:50 +0530104 // 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 Vachuska164fa5c2014-12-02 21:59:41 -0800106 Set<ConnectPoint> edgePoints,
107 Iterable<Intent> sourceIntents) {
108 List<Intent> intents = new ArrayList<>();
Prince Pereira46c82d42016-09-19 13:30:50 +0530109 if (hosts.isEmpty() && devices.isEmpty() && links.isEmpty()) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800110 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 Vachuska164fa5c2014-12-02 21:59:41 -0800116 for (Intent intent : sourceIntents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800117 if (intentService.getIntentState(intent.key()) == INSTALLED) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800118 boolean isRelevant = false;
119 if (intent instanceof HostToHostIntent) {
120 isRelevant = isIntentRelevantToHosts((HostToHostIntent) intent, hosts) &&
Prince Pereira46c82d42016-09-19 13:30:50 +0530121 isIntentRelevantToDevices(intent, devices) && isIntentRelevantToLinks(intent, links);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800122 } else if (intent instanceof PointToPointIntent) {
123 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints) &&
Prince Pereira46c82d42016-09-19 13:30:50 +0530124 isIntentRelevantToDevices(intent, devices) && isIntentRelevantToLinks(intent, links);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800125 } else if (intent instanceof MultiPointToSinglePointIntent) {
126 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints) &&
Prince Pereira46c82d42016-09-19 13:30:50 +0530127 isIntentRelevantToDevices(intent, devices) && isIntentRelevantToLinks(intent, links);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800128 } 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 Vachuska164fa5c2014-12-02 21:59:41 -0800151 private boolean isIntentRelevantToHosts(HostToHostIntent intent, Iterable<Host> hosts) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800152 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 Vachuska164fa5c2014-12-02 21:59:41 -0800163 private boolean isIntentRelevantToDevices(Intent intent, Iterable<Device> devices) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800164 List<Intent> installables = intentService.getInstallableIntents(intent.key());
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800165 for (Device device : devices) {
166 if (!isIntentRelevantToDevice(installables, device)) {
167 return false;
168 }
169 }
170 return true;
171 }
172
Prince Pereira46c82d42016-09-19 13:30:50 +0530173 // 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 Vachuska5fedb7a2014-11-20 00:55:08 -0800184 // Indicates whether the specified intent involves the given device.
185 private boolean isIntentRelevantToDevice(List<Intent> installables, Device device) {
Thomas Vachuska4731f122014-11-20 04:56:19 -0800186 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 Vachuska48958b12015-06-10 10:54:53 -0700193 } else if (installable instanceof FlowRuleIntent) {
194 FlowRuleIntent flowRuleIntent = (FlowRuleIntent) installable;
195 if (rulesContainDevice(flowRuleIntent.flowRules(), device.id())) {
196 return true;
197 }
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800198 } else if (installable instanceof FlowObjectiveIntent) {
199 FlowObjectiveIntent objectiveIntent = (FlowObjectiveIntent) installable;
200 return objectiveIntent.devices().contains(device.id());
201
Thomas Vachuska4731f122014-11-20 04:56:19 -0800202 } else if (installable instanceof LinkCollectionIntent) {
203 LinkCollectionIntent linksIntent = (LinkCollectionIntent) installable;
204 if (pathContainsDevice(linksIntent.links(), device.id())) {
205 return true;
206 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800207 }
208 }
209 }
210 return false;
211 }
212
Prince Pereira46c82d42016-09-19 13:30:50 +0530213 // 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 Vachuska48958b12015-06-10 10:54:53 -0700245 // Indicates whether the specified links involve the given device.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800246 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 Hunt441c9ae2017-02-03 18:22:31 -0800255 // Indicates whether the specified flow rules involves the given device.
Thomas Vachuska48958b12015-06-10 10:54:53 -0700256 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 Vachuska164fa5c2014-12-02 21:59:41 -0800265 private boolean isIntentRelevant(PointToPointIntent intent,
266 Iterable<ConnectPoint> edgePoints) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800267 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 Vachuska164fa5c2014-12-02 21:59:41 -0800279 Iterable<ConnectPoint> edgePoints) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800280 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 Vachuska164fa5c2014-12-02 21:59:41 -0800292 Iterable<Intent> intents) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800293 Link ccSrc = getFirstLink(opticalIntent.getSrc(), false);
294 Link ccDst = getFirstLink(opticalIntent.getDst(), true);
Thomas Vachuska48958b12015-06-10 10:54:53 -0700295 if (ccSrc == null || ccDst == null) {
296 return false;
297 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800298
299 for (Intent intent : intents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800300 List<Intent> installables = intentService.getInstallableIntents(intent.key());
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800301 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 Vachuska48958b12015-06-10 10:54:53 -0700306 if (Objects.equals(tunnel.src(), ccSrc.src()) &&
307 Objects.equals(tunnel.dst(), ccDst.dst())) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800308 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 Vachuska5fedb7a2014-11-20 00:55:08 -0800325}