blob: 3c386ccb4163dc63b4211ad275b0a9ddfa97418e [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;
Luca Prete670ac5d2017-02-03 15:55:43 -080024import org.onosproject.net.ResourceGroup;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorb876bf12014-10-02 14:59:37 -070027
Ray Milkeyebc5d222015-03-18 15:45:36 -070028import com.google.common.base.MoreObjects;
Ray Milkeyebc5d222015-03-18 15:45:36 -070029import com.google.common.collect.Iterables;
Jonathan Hart23b5a762015-01-26 14:47:33 -080030
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080031import static com.google.common.base.Preconditions.checkArgument;
32
Brian O'Connorb876bf12014-10-02 14:59:37 -070033/**
34 * Abstraction of explicitly path specified connectivity intent.
35 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040036@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070037public class PathIntent extends ConnectivityIntent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070038
39 private final Path path;
helenyrwu2a674902016-07-20 09:48:04 -070040 private ProtectionType type;
Brian O'Connorb876bf12014-10-02 14:59:37 -070041
42 /**
43 * Creates a new point-to-point intent with the supplied ingress/egress
helenyrwu2a674902016-07-20 09:48:04 -070044 * ports and using the specified explicit path. Path is primary by default.
Brian O'Connorb876bf12014-10-02 14:59:37 -070045 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070046 * @param appId application identifier
ilhem fajjarib0a06842015-11-02 18:59:02 +010047 * @param key intent key
Thomas Vachuskac96058a2014-10-20 23:00:16 -070048 * @param selector traffic selector
49 * @param treatment treatment
50 * @param path traversed links
Ray Milkey460f4022014-11-05 15:41:43 -080051 * @param constraints optional list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070052 * @param priority priority to use for the generated flows
Ray Milkey460f4022014-11-05 15:41:43 -080053 * @throws NullPointerException {@code path} is null
Luca Prete670ac5d2017-02-03 15:55:43 -080054 * @deprecated 1.9.1
Ray Milkey460f4022014-11-05 15:41:43 -080055 */
Luca Prete670ac5d2017-02-03 15:55:43 -080056 @Deprecated
Ray Milkeyebc5d222015-03-18 15:45:36 -070057 protected PathIntent(ApplicationId appId,
ilhem fajjarib0a06842015-11-02 18:59:02 +010058 Key key,
Ray Milkeyebc5d222015-03-18 15:45:36 -070059 TrafficSelector selector,
60 TrafficTreatment treatment,
61 Path path,
62 List<Constraint> constraints,
63 int priority) {
helenyrwu2a674902016-07-20 09:48:04 -070064 this(appId, key, selector, treatment, path, constraints, priority,
Luca Prete670ac5d2017-02-03 15:55:43 -080065 ProtectionType.PRIMARY, null);
helenyrwu2a674902016-07-20 09:48:04 -070066 }
67
68 /**
69 * Creates a new point-to-point intent with the supplied ingress/egress
70 * ports and using the specified explicit path, which can be classified
71 * as PRIMARY or BACKUP.
72 *
73 * @param appId application identifier
74 * @param key intent key
75 * @param selector traffic selector
76 * @param treatment treatment
77 * @param path traversed links
78 * @param constraints optional list of constraints
79 * @param priority priority to use for the generated flows
80 * @param type PRIMARY or BACKUP
Luca Prete670ac5d2017-02-03 15:55:43 -080081 * @param resourceGroup resource group for this intent
helenyrwu2a674902016-07-20 09:48:04 -070082 * @throws NullPointerException {@code path} is null
83 */
84 protected PathIntent(ApplicationId appId,
85 Key key,
86 TrafficSelector selector,
87 TrafficTreatment treatment,
88 Path path,
89 List<Constraint> constraints,
90 int priority,
Luca Prete670ac5d2017-02-03 15:55:43 -080091 ProtectionType type,
92 ResourceGroup resourceGroup) {
ilhem fajjarib0a06842015-11-02 18:59:02 +010093 super(appId, key, resources(path.links()), selector, treatment, constraints,
Luca Prete670ac5d2017-02-03 15:55:43 -080094 priority, resourceGroup);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080095 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080096 this.path = path;
helenyrwu2a674902016-07-20 09:48:04 -070097 this.type = type;
Ray Milkey460f4022014-11-05 15:41:43 -080098 }
99
100 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700101 * Constructor for serializer.
102 */
Brian O'Connorb876bf12014-10-02 14:59:37 -0700103 protected PathIntent() {
104 super();
105 this.path = null;
helenyrwu2a674902016-07-20 09:48:04 -0700106 this.type = ProtectionType.PRIMARY;
Brian O'Connorb876bf12014-10-02 14:59:37 -0700107 }
108
Ray Milkeyebc5d222015-03-18 15:45:36 -0700109 /**
110 * Returns a new host to host intent builder.
111 *
112 * @return host to host intent builder
113 */
114 public static Builder builder() {
115 return new Builder();
116 }
117
118 /**
119 * Builder of a host to host intent.
120 */
121 public static class Builder extends ConnectivityIntent.Builder {
122 Path path;
helenyrwu2a674902016-07-20 09:48:04 -0700123 ProtectionType type;
Ray Milkeyebc5d222015-03-18 15:45:36 -0700124
125 protected Builder() {
126 // Hide default constructor
127 }
128
129 @Override
130 public Builder appId(ApplicationId appId) {
131 return (Builder) super.appId(appId);
132 }
133
134 @Override
135 public Builder key(Key key) {
136 return (Builder) super.key(key);
137 }
138
139 @Override
140 public Builder selector(TrafficSelector selector) {
141 return (Builder) super.selector(selector);
142 }
143
144 @Override
145 public Builder treatment(TrafficTreatment treatment) {
146 return (Builder) super.treatment(treatment);
147 }
148
149 @Override
150 public Builder constraints(List<Constraint> constraints) {
151 return (Builder) super.constraints(constraints);
152 }
153
154 @Override
155 public Builder priority(int priority) {
156 return (Builder) super.priority(priority);
157 }
158
Luca Prete670ac5d2017-02-03 15:55:43 -0800159 @Override
160 public Builder resourceGroup(ResourceGroup resourceGroup) {
161 return (Builder) super.resourceGroup(resourceGroup);
162 }
163
Ray Milkeyebc5d222015-03-18 15:45:36 -0700164 /**
165 * Sets the path of the intent that will be built.
166 *
167 * @param path path for the intent
168 * @return this builder
169 */
170 public Builder path(Path path) {
171 this.path = path;
172 return this;
173 }
174
helenyrwu2a674902016-07-20 09:48:04 -0700175 public Builder setType(ProtectionType type) {
176 this.type = type;
177 return this;
178 }
179
Ray Milkeyebc5d222015-03-18 15:45:36 -0700180 /**
181 * Builds a path intent from the accumulated parameters.
182 *
183 * @return point to point intent
184 */
185 public PathIntent build() {
186
187 return new PathIntent(
188 appId,
ilhem fajjarib0a06842015-11-02 18:59:02 +0100189 key,
Ray Milkeyebc5d222015-03-18 15:45:36 -0700190 selector,
191 treatment,
192 path,
193 constraints,
helenyrwu2a674902016-07-20 09:48:04 -0700194 priority,
Luca Prete670ac5d2017-02-03 15:55:43 -0800195 type == null ? ProtectionType.PRIMARY : type,
196 resourceGroup
Ray Milkeyebc5d222015-03-18 15:45:36 -0700197 );
198 }
199 }
200
201
202
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800203 // NOTE: This methods takes linear time with the number of links.
204 /**
205 * Validates that source element ID and destination element ID of a link are
206 * different for the specified all links and that destination element ID of a link and source
207 * element ID of the next adjacent source element ID are same for the specified all links.
208 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -0800209 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800210 */
211 public static void validate(List<Link> links) {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700212 checkArgument(Iterables.all(links, link -> !link.src().elementId().equals(link.dst().elementId())),
213 "element of src and dst in a link must be different: {}", links);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800214
215 boolean adjacentSame = true;
216 for (int i = 0; i < links.size() - 1; i++) {
217 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
218 adjacentSame = false;
219 break;
220 }
221 }
222 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
223 }
224
Brian O'Connorb876bf12014-10-02 14:59:37 -0700225 /**
226 * Returns the links which the traffic goes along.
227 *
228 * @return traversed links
229 */
tom85258ee2014-10-07 00:10:02 -0700230 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700231 return path;
232 }
233
helenyrwu2a674902016-07-20 09:48:04 -0700234 public ProtectionType type() {
235 return type;
236 }
237
Brian O'Connorb876bf12014-10-02 14:59:37 -0700238 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700239 public String toString() {
240 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700241 .add("id", id())
ilhem fajjarib0a06842015-11-02 18:59:02 +0100242 .add("key", key())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700243 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700244 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800245 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700246 .add("selector", selector())
247 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800248 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700249 .add("path", path)
helenyrwu2a674902016-07-20 09:48:04 -0700250 .add("type", type)
Luca Prete670ac5d2017-02-03 15:55:43 -0800251 .add("resourceGroup", resourceGroup())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700252 .toString();
253 }
tom95329eb2014-10-06 08:40:06 -0700254
helenyrwu2a674902016-07-20 09:48:04 -0700255 // for path protection purposes
Yuta HIGUCHI4859b3a2016-08-16 12:58:23 -0700256 @Beta
helenyrwu2a674902016-07-20 09:48:04 -0700257 public enum ProtectionType {
258 /**
259 * Intent within primary path.
260 */
261 PRIMARY,
262 /**
263 * Intent within backup path.
264 */
265 BACKUP,
266 /**
267 * Intent whose flow rule serves as the fast failover
268 * between primary and backup paths.
269 */
270 FAILOVER
271 }
272
Brian O'Connorb876bf12014-10-02 14:59:37 -0700273}