Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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; |
| 39 | |
| 40 | /** |
| 41 | * Creates a new point-to-point intent with the supplied ingress/egress |
| 42 | * ports and using the specified explicit path. |
| 43 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 44 | * @param appId application identifier |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 45 | * @param key intent key |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 46 | * @param selector traffic selector |
| 47 | * @param treatment treatment |
| 48 | * @param path traversed links |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 49 | * @param constraints optional list of constraints |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 50 | * @param priority priority to use for the generated flows |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 51 | * @throws NullPointerException {@code path} is null |
| 52 | */ |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 53 | protected PathIntent(ApplicationId appId, |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 54 | Key key, |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 55 | TrafficSelector selector, |
| 56 | TrafficTreatment treatment, |
| 57 | Path path, |
| 58 | List<Constraint> constraints, |
| 59 | int priority) { |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 60 | super(appId, key, resources(path.links()), selector, treatment, constraints, |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 61 | priority); |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 62 | PathIntent.validate(path.links()); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 63 | this.path = path; |
| 64 | } |
| 65 | |
| 66 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 67 | * Constructor for serializer. |
| 68 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 69 | protected PathIntent() { |
| 70 | super(); |
| 71 | this.path = null; |
| 72 | } |
| 73 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Returns a new host to host intent builder. |
| 76 | * |
| 77 | * @return host to host intent builder |
| 78 | */ |
| 79 | public static Builder builder() { |
| 80 | return new Builder(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Builder of a host to host intent. |
| 85 | */ |
| 86 | public static class Builder extends ConnectivityIntent.Builder { |
| 87 | Path path; |
| 88 | |
| 89 | protected Builder() { |
| 90 | // Hide default constructor |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Builder appId(ApplicationId appId) { |
| 95 | return (Builder) super.appId(appId); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Builder key(Key key) { |
| 100 | return (Builder) super.key(key); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Builder selector(TrafficSelector selector) { |
| 105 | return (Builder) super.selector(selector); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Builder treatment(TrafficTreatment treatment) { |
| 110 | return (Builder) super.treatment(treatment); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Builder constraints(List<Constraint> constraints) { |
| 115 | return (Builder) super.constraints(constraints); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Builder priority(int priority) { |
| 120 | return (Builder) super.priority(priority); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets the path of the intent that will be built. |
| 125 | * |
| 126 | * @param path path for the intent |
| 127 | * @return this builder |
| 128 | */ |
| 129 | public Builder path(Path path) { |
| 130 | this.path = path; |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Builds a path intent from the accumulated parameters. |
| 136 | * |
| 137 | * @return point to point intent |
| 138 | */ |
| 139 | public PathIntent build() { |
| 140 | |
| 141 | return new PathIntent( |
| 142 | appId, |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 143 | key, |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 144 | selector, |
| 145 | treatment, |
| 146 | path, |
| 147 | constraints, |
| 148 | priority |
| 149 | ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 155 | // NOTE: This methods takes linear time with the number of links. |
| 156 | /** |
| 157 | * Validates that source element ID and destination element ID of a link are |
| 158 | * different for the specified all links and that destination element ID of a link and source |
| 159 | * element ID of the next adjacent source element ID are same for the specified all links. |
| 160 | * |
Sho SHIMIZU | 7338ccc | 2014-11-21 15:00:53 -0800 | [diff] [blame] | 161 | * @param links links to be validated |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 162 | */ |
| 163 | public static void validate(List<Link> links) { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 164 | checkArgument(Iterables.all(links, link -> !link.src().elementId().equals(link.dst().elementId())), |
| 165 | "element of src and dst in a link must be different: {}", links); |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 166 | |
| 167 | boolean adjacentSame = true; |
| 168 | for (int i = 0; i < links.size() - 1; i++) { |
| 169 | if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) { |
| 170 | adjacentSame = false; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | checkArgument(adjacentSame, "adjacent links must share the same element: {}", links); |
| 175 | } |
| 176 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Returns the links which the traffic goes along. |
| 179 | * |
| 180 | * @return traversed links |
| 181 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 182 | public Path path() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 183 | return path; |
| 184 | } |
| 185 | |
| 186 | @Override |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 187 | public String toString() { |
| 188 | return MoreObjects.toStringHelper(getClass()) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 189 | .add("id", id()) |
ilhem fajjari | b0a0684 | 2015-11-02 18:59:02 +0100 | [diff] [blame] | 190 | .add("key", key()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 191 | .add("appId", appId()) |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 192 | .add("priority", priority()) |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 193 | .add("resources", resources()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 194 | .add("selector", selector()) |
| 195 | .add("treatment", treatment()) |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 196 | .add("constraints", constraints()) |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 197 | .add("path", path) |
| 198 | .toString(); |
| 199 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 200 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 201 | } |