pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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.segmentrouting.policy.impl; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | import java.util.Optional; |
| 20 | |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | import org.onosproject.net.flowobjective.Objective; |
| 23 | import org.onosproject.segmentrouting.policy.api.Policy; |
| 24 | import org.onosproject.segmentrouting.policy.api.TrafficMatch; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * keep track of the operation and of its status in the system. |
| 30 | */ |
| 31 | final class Operation { |
| 32 | private boolean isInstall; |
| 33 | private boolean isDone; |
| 34 | private Objective objectiveOperation; |
| 35 | private Policy policy; |
| 36 | private TrafficMatch trafficMatch; |
| 37 | |
| 38 | private Operation(boolean install, boolean done, Objective objective, |
| 39 | Policy pol, TrafficMatch tMatch) { |
| 40 | isInstall = install; |
| 41 | isDone = done; |
| 42 | objectiveOperation = objective; |
| 43 | policy = pol; |
| 44 | trafficMatch = tMatch; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns whether or not the operation is done. |
| 49 | * |
| 50 | * @return true if operation is done. False otherwise. |
| 51 | */ |
| 52 | public boolean isDone() { |
| 53 | return isDone; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns whether or not it is an installation. |
| 58 | * |
| 59 | * @return true if it is an installation. False for removal |
| 60 | */ |
| 61 | public boolean isInstall() { |
| 62 | return isInstall; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the objective operation. |
| 67 | * |
| 68 | * @return the associated flow objective |
| 69 | */ |
| 70 | public Objective objectiveOperation() { |
| 71 | return objectiveOperation; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the policy if present. |
| 76 | * |
| 77 | * @return the policy |
| 78 | */ |
| 79 | public Optional<Policy> policy() { |
| 80 | return Optional.ofNullable(policy); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the traffic match if present. |
| 85 | * |
| 86 | * @return the traffic match |
| 87 | */ |
| 88 | public Optional<TrafficMatch> trafficMatch() { |
| 89 | return Optional.ofNullable(trafficMatch); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Updates isDone. |
| 94 | * |
| 95 | * @param isDone if it is done |
| 96 | */ |
| 97 | public void isDone(boolean isDone) { |
| 98 | this.isDone = isDone; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Updates the flowObjective. |
| 103 | * |
| 104 | * @param objectiveOperation the flowObjective |
| 105 | */ |
| 106 | public void objectiveOperation(Objective objectiveOperation) { |
| 107 | this.objectiveOperation = objectiveOperation; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean equals(final Object obj) { |
| 112 | if (this == obj) { |
| 113 | return true; |
| 114 | } |
| 115 | if (!(obj instanceof Operation)) { |
| 116 | return false; |
| 117 | } |
| 118 | final Operation other = (Operation) obj; |
| 119 | return this.isInstall == other.isInstall && |
| 120 | this.isDone == other.isDone && |
| 121 | Objects.equals(this.objectiveOperation, other.objectiveOperation) && |
| 122 | Objects.equals(this.policy, other.policy) && |
| 123 | Objects.equals(this.trafficMatch, other.trafficMatch); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int hashCode() { |
| 128 | return Objects.hash(isInstall, isDone, objectiveOperation, policy, trafficMatch); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String toString() { |
| 133 | MoreObjects.ToStringHelper helper = toStringHelper(this) |
| 134 | .add("isInstall", isInstall) |
| 135 | .add("isDone", isDone) |
| 136 | .add("objective", objectiveOperation); |
| 137 | if (policy != null) { |
| 138 | helper.add("policy", policy); |
| 139 | } |
| 140 | if (trafficMatch != null) { |
| 141 | helper.add("trafficMatch", trafficMatch); |
| 142 | } |
| 143 | return helper.toString(); |
| 144 | } |
| 145 | |
| 146 | public String toStringMinimal() { |
| 147 | MoreObjects.ToStringHelper helper = toStringHelper(this) |
| 148 | .add("isInstall", isInstall) |
| 149 | .add("isDone", isDone); |
| 150 | if (policy != null) { |
| 151 | helper.add("policy", policy); |
| 152 | } |
| 153 | if (trafficMatch != null) { |
| 154 | helper.add("trafficMatch", trafficMatch); |
| 155 | } |
| 156 | return helper.toString(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Creates a new operation builder. |
| 161 | * |
| 162 | * @return an operation builder |
| 163 | */ |
| 164 | public static Operation.Builder builder() { |
| 165 | return new Operation.Builder(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Creates a new operation builder using the supplied operation. |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame^] | 170 | * The boolean isDone and the objective operation won't be copied |
| 171 | * by the supplied operation. |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 172 | * |
| 173 | * @param operation the operation |
| 174 | * @return an operation builder |
| 175 | */ |
| 176 | public static Operation.Builder builder(Operation operation) { |
| 177 | return new Operation.Builder(operation); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Builder for Operation objects. |
| 182 | */ |
| 183 | public static final class Builder { |
| 184 | private boolean isInstall; |
| 185 | private boolean isDone; |
| 186 | private Objective objectiveOperation; |
| 187 | private Policy policy; |
| 188 | private TrafficMatch trafficMatch; |
| 189 | |
| 190 | private Builder() { |
| 191 | // Hide constructor |
| 192 | } |
| 193 | |
| 194 | private Builder(Operation operation) { |
| 195 | isInstall = operation.isInstall(); |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 196 | policy = operation.policy().orElse(null); |
| 197 | trafficMatch = operation.trafficMatch().orElse(null); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets the flowObjective. |
| 202 | * |
| 203 | * @param objectiveOperation the flowObjective |
| 204 | * @return this builder |
| 205 | */ |
| 206 | public Builder objectiveOperation(Objective objectiveOperation) { |
| 207 | this.objectiveOperation = objectiveOperation; |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Sets if it is done. |
| 213 | * |
| 214 | * @param isDone if it is done |
| 215 | * @return this builder |
| 216 | */ |
| 217 | public Builder isDone(boolean isDone) { |
| 218 | this.isDone = isDone; |
| 219 | return this; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Sets if it is an installation. |
| 224 | * |
| 225 | * @param isInstall if it is an installation |
| 226 | * @return this builder |
| 227 | */ |
| 228 | public Builder isInstall(boolean isInstall) { |
| 229 | this.isInstall = isInstall; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Sets the policy. |
| 235 | * |
| 236 | * @param policy the policy |
| 237 | * @return this builder |
| 238 | */ |
| 239 | public Builder policy(Policy policy) { |
| 240 | this.policy = policy; |
| 241 | return this; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets the traffic match. |
| 246 | * |
| 247 | * @param trafficMatch the traffic match |
| 248 | * @return this builder |
| 249 | */ |
| 250 | public Builder trafficMatch(TrafficMatch trafficMatch) { |
| 251 | this.trafficMatch = trafficMatch; |
| 252 | return this; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Builds an operation object from the accumulated parameters. |
| 257 | * |
| 258 | * @return operation object |
| 259 | */ |
| 260 | public Operation build() { |
| 261 | if ((policy == null && trafficMatch == null) || |
| 262 | (policy != null && trafficMatch != null)) { |
| 263 | throw new IllegalArgumentException("Policy and traffic cannot be both null or both set"); |
| 264 | } |
| 265 | return new Operation(isInstall, isDone, objectiveOperation, policy, trafficMatch); |
| 266 | } |
| 267 | } |
| 268 | } |