2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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
8 package org.opendaylight.mdsal.dom.spi;
10 import static java.util.Objects.requireNonNull;
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.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 public final class SimpleDOMActionResult implements DOMActionResult, Immutable {
32 private final Collection<RpcError> errors;
33 private final @Nullable ContainerNode output;
35 private SimpleDOMActionResult(final Collection<RpcError> errors, final @Nullable ContainerNode output) {
36 this.errors = ImmutableList.copyOf(errors);
40 public SimpleDOMActionResult(final ContainerNode output) {
41 errors = ImmutableList.of();
42 this.output = requireNonNull(output);
45 @SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL")
46 public SimpleDOMActionResult(final Collection<RpcError> errors) {
50 public SimpleDOMActionResult(final ContainerNode output, final Collection<RpcError> errors) {
51 this(errors, requireNonNull(output));
54 // As per RFC7950 page 80 (top)
55 public static SimpleDOMActionResult ofMalformedMessage(final Exception cause) {
56 return new SimpleDOMActionResult(ImmutableList.of(RpcResultBuilder.newError(ErrorType.RPC,
57 ErrorTag.MALFORMED_MESSAGE, cause.getMessage(), null, null, requireNonNull(cause))), null);
61 public Collection<RpcError> getErrors() {
66 public Optional<ContainerNode> getOutput() {
67 return (Optional) Optional.<@Nullable ContainerNode>ofNullable(output);
71 public String toString() {
72 final ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues().add("output", output);
73 if (!errors.isEmpty()) {
74 helper.add("errors", errors);
76 return helper.toString();