2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.common;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.io.Serializable;
15 import java.util.Collection;
16 import java.util.Collections;
17 import org.opendaylight.yangtools.concepts.Builder;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
22 * A builder for creating RpcResult instances.
24 * @author Thomas Pantelis
26 * @param <T> the result value type
28 public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
30 private static class RpcResultImpl<T> implements RpcResult<T>, Serializable {
31 private static final long serialVersionUID = 1L;
33 private final Collection<RpcError> errors;
34 private final T result;
35 private final boolean successful;
37 RpcResultImpl(final boolean successful, final T result,
38 final Collection<RpcError> errors) {
39 this.successful = successful;
45 public Collection<RpcError> getErrors() {
50 public T getResult() {
55 public boolean isSuccessful() {
60 public String toString() {
61 return "RpcResult [successful=" + successful + ", result="
62 + result + ", errors=" + errors + "]";
66 private static class RpcErrorImpl implements RpcError, Serializable {
67 private static final long serialVersionUID = 1L;
69 private final String applicationTag;
70 private final String tag;
71 private final String info;
72 private final ErrorSeverity severity;
73 private final String message;
74 private final ErrorType errorType;
75 private final Throwable cause;
77 RpcErrorImpl(final ErrorSeverity severity, final ErrorType errorType,
78 final String tag, final String message, final String applicationTag, final String info,
79 final Throwable cause) {
80 this.severity = severity;
81 this.errorType = errorType;
83 this.message = message;
84 this.applicationTag = applicationTag;
90 public String getApplicationTag() {
91 return applicationTag;
95 public String getTag() {
100 public String getInfo() {
105 public ErrorSeverity getSeverity() {
110 public String getMessage() {
115 public ErrorType getErrorType() {
120 public Throwable getCause() {
125 public String toString() {
126 return "RpcError [message=" + message + ", severity="
127 + severity + ", errorType=" + errorType + ", tag=" + tag
128 + ", applicationTag=" + applicationTag + ", info=" + info
129 + ", cause=" + cause + "]";
133 private ImmutableList.Builder<RpcError> errors;
135 private final boolean successful;
137 private RpcResultBuilder(final boolean successful, final T result) {
138 this.successful = successful;
139 this.result = result;
143 * Returns a builder for a successful result.
145 public static <T> RpcResultBuilder<T> success() {
146 return new RpcResultBuilder<>(true, null);
150 * Returns a builder for a successful result.
152 * @param result the result value
154 public static <T> RpcResultBuilder<T> success(final T result) {
155 return new RpcResultBuilder<>(true, result);
159 * Returns a builder for a successful result.
161 * @param builder builder for the result value
163 public static <T> RpcResultBuilder<T> success(final Builder<T> builder) {
164 return success(builder.build());
168 * Returns a builder for a failed result.
170 public static <T> RpcResultBuilder<T> failed() {
171 return new RpcResultBuilder<>(false, null);
175 * Returns a builder based on the given status.
177 * @param success true if successful, false otherwise.
179 public static <T> RpcResultBuilder<T> status(final boolean success) {
180 return new RpcResultBuilder<>(success, null);
184 * Returns a builder from another RpcResult.
186 * @param other the other RpcResult.
188 public static <T> RpcResultBuilder<T> from(final RpcResult<T> other) {
189 return new RpcResultBuilder<>(other.isSuccessful(), other.getResult())
190 .withRpcErrors(other.getErrors());
194 * Creates an RpcError with severity ERROR for reuse.
196 * @param errorType the conceptual layer at which the error occurred.
197 * @param tag a short string that identifies the general type of error condition. See
198 * {@link RpcError#getTag} for a list of suggested values.
199 * @param message a string suitable for human display that describes the error condition.
201 * @return an RpcError
203 public static RpcError newError(final ErrorType errorType, final String tag, final String message) {
204 return new RpcErrorImpl(ErrorSeverity.ERROR, errorType,
205 tag != null ? tag : "operation-failed", message, null, null, null);
209 * Creates an RpcError with severity ERROR for reuse.
211 * @param errorType the conceptual layer at which the error occurred.
212 * @param tag a short string that identifies the general type of error condition. See
213 * {@link RpcError#getTag} for a list of suggested values.
214 * @param message a string suitable for human display that describes the error condition.
215 * @param applicationTag a short string that identifies the specific type of error condition.
216 * @param info a string containing additional information to provide extended
217 * and/or implementation-specific debugging information.
218 * @param cause the exception that triggered the error.
220 * @return an RpcError
222 public static RpcError newError(final ErrorType errorType, final String tag, final String message,
223 final String applicationTag, final String info, final Throwable cause) {
224 return new RpcErrorImpl(ErrorSeverity.ERROR, errorType,
225 tag != null ? tag : "operation-failed", message, applicationTag, info, cause);
229 * Creates an RpcError with severity WARNING for reuse.
231 * @param errorType the conceptual layer at which the warning occurred.
232 * @param tag a short string that identifies the general type of warning condition. See
233 * {@link RpcError#getTag} for a list of suggested values.
234 * @param message a string suitable for human display that describes the warning condition.
236 * @return an RpcError
238 public static RpcError newWarning(final ErrorType errorType, final String tag, final String message) {
239 return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message, null, null, null);
243 * Creates an RpcError with severity WARNING for reuse.
245 * @param errorType the conceptual layer at which the warning occurred.
246 * @param tag a short string that identifies the general type of warning condition. See
247 * {@link RpcError#getTag} for a list of suggested values.
248 * @param message a string suitable for human display that describes the warning condition.
249 * @param applicationTag a short string that identifies the specific type of warning condition.
250 * @param info a string containing additional information to provide extended
251 * and/or implementation-specific debugging information.
252 * @param cause the exception that triggered the warning.
254 * @return an RpcError
256 public static RpcError newWarning(final ErrorType errorType, final String tag, final String message,
257 final String applicationTag, final String info, final Throwable cause) {
258 return new RpcErrorImpl(ErrorSeverity.WARNING, errorType, tag, message,
259 applicationTag, info, cause);
263 * Sets the value of the result.
265 * @param result the result value
267 public RpcResultBuilder<T> withResult(final T result) {
268 this.result = result;
273 * Sets the value of the result.
275 * @param builder builder for the result value
277 public RpcResultBuilder<T> withResult(final Builder<T> builder) {
278 return withResult(builder.build());
281 private void addError(final ErrorSeverity severity, final ErrorType errorType,
282 final String tag, final String message, final String applicationTag, final String info,
283 final Throwable cause) {
285 addError(new RpcErrorImpl(severity, errorType,
286 tag != null ? tag : "operation-failed", message,
287 applicationTag, info, cause));
290 private void addError(final RpcError error) {
292 if (errors == null) {
293 errors = new ImmutableList.Builder<>();
300 * Adds a warning to the result.
302 * @param errorType the conceptual layer at which the warning occurred.
303 * @param tag a short string that identifies the general type of warning condition. See
304 * {@link RpcError#getTag} for a list of suggested values.
305 * @param message a string suitable for human display that describes the warning condition.
307 public RpcResultBuilder<T> withWarning(final ErrorType errorType, final String tag, final String message) {
308 addError(ErrorSeverity.WARNING, errorType, tag, message, null, null, null);
313 * Adds a warning to the result.
315 * @param errorType the conceptual layer at which the warning occurred.
316 * @param tag a short string that identifies the general type of warning condition. See
317 * {@link RpcError#getTag} for a list of suggested values.
318 * @param message a string suitable for human display that describes the warning condition.
319 * @param applicationTag a short string that identifies the specific type of warning condition.
320 * @param info a string containing additional information to provide extended
321 * and/or implementation-specific debugging information.
322 * @param cause the exception that triggered the warning.
324 public RpcResultBuilder<T> withWarning(final ErrorType errorType, final String tag, final String message,
325 final String applicationTag, final String info, final Throwable cause) {
326 addError(ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause);
331 * Adds an error to the result. The general error tag defaults to "operation-failed".
333 * @param errorType the conceptual layer at which the error occurred.
334 * @param message a string suitable for human display that describes the error condition.
336 public RpcResultBuilder<T> withError(final ErrorType errorType, final String message) {
337 addError(ErrorSeverity.ERROR, errorType, null, message, null, null, null);
342 * Adds an error to the result.
344 * @param errorType the conceptual layer at which the error occurred.
345 * @param tag a short string that identifies the general type of error condition. See
346 * {@link RpcError#getTag} for a list of suggested values.
347 * @param message a string suitable for human display that describes the error condition.
349 public RpcResultBuilder<T> withError(final ErrorType errorType, final String tag, final String message) {
350 addError(ErrorSeverity.ERROR, errorType, tag, message, null, null, null);
355 * Adds an error to the result. The general error tag defaults to "operation-failed".
357 * @param errorType the conceptual layer at which the error occurred.
358 * @param message a string suitable for human display that describes the error condition.
359 * @param cause the exception that triggered the error.
361 public RpcResultBuilder<T> withError(final ErrorType errorType, final String message,
362 final Throwable cause) {
363 addError(ErrorSeverity.ERROR, errorType, null, message, null, null, cause);
368 * Adds an error to the result.
370 * @param errorType the conceptual layer at which the error occurred.
371 * @param tag a short string that identifies the general type of error condition. See
372 * {@link RpcError#getTag} for a list of suggested values.
373 * @param message a string suitable for human display that describes the error condition.
374 * @param applicationTag a short string that identifies the specific type of error condition.
375 * @param info a string containing additional information to provide extended
376 * and/or implementation-specific debugging information.
377 * @param cause the exception that triggered the error.
379 public RpcResultBuilder<T> withError(final ErrorType errorType, final String tag, final String message,
380 final String applicationTag, final String info, final Throwable cause) {
381 addError(ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause);
388 * @param error the RpcError
390 public RpcResultBuilder<T> withRpcError(final RpcError error) {
398 * @param errors the list of RpcErrors
400 public RpcResultBuilder<T> withRpcErrors(final Collection<RpcError> errors) {
401 if (errors != null) {
402 for (RpcError error : errors) {
410 public RpcResult<T> build() {
412 return new RpcResultImpl<>(successful, result,
413 errors != null ? errors.build() : Collections.emptyList());
417 * Builds RpcResult and wraps it in a Future
420 * This is a convenience method to assist those writing rpcs
421 * that produce immediate results. It allows you to replace
423 * {@code Futures.immediateFuture(rpcResult.build())}
425 * {@code rpcResult.buildFuture();}
427 * @return Future for RpcResult built by RpcResultBuilder
430 public ListenableFuture<RpcResult<T>> buildFuture() {
431 return Futures.immediateFuture(build());