blob: b0869b7aefb3378e63ba85564733db318fa8dad3 [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
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, which can be classified
45 * as PRIMARY or BACKUP.
46 *
47 * @param appId application identifier
48 * @param key intent key
49 * @param selector traffic selector
50 * @param treatment treatment
51 * @param path traversed links
52 * @param constraints optional list of constraints
53 * @param priority priority to use for the generated flows
54 * @param type PRIMARY or BACKUP
Luca Prete670ac5d2017-02-03 15:55:43 -080055 * @param resourceGroup resource group for this intent
helenyrwu2a674902016-07-20 09:48:04 -070056 * @throws NullPointerException {@code path} is null
57 */
58 protected PathIntent(ApplicationId appId,
59 Key key,
60 TrafficSelector selector,
61 TrafficTreatment treatment,
62 Path path,
63 List<Constraint> constraints,
64 int priority,
Luca Prete670ac5d2017-02-03 15:55:43 -080065 ProtectionType type,
66 ResourceGroup resourceGroup) {
ilhem fajjarib0a06842015-11-02 18:59:02 +010067 super(appId, key, resources(path.links()), selector, treatment, constraints,
Luca Prete670ac5d2017-02-03 15:55:43 -080068 priority, resourceGroup);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080069 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080070 this.path = path;
helenyrwu2a674902016-07-20 09:48:04 -070071 this.type = type;
Ray Milkey460f4022014-11-05 15:41:43 -080072 }
73
74 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070075 * Constructor for serializer.
76 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070077 protected PathIntent() {
78 super();
79 this.path = null;
helenyrwu2a674902016-07-20 09:48:04 -070080 this.type = ProtectionType.PRIMARY;
Brian O'Connorb876bf12014-10-02 14:59:37 -070081 }
82
Ray Milkeyebc5d222015-03-18 15:45:36 -070083 /**
84 * Returns a new host to host intent builder.
85 *
86 * @return host to host intent builder
87 */
88 public static Builder builder() {
89 return new Builder();
90 }
91
92 /**
93 * Builder of a host to host intent.
94 */
95 public static class Builder extends ConnectivityIntent.Builder {
96 Path path;
helenyrwu2a674902016-07-20 09:48:04 -070097 ProtectionType type;
Ray Milkeyebc5d222015-03-18 15:45:36 -070098
99 protected Builder() {
100 // Hide default constructor
101 }
102
103 @Override
104 public Builder appId(ApplicationId appId) {
105 return (Builder) super.appId(appId);
106 }
107
108 @Override
109 public Builder key(Key key) {
110 return (Builder) super.key(key);
111 }
112
113 @Override
114 public Builder selector(TrafficSelector selector) {
115 return (Builder) super.selector(selector);
116 }
117
118 @Override
119 public Builder treatment(TrafficTreatment treatment) {
120 return (Builder) super.treatment(treatment);
121 }
122
123 @Override
124 public Builder constraints(List<Constraint> constraints) {
125 return (Builder) super.constraints(constraints);
126 }
127
128 @Override
129 public Builder priority(int priority) {
130 return (Builder) super.priority(priority);
131 }
132
Luca Prete670ac5d2017-02-03 15:55:43 -0800133 @Override
134 public Builder resourceGroup(ResourceGroup resourceGroup) {
135 return (Builder) super.resourceGroup(resourceGroup);
136 }
137
Ray Milkeyebc5d222015-03-18 15:45:36 -0700138 /**
139 * Sets the path of the intent that will be built.
140 *
141 * @param path path for the intent
142 * @return this builder
143 */
144 public Builder path(Path path) {
145 this.path = path;
146 return this;
147 }
148
helenyrwu2a674902016-07-20 09:48:04 -0700149 public Builder setType(ProtectionType type) {
150 this.type = type;
151 return this;
152 }
153
Ray Milkeyebc5d222015-03-18 15:45:36 -0700154 /**
155 * Builds a path intent from the accumulated parameters.
156 *
157 * @return point to point intent
158 */
159 public PathIntent build() {
160
161 return new PathIntent(
162 appId,
ilhem fajjarib0a06842015-11-02 18:59:02 +0100163 key,
Ray Milkeyebc5d222015-03-18 15:45:36 -0700164 selector,
165 treatment,
166 path,
167 constraints,
helenyrwu2a674902016-07-20 09:48:04 -0700168 priority,
Luca Prete670ac5d2017-02-03 15:55:43 -0800169 type == null ? ProtectionType.PRIMARY : type,
170 resourceGroup
Ray Milkeyebc5d222015-03-18 15:45:36 -0700171 );
172 }
173 }
174
175
176
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800177 // NOTE: This methods takes linear time with the number of links.
178 /**
179 * Validates that source element ID and destination element ID of a link are
180 * different for the specified all links and that destination element ID of a link and source
181 * element ID of the next adjacent source element ID are same for the specified all links.
182 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -0800183 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800184 */
185 public static void validate(List<Link> links) {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700186 checkArgument(Iterables.all(links, link -> !link.src().elementId().equals(link.dst().elementId())),
187 "element of src and dst in a link must be different: {}", links);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800188
189 boolean adjacentSame = true;
190 for (int i = 0; i < links.size() - 1; i++) {
191 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
192 adjacentSame = false;
193 break;
194 }
195 }
196 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
197 }
198
Brian O'Connorb876bf12014-10-02 14:59:37 -0700199 /**
200 * Returns the links which the traffic goes along.
201 *
202 * @return traversed links
203 */
tom85258ee2014-10-07 00:10:02 -0700204 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700205 return path;
206 }
207
helenyrwu2a674902016-07-20 09:48:04 -0700208 public ProtectionType type() {
209 return type;
210 }
211
Brian O'Connorb876bf12014-10-02 14:59:37 -0700212 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700213 public String toString() {
214 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700215 .add("id", id())
ilhem fajjarib0a06842015-11-02 18:59:02 +0100216 .add("key", key())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700217 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700218 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800219 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700220 .add("selector", selector())
221 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800222 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700223 .add("path", path)
helenyrwu2a674902016-07-20 09:48:04 -0700224 .add("type", type)
Luca Prete670ac5d2017-02-03 15:55:43 -0800225 .add("resourceGroup", resourceGroup())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700226 .toString();
227 }
tom95329eb2014-10-06 08:40:06 -0700228
helenyrwu2a674902016-07-20 09:48:04 -0700229 // for path protection purposes
Yuta HIGUCHI4859b3a2016-08-16 12:58:23 -0700230 @Beta
helenyrwu2a674902016-07-20 09:48:04 -0700231 public enum ProtectionType {
232 /**
233 * Intent within primary path.
234 */
235 PRIMARY,
236 /**
237 * Intent within backup path.
238 */
239 BACKUP,
240 /**
241 * Intent whose flow rule serves as the fast failover
242 * between primary and backup paths.
243 */
244 FAILOVER
245 }
246
Brian O'Connorb876bf12014-10-02 14:59:37 -0700247}