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.util.Collection;
15 import java.util.Collections;
16 import org.opendaylight.yangtools.concepts.Builder;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
21 * A builder for creating RpcResult instances.
23 * @author Thomas Pantelis
25 * @param <T> the result value type
27 public final class RpcResultBuilder<T> implements Builder<RpcResult<T>> {
29 private static class RpcResultImpl<T> implements RpcResult<T> {
31 private final Collection<RpcError> errors;
32 private final T result;
33 private final boolean successful;
35 RpcResultImpl( final boolean successful, final T result,
36 final Collection<RpcError> errors ) {
37 this.successful = successful;
43 public Collection<RpcError> getErrors() {
48 public T getResult() {
53 public boolean isSuccessful() {
58 public String toString(){
59 return "RpcResult [successful=" + successful + ", result="
60 + result + ", errors=" + errors + "]";
64 private static class RpcErrorImpl implements RpcError {
66 private final String applicationTag;
67 private final String tag;
68 private final String info;
69 private final ErrorSeverity severity;
70 private final String message;
71 private final ErrorType errorType;
72 private final Throwable cause;
74 RpcErrorImpl( final ErrorSeverity severity, final ErrorType errorType,
75 final String tag, final String message, final String applicationTag, final String info,
76 final Throwable cause ) {
77 this.severity = severity;
78 this.errorType = errorType;
80 this.message = message;
81 this.applicationTag = applicationTag;
87 public String getApplicationTag() {
88 return applicationTag;
92 public String getTag() {
97 public String getInfo() {
102 public ErrorSeverity getSeverity() {
107 public String getMessage(){
112 public ErrorType getErrorType() {
117 public Throwable getCause() {
122 public String toString(){
123 return "RpcError [message=" + message + ", severity="
124 + severity + ", errorType=" + errorType + ", tag=" + tag
125 + ", applicationTag=" + applicationTag + ", info=" + info
126 + ", cause=" + cause + "]";
130 private ImmutableList.Builder<RpcError> errors;
132 private final boolean successful;
134 private RpcResultBuilder( final boolean successful, final T result ) {
135 this.successful = successful;
136 this.result = result;
140 * Returns a builder for a successful result.
142 public static <T> RpcResultBuilder<T> success() {
143 return new RpcResultBuilder<T>( true, null );
147 * Returns a builder for a successful result.
149 * @param result the result value
151 public static <T> RpcResultBuilder<T> success( final T result ) {
152 return new RpcResultBuilder<T>( true, result );
156 * Returns a builder for a successful result.
158 * @param builder builder for the result value
160 public static <T> RpcResultBuilder<T> success( final Builder<T> builder ) {
161 return success(builder.build());
165 * Returns a builder for a failed result.
167 public static <T> RpcResultBuilder<T> failed() {
168 return new RpcResultBuilder<T>( false, null );
172 * Returns a builder based on the given status.
174 * @param success true if successful, false otherwise.
176 public static <T> RpcResultBuilder<T> status( final boolean success ) {
177 return new RpcResultBuilder<T>( success, null );
181 * Returns a builder from another RpcResult.
183 * @param other the other RpcResult.
185 public static <T> RpcResultBuilder<T> from( final RpcResult<T> other ) {
186 return new RpcResultBuilder<T>( other.isSuccessful(), other.getResult() )
187 .withRpcErrors( other.getErrors() );
191 * Creates an RpcError with severity ERROR for reuse.
193 * @param errorType the conceptual layer at which the error occurred.
194 * @param tag a short string that identifies the general type of error condition. See
195 * {@link RpcError#getTag} for a list of suggested values.
196 * @param message a string suitable for human display that describes the error condition.
198 * @return an RpcError
200 public static RpcError newError( final ErrorType errorType, final String tag, final String message ) {
201 return new RpcErrorImpl( ErrorSeverity.ERROR, errorType,
202 tag != null ? tag : "operation-failed", message, null, null, null );
206 * Creates an RpcError with severity ERROR for reuse.
208 * @param errorType the conceptual layer at which the error occurred.
209 * @param tag a short string that identifies the general type of error condition. See
210 * {@link RpcError#getTag} for a list of suggested values.
211 * @param message a string suitable for human display that describes the error condition.
212 * * @param applicationTag a short string that identifies the specific type of error condition.
213 * @param info a string containing additional information to provide extended
214 * and/or implementation-specific debugging information.
215 * @param cause the exception that triggered the error.
217 * @return an RpcError
219 public static RpcError newError( final ErrorType errorType, final String tag, final String message,
220 final String applicationTag, final String info, final Throwable cause ) {
221 return new RpcErrorImpl( ErrorSeverity.ERROR, errorType,
222 tag != null ? tag : "operation-failed", message, applicationTag, info, cause );
226 * Creates an RpcError with severity WARNING for reuse.
228 * @param errorType the conceptual layer at which the warning occurred.
229 * @param tag a short string that identifies the general type of warning condition. See
230 * {@link RpcError#getTag} for a list of suggested values.
231 * @param message a string suitable for human display that describes the warning condition.
233 * @return an RpcError
235 public static RpcError newWarning( final ErrorType errorType, final String tag, final String message ) {
236 return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
240 * Creates an RpcError with severity WARNING for reuse.
242 * @param errorType the conceptual layer at which the warning occurred.
243 * @param tag a short string that identifies the general type of warning condition. See
244 * {@link RpcError#getTag} for a list of suggested values.
245 * @param message a string suitable for human display that describes the warning condition.
246 * * @param applicationTag a short string that identifies the specific type of warning condition.
247 * @param info a string containing additional information to provide extended
248 * and/or implementation-specific debugging information.
249 * @param cause the exception that triggered the warning.
251 * @return an RpcError
253 public static RpcError newWarning( final ErrorType errorType, final String tag, final String message,
254 final String applicationTag, final String info, final Throwable cause ) {
255 return new RpcErrorImpl( ErrorSeverity.WARNING, errorType, tag, message,
256 applicationTag, info, cause );
260 * Sets the value of the result.
262 * @param result the result value
264 public RpcResultBuilder<T> withResult( final T result ) {
265 this.result = result;
270 * Sets the value of the result.
272 * @param builder builder for the result value
274 public RpcResultBuilder<T> withResult( final Builder<T> builder ) {
275 return withResult(builder.build());
278 private void addError( final ErrorSeverity severity, final ErrorType errorType,
279 final String tag, final String message, final String applicationTag, final String info,
280 final Throwable cause ) {
282 addError( new RpcErrorImpl( severity, errorType,
283 tag != null ? tag : "operation-failed", message,
284 applicationTag, info, cause ) );
287 private void addError( final RpcError error ) {
289 if( errors == null ) {
290 errors = new ImmutableList.Builder<RpcError>();
297 * Adds a warning to the result.
299 * @param errorType the conceptual layer at which the warning occurred.
300 * @param tag a short string that identifies the general type of warning condition. See
301 * {@link RpcError#getTag} for a list of suggested values.
302 * @param message a string suitable for human display that describes the warning condition.
304 public RpcResultBuilder<T> withWarning( final ErrorType errorType, final String tag, final String message ) {
305 addError( ErrorSeverity.WARNING, errorType, tag, message, null, null, null );
310 * Adds a warning to the result.
312 * @param errorType the conceptual layer at which the warning occurred.
313 * @param tag a short string that identifies the general type of warning condition. See
314 * {@link RpcError#getTag} for a list of suggested values.
315 * @param message a string suitable for human display that describes the warning condition.
316 * @param applicationTag a short string that identifies the specific type of warning condition.
317 * @param info a string containing additional information to provide extended
318 * and/or implementation-specific debugging information.
319 * @param cause the exception that triggered the warning.
321 public RpcResultBuilder<T> withWarning( final ErrorType errorType, final String tag, final String message,
322 final String applicationTag, final String info, final Throwable cause ) {
323 addError( ErrorSeverity.WARNING, errorType, tag, message, applicationTag, info, cause );
328 * Adds an error to the result. The general error tag defaults to "operation-failed".
330 * @param errorType the conceptual layer at which the error occurred.
331 * @param message a string suitable for human display that describes the error condition.
333 public RpcResultBuilder<T> withError( final ErrorType errorType, final String message ) {
334 addError( ErrorSeverity.ERROR, errorType, null, message, null, null, null );
339 * Adds an error to the result.
341 * @param errorType the conceptual layer at which the error occurred.
342 * @param tag a short string that identifies the general type of error condition. See
343 * {@link RpcError#getTag} for a list of suggested values.
344 * @param message a string suitable for human display that describes the error condition.
346 public RpcResultBuilder<T> withError( final ErrorType errorType, final String tag, final String message ) {
347 addError( ErrorSeverity.ERROR, errorType, tag, message, null, null, null );
352 * Adds an error to the result. The general error tag defaults to "operation-failed".
354 * @param errorType the conceptual layer at which the error occurred.
355 * @param message a string suitable for human display that describes the error condition.
356 * @param cause the exception that triggered the error.
358 public RpcResultBuilder<T> withError( final ErrorType errorType, final String message,
359 final Throwable cause ) {
360 addError( ErrorSeverity.ERROR, errorType, null, message, null, null, cause );
365 * Adds an error to the result.
367 * @param errorType the conceptual layer at which the error occurred.
368 * @param tag a short string that identifies the general type of error condition. See
369 * {@link RpcError#getTag} for a list of suggested values.
370 * @param message a string suitable for human display that describes the error condition.
371 * @param applicationTag a short string that identifies the specific type of error condition.
372 * @param info a string containing additional information to provide extended
373 * and/or implementation-specific debugging information.
374 * @param cause the exception that triggered the error.
376 public RpcResultBuilder<T> withError( final ErrorType errorType, final String tag, final String message,
377 final String applicationTag, final String info, final Throwable cause ) {
378 addError( ErrorSeverity.ERROR, errorType, tag, message, applicationTag, info, cause );
385 * @param error the RpcError
387 public RpcResultBuilder<T> withRpcError( final RpcError error ) {
395 * @param errors the list of RpcErrors
397 public RpcResultBuilder<T> withRpcErrors( final Collection<RpcError> errors ) {
398 if( errors != null ) {
399 for( RpcError error: errors ) {
407 public RpcResult<T> build() {
409 return new RpcResultImpl<T>( successful, result,
410 errors != null ? errors.build() : Collections.<RpcError>emptyList() );
414 * Builds RpcResult and wraps it in a Future
416 * This is a convenience method to assist those writing rpcs
417 * that produce immediate results. It allows you to replace
419 * Futures.immediateFuture(rpcResult.build())
423 * rpcResult.buildFuture();
425 * @return Future for RpcResult built by RpcResultBuilder
428 public ListenableFuture<RpcResult<T>> buildFuture() {
429 return Futures.immediateFuture(build());