aec8e492f58043cc5bd1bb37bdc6d9ed24cc3138
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / SimpleDOMActionResult.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.collect.ImmutableList;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.Collection;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.mdsal.dom.api.DOMActionResult;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27
28 @Beta
29 @NonNullByDefault
30 public final class SimpleDOMActionResult implements DOMActionResult, Immutable {
31     private final Collection<RpcError> errors;
32     private final @Nullable ContainerNode output;
33
34     private SimpleDOMActionResult(final Collection<RpcError> errors, final @Nullable ContainerNode output) {
35         this.errors = ImmutableList.copyOf(errors);
36         this.output = output;
37     }
38
39     public SimpleDOMActionResult(final ContainerNode output) {
40         this.errors = ImmutableList.of();
41         this.output = requireNonNull(output);
42     }
43
44     @SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL")
45     public SimpleDOMActionResult(final Collection<RpcError> errors) {
46         this(errors, null);
47     }
48
49     public SimpleDOMActionResult(final ContainerNode output, final Collection<RpcError> errors) {
50         this(errors, requireNonNull(output));
51     }
52
53     // As per RFC7950 page 80 (top)
54     public static SimpleDOMActionResult ofMalformedMessage(final Exception cause) {
55         return new SimpleDOMActionResult(ImmutableList.of(RpcResultBuilder.newError(ErrorType.RPC, "malformed-message",
56             cause.getMessage(), null, null, requireNonNull(cause))), null);
57     }
58
59     @Override
60     public Collection<RpcError> getErrors() {
61         return errors;
62     }
63
64     @Override
65     public Optional<ContainerNode> getOutput() {
66         return (Optional) Optional.<@Nullable ContainerNode>ofNullable(output);
67     }
68
69     @Override
70     public String toString() {
71         final ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues().add("output", output);
72         if (!errors.isEmpty()) {
73             helper.add("errors", errors);
74         }
75         return helper.toString();
76     }
77 }