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