blob: 2b657623c0315b4af7dd6317e7bea8392888d6eb [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
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080077 * @return set of intents that 'match' all hosts and devices given
78 */
Simon Hunta17fa672015-08-19 18:42:22 -070079 public List<Intent> findPathIntents(Set<Host> hosts, Set<Device> devices) {
80 // start with all intents
81 Iterable<Intent> sourceIntents = intentService.getIntents();
82
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080083 // 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 Vachuska164fa5c2014-12-02 21:59:41 -080088 return getIntents(hosts, devices, edgePoints, sourceIntents);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080089 }
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 Vachuska164fa5c2014-12-02 21:59:41 -0800101 // 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 Vachuska5fedb7a2014-11-20 00:55:08 -0800106 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 Vachuska164fa5c2014-12-02 21:59:41 -0800113 for (Intent intent : sourceIntents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800114 if (intentService.getIntentState(intent.key()) == INSTALLED) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800115 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 Vachuska164fa5c2014-12-02 21:59:41 -0800148 private boolean isIntentRelevantToHosts(HostToHostIntent intent, Iterable<Host> hosts) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800149 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 Vachuska164fa5c2014-12-02 21:59:41 -0800160 private boolean isIntentRelevantToDevices(Intent intent, Iterable<Device> devices) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800161 List<Intent> installables = intentService.getInstallableIntents(intent.key());
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800162 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 Vachuska4731f122014-11-20 04:56:19 -0800172 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 Vachuska48958b12015-06-10 10:54:53 -0700179 } else if (installable instanceof FlowRuleIntent) {
180 FlowRuleIntent flowRuleIntent = (FlowRuleIntent) installable;
181 if (rulesContainDevice(flowRuleIntent.flowRules(), device.id())) {
182 return true;
183 }
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800184 } else if (installable instanceof FlowObjectiveIntent) {
185 FlowObjectiveIntent objectiveIntent = (FlowObjectiveIntent) installable;
186 return objectiveIntent.devices().contains(device.id());
187
Thomas Vachuska4731f122014-11-20 04:56:19 -0800188 } else if (installable instanceof LinkCollectionIntent) {
189 LinkCollectionIntent linksIntent = (LinkCollectionIntent) installable;
190 if (pathContainsDevice(linksIntent.links(), device.id())) {
191 return true;
192 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800193 }
194 }
195 }
196 return false;
197 }
198
Thomas Vachuska48958b12015-06-10 10:54:53 -0700199 // Indicates whether the specified links involve the given device.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800200 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 Vachuska48958b12015-06-10 10:54:53 -0700209 // 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 Vachuska164fa5c2014-12-02 21:59:41 -0800219 private boolean isIntentRelevant(PointToPointIntent intent,
220 Iterable<ConnectPoint> edgePoints) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800221 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 Vachuska164fa5c2014-12-02 21:59:41 -0800233 Iterable<ConnectPoint> edgePoints) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800234 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 Vachuska164fa5c2014-12-02 21:59:41 -0800246 Iterable<Intent> intents) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800247 Link ccSrc = getFirstLink(opticalIntent.getSrc(), false);
248 Link ccDst = getFirstLink(opticalIntent.getDst(), true);
Thomas Vachuska48958b12015-06-10 10:54:53 -0700249 if (ccSrc == null || ccDst == null) {
250 return false;
251 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800252
253 for (Intent intent : intents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800254 List<Intent> installables = intentService.getInstallableIntents(intent.key());
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800255 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 Vachuska48958b12015-06-10 10:54:53 -0700260 if (Objects.equals(tunnel.src(), ccSrc.src()) &&
261 Objects.equals(tunnel.dst(), ccDst.dst())) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800262 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 Vachuska5fedb7a2014-11-20 00:55:08 -0800279}