blob: 5dc54a006a787c079b8b263bc7343592afeec450 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070019import com.google.common.collect.ImmutableList;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070020import com.google.common.collect.ImmutableSet;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.net.Link;
23import org.onosproject.net.NetworkResource;
Luca Prete670ac5d2017-02-03 15:55:43 -080024import org.onosproject.net.ResourceGroup;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070025import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorb876bf12014-10-02 14:59:37 -070029
Thomas Vachuskac96058a2014-10-20 23:00:16 -070030import java.util.Collection;
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -080031import java.util.Collections;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080032import java.util.List;
Yuta HIGUCHI051be022017-01-13 18:11:33 -080033import static com.google.common.base.MoreObjects.firstNonNull;
toma1d16b62014-10-02 23:45:11 -070034import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorb876bf12014-10-02 14:59:37 -070035
36/**
37 * Abstraction of connectivity intent for traffic matching some criteria.
38 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040039@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070040public abstract class ConnectivityIntent extends Intent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070041
42 // TODO: other forms of intents should be considered for this family:
43 // point-to-point with constraints (waypoints/obstacles)
44 // multi-to-single point with constraints (waypoints/obstacles)
45 // single-to-multi point with constraints (waypoints/obstacles)
46 // concrete path (with alternate)
47 // ...
48
49 private final TrafficSelector selector;
Brian O'Connorb876bf12014-10-02 14:59:37 -070050 private final TrafficTreatment treatment;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080051 private final List<Constraint> constraints;
Brian O'Connorb876bf12014-10-02 14:59:37 -070052
53 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070054 * Creates a connectivity intent that matches on the specified selector
55 * and applies the specified treatment.
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080056 * <p>
Ray Milkey5b3717e2015-02-05 11:44:08 -080057 * Path will be optimized based on the first constraint if one is given.
58 * </p>
59 *
60 * @param appId application identifier
61 * @param key explicit key to use for intent
62 * @param resources required network resources (optional)
63 * @param selector traffic selector
64 * @param treatment treatment
65 * @param constraints optional prioritized list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070066 * @param priority priority to use for flows generated by this intent
Ray Milkey5b3717e2015-02-05 11:44:08 -080067 * @throws NullPointerException if the selector or treatment is null
Luca Prete670ac5d2017-02-03 15:55:43 -080068 * @deprecated 1.9.1
Ray Milkey5b3717e2015-02-05 11:44:08 -080069 */
Luca Prete670ac5d2017-02-03 15:55:43 -080070 @Deprecated
Ray Milkey5b3717e2015-02-05 11:44:08 -080071 protected ConnectivityIntent(ApplicationId appId,
72 Key key,
73 Collection<NetworkResource> resources,
74 TrafficSelector selector,
75 TrafficTreatment treatment,
Ray Milkeyc24cde32015-03-10 18:20:18 -070076 List<Constraint> constraints,
77 int priority) {
78 super(appId, key, resources, priority);
Ray Milkey5b3717e2015-02-05 11:44:08 -080079 this.selector = checkNotNull(selector);
80 this.treatment = checkNotNull(treatment);
81 this.constraints = checkNotNull(constraints);
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080082 }
83
84 /**
Luca Prete670ac5d2017-02-03 15:55:43 -080085 * Creates a connectivity intent that matches on the specified selector
86 * and applies the specified treatment.
87 * <p>
88 * Path will be optimized based on the first constraint if one is given.
89 * </p>
90 *
91 * @param appId application identifier
92 * @param key explicit key to use for intent
93 * @param resources required network resources (optional)
94 * @param selector traffic selector
95 * @param treatment treatment
96 * @param constraints optional prioritized list of constraints
97 * @param priority priority to use for flows generated by this intent
98 * @param resourceGroup resource group for this intent
99 * @throws NullPointerException if the selector or treatment is null
100 */
101 protected ConnectivityIntent(ApplicationId appId,
102 Key key,
103 Collection<NetworkResource> resources,
104 TrafficSelector selector,
105 TrafficTreatment treatment,
106 List<Constraint> constraints,
107 int priority,
108 ResourceGroup resourceGroup) {
109 super(appId, key, resources, priority, resourceGroup);
110 this.selector = checkNotNull(selector);
111 this.treatment = checkNotNull(treatment);
112 this.constraints = checkNotNull(constraints);
113 }
114
115 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -0700116 * Constructor for serializer.
117 */
118 protected ConnectivityIntent() {
119 super();
120 this.selector = null;
121 this.treatment = null;
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -0800122 this.constraints = Collections.emptyList();
Brian O'Connorb876bf12014-10-02 14:59:37 -0700123 }
124
125 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700126 * Abstract builder for connectivity intents.
127 */
128 public abstract static class Builder extends Intent.Builder {
129 protected TrafficSelector selector = DefaultTrafficSelector.emptySelector();
130 protected TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
131 protected List<Constraint> constraints = ImmutableList.of();
132
Jonathan Hartb14221c2016-03-07 09:55:50 -0800133 /**
134 * Creates a new empty builder.
135 */
136 protected Builder() {
137 }
138
139 /**
140 * Creates a new builder pre-populated with the information in the given
141 * intent.
142 *
143 * @param intent initial intent
144 */
145 protected Builder(ConnectivityIntent intent) {
146 super(intent);
147
148 this.selector(intent.selector())
149 .treatment(intent.treatment())
150 .constraints(intent.constraints());
151 }
152
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700153 @Override
154 public Builder appId(ApplicationId appId) {
155 return (Builder) super.appId(appId);
156 }
157
158 @Override
159 public Builder key(Key key) {
160 return (Builder) super.key(key);
161 }
162
163 @Override
164 public Builder priority(int priority) {
165 return (Builder) super.priority(priority);
166 }
167
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700168 /**
169 * Sets the traffic selector for the intent that will be built.
170 *
171 * @param selector selector to use for built intent
172 * @return this builder
173 */
174 public Builder selector(TrafficSelector selector) {
175 this.selector = selector;
176 return this;
177 }
178
179 /**
180 * Sets the traffic treatment for the intent that will be built.
181 *
182 * @param treatment treatment to use for built intent
183 * @return this builder
184 */
185 public Builder treatment(TrafficTreatment treatment) {
186 this.treatment = treatment;
187 return this;
188 }
189
190 /**
191 * Sets the constraints for the intent that will be built.
192 *
193 * @param constraints constraints to use for built intent
194 * @return this builder
195 */
196 public Builder constraints(List<Constraint> constraints) {
197 this.constraints = ImmutableList.copyOf(constraints);
198 return this;
199 }
200 }
201
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700202 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -0700203 * Returns the match specifying the type of traffic.
204 *
205 * @return traffic match
206 */
tom85258ee2014-10-07 00:10:02 -0700207 public TrafficSelector selector() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700208 return selector;
209 }
210
211 /**
212 * Returns the action applied to the traffic.
213 *
214 * @return applied action
215 */
tom85258ee2014-10-07 00:10:02 -0700216 public TrafficTreatment treatment() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700217 return treatment;
218 }
219
Thomas Vachuskae291c842014-10-21 02:52:38 -0700220 /**
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800221 * Returns the set of connectivity constraints.
222 *
223 * @return list of intent constraints
224 */
225 public List<Constraint> constraints() {
226 return constraints;
227 }
228
229 /**
Thomas Vachuskae291c842014-10-21 02:52:38 -0700230 * Produces a collection of network resources from the given links.
231 *
Yuta HIGUCHI051be022017-01-13 18:11:33 -0800232 * @param resources base resources
233 * @param links collection of links
234 * @return collection of resources
235 */
236 protected static Collection<NetworkResource> resources(Collection<NetworkResource> resources,
237 Collection<Link> links) {
238 return ImmutableSet.<NetworkResource>builder()
239 .addAll(firstNonNull(resources, ImmutableList.of()))
240 .addAll(links)
241 .build();
242 }
243
244 /**
245 * Produces a collection of network resources from the given links.
246 *
Thomas Vachuskae291c842014-10-21 02:52:38 -0700247 * @param links collection of links
248 * @return collection of link resources
249 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700250 protected static Collection<NetworkResource> resources(Collection<Link> links) {
Sho SHIMIZUf4dbc7262014-11-04 18:49:02 -0800251 return ImmutableSet.copyOf(links);
Brian O'Connorb876bf12014-10-02 14:59:37 -0700252 }
253
254}