Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | |
| 17 | package org.onosproject.p4runtime.ctl.client; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ArrayListMultimap; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableListMultimap; |
| 23 | import com.google.common.collect.ListMultimap; |
| 24 | import com.google.common.collect.Lists; |
| 25 | import com.google.common.collect.Maps; |
| 26 | import com.google.protobuf.Any; |
| 27 | import com.google.protobuf.InvalidProtocolBufferException; |
| 28 | import com.google.protobuf.TextFormat; |
| 29 | import io.grpc.Metadata; |
| 30 | import io.grpc.Status; |
| 31 | import io.grpc.StatusRuntimeException; |
| 32 | import io.grpc.protobuf.lite.ProtoLiteUtils; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.pi.runtime.PiEntity; |
| 35 | import org.onosproject.net.pi.runtime.PiEntityType; |
| 36 | import org.onosproject.net.pi.runtime.PiHandle; |
| 37 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 38 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.EntityUpdateRequest; |
| 39 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.EntityUpdateResponse; |
| 40 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.EntityUpdateStatus; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 41 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient.UpdateType; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import p4.v1.P4RuntimeOuterClass; |
| 44 | |
| 45 | import java.util.Collection; |
| 46 | import java.util.Collections; |
| 47 | import java.util.List; |
| 48 | import java.util.Map; |
| 49 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 50 | import static com.google.common.base.Preconditions.checkArgument; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 51 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 52 | import static com.google.common.base.Preconditions.checkState; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 53 | import static java.lang.String.format; |
| 54 | import static java.util.stream.Collectors.toList; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | /** |
| 58 | * Handles the creation of WriteResponse and parsing of P4Runtime errors |
| 59 | * received from server, as well as logging of RPC errors. |
| 60 | */ |
| 61 | final class WriteResponseImpl implements P4RuntimeWriteClient.WriteResponse { |
| 62 | |
| 63 | private static final Metadata.Key<com.google.rpc.Status> STATUS_DETAILS_KEY = |
| 64 | Metadata.Key.of( |
| 65 | "grpc-status-details-bin", |
| 66 | ProtoLiteUtils.metadataMarshaller( |
| 67 | com.google.rpc.Status.getDefaultInstance())); |
| 68 | |
| 69 | static final WriteResponseImpl EMPTY = new WriteResponseImpl( |
| 70 | ImmutableList.of(), ImmutableListMultimap.of()); |
| 71 | |
| 72 | private static final Logger log = getLogger(WriteResponseImpl.class); |
| 73 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 74 | private final ImmutableList<EntityUpdateResponse> entityResponses; |
| 75 | private final ImmutableListMultimap<EntityUpdateStatus, EntityUpdateResponse> statusMultimap; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 76 | |
| 77 | private WriteResponseImpl( |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 78 | ImmutableList<EntityUpdateResponse> allResponses, |
| 79 | ImmutableListMultimap<EntityUpdateStatus, EntityUpdateResponse> statusMultimap) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 80 | this.entityResponses = allResponses; |
| 81 | this.statusMultimap = statusMultimap; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean isSuccess() { |
| 86 | return success().size() == all().size(); |
| 87 | } |
| 88 | |
| 89 | @Override |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 90 | public Collection<EntityUpdateResponse> all() { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 91 | return entityResponses; |
| 92 | } |
| 93 | |
| 94 | @Override |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 95 | public Collection<EntityUpdateResponse> success() { |
| 96 | return statusMultimap.get(EntityUpdateStatus.OK); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 100 | public Collection<EntityUpdateResponse> failed() { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 101 | return isSuccess() |
| 102 | ? Collections.emptyList() |
| 103 | : entityResponses.stream().filter(r -> !r.isSuccess()).collect(toList()); |
| 104 | } |
| 105 | |
| 106 | @Override |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 107 | public Collection<EntityUpdateResponse> status( |
| 108 | EntityUpdateStatus status) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 109 | checkNotNull(status); |
| 110 | return statusMultimap.get(status); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns a new response builder for the given device. |
| 115 | * |
| 116 | * @param deviceId device ID |
| 117 | * @return response builder |
| 118 | */ |
| 119 | static Builder builder(DeviceId deviceId) { |
| 120 | return new Builder(deviceId); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Builder of P4RuntimeWriteResponseImpl. |
| 125 | */ |
| 126 | static final class Builder { |
| 127 | |
| 128 | private final DeviceId deviceId; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 129 | private final Map<Integer, EntityUpdateResponseImpl> pendingResponses = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 130 | Maps.newHashMap(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 131 | private final List<EntityUpdateResponse> allResponses = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 132 | Lists.newArrayList(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 133 | private final ListMultimap<EntityUpdateStatus, EntityUpdateResponse> statusMap = |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 134 | ArrayListMultimap.create(); |
| 135 | |
| 136 | private Builder(DeviceId deviceId) { |
| 137 | this.deviceId = deviceId; |
| 138 | } |
| 139 | |
| 140 | void addPendingResponse(PiHandle handle, PiEntity entity, UpdateType updateType) { |
| 141 | synchronized (this) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 142 | final EntityUpdateResponseImpl resp = new EntityUpdateResponseImpl( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 143 | handle, entity, updateType); |
| 144 | allResponses.add(resp); |
| 145 | pendingResponses.put(pendingResponses.size(), resp); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void addFailedResponse(PiHandle handle, PiEntity entity, UpdateType updateType, |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 150 | String explanation, EntityUpdateStatus status) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 151 | synchronized (this) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 152 | final EntityUpdateResponseImpl resp = new EntityUpdateResponseImpl( |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 153 | handle, entity, updateType) |
| 154 | .withFailure(explanation, status); |
| 155 | allResponses.add(resp); |
| 156 | } |
| 157 | } |
| 158 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 159 | Collection<EntityUpdateRequest> pendingUpdates() { |
| 160 | return ImmutableList.copyOf(pendingResponses.values()); |
| 161 | } |
| 162 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 163 | WriteResponseImpl buildAsIs() { |
| 164 | synchronized (this) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 165 | final long pendingCount = pendingResponses.values().stream() |
| 166 | .filter(r -> r.status() == EntityUpdateStatus.PENDING) |
| 167 | .count(); |
| 168 | if (pendingCount > 0) { |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 169 | log.warn("Partial response from {}, {} of {} total " + |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 170 | "updates are still in status PENDING", |
| 171 | deviceId, pendingCount, allResponses.size()); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 172 | } |
| 173 | return new WriteResponseImpl( |
| 174 | ImmutableList.copyOf(allResponses), |
| 175 | ImmutableListMultimap.copyOf(statusMap)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | WriteResponseImpl setSuccessAllAndBuild() { |
| 180 | synchronized (this) { |
| 181 | pendingResponses.values().forEach(this::doSetSuccess); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 182 | return buildAsIs(); |
| 183 | } |
| 184 | } |
| 185 | |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 186 | WriteResponseImpl setFailAllAndBuild(Throwable throwable) { |
| 187 | synchronized (this) { |
| 188 | pendingResponses.values().forEach(r -> r.setFailure(throwable)); |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 189 | return buildAsIs(); |
| 190 | } |
| 191 | } |
| 192 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 193 | WriteResponseImpl setErrorsAndBuild(Throwable throwable) { |
| 194 | synchronized (this) { |
| 195 | return doSetErrorsAndBuild(throwable); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private void setSuccess(int index) { |
| 200 | synchronized (this) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 201 | final EntityUpdateResponseImpl resp = pendingResponses.get(index); |
| 202 | if (resp != null && resp.status == EntityUpdateStatus.PENDING) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 203 | doSetSuccess(resp); |
| 204 | } else { |
| 205 | log.error("Missing pending response at index {}", index); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 210 | private void doSetSuccess(EntityUpdateResponseImpl resp) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 211 | resp.setSuccess(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 212 | statusMap.put(EntityUpdateStatus.OK, resp); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private void setFailure(int index, |
| 216 | String explanation, |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 217 | EntityUpdateStatus status) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 218 | synchronized (this) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 219 | final EntityUpdateResponseImpl resp = pendingResponses.get(index); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 220 | if (resp != null) { |
| 221 | resp.withFailure(explanation, status); |
| 222 | statusMap.put(status, resp); |
| 223 | log.warn("Unable to {} {} on {}: {} {} [{}]", |
| 224 | resp.updateType(), |
| 225 | resp.entityType().humanReadableName(), |
| 226 | deviceId, |
| 227 | status, explanation, |
| 228 | resp.entity() != null ? resp.entity() : resp.handle()); |
| 229 | } else { |
| 230 | log.error("Missing pending response at index {}", index); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | private WriteResponseImpl doSetErrorsAndBuild(Throwable throwable) { |
| 236 | if (!(throwable instanceof StatusRuntimeException)) { |
| 237 | // Leave all entity responses in pending state. |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 238 | return setFailAllAndBuild(throwable); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 239 | } |
| 240 | final StatusRuntimeException sre = (StatusRuntimeException) throwable; |
| 241 | if (!sre.getStatus().equals(Status.UNKNOWN)) { |
| 242 | // Error trailers expected only if status is UNKNOWN. |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 243 | return setFailAllAndBuild(throwable); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 244 | } |
| 245 | // Extract error details. |
| 246 | if (!sre.getTrailers().containsKey(STATUS_DETAILS_KEY)) { |
| 247 | log.warn("Cannot parse write error details from {}, " + |
| 248 | "missing status trailers in StatusRuntimeException", |
| 249 | deviceId); |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 250 | return setFailAllAndBuild(throwable); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 251 | } |
| 252 | com.google.rpc.Status status = sre.getTrailers().get(STATUS_DETAILS_KEY); |
| 253 | if (status == null) { |
| 254 | log.warn("Cannot parse write error details from {}, " + |
| 255 | "found NULL status trailers in StatusRuntimeException", |
| 256 | deviceId); |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 257 | return setFailAllAndBuild(throwable); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 258 | } |
| 259 | final boolean reconcilable = status.getDetailsList().size() == pendingResponses.size(); |
| 260 | // We expect one error for each entity... |
| 261 | if (!reconcilable) { |
| 262 | log.warn("Unable to reconcile write error details from {}, " + |
| 263 | "sent {} updates, but server returned {} errors", |
| 264 | deviceId, pendingResponses.size(), status.getDetailsList().size()); |
| 265 | } |
| 266 | // ...in the same order as in the request. |
| 267 | int index = 0; |
| 268 | for (Any any : status.getDetailsList()) { |
| 269 | // Set response entities only if reconcilable, otherwise log. |
| 270 | unpackP4Error(index, any, reconcilable); |
| 271 | index += 1; |
| 272 | } |
| 273 | return buildAsIs(); |
| 274 | } |
| 275 | |
| 276 | private void unpackP4Error(int index, Any any, boolean reconcilable) { |
| 277 | final P4RuntimeOuterClass.Error p4Error; |
| 278 | try { |
| 279 | p4Error = any.unpack(P4RuntimeOuterClass.Error.class); |
| 280 | } catch (InvalidProtocolBufferException e) { |
| 281 | final String unpackErr = format( |
| 282 | "P4Runtime Error message format not recognized [%s]", |
| 283 | TextFormat.shortDebugString(any)); |
| 284 | if (reconcilable) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 285 | setFailure(index, unpackErr, EntityUpdateStatus.OTHER_ERROR); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 286 | } else { |
| 287 | log.warn(unpackErr); |
| 288 | } |
| 289 | return; |
| 290 | } |
| 291 | // Map gRPC status codes to our WriteResponseStatus codes. |
| 292 | final Status.Code p4Code = Status.fromCodeValue( |
| 293 | p4Error.getCanonicalCode()).getCode(); |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 294 | final EntityUpdateStatus ourCode; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 295 | switch (p4Code) { |
| 296 | case OK: |
| 297 | if (reconcilable) { |
| 298 | setSuccess(index); |
| 299 | } |
| 300 | return; |
| 301 | case NOT_FOUND: |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 302 | ourCode = EntityUpdateStatus.NOT_FOUND; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 303 | break; |
| 304 | case ALREADY_EXISTS: |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 305 | ourCode = EntityUpdateStatus.ALREADY_EXIST; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 306 | break; |
| 307 | default: |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 308 | ourCode = EntityUpdateStatus.OTHER_ERROR; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | // Put the p4Code in the explanation only if ourCode is OTHER_ERROR. |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 312 | final String explanationCode = ourCode == EntityUpdateStatus.OTHER_ERROR |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 313 | ? p4Code.name() + " " : ""; |
| 314 | final String details = p4Error.hasDetails() |
| 315 | ? ", " + p4Error.getDetails().toString() : ""; |
| 316 | final String explanation = format( |
| 317 | "%s%s%s (%s:%d)", explanationCode, p4Error.getMessage(), |
| 318 | details, p4Error.getSpace(), p4Error.getCode()); |
| 319 | if (reconcilable) { |
| 320 | setFailure(index, explanation, ourCode); |
| 321 | } else { |
| 322 | log.warn("P4Runtime write error: {}", explanation); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 328 | * Internal implementation of EntityUpdateResponse. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 329 | */ |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 330 | private static final class EntityUpdateResponseImpl implements EntityUpdateResponse { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 331 | |
| 332 | private final PiHandle handle; |
| 333 | private final PiEntity entity; |
| 334 | private final UpdateType updateType; |
| 335 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 336 | private EntityUpdateStatus status = EntityUpdateStatus.PENDING; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 337 | private String explanation; |
| 338 | private Throwable throwable; |
| 339 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 340 | private EntityUpdateResponseImpl(PiHandle handle, PiEntity entity, UpdateType updateType) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 341 | this.handle = handle; |
| 342 | this.entity = entity; |
| 343 | this.updateType = updateType; |
| 344 | } |
| 345 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 346 | private EntityUpdateResponseImpl withFailure( |
| 347 | String explanation, EntityUpdateStatus status) { |
| 348 | setStatus(status); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 349 | this.explanation = explanation; |
| 350 | this.throwable = null; |
| 351 | return this; |
| 352 | } |
| 353 | |
| 354 | private void setSuccess() { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 355 | setStatus(EntityUpdateStatus.OK); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 358 | private void setFailure(Throwable throwable) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 359 | setStatus(EntityUpdateStatus.OTHER_ERROR); |
Carmelo Cascone | b203b64 | 2019-02-06 17:23:05 -0800 | [diff] [blame] | 360 | this.explanation = throwable.toString(); |
| 361 | this.throwable = throwable; |
| 362 | } |
| 363 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 364 | private void setStatus(EntityUpdateStatus newStatus) { |
| 365 | checkState(this.status == EntityUpdateStatus.PENDING, |
| 366 | "Cannot set status for non-pending update"); |
| 367 | checkArgument(newStatus != EntityUpdateStatus.PENDING, |
| 368 | "newStatus must be different than pending"); |
| 369 | this.status = newStatus; |
| 370 | } |
| 371 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 372 | @Override |
| 373 | public PiHandle handle() { |
| 374 | return handle; |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public PiEntity entity() { |
| 379 | return entity; |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public UpdateType updateType() { |
| 384 | return updateType; |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public PiEntityType entityType() { |
| 389 | return handle.entityType(); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public boolean isSuccess() { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 394 | return status().equals(EntityUpdateStatus.OK); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame^] | 398 | public EntityUpdateStatus status() { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 399 | return status; |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public String explanation() { |
| 404 | return explanation; |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public Throwable throwable() { |
| 409 | return throwable; |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public String toString() { |
| 414 | return MoreObjects.toStringHelper(this) |
| 415 | .add("handle", handle) |
| 416 | .add("entity", entity) |
| 417 | .add("updateType", updateType) |
| 418 | .add("status", status) |
| 419 | .add("explanation", explanation) |
| 420 | .add("throwable", throwable) |
| 421 | .toString(); |
| 422 | } |
| 423 | } |
| 424 | } |