blob: f6d0e3e8c04147698f4fd48e666fe367e3cca8fb [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
Luca Prete670ac5d2017-02-03 15:55:43 -080067 * @param resourceGroup resource group for this intent
68 * @throws NullPointerException if the selector or treatment is null
69 */
70 protected ConnectivityIntent(ApplicationId appId,
71 Key key,
72 Collection<NetworkResource> resources,
73 TrafficSelector selector,
74 TrafficTreatment treatment,
75 List<Constraint> constraints,
76 int priority,
77 ResourceGroup resourceGroup) {
78 super(appId, key, resources, priority, resourceGroup);
79 this.selector = checkNotNull(selector);
80 this.treatment = checkNotNull(treatment);
81 this.constraints = checkNotNull(constraints);
82 }
83
84 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -070085 * Constructor for serializer.
86 */
87 protected ConnectivityIntent() {
88 super();
89 this.selector = null;
90 this.treatment = null;
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -080091 this.constraints = Collections.emptyList();
Brian O'Connorb876bf12014-10-02 14:59:37 -070092 }
93
94 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070095 * Abstract builder for connectivity intents.
96 */
97 public abstract static class Builder extends Intent.Builder {
98 protected TrafficSelector selector = DefaultTrafficSelector.emptySelector();
99 protected TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
100 protected List<Constraint> constraints = ImmutableList.of();
101
Jonathan Hartb14221c2016-03-07 09:55:50 -0800102 /**
103 * Creates a new empty builder.
104 */
105 protected Builder() {
106 }
107
108 /**
109 * Creates a new builder pre-populated with the information in the given
110 * intent.
111 *
112 * @param intent initial intent
113 */
114 protected Builder(ConnectivityIntent intent) {
115 super(intent);
116
117 this.selector(intent.selector())
118 .treatment(intent.treatment())
119 .constraints(intent.constraints());
120 }
121
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700122 @Override
123 public Builder appId(ApplicationId appId) {
124 return (Builder) super.appId(appId);
125 }
126
127 @Override
128 public Builder key(Key key) {
129 return (Builder) super.key(key);
130 }
131
132 @Override
133 public Builder priority(int priority) {
134 return (Builder) super.priority(priority);
135 }
136
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700137 /**
138 * Sets the traffic selector for the intent that will be built.
139 *
140 * @param selector selector to use for built intent
141 * @return this builder
142 */
143 public Builder selector(TrafficSelector selector) {
144 this.selector = selector;
145 return this;
146 }
147
148 /**
149 * Sets the traffic treatment for the intent that will be built.
150 *
151 * @param treatment treatment to use for built intent
152 * @return this builder
153 */
154 public Builder treatment(TrafficTreatment treatment) {
155 this.treatment = treatment;
156 return this;
157 }
158
159 /**
160 * Sets the constraints for the intent that will be built.
161 *
162 * @param constraints constraints to use for built intent
163 * @return this builder
164 */
165 public Builder constraints(List<Constraint> constraints) {
166 this.constraints = ImmutableList.copyOf(constraints);
167 return this;
168 }
169 }
170
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700171 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -0700172 * Returns the match specifying the type of traffic.
173 *
174 * @return traffic match
175 */
tom85258ee2014-10-07 00:10:02 -0700176 public TrafficSelector selector() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700177 return selector;
178 }
179
180 /**
181 * Returns the action applied to the traffic.
182 *
183 * @return applied action
184 */
tom85258ee2014-10-07 00:10:02 -0700185 public TrafficTreatment treatment() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700186 return treatment;
187 }
188
Thomas Vachuskae291c842014-10-21 02:52:38 -0700189 /**
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800190 * Returns the set of connectivity constraints.
191 *
192 * @return list of intent constraints
193 */
194 public List<Constraint> constraints() {
195 return constraints;
196 }
197
198 /**
Thomas Vachuskae291c842014-10-21 02:52:38 -0700199 * Produces a collection of network resources from the given links.
200 *
Yuta HIGUCHI051be022017-01-13 18:11:33 -0800201 * @param resources base resources
202 * @param links collection of links
203 * @return collection of resources
204 */
205 protected static Collection<NetworkResource> resources(Collection<NetworkResource> resources,
206 Collection<Link> links) {
207 return ImmutableSet.<NetworkResource>builder()
208 .addAll(firstNonNull(resources, ImmutableList.of()))
209 .addAll(links)
210 .build();
211 }
212
213 /**
214 * Produces a collection of network resources from the given links.
215 *
Thomas Vachuskae291c842014-10-21 02:52:38 -0700216 * @param links collection of links
217 * @return collection of link resources
218 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700219 protected static Collection<NetworkResource> resources(Collection<Link> links) {
Sho SHIMIZUf4dbc7262014-11-04 18:49:02 -0800220 return ImmutableSet.copyOf(links);
Brian O'Connorb876bf12014-10-02 14:59:37 -0700221 }
222
223}