Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ResourceGroup; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 27 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 28 | import com.google.common.base.MoreObjects; |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 29 | import com.google.common.collect.Iterables; |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 30 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Abstraction of explicitly path specified connectivity intent. |
| 35 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 36 | @Beta |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 37 | public class PathIntent extends ConnectivityIntent { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 38 | |
| 39 | private final Path path; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 40 | private ProtectionType type; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Creates a new point-to-point intent with the supplied ingress/egress |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 44 | * 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 Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 55 | * @param resourceGroup resource group for this intent |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 56 | * @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 Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 65 | ProtectionType type, |
| 66 | ResourceGroup resourceGroup) { |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 67 | super(appId, key, resources(path.links()), selector, treatment, constraints, |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 68 | priority, resourceGroup); |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 69 | PathIntent.validate(path.links()); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 70 | this.path = path; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 71 | this.type = type; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 75 | * Constructor for serializer. |
| 76 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 77 | protected PathIntent() { |
| 78 | super(); |
| 79 | this.path = null; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 80 | this.type = ProtectionType.PRIMARY; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 83 | /** |
| 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; |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 97 | ProtectionType type; |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 98 | |
| 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 Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 133 | @Override |
| 134 | public Builder resourceGroup(ResourceGroup resourceGroup) { |
| 135 | return (Builder) super.resourceGroup(resourceGroup); |
| 136 | } |
| 137 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 138 | /** |
| 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 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 149 | public Builder setType(ProtectionType type) { |
| 150 | this.type = type; |
| 151 | return this; |
| 152 | } |
| 153 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 154 | /** |
| 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 fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 163 | key, |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 164 | selector, |
| 165 | treatment, |
| 166 | path, |
| 167 | constraints, |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 168 | priority, |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 169 | type == null ? ProtectionType.PRIMARY : type, |
| 170 | resourceGroup |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 171 | ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 177 | // 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 SHIMIZU | 7338ccc | 2014-11-21 15:00:53 -0800 | [diff] [blame] | 183 | * @param links links to be validated |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 184 | */ |
| 185 | public static void validate(List<Link> links) { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 186 | 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 SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 188 | |
| 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'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Returns the links which the traffic goes along. |
| 201 | * |
| 202 | * @return traversed links |
| 203 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 204 | public Path path() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 205 | return path; |
| 206 | } |
| 207 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 208 | public ProtectionType type() { |
| 209 | return type; |
| 210 | } |
| 211 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 212 | @Override |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 213 | public String toString() { |
| 214 | return MoreObjects.toStringHelper(getClass()) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 215 | .add("id", id()) |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 216 | .add("key", key()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 217 | .add("appId", appId()) |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 218 | .add("priority", priority()) |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 219 | .add("resources", resources()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 220 | .add("selector", selector()) |
| 221 | .add("treatment", treatment()) |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 222 | .add("constraints", constraints()) |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 223 | .add("path", path) |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 224 | .add("type", type) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 225 | .add("resourceGroup", resourceGroup()) |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 226 | .toString(); |
| 227 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 228 | |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 229 | // for path protection purposes |
Yuta HIGUCHI | 4859b3a | 2016-08-16 12:58:23 -0700 | [diff] [blame] | 230 | @Beta |
helenyrwu | 2a67490 | 2016-07-20 09:48:04 -0700 | [diff] [blame] | 231 | 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'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 247 | } |