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