blob: 113845d25de2aa0a6acc7023f3136a3e8bfe439c [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 */
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;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070024import org.onosproject.net.flow.DefaultTrafficSelector;
25import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorb876bf12014-10-02 14:59:37 -070028
Thomas Vachuskac96058a2014-10-20 23:00:16 -070029import java.util.Collection;
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -080030import java.util.Collections;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080031import java.util.List;
Yuta HIGUCHI051be022017-01-13 18:11:33 -080032import static com.google.common.base.MoreObjects.firstNonNull;
toma1d16b62014-10-02 23:45:11 -070033import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorb876bf12014-10-02 14:59:37 -070034
35/**
36 * Abstraction of connectivity intent for traffic matching some criteria.
37 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040038@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070039public abstract class ConnectivityIntent extends Intent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070040
41 // TODO: other forms of intents should be considered for this family:
42 // point-to-point with constraints (waypoints/obstacles)
43 // multi-to-single point with constraints (waypoints/obstacles)
44 // single-to-multi point with constraints (waypoints/obstacles)
45 // concrete path (with alternate)
46 // ...
47
48 private final TrafficSelector selector;
Brian O'Connorb876bf12014-10-02 14:59:37 -070049 private final TrafficTreatment treatment;
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080050 private final List<Constraint> constraints;
Brian O'Connorb876bf12014-10-02 14:59:37 -070051
52 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070053 * Creates a connectivity intent that matches on the specified selector
54 * and applies the specified treatment.
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080055 * <p>
Ray Milkey5b3717e2015-02-05 11:44:08 -080056 * Path will be optimized based on the first constraint if one is given.
57 * </p>
58 *
59 * @param appId application identifier
60 * @param key explicit key to use for intent
61 * @param resources required network resources (optional)
62 * @param selector traffic selector
63 * @param treatment treatment
64 * @param constraints optional prioritized list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070065 * @param priority priority to use for flows generated by this intent
Ray Milkey5b3717e2015-02-05 11:44:08 -080066 * @throws NullPointerException if the selector or treatment is null
67 */
Ray Milkey5b3717e2015-02-05 11:44:08 -080068 protected ConnectivityIntent(ApplicationId appId,
69 Key key,
70 Collection<NetworkResource> resources,
71 TrafficSelector selector,
72 TrafficTreatment treatment,
Ray Milkeyc24cde32015-03-10 18:20:18 -070073 List<Constraint> constraints,
74 int priority) {
75 super(appId, key, resources, priority);
Ray Milkey5b3717e2015-02-05 11:44:08 -080076 this.selector = checkNotNull(selector);
77 this.treatment = checkNotNull(treatment);
78 this.constraints = checkNotNull(constraints);
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080079 }
80
81 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -070082 * Constructor for serializer.
83 */
84 protected ConnectivityIntent() {
85 super();
86 this.selector = null;
87 this.treatment = null;
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -080088 this.constraints = Collections.emptyList();
Brian O'Connorb876bf12014-10-02 14:59:37 -070089 }
90
91 /**
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070092 * Abstract builder for connectivity intents.
93 */
94 public abstract static class Builder extends Intent.Builder {
95 protected TrafficSelector selector = DefaultTrafficSelector.emptySelector();
96 protected TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
97 protected List<Constraint> constraints = ImmutableList.of();
98
Jonathan Hartb14221c2016-03-07 09:55:50 -080099 /**
100 * Creates a new empty builder.
101 */
102 protected Builder() {
103 }
104
105 /**
106 * Creates a new builder pre-populated with the information in the given
107 * intent.
108 *
109 * @param intent initial intent
110 */
111 protected Builder(ConnectivityIntent intent) {
112 super(intent);
113
114 this.selector(intent.selector())
115 .treatment(intent.treatment())
116 .constraints(intent.constraints());
117 }
118
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700119 @Override
120 public Builder appId(ApplicationId appId) {
121 return (Builder) super.appId(appId);
122 }
123
124 @Override
125 public Builder key(Key key) {
126 return (Builder) super.key(key);
127 }
128
129 @Override
130 public Builder priority(int priority) {
131 return (Builder) super.priority(priority);
132 }
133
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700134 /**
135 * Sets the traffic selector for the intent that will be built.
136 *
137 * @param selector selector to use for built intent
138 * @return this builder
139 */
140 public Builder selector(TrafficSelector selector) {
141 this.selector = selector;
142 return this;
143 }
144
145 /**
146 * Sets the traffic treatment for the intent that will be built.
147 *
148 * @param treatment treatment to use for built intent
149 * @return this builder
150 */
151 public Builder treatment(TrafficTreatment treatment) {
152 this.treatment = treatment;
153 return this;
154 }
155
156 /**
157 * Sets the constraints for the intent that will be built.
158 *
159 * @param constraints constraints to use for built intent
160 * @return this builder
161 */
162 public Builder constraints(List<Constraint> constraints) {
163 this.constraints = ImmutableList.copyOf(constraints);
164 return this;
165 }
166 }
167
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700168 /**
Brian O'Connorb876bf12014-10-02 14:59:37 -0700169 * Returns the match specifying the type of traffic.
170 *
171 * @return traffic match
172 */
tom85258ee2014-10-07 00:10:02 -0700173 public TrafficSelector selector() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700174 return selector;
175 }
176
177 /**
178 * Returns the action applied to the traffic.
179 *
180 * @return applied action
181 */
tom85258ee2014-10-07 00:10:02 -0700182 public TrafficTreatment treatment() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700183 return treatment;
184 }
185
Thomas Vachuskae291c842014-10-21 02:52:38 -0700186 /**
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800187 * Returns the set of connectivity constraints.
188 *
189 * @return list of intent constraints
190 */
191 public List<Constraint> constraints() {
192 return constraints;
193 }
194
195 /**
Thomas Vachuskae291c842014-10-21 02:52:38 -0700196 * Produces a collection of network resources from the given links.
197 *
Yuta HIGUCHI051be022017-01-13 18:11:33 -0800198 * @param resources base resources
199 * @param links collection of links
200 * @return collection of resources
201 */
202 protected static Collection<NetworkResource> resources(Collection<NetworkResource> resources,
203 Collection<Link> links) {
204 return ImmutableSet.<NetworkResource>builder()
205 .addAll(firstNonNull(resources, ImmutableList.of()))
206 .addAll(links)
207 .build();
208 }
209
210 /**
211 * Produces a collection of network resources from the given links.
212 *
Thomas Vachuskae291c842014-10-21 02:52:38 -0700213 * @param links collection of links
214 * @return collection of link resources
215 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700216 protected static Collection<NetworkResource> resources(Collection<Link> links) {
Sho SHIMIZUf4dbc7262014-11-04 18:49:02 -0800217 return ImmutableSet.copyOf(links);
Brian O'Connorb876bf12014-10-02 14:59:37 -0700218 }
219
220}