alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.net.flowobjective; |
| 17 | |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.net.flow.TrafficTreatment; |
| 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.List; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 25 | import java.util.Optional; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Default implementation of a next objective. |
| 32 | */ |
Thomas Vachuska | a9d491e | 2015-05-20 11:17:21 -0700 | [diff] [blame] | 33 | @Beta |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 34 | public final class DefaultNextObjective implements NextObjective { |
| 35 | |
| 36 | private final List<TrafficTreatment> treatments; |
| 37 | private final ApplicationId appId; |
| 38 | private final Type type; |
| 39 | private final Integer id; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 40 | private final Operation op; |
| 41 | private final Optional<ObjectiveContext> context; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 42 | |
| 43 | private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments, |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 44 | ApplicationId appId, Type type, Operation op) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 45 | this.treatments = treatments; |
| 46 | this.appId = appId; |
| 47 | this.type = type; |
| 48 | this.id = id; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 49 | this.op = op; |
| 50 | this.context = Optional.empty(); |
| 51 | } |
| 52 | |
| 53 | private DefaultNextObjective(Integer id, List<TrafficTreatment> treatments, |
| 54 | ApplicationId appId, ObjectiveContext context, |
| 55 | Type type, Operation op) { |
| 56 | this.treatments = treatments; |
| 57 | this.appId = appId; |
| 58 | this.type = type; |
| 59 | this.id = id; |
| 60 | this.op = op; |
| 61 | this.context = Optional.ofNullable(context); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public Collection<TrafficTreatment> next() { |
| 66 | return treatments; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Type type() { |
| 71 | return type; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int id() { |
| 76 | return id; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int priority() { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public ApplicationId appId() { |
| 86 | return appId; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int timeout() { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean permanent() { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Operation op() { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 101 | return op; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Optional<ObjectiveContext> context() { |
| 106 | return context; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns a new builder. |
| 111 | * |
| 112 | * @return new builder |
| 113 | */ |
| 114 | public static Builder builder() { |
| 115 | return new Builder(); |
| 116 | } |
| 117 | |
| 118 | public static final class Builder implements NextObjective.Builder { |
| 119 | |
| 120 | private ApplicationId appId; |
| 121 | private Type type; |
| 122 | private Integer id; |
| 123 | |
| 124 | private final ImmutableList.Builder<TrafficTreatment> listBuilder |
| 125 | = ImmutableList.builder(); |
| 126 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public NextObjective.Builder withId(int nextId) { |
| 129 | this.id = nextId; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Builder withType(Type type) { |
| 135 | this.type = type; |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Builder addTreatment(TrafficTreatment treatment) { |
| 141 | listBuilder.add(treatment); |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Noop. This method has no effect. |
| 147 | * |
| 148 | * @param timeout a timeout |
| 149 | * @return a next objective builder |
| 150 | */ |
| 151 | @Override |
| 152 | public Builder makeTemporary(int timeout) { |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Noop. This method has no effect. |
| 158 | * |
| 159 | * @return a next objective builder |
| 160 | */ |
| 161 | @Override |
| 162 | public Builder makePermanent() { |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 167 | public NextObjective.Builder fromApp(ApplicationId appId) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 168 | this.appId = appId; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Noop. This method has no effect. |
| 174 | * |
| 175 | * @param priority an integer |
| 176 | * @return a next objective builder |
| 177 | */ |
| 178 | @Override |
| 179 | public Builder withPriority(int priority) { |
| 180 | return this; |
| 181 | } |
| 182 | |
| 183 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 184 | public NextObjective add() { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 185 | List<TrafficTreatment> treatments = listBuilder.build(); |
| 186 | checkNotNull(appId, "Must supply an application id"); |
| 187 | checkNotNull(id, "id cannot be null"); |
| 188 | checkNotNull(type, "The type cannot be null"); |
| 189 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 190 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 191 | return new DefaultNextObjective(id, treatments, appId, type, Operation.ADD); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public NextObjective remove() { |
| 196 | List<TrafficTreatment> treatments = listBuilder.build(); |
| 197 | checkNotNull(appId, "Must supply an application id"); |
| 198 | checkNotNull(id, "id cannot be null"); |
| 199 | checkNotNull(type, "The type cannot be null"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 200 | |
| 201 | return new DefaultNextObjective(id, treatments, appId, type, Operation.REMOVE); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public NextObjective add(ObjectiveContext context) { |
| 206 | List<TrafficTreatment> treatments = listBuilder.build(); |
| 207 | checkNotNull(appId, "Must supply an application id"); |
| 208 | checkNotNull(id, "id cannot be null"); |
| 209 | checkNotNull(type, "The type cannot be null"); |
| 210 | checkArgument(!treatments.isEmpty(), "Must have at least one treatment"); |
| 211 | |
| 212 | return new DefaultNextObjective(id, treatments, appId, |
| 213 | context, type, Operation.ADD); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public NextObjective remove(ObjectiveContext context) { |
| 218 | List<TrafficTreatment> treatments = listBuilder.build(); |
| 219 | checkNotNull(appId, "Must supply an application id"); |
| 220 | checkNotNull(id, "id cannot be null"); |
| 221 | checkNotNull(type, "The type cannot be null"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 222 | |
| 223 | return new DefaultNextObjective(id, treatments, appId, |
| 224 | context, type, Operation.REMOVE); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |