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.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;
30 public final class SimpleDOMActionResult implements DOMActionResult, Immutable {
31 private final Collection<RpcError> errors;
32 private final @Nullable ContainerNode output;
34 private SimpleDOMActionResult(final Collection<RpcError> errors, final @Nullable ContainerNode output) {
35 this.errors = ImmutableList.copyOf(errors);
39 public SimpleDOMActionResult(final ContainerNode output) {
40 this.errors = ImmutableList.of();
41 this.output = requireNonNull(output);
44 @SuppressFBWarnings("NP_NULL_PARAM_DEREF_NONVIRTUAL")
45 public SimpleDOMActionResult(final Collection<RpcError> errors) {
49 public SimpleDOMActionResult(final ContainerNode output, final Collection<RpcError> errors) {
50 this(errors, requireNonNull(output));
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);
60 public Collection<RpcError> getErrors() {
65 public Optional<ContainerNode> getOutput() {
66 return (Optional) Optional.<@Nullable ContainerNode>ofNullable(output);
70 public String toString() {
71 final ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues().add("output", output);
72 if (!errors.isEmpty()) {
73 helper.add("errors", errors);
75 return helper.toString();