blob: 2b5d6631cd3adffa898b286a3c5bf31ec5265821 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Pier Ventre647138f2016-08-26 17:32:44 -070016
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.net.intent;
Ray Milkey0742ec92014-10-13 08:39:55 -070018
Yuta HIGUCHI051be022017-01-13 18:11:33 -080019import java.util.Collection;
Pier Ventre27d42572016-08-29 17:37:08 -070020import java.util.List;
Pier Ventre27d42572016-08-29 17:37:08 -070021import java.util.Set;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070022import java.util.stream.Collectors;
Pier Ventre27d42572016-08-29 17:37:08 -070023
Brian O'Connor9476fa12015-06-25 15:17:17 -040024import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.net.ConnectPoint;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070027import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Link;
Yuta HIGUCHI051be022017-01-13 18:11:33 -080029import org.onosproject.net.NetworkResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
Ray Milkey0742ec92014-10-13 08:39:55 -070032
Pier Ventre27d42572016-08-29 17:37:08 -070033import com.google.common.base.MoreObjects;
34import com.google.common.collect.ImmutableSet;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070035import org.slf4j.Logger;
36
37import static org.slf4j.LoggerFactory.getLogger;
Ray Milkey0742ec92014-10-13 08:39:55 -070038
39/**
40 * Abstraction of a connectivity intent that is implemented by a set of path
41 * segments.
42 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040043@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070044public final class LinkCollectionIntent extends ConnectivityIntent {
Ray Milkey0742ec92014-10-13 08:39:55 -070045
46 private final Set<Link> links;
47
Yi Tseng2a81c9d2016-09-14 10:14:24 -070048 private final Set<FilteredConnectPoint> ingressPoints;
49 private final Set<FilteredConnectPoint> egressPoints;
Pier Ventre27d42572016-08-29 17:37:08 -070050 private final boolean egressTreatmentFlag;
Pier Ventreffe88d62016-10-13 14:34:40 -070051 private final double cost;
52 private static final int DEFAULT_COST = 1;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070053
Pier Ventreffe88d62016-10-13 14:34:40 -070054 /**
55 * Creates a new actionable intent capable of funneling the selected
56 * traffic along the specified convergent tree and out the given egress
57 * point satisfying the specified constraints.
58 *
59 * @param appId application identifier
60 * @param key key to use for the intent
61 * @param selector traffic match
62 * @param treatment action
63 * @param links traversed links
64 * @param ingressPoints filtered ingress points
65 * @param egressPoints filtered egress points
66 * @param constraints optional list of constraints
67 * @param priority priority to use for the flows generated by this intent
68 * @param egressTreatment true if treatment should be applied by the egress device
69 * @param cost the cost of the links
70 * @throws NullPointerException {@code path} is null
71 */
72 private LinkCollectionIntent(ApplicationId appId,
73 Key key,
74 TrafficSelector selector,
75 TrafficTreatment treatment,
Yuta HIGUCHI051be022017-01-13 18:11:33 -080076 Collection<NetworkResource> resources,
Pier Ventreffe88d62016-10-13 14:34:40 -070077 Set<Link> links,
78 Set<FilteredConnectPoint> ingressPoints,
79 Set<FilteredConnectPoint> egressPoints,
80 List<Constraint> constraints,
81 int priority,
82 boolean egressTreatment,
83 double cost) {
Yuta HIGUCHI051be022017-01-13 18:11:33 -080084 super(appId, key, resources(resources, links), selector, treatment, constraints, priority);
Pier Ventreffe88d62016-10-13 14:34:40 -070085 this.links = links;
86 this.ingressPoints = ingressPoints;
87 this.egressPoints = egressPoints;
88 this.egressTreatmentFlag = egressTreatment;
89 this.cost = cost;
90 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070091
Ray Milkey0742ec92014-10-13 08:39:55 -070092 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070093 * Constructor for serializer.
94 */
Ray Milkey0742ec92014-10-13 08:39:55 -070095 protected LinkCollectionIntent() {
Ray Milkey0742ec92014-10-13 08:39:55 -070096 this.links = null;
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080097 this.ingressPoints = null;
Michele Santuari4a338072014-11-05 18:38:55 +010098 this.egressPoints = null;
Nicholas Dean126b8af2016-07-18 14:43:13 -070099 this.egressTreatmentFlag = false;
Pier Ventreffe88d62016-10-13 14:34:40 -0700100 this.cost = DEFAULT_COST;
Ray Milkey0742ec92014-10-13 08:39:55 -0700101 }
102
Ray Milkeye6684082014-10-16 16:59:47 -0700103 /**
Ray Milkeyebc5d222015-03-18 15:45:36 -0700104 * Returns a new link collection intent builder. The application id,
105 * ingress point and egress points are required fields. If they are
106 * not set by calls to the appropriate methods, an exception will
107 * be thrown.
108 *
109 * @return single point to multi point builder
110 */
111 public static Builder builder() {
112 return new Builder();
113 }
114
115 /**
116 * Builder of a single point to multi point intent.
117 */
118 public static final class Builder extends ConnectivityIntent.Builder {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700119 private final Logger log = getLogger(getClass());
120 private Set<Link> links;
121 private Set<FilteredConnectPoint> ingressPoints;
122 private Set<FilteredConnectPoint> egressPoints;
123 private boolean egressTreatmentFlag;
Pier Ventreffe88d62016-10-13 14:34:40 -0700124 private double cost;
125
Ray Milkeyebc5d222015-03-18 15:45:36 -0700126
127 private Builder() {
128 // Hide constructor
129 }
130
131 @Override
132 public Builder appId(ApplicationId appId) {
133 return (Builder) super.appId(appId);
134 }
135
136 @Override
137 public Builder key(Key key) {
138 return (Builder) super.key(key);
139 }
140
141 @Override
142 public Builder selector(TrafficSelector selector) {
143 return (Builder) super.selector(selector);
144 }
145
146 @Override
147 public Builder treatment(TrafficTreatment treatment) {
148 return (Builder) super.treatment(treatment);
149 }
150
151 @Override
152 public Builder constraints(List<Constraint> constraints) {
153 return (Builder) super.constraints(constraints);
154 }
155
156 @Override
157 public Builder priority(int priority) {
158 return (Builder) super.priority(priority);
159 }
160
Yuta HIGUCHI051be022017-01-13 18:11:33 -0800161 @Override
162 public Builder resources(Collection<NetworkResource> resources) {
163 return (Builder) super.resources(resources);
164 }
165
Ray Milkeyebc5d222015-03-18 15:45:36 -0700166 /**
167 * Sets the ingress point of the single point to multi point intent
168 * that will be built.
169 *
170 * @param ingressPoints ingress connect points
171 * @return this builder
172 */
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700173 @Deprecated
Ray Milkeyebc5d222015-03-18 15:45:36 -0700174 public Builder ingressPoints(Set<ConnectPoint> ingressPoints) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700175 if (this.ingressPoints != null) {
176 log.warn("Ingress points are already set, " +
177 "this will override original ingress points.");
178 }
179 this.ingressPoints = ingressPoints.stream()
180 .map(FilteredConnectPoint::new)
181 .collect(Collectors.toSet());
Ray Milkeyebc5d222015-03-18 15:45:36 -0700182 return this;
183 }
184
185 /**
186 * Sets the egress points of the single point to multi point intent
187 * that will be built.
188 *
189 * @param egressPoints egress connect points
190 * @return this builder
191 */
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700192 @Deprecated
Ray Milkeyebc5d222015-03-18 15:45:36 -0700193 public Builder egressPoints(Set<ConnectPoint> egressPoints) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700194 if (this.egressPoints != null) {
195 log.warn("Egress points are already set, " +
196 "this will override original egress points.");
197 }
198 this.egressPoints = egressPoints.stream()
199 .map(FilteredConnectPoint::new)
200 .collect(Collectors.toSet());
201 return this;
202 }
203
204 /**
205 * Sets the filtered ingress point of the single point to multi point intent
206 * that will be built.
207 *
208 * @param ingressPoints ingress connect points
209 * @return this builder
210 */
211 public Builder filteredIngressPoints(Set<FilteredConnectPoint> ingressPoints) {
212 this.ingressPoints = ImmutableSet.copyOf(ingressPoints);
213 return this;
214 }
215
216 /**
217 * Sets the filtered egress points of the single point to multi point intent
218 * that will be built.
219 *
220 * @param egressPoints egress connect points
221 * @return this builder
222 */
223 public Builder filteredEgressPoints(Set<FilteredConnectPoint> egressPoints) {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700224 this.egressPoints = ImmutableSet.copyOf(egressPoints);
225 return this;
226 }
227
228 /**
229 * Sets the links of the link collection intent
230 * that will be built.
231 *
232 * @param links links for the intent
233 * @return this builder
234 */
235 public Builder links(Set<Link> links) {
236 this.links = ImmutableSet.copyOf(links);
237 return this;
238 }
239
Nicholas Dean126b8af2016-07-18 14:43:13 -0700240 /**
241 * Sets the intent to apply treatment at the egress rather than the
242 * ingress.
243 *
244 * @param treatmentOnEgress true applies treatment on egress device
245 * @return this builder
246 */
247 public Builder applyTreatmentOnEgress(boolean treatmentOnEgress) {
248 this.egressTreatmentFlag = treatmentOnEgress;
249 return this;
250 }
Ray Milkeyebc5d222015-03-18 15:45:36 -0700251
252 /**
Pier Ventreffe88d62016-10-13 14:34:40 -0700253 * Sets the cost for the links of the Intent.
254 *
255 * @param cost the cost of the links
256 * @return this builder
257 */
258 public Builder cost(double cost) {
259 this.cost = cost;
260 return this;
261 }
262
263 /**
Ray Milkeyebc5d222015-03-18 15:45:36 -0700264 * Builds a single point to multi point intent from the
265 * accumulated parameters.
266 *
267 * @return point to point intent
268 */
269 public LinkCollectionIntent build() {
270
271 return new LinkCollectionIntent(
272 appId,
273 key,
274 selector,
275 treatment,
Yuta HIGUCHI051be022017-01-13 18:11:33 -0800276 resources,
Ray Milkeyebc5d222015-03-18 15:45:36 -0700277 links,
278 ingressPoints,
279 egressPoints,
280 constraints,
Nicholas Dean126b8af2016-07-18 14:43:13 -0700281 priority,
Pier Ventreffe88d62016-10-13 14:34:40 -0700282 egressTreatmentFlag,
283 cost
Ray Milkeyebc5d222015-03-18 15:45:36 -0700284 );
285 }
286 }
287
Ray Milkeyebc5d222015-03-18 15:45:36 -0700288 /**
Ray Milkeye6684082014-10-16 16:59:47 -0700289 * Returns the set of links that represent the network connections needed
290 * by this intent.
291 *
292 * @return Set of links for the network hops needed by this intent
293 */
Ray Milkey0742ec92014-10-13 08:39:55 -0700294 public Set<Link> links() {
295 return links;
296 }
297
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700298 /**
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800299 * Returns the ingress points of the intent.
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700300 *
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800301 * @return the ingress points
302 */
303 public Set<ConnectPoint> ingressPoints() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700304 if (this.ingressPoints == null) {
305 return null;
306 }
307 return ingressPoints.stream()
308 .map(FilteredConnectPoint::connectPoint)
309 .collect(Collectors.toSet());
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800310 }
311
312 /**
313 * Returns the egress points of the intent.
314 *
315 * @return the egress points
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700316 */
Michele Santuari4a338072014-11-05 18:38:55 +0100317 public Set<ConnectPoint> egressPoints() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700318 if (this.egressPoints == null) {
319 return null;
320 }
321 return egressPoints.stream()
322 .map(FilteredConnectPoint::connectPoint)
323 .collect(Collectors.toSet());
324 }
325
326 /**
327 * Returns the filtered ingress points of the intent.
328 *
329 * @return the ingress points
330 */
331 public Set<FilteredConnectPoint> filteredIngressPoints() {
332 return ingressPoints;
333 }
334
335 /**
336 * Returns the egress points of the intent.
337 *
338 * @return the egress points
339 */
340 public Set<FilteredConnectPoint> filteredEgressPoints() {
Michele Santuari4a338072014-11-05 18:38:55 +0100341 return egressPoints;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700342 }
343
Nicholas Dean126b8af2016-07-18 14:43:13 -0700344 /**
345 * Returns whether treatment should be applied on egress.
346 *
347 * @return the egress treatment flag
348 */
349 public boolean applyTreatmentOnEgress() {
350 return egressTreatmentFlag;
351 }
352
Pier Ventreffe88d62016-10-13 14:34:40 -0700353 /**
354 * Returns the cost of the links of this intent.
355 *
356 * @return the cost of the links
357 */
358 public double cost() {
359 return cost;
360 }
361
Ray Milkey0742ec92014-10-13 08:39:55 -0700362 @Override
Ray Milkey0742ec92014-10-13 08:39:55 -0700363 public String toString() {
364 return MoreObjects.toStringHelper(getClass())
365 .add("id", id())
Ray Milkeyc3573812015-02-09 09:18:34 -0800366 .add("key", key())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700367 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700368 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800369 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700370 .add("selector", selector())
371 .add("treatment", treatment())
Ray Milkey0742ec92014-10-13 08:39:55 -0700372 .add("links", links())
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800373 .add("ingress", ingressPoints())
Michele Santuari4a338072014-11-05 18:38:55 +0100374 .add("egress", egressPoints())
Pier Ventre27d42572016-08-29 17:37:08 -0700375 .add("treatementOnEgress", applyTreatmentOnEgress())
Pier Ventreffe88d62016-10-13 14:34:40 -0700376 .add("cost", cost())
Ray Milkey0742ec92014-10-13 08:39:55 -0700377 .toString();
378 }
Pier Ventre647138f2016-08-26 17:32:44 -0700379}