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 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
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.NetworkResource; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 28 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
Sho SHIMIZU | c3df36b | 2014-11-11 18:19:29 -0800 | [diff] [blame] | 30 | import java.util.Collections; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 31 | import java.util.List; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 32 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Abstraction of connectivity intent for traffic matching some criteria. |
| 37 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 38 | @Beta |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 39 | public abstract class ConnectivityIntent extends Intent { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 40 | |
| 41 | // TODO: other forms of intents should be considered for this family: |
| 42 | // point-to-point with constraints (waypoints/obstacles) |
| 43 | // multi-to-single point with constraints (waypoints/obstacles) |
| 44 | // single-to-multi point with constraints (waypoints/obstacles) |
| 45 | // concrete path (with alternate) |
| 46 | // ... |
| 47 | |
| 48 | private final TrafficSelector selector; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 49 | private final TrafficTreatment treatment; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 50 | private final List<Constraint> constraints; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 53 | * Creates a connectivity intent that matches on the specified selector |
| 54 | * and applies the specified treatment. |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 55 | * <p> |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 56 | * Path will be optimized based on the first constraint if one is given. |
| 57 | * </p> |
| 58 | * |
| 59 | * @param appId application identifier |
| 60 | * @param key explicit key to use for intent |
| 61 | * @param resources required network resources (optional) |
| 62 | * @param selector traffic selector |
| 63 | * @param treatment treatment |
| 64 | * @param constraints optional prioritized list of constraints |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 65 | * @param priority priority to use for flows generated by this intent |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 66 | * @throws NullPointerException if the selector or treatment is null |
| 67 | */ |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 68 | protected ConnectivityIntent(ApplicationId appId, |
| 69 | Key key, |
| 70 | Collection<NetworkResource> resources, |
| 71 | TrafficSelector selector, |
| 72 | TrafficTreatment treatment, |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 73 | List<Constraint> constraints, |
| 74 | int priority) { |
| 75 | super(appId, key, resources, priority); |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 76 | this.selector = checkNotNull(selector); |
| 77 | this.treatment = checkNotNull(treatment); |
| 78 | this.constraints = checkNotNull(constraints); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 82 | * Constructor for serializer. |
| 83 | */ |
| 84 | protected ConnectivityIntent() { |
| 85 | super(); |
| 86 | this.selector = null; |
| 87 | this.treatment = null; |
Sho SHIMIZU | c3df36b | 2014-11-11 18:19:29 -0800 | [diff] [blame] | 88 | this.constraints = Collections.emptyList(); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 92 | * Abstract builder for connectivity intents. |
| 93 | */ |
| 94 | public abstract static class Builder extends Intent.Builder { |
| 95 | protected TrafficSelector selector = DefaultTrafficSelector.emptySelector(); |
| 96 | protected TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 97 | protected List<Constraint> constraints = ImmutableList.of(); |
| 98 | |
Jonathan Hart | b14221c | 2016-03-07 09:55:50 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Creates a new empty builder. |
| 101 | */ |
| 102 | protected Builder() { |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates a new builder pre-populated with the information in the given |
| 107 | * intent. |
| 108 | * |
| 109 | * @param intent initial intent |
| 110 | */ |
| 111 | protected Builder(ConnectivityIntent intent) { |
| 112 | super(intent); |
| 113 | |
| 114 | this.selector(intent.selector()) |
| 115 | .treatment(intent.treatment()) |
| 116 | .constraints(intent.constraints()); |
| 117 | } |
| 118 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public Builder appId(ApplicationId appId) { |
| 121 | return (Builder) super.appId(appId); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Builder key(Key key) { |
| 126 | return (Builder) super.key(key); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Builder priority(int priority) { |
| 131 | return (Builder) super.priority(priority); |
| 132 | } |
| 133 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Sets the traffic selector for the intent that will be built. |
| 136 | * |
| 137 | * @param selector selector to use for built intent |
| 138 | * @return this builder |
| 139 | */ |
| 140 | public Builder selector(TrafficSelector selector) { |
| 141 | this.selector = selector; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Sets the traffic treatment for the intent that will be built. |
| 147 | * |
| 148 | * @param treatment treatment to use for built intent |
| 149 | * @return this builder |
| 150 | */ |
| 151 | public Builder treatment(TrafficTreatment treatment) { |
| 152 | this.treatment = treatment; |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the constraints for the intent that will be built. |
| 158 | * |
| 159 | * @param constraints constraints to use for built intent |
| 160 | * @return this builder |
| 161 | */ |
| 162 | public Builder constraints(List<Constraint> constraints) { |
| 163 | this.constraints = ImmutableList.copyOf(constraints); |
| 164 | return this; |
| 165 | } |
| 166 | } |
| 167 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 168 | /** |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 169 | * Returns the match specifying the type of traffic. |
| 170 | * |
| 171 | * @return traffic match |
| 172 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 173 | public TrafficSelector selector() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 174 | return selector; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the action applied to the traffic. |
| 179 | * |
| 180 | * @return applied action |
| 181 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 182 | public TrafficTreatment treatment() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 183 | return treatment; |
| 184 | } |
| 185 | |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 186 | /** |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 187 | * Returns the set of connectivity constraints. |
| 188 | * |
| 189 | * @return list of intent constraints |
| 190 | */ |
| 191 | public List<Constraint> constraints() { |
| 192 | return constraints; |
| 193 | } |
| 194 | |
| 195 | /** |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 196 | * Produces a collection of network resources from the given links. |
| 197 | * |
| 198 | * @param links collection of links |
| 199 | * @return collection of link resources |
| 200 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 201 | protected static Collection<NetworkResource> resources(Collection<Link> links) { |
Sho SHIMIZU | f4dbc726 | 2014-11-04 18:49:02 -0800 | [diff] [blame] | 202 | return ImmutableSet.copyOf(links); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | } |