Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 18 | import java.util.List; |
| 19 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 20 | import com.google.common.annotations.Beta; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.Path; |
| 24 | import org.onosproject.net.flow.TrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 26 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 27 | import com.google.common.base.MoreObjects; |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 28 | import com.google.common.collect.Iterables; |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 29 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Abstraction of explicitly path specified connectivity intent. |
| 34 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 35 | @Beta |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 36 | public class PathIntent extends ConnectivityIntent { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 37 | |
| 38 | private final Path path; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 39 | private ProtectionType type; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Creates a new point-to-point intent with the supplied ingress/egress |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 43 | * ports and using the specified explicit path. Path is primary by default. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 44 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 45 | * @param appId application identifier |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 46 | * @param key intent key |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 47 | * @param selector traffic selector |
| 48 | * @param treatment treatment |
| 49 | * @param path traversed links |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 50 | * @param constraints optional list of constraints |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 51 | * @param priority priority to use for the generated flows |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 52 | * @throws NullPointerException {@code path} is null |
| 53 | */ |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 54 | protected PathIntent(ApplicationId appId, |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 55 | Key key, |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 56 | TrafficSelector selector, |
| 57 | TrafficTreatment treatment, |
| 58 | Path path, |
| 59 | List<Constraint> constraints, |
| 60 | int priority) { |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 61 | 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 fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 88 | super(appId, key, resources(path.links()), selector, treatment, constraints, |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 89 | priority); |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 90 | PathIntent.validate(path.links()); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 91 | this.path = path; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 92 | this.type = type; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 96 | * Constructor for serializer. |
| 97 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 98 | protected PathIntent() { |
| 99 | super(); |
| 100 | this.path = null; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 101 | this.type = ProtectionType.PRIMARY; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 104 | /** |
| 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; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 118 | ProtectionType type; |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 119 | |
| 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 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 165 | public Builder setType(ProtectionType type) { |
| 166 | this.type = type; |
| 167 | return this; |
| 168 | } |
| 169 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 170 | /** |
| 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 fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 179 | key, |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 180 | selector, |
| 181 | treatment, |
| 182 | path, |
| 183 | constraints, |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 184 | priority, |
| 185 | type == null ? ProtectionType.PRIMARY : type |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 186 | ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 192 | // 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 SHIMIZU | 7338ccc | 2014-11-21 15:00:53 -0800 | [diff] [blame] | 198 | * @param links links to be validated |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 199 | */ |
| 200 | public static void validate(List<Link> links) { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 201 | 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 SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 203 | |
| 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'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Returns the links which the traffic goes along. |
| 216 | * |
| 217 | * @return traversed links |
| 218 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 219 | public Path path() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 220 | return path; |
| 221 | } |
| 222 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 223 | public ProtectionType type() { |
| 224 | return type; |
| 225 | } |
| 226 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 227 | @Override |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 228 | public String toString() { |
| 229 | return MoreObjects.toStringHelper(getClass()) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 230 | .add("id", id()) |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 231 | .add("key", key()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 232 | .add("appId", appId()) |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 233 | .add("priority", priority()) |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 234 | .add("resources", resources()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 235 | .add("selector", selector()) |
| 236 | .add("treatment", treatment()) |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 237 | .add("constraints", constraints()) |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 238 | .add("path", path) |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 239 | .add("type", type) |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 240 | .toString(); |
| 241 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 242 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 243 | // for path protection purposes |
Yuta HIGUCHI | 4859b3a | 2016-08-16 12:58:23 -0700 | [diff] [blame] | 244 | @Beta |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 245 | public enum ProtectionType { |
| 246 | /** |
| 247 | * Intent within primary path. |
| 248 | */ |
| 249 | PRIMARY, |
| 250 | /** |
| 251 | * Intent within backup path. |
| 252 | */ |
| 253 | BACKUP, |
| 254 | /** |
| 255 | * Intent whose flow rule serves as the fast failover |
| 256 | * between primary and backup paths. |
| 257 | */ |
| 258 | FAILOVER |
| 259 | } |
| 260 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 261 | } |