blob: 8372dede68fc188c794b50dd74a6244d67e6eba8 [file] [log] [blame]
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08003 *
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 Hunta17fa672015-08-19 18:42:22 -070016package org.onosproject.ui.impl.topo;
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 Vachuska48958b12015-06-10 10:54:53 -070027import org.onosproject.net.intent.FlowRuleIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.intent.HostToHostIntent;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentService;
31import org.onosproject.net.intent.LinkCollectionIntent;
32import org.onosproject.net.intent.MultiPointToSinglePointIntent;
33import org.onosproject.net.intent.OpticalConnectivityIntent;
34import org.onosproject.net.intent.PathIntent;
35import org.onosproject.net.intent.PointToPointIntent;
36import org.onosproject.net.link.LinkService;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080037
Thomas Vachuska164fa5c2014-12-02 21:59:41 -080038import java.util.ArrayList;
Thomas Vachuska48958b12015-06-10 10:54:53 -070039import java.util.Collection;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080040import java.util.HashSet;
41import java.util.List;
Thomas Vachuska48958b12015-06-10 10:54:53 -070042import java.util.Objects;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080043import java.util.Set;
44
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.intent.IntentState.INSTALLED;
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080046
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 */
Simon Hunt4fc86852015-08-20 17:57:52 -070051public class TopoIntentFilter {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080052
53 private final IntentService intentService;
54 private final DeviceService deviceService;
55 private final HostService hostService;
56 private final LinkService linkService;
57
58 /**
Simon Hunta17fa672015-08-19 18:42:22 -070059 * Creates an intent filter.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080060 *
Simon Hunta17fa672015-08-19 18:42:22 -070061 * @param services service references bundle
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080062 */
Simon Hunt4fc86852015-08-20 17:57:52 -070063 public TopoIntentFilter(ServicesBundle services) {
Simon Hunta17fa672015-08-19 18:42:22 -070064 this.intentService = services.intentService();
65 this.deviceService = services.deviceService();
66 this.hostService = services.hostService();
67 this.linkService = services.linkService();
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080068 }
69
70 /**
Simon Hunta17fa672015-08-19 18:42:22 -070071 * Finds all path (host-to-host or point-to-point) intents that pertain
72 * to the given hosts and devices.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080073 *
Thomas Vachuska164fa5c2014-12-02 21:59:41 -080074 * @param hosts set of hosts to query by
75 * @param devices set of devices to query by
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080076 * @return set of intents that 'match' all hosts and devices given
77 */
Simon Hunta17fa672015-08-19 18:42:22 -070078 public List<Intent> findPathIntents(Set<Host> hosts, Set<Device> devices) {
79 // start with all intents
80 Iterable<Intent> sourceIntents = intentService.getIntents();
81
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080082 // Derive from this the set of edge connect points.
83 Set<ConnectPoint> edgePoints = getEdgePoints(hosts);
84
85 // Iterate over all intents and produce a set that contains only those
86 // intents that target all selected hosts or derived edge connect points.
Thomas Vachuska164fa5c2014-12-02 21:59:41 -080087 return getIntents(hosts, devices, edgePoints, sourceIntents);
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -080088 }
89
90
91 // Produces a set of edge points from the specified set of hosts.
92 private Set<ConnectPoint> getEdgePoints(Set<Host> hosts) {
93 Set<ConnectPoint> edgePoints = new HashSet<>();
94 for (Host host : hosts) {
95 edgePoints.add(host.location());
96 }
97 return edgePoints;
98 }
99
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800100 // Produces a list of intents that target all selected hosts, devices or connect points.
101 private List<Intent> getIntents(Set<Host> hosts, Set<Device> devices,
102 Set<ConnectPoint> edgePoints,
103 Iterable<Intent> sourceIntents) {
104 List<Intent> intents = new ArrayList<>();
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800105 if (hosts.isEmpty() && devices.isEmpty()) {
106 return intents;
107 }
108
109 Set<OpticalConnectivityIntent> opticalIntents = new HashSet<>();
110
111 // Search through all intents and see if they are relevant to our search.
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800112 for (Intent intent : sourceIntents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800113 if (intentService.getIntentState(intent.key()) == INSTALLED) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800114 boolean isRelevant = false;
115 if (intent instanceof HostToHostIntent) {
116 isRelevant = isIntentRelevantToHosts((HostToHostIntent) intent, hosts) &&
117 isIntentRelevantToDevices(intent, devices);
118 } else if (intent instanceof PointToPointIntent) {
119 isRelevant = isIntentRelevant((PointToPointIntent) intent, edgePoints) &&
120 isIntentRelevantToDevices(intent, devices);
121 } else if (intent instanceof MultiPointToSinglePointIntent) {
122 isRelevant = isIntentRelevant((MultiPointToSinglePointIntent) intent, edgePoints) &&
123 isIntentRelevantToDevices(intent, devices);
124 } else if (intent instanceof OpticalConnectivityIntent) {
125 opticalIntents.add((OpticalConnectivityIntent) intent);
126 }
127 // TODO: add other intents, e.g. SinglePointToMultiPointIntent
128
129 if (isRelevant) {
130 intents.add(intent);
131 }
132 }
133 }
134
135 // As a second pass, try to link up any optical intents with the
136 // packet-level ones.
137 for (OpticalConnectivityIntent intent : opticalIntents) {
138 if (isIntentRelevant(intent, intents) &&
139 isIntentRelevantToDevices(intent, devices)) {
140 intents.add(intent);
141 }
142 }
143 return intents;
144 }
145
146 // Indicates whether the specified intent involves all of the given hosts.
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800147 private boolean isIntentRelevantToHosts(HostToHostIntent intent, Iterable<Host> hosts) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800148 for (Host host : hosts) {
149 HostId id = host.id();
150 // Bail if intent does not involve this host.
151 if (!id.equals(intent.one()) && !id.equals(intent.two())) {
152 return false;
153 }
154 }
155 return true;
156 }
157
158 // Indicates whether the specified intent involves all of the given devices.
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800159 private boolean isIntentRelevantToDevices(Intent intent, Iterable<Device> devices) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800160 List<Intent> installables = intentService.getInstallableIntents(intent.key());
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800161 for (Device device : devices) {
162 if (!isIntentRelevantToDevice(installables, device)) {
163 return false;
164 }
165 }
166 return true;
167 }
168
169 // Indicates whether the specified intent involves the given device.
170 private boolean isIntentRelevantToDevice(List<Intent> installables, Device device) {
Thomas Vachuska4731f122014-11-20 04:56:19 -0800171 if (installables != null) {
172 for (Intent installable : installables) {
173 if (installable instanceof PathIntent) {
174 PathIntent pathIntent = (PathIntent) installable;
175 if (pathContainsDevice(pathIntent.path().links(), device.id())) {
176 return true;
177 }
Thomas Vachuska48958b12015-06-10 10:54:53 -0700178 } else if (installable instanceof FlowRuleIntent) {
179 FlowRuleIntent flowRuleIntent = (FlowRuleIntent) installable;
180 if (rulesContainDevice(flowRuleIntent.flowRules(), device.id())) {
181 return true;
182 }
Thomas Vachuska4731f122014-11-20 04:56:19 -0800183 } else if (installable instanceof LinkCollectionIntent) {
184 LinkCollectionIntent linksIntent = (LinkCollectionIntent) installable;
185 if (pathContainsDevice(linksIntent.links(), device.id())) {
186 return true;
187 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800188 }
189 }
190 }
191 return false;
192 }
193
Thomas Vachuska48958b12015-06-10 10:54:53 -0700194 // Indicates whether the specified links involve the given device.
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800195 private boolean pathContainsDevice(Iterable<Link> links, DeviceId id) {
196 for (Link link : links) {
197 if (link.src().elementId().equals(id) || link.dst().elementId().equals(id)) {
198 return true;
199 }
200 }
201 return false;
202 }
203
Thomas Vachuska48958b12015-06-10 10:54:53 -0700204 // Indicates whether the specified flow rules involvesthe given device.
205 private boolean rulesContainDevice(Collection<FlowRule> flowRules, DeviceId id) {
206 for (FlowRule rule : flowRules) {
207 if (rule.deviceId().equals(id)) {
208 return true;
209 }
210 }
211 return false;
212 }
213
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800214 private boolean isIntentRelevant(PointToPointIntent intent,
215 Iterable<ConnectPoint> edgePoints) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800216 for (ConnectPoint point : edgePoints) {
217 // Bail if intent does not involve this edge point.
218 if (!point.equals(intent.egressPoint()) &&
219 !point.equals(intent.ingressPoint())) {
220 return false;
221 }
222 }
223 return true;
224 }
225
226 // Indicates whether the specified intent involves all of the given edge points.
227 private boolean isIntentRelevant(MultiPointToSinglePointIntent intent,
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800228 Iterable<ConnectPoint> edgePoints) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800229 for (ConnectPoint point : edgePoints) {
230 // Bail if intent does not involve this edge point.
231 if (!point.equals(intent.egressPoint()) &&
232 !intent.ingressPoints().contains(point)) {
233 return false;
234 }
235 }
236 return true;
237 }
238
239 // Indicates whether the specified intent involves all of the given edge points.
240 private boolean isIntentRelevant(OpticalConnectivityIntent opticalIntent,
Thomas Vachuska164fa5c2014-12-02 21:59:41 -0800241 Iterable<Intent> intents) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800242 Link ccSrc = getFirstLink(opticalIntent.getSrc(), false);
243 Link ccDst = getFirstLink(opticalIntent.getDst(), true);
Thomas Vachuska48958b12015-06-10 10:54:53 -0700244 if (ccSrc == null || ccDst == null) {
245 return false;
246 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800247
248 for (Intent intent : intents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800249 List<Intent> installables = intentService.getInstallableIntents(intent.key());
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800250 for (Intent installable : installables) {
251 if (installable instanceof PathIntent) {
252 List<Link> links = ((PathIntent) installable).path().links();
253 if (links.size() == 3) {
254 Link tunnel = links.get(1);
Thomas Vachuska48958b12015-06-10 10:54:53 -0700255 if (Objects.equals(tunnel.src(), ccSrc.src()) &&
256 Objects.equals(tunnel.dst(), ccDst.dst())) {
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800257 return true;
258 }
259 }
260 }
261 }
262 }
263 return false;
264 }
265
266 private Link getFirstLink(ConnectPoint point, boolean ingress) {
267 for (Link link : linkService.getLinks(point)) {
268 if (point.equals(ingress ? link.src() : link.dst())) {
269 return link;
270 }
271 }
272 return null;
273 }
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -0800274}