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;
15 import java.io.Serializable;
16 import java.util.Collection;
17 import java.util.Collections;
19 import org.opendaylight.yangtools.concepts.Builder;
20 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
21 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
24 * A builder for creating RpcResult instances.
26 * @author Thomas Pantelis
28 * @param <T> the result value type
30 public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
32 private static class RpcResultImpl<T> implements RpcResult<T>, Serializable {
33 private static final long serialVersionUID = 1L;
35 private final Collection<RpcError> errors;
36 private final T result;
37 private final boolean successful;
39 RpcResultImpl( final boolean successful, final T result,
40 final Collection<RpcError> errors ) {
41 this.successful = successful;
47 public Collection<RpcError> getErrors() {
52 public T getResult() {
57 public boolean isSuccessful() {
62 public String toString(){
63 return "RpcResult [successful=" + successful + ", result="
64 + result + ", errors=" + errors + "]";
68 private static class RpcErrorImpl implements RpcError, Serializable {
69 private static final long serialVersionUID = 1L;
71 private final String applicationTag;
72 private final String tag;
73 private final String info;
74 private final ErrorSeverity severity;
75 private final String message;
76 private final ErrorType errorType;
77 private final Throwable cause;
79 RpcErrorImpl( final ErrorSeverity severity, final ErrorType errorType,
80 final String tag, final String message, final String applicationTag, final String info,
81 final Throwable cause ) {
82 this.severity = severity;
83 this.errorType = errorType;
85 this.message = message;
86 this.applicationTag = applicationTag;
92 public String getApplicationTag() {
93 return applicationTag;
97 public String getTag() {
102 public String getInfo() {
107 public ErrorSeverity getSeverity() {
112 public String getMessage(){
117 public ErrorType getErrorType() {
122 public Throwable getCause() {
127 public String toString(){
128 return "RpcError [message=" + message + ", severity="
129 + severity + ", errorType=" + errorType + ", tag=" + tag
130 + ", applicationTag=" + applicationTag + ", info=" + info
131 + ", cause=" + cause + "]";
135 private ImmutableList.Builder<RpcError> errors;
137 private final boolean successful;
139 private RpcResultBuilder( final boolean successful, final T result ) {
140 this.successful = successful;
141 this.result = result;
145 * Returns a builder for a successful result.
147 public static <T> RpcResultBuilder<T> success() {
148 return new RpcResultBuilder<T>( true, null );
152 * Returns a builder for a successful result.
154 * @param result the result value
156 public static <T> RpcResultBuilder<T> success( final T result ) {
157 return new RpcResultBuilder<T>( true, result );
161 * Returns a builder for a successful result.
163 * @param builder builder for the result value
165 public static <T> RpcResultBuilder<T> success( final Builder<T> builder ) {
166 return success(builder.build());
170 * Returns a builder for a failed result.
172 public static <T> RpcResultBuilder<T> failed() {
173 return new RpcResultBuilder<T>( false, null );
177 * Returns a builder based on the given status.
179 * @param success true if successful, false otherwise.
181 public static <T> RpcResultBuilder<T> status( final boolean success ) {
182 return new RpcResultBuilder<T>( success, null );
186 * Returns a builder from another RpcResult.
188 * @param other the other RpcResult.
190 public static <T> RpcResultBuilder<T> from( final RpcResult<T> other ) {
191 return new RpcResultBuilder<T>( other.isSuccessful(), other.getResult() )
192 .withRpcErrors( other.getErrors() );
196 * Creates an RpcError with severity ERROR for reuse.
198 * @param errorType the conceptual layer at which the error occurred.
199 * @param tag a short string that identifies the general type of error condition. See
200 * {@link RpcError#getTag} for a list of suggested values.
201 * @param message a string suitable for human display that describes the error condition.
203 * @return an RpcError
205 public static RpcError newError( final ErrorType errorType, final String tag, final String message ) {
206 return new RpcErrorImpl( ErrorSeverity.ERROR, errorType,
207 tag != null ? tag : "operation-failed", message, null, null, null );
211 * Creates an RpcError with severity ERROR for reuse.
213 * @param errorType the conceptual layer at which the error occurred.
214 * @param tag a short string that identifies the general type of error condition. See
215 * {@link RpcError#getTag} for a list of suggested values.
216 * @param message a string suitable for human display that describes the error condition.
217 * * @param applicationTag a short string that identifies the specific type of error condition.
218 * @param info a string containing additional information to provide extended
219 * and/or implementation-specific debugging information.
220 * @param cause the exception that triggered the error.
222 * @return an RpcError
224 public static RpcError newError( final ErrorType errorType, final String tag, final String message,
225 final String applicationTag, final String info, final Throwable cause ) {
226 return new RpcErrorImpl( ErrorSeverity.ERROR, errorType,
227 tag != null ? tag : "operation-failed", message, applicationTag, info, cause );
231 * Creates an RpcError with severity WARNING for reuse.
233 * @param errorType the conceptual layer at which the warning occurred.
234 * @param tag a short string that identifies the general type of warning condition. See
235 * {@link RpcError#getTag} for a list of suggested values.
236 * @param message a string suitable for human display that describes the warning condition.
238 * @return an RpcError
240 public static RpcError newWarning( final ErrorType errorType, final String tag, final String message ) {
241 return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
245 * Creates an RpcError with severity WARNING for reuse.
247 * @param errorType the conceptual layer at which the warning occurred.
248 * @param tag a short string that identifies the general type of warning condition. See
249 * {@link RpcError#getTag} for a list of suggested values.
250 * @param message a string suitable for human display that describes the warning condition.
251 * * @param applicationTag a short string that identifies the specific type of warning condition.
252 * @param info a string containing additional information to provide extended
253 * and/or implementation-specific debugging information.
254 * @param cause the exception that triggered the warning.
256 * @return an RpcError
258 public static RpcError newWarning( final ErrorType errorType, final String tag, final String message,
259 final String applicationTag, final String info, final Throwable cause ) {
260 return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message,
261 applicationTag, info, cause );
265 * Sets the value of the result.
267 * @param result the result value
269 public RpcResultBuilder<T> withResult( final T result ) {
270 this.result = result;
275 * Sets the value of the result.
277 * @param builder builder for the result value
279 public RpcResultBuilder<T> withResult( final Builder<T> builder ) {
280 return withResult(builder.build());
283 private void addError( final ErrorSeverity severity, final ErrorType errorType,
284 final String tag, final String message, final String applicationTag, final String info,
285 final Throwable cause ) {
287 addError( new RpcErrorImpl( severity, errorType,
288 tag != null ? tag : "operation-failed", message,
289 applicationTag, info, cause ) );
292 private void addError( final RpcError error ) {
294 if( errors == null ) {
295 errors = new ImmutableList.Builder<RpcError>();
302 * Adds a warning to the result.
304 * @param errorType the conceptual layer at which the warning occurred.
305 * @param tag a short string that identifies the general type of warning condition. See
306 * {@link RpcError#getTag} for a list of suggested values.
307 * @param message a string suitable for human display that describes the warning condition.
309 public RpcResultBuilder<T> withWarning( final ErrorType errorType, final String tag, final String message ) {
310 addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
315 * Adds a warning to the result.
317 * @param errorType the conceptual layer at which the warning occurred.
318 * @param tag a short string that identifies the general type of warning condition. See
319 * {@link RpcError#getTag} for a list of suggested values.
320 * @param message a string suitable for human display that describes the warning condition.
321 * @param applicationTag a short string that identifies the specific type of warning condition.
322 * @param info a string containing additional information to provide extended
323 * and/or implementation-specific debugging information.
324 * @param cause the exception that triggered the warning.
326 public RpcResultBuilder<T> withWarning( final ErrorType errorType, final String tag, final String message,
327 final String applicationTag, final String info, final Throwable cause ) {
328 addError( ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause );
333 * Adds an error to the result. The general error tag defaults to "operation-failed".
335 * @param errorType the conceptual layer at which the error occurred.
336 * @param message a string suitable for human display that describes the error condition.
338 public RpcResultBuilder<T> withError( final ErrorType errorType, final String message ) {
339 addError( ErrorSeverity.ERROR, errorType, null, message, null, null, null );
344 * Adds an error to the result.
346 * @param errorType the conceptual layer at which the error occurred.
347 * @param tag a short string that identifies the general type of error condition. See
348 * {@link RpcError#getTag} for a list of suggested values.
349 * @param message a string suitable for human display that describes the error condition.
351 public RpcResultBuilder<T> withError( final ErrorType errorType, final String tag, final String message ) {
352 addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
357 * Adds an error to the result. The general error tag defaults to "operation-failed".
359 * @param errorType the conceptual layer at which the error occurred.
360 * @param message a string suitable for human display that describes the error condition.
361 * @param cause the exception that triggered the error.
363 public RpcResultBuilder<T> withError( final ErrorType errorType, final String message,
364 final Throwable cause ) {
365 addError( ErrorSeverity.ERROR, errorType, null, message, null, null, cause );
370 * Adds an error to the result.
372 * @param errorType the conceptual layer at which the error occurred.
373 * @param tag a short string that identifies the general type of error condition. See
374 * {@link RpcError#getTag} for a list of suggested values.
375 * @param message a string suitable for human display that describes the error condition.
376 * @param applicationTag a short string that identifies the specific type of error condition.
377 * @param info a string containing additional information to provide extended
378 * and/or implementation-specific debugging information.
379 * @param cause the exception that triggered the error.
381 public RpcResultBuilder<T> withError( final ErrorType errorType, final String tag, final String message,
382 final String applicationTag, final String info, final Throwable cause ) {
383 addError( ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause );
390 * @param error the RpcError
392 public RpcResultBuilder<T> withRpcError( final RpcError error ) {
400 * @param errors the list of RpcErrors
402 public RpcResultBuilder<T> withRpcErrors( final Collection<RpcError> errors ) {
403 if( errors != null ) {
404 for( RpcError error: errors ) {
412 public RpcResult<T> build() {
414 return new RpcResultImpl<T>( successful, result,
415 errors != null ? errors.build() : Collections.<RpcError>emptyList() );
419 * Builds RpcResult and wraps it in a Future
421 * This is a convenience method to assist those writing rpcs
422 * that produce immediate results. It allows you to replace
424 * Futures.immediateFuture(rpcResult.build())
428 * rpcResult.buildFuture();
430 * @return Future for RpcResult built by RpcResultBuilder
433 public ListenableFuture<RpcResult<T>> buildFuture() {
434 return Futures.immediateFuture(build());