blob: 7ac22b33c42af2008b8443fad928412a6c3f2883 [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
Ray Milkeyebc5d222015-03-18 15:45:36 -070018import java.util.List;
19
Brian O'Connor9476fa12015-06-25 15:17:17 -040020import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.net.Link;
23import org.onosproject.net.Path;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorb876bf12014-10-02 14:59:37 -070026
Ray Milkeyebc5d222015-03-18 15:45:36 -070027import com.google.common.base.MoreObjects;
Ray Milkeyebc5d222015-03-18 15:45:36 -070028import com.google.common.collect.Iterables;
Jonathan Hart23b5a762015-01-26 14:47:33 -080029
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080030import static com.google.common.base.Preconditions.checkArgument;
31
Brian O'Connorb876bf12014-10-02 14:59:37 -070032/**
33 * Abstraction of explicitly path specified connectivity intent.
34 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040035@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070036public class PathIntent extends ConnectivityIntent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070037
38 private final Path path;
helenyrwu2a674902016-07-20 09:48:04 -070039 private ProtectionType type;
Brian O'Connorb876bf12014-10-02 14:59:37 -070040
41 /**
42 * Creates a new point-to-point intent with the supplied ingress/egress
helenyrwu2a674902016-07-20 09:48:04 -070043 * ports and using the specified explicit path. Path is primary by default.
Brian O'Connorb876bf12014-10-02 14:59:37 -070044 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070045 * @param appId application identifier
ilhem fajjarib0a06842015-11-02 18:59:02 +010046 * @param key intent key
Thomas Vachuskac96058a2014-10-20 23:00:16 -070047 * @param selector traffic selector
48 * @param treatment treatment
49 * @param path traversed links
Ray Milkey460f4022014-11-05 15:41:43 -080050 * @param constraints optional list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070051 * @param priority priority to use for the generated flows
Ray Milkey460f4022014-11-05 15:41:43 -080052 * @throws NullPointerException {@code path} is null
53 */
Ray Milkeyebc5d222015-03-18 15:45:36 -070054 protected PathIntent(ApplicationId appId,
ilhem fajjarib0a06842015-11-02 18:59:02 +010055 Key key,
Ray Milkeyebc5d222015-03-18 15:45:36 -070056 TrafficSelector selector,
57 TrafficTreatment treatment,
58 Path path,
59 List<Constraint> constraints,
60 int priority) {
helenyrwu2a674902016-07-20 09:48:04 -070061 this(appId, key, selector, treatment, path, constraints, priority,
62 ProtectionType.PRIMARY);
63 }
64
65 /**
66 * Creates a new point-to-point intent with the supplied ingress/egress
67 * ports and using the specified explicit path, which can be classified
68 * as PRIMARY or BACKUP.
69 *
70 * @param appId application identifier
71 * @param key intent key
72 * @param selector traffic selector
73 * @param treatment treatment
74 * @param path traversed links
75 * @param constraints optional list of constraints
76 * @param priority priority to use for the generated flows
77 * @param type PRIMARY or BACKUP
78 * @throws NullPointerException {@code path} is null
79 */
80 protected PathIntent(ApplicationId appId,
81 Key key,
82 TrafficSelector selector,
83 TrafficTreatment treatment,
84 Path path,
85 List<Constraint> constraints,
86 int priority,
87 ProtectionType type) {
ilhem fajjarib0a06842015-11-02 18:59:02 +010088 super(appId, key, resources(path.links()), selector, treatment, constraints,
helenyrwu2a674902016-07-20 09:48:04 -070089 priority);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080090 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080091 this.path = path;
helenyrwu2a674902016-07-20 09:48:04 -070092 this.type = type;
Ray Milkey460f4022014-11-05 15:41:43 -080093 }
94
95 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070096 * Constructor for serializer.
97 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070098 protected PathIntent() {
99 super();
100 this.path = null;
helenyrwu2a674902016-07-20 09:48:04 -0700101 this.type = ProtectionType.PRIMARY;
Brian O'Connorb876bf12014-10-02 14:59:37 -0700102 }
103
Ray Milkeyebc5d222015-03-18 15:45:36 -0700104 /**
105 * Returns a new host to host intent builder.
106 *
107 * @return host to host intent builder
108 */
109 public static Builder builder() {
110 return new Builder();
111 }
112
113 /**
114 * Builder of a host to host intent.
115 */
116 public static class Builder extends ConnectivityIntent.Builder {
117 Path path;
helenyrwu2a674902016-07-20 09:48:04 -0700118 ProtectionType type;
Ray Milkeyebc5d222015-03-18 15:45:36 -0700119
120 protected Builder() {
121 // Hide default constructor
122 }
123
124 @Override
125 public Builder appId(ApplicationId appId) {
126 return (Builder) super.appId(appId);
127 }
128
129 @Override
130 public Builder key(Key key) {
131 return (Builder) super.key(key);
132 }
133
134 @Override
135 public Builder selector(TrafficSelector selector) {
136 return (Builder) super.selector(selector);
137 }
138
139 @Override
140 public Builder treatment(TrafficTreatment treatment) {
141 return (Builder) super.treatment(treatment);
142 }
143
144 @Override
145 public Builder constraints(List<Constraint> constraints) {
146 return (Builder) super.constraints(constraints);
147 }
148
149 @Override
150 public Builder priority(int priority) {
151 return (Builder) super.priority(priority);
152 }
153
154 /**
155 * Sets the path of the intent that will be built.
156 *
157 * @param path path for the intent
158 * @return this builder
159 */
160 public Builder path(Path path) {
161 this.path = path;
162 return this;
163 }
164
helenyrwu2a674902016-07-20 09:48:04 -0700165 public Builder setType(ProtectionType type) {
166 this.type = type;
167 return this;
168 }
169
Ray Milkeyebc5d222015-03-18 15:45:36 -0700170 /**
171 * Builds a path intent from the accumulated parameters.
172 *
173 * @return point to point intent
174 */
175 public PathIntent build() {
176
177 return new PathIntent(
178 appId,
ilhem fajjarib0a06842015-11-02 18:59:02 +0100179 key,
Ray Milkeyebc5d222015-03-18 15:45:36 -0700180 selector,
181 treatment,
182 path,
183 constraints,
helenyrwu2a674902016-07-20 09:48:04 -0700184 priority,
185 type == null ? ProtectionType.PRIMARY : type
Ray Milkeyebc5d222015-03-18 15:45:36 -0700186 );
187 }
188 }
189
190
191
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800192 // NOTE: This methods takes linear time with the number of links.
193 /**
194 * Validates that source element ID and destination element ID of a link are
195 * different for the specified all links and that destination element ID of a link and source
196 * element ID of the next adjacent source element ID are same for the specified all links.
197 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -0800198 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800199 */
200 public static void validate(List<Link> links) {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700201 checkArgument(Iterables.all(links, link -> !link.src().elementId().equals(link.dst().elementId())),
202 "element of src and dst in a link must be different: {}", links);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800203
204 boolean adjacentSame = true;
205 for (int i = 0; i < links.size() - 1; i++) {
206 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
207 adjacentSame = false;
208 break;
209 }
210 }
211 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
212 }
213
Brian O'Connorb876bf12014-10-02 14:59:37 -0700214 /**
215 * Returns the links which the traffic goes along.
216 *
217 * @return traversed links
218 */
tom85258ee2014-10-07 00:10:02 -0700219 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700220 return path;
221 }
222
helenyrwu2a674902016-07-20 09:48:04 -0700223 public ProtectionType type() {
224 return type;
225 }
226
Brian O'Connorb876bf12014-10-02 14:59:37 -0700227 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700228 public String toString() {
229 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700230 .add("id", id())
ilhem fajjarib0a06842015-11-02 18:59:02 +0100231 .add("key", key())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700232 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700233 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800234 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700235 .add("selector", selector())
236 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800237 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700238 .add("path", path)
helenyrwu2a674902016-07-20 09:48:04 -0700239 .add("type", type)
Brian O'Connorb876bf12014-10-02 14:59:37 -0700240 .toString();
241 }
tom95329eb2014-10-06 08:40:06 -0700242
helenyrwu2a674902016-07-20 09:48:04 -0700243 // for path protection purposes
244 public enum ProtectionType {
245 /**
246 * Intent within primary path.
247 */
248 PRIMARY,
249 /**
250 * Intent within backup path.
251 */
252 BACKUP,
253 /**
254 * Intent whose flow rule serves as the fast failover
255 * between primary and backup paths.
256 */
257 FAILOVER
258 }
259
Brian O'Connorb876bf12014-10-02 14:59:37 -0700260}