Add YangErrorInfo 82/96882/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Jul 2021 08:54:42 +0000 (10:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Jul 2021 09:00:26 +0000 (11:00 +0200)
Add YangErrorInfo class to hold NormalizedNode-based additional
information for RpcError-like constructs. Also add YangErrorInfos, which
makes it super easy to create well-known error infos like bad-element et
al.

JIRA: YANGTOOLS-1304
Change-Id: If2f45fb0c617396531de7592e29d4e09a1c06f3a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangErrorInfo.java [new file with mode: 0644]
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/YangErrorInfos.java [new file with mode: 0644]

diff --git a/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangErrorInfo.java b/data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangErrorInfo.java
new file mode 100644 (file)
index 0000000..e162abb
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ * An element of {@code error-info} container, as modeled in {@code errorInfoType} of
+ * <a href="https://datatracker.ietf.org/doc/html/rfc6241#appendix-B">RFC6241, Appendix B</a>.
+ */
+@Beta
+@NonNullByDefault
+public final class YangErrorInfo {
+    private final NormalizedNode value;
+
+    private YangErrorInfo(final NormalizedNode value) {
+        this.value = requireNonNull(value);
+    }
+
+    public static YangErrorInfo of(final NormalizedNode value) {
+        return new YangErrorInfo(value);
+    }
+
+    public NormalizedNode value() {
+        return value;
+    }
+
+    @Override
+    public int hashCode() {
+        return value.hashCode();
+    }
+
+    @Override
+    public boolean equals(final @Nullable Object obj) {
+        return obj == this || obj instanceof YangErrorInfo && value.equals(((YangErrorInfo) obj).value);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this).add("value", value).toString();
+    }
+}
diff --git a/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/YangErrorInfos.java b/data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/YangErrorInfos.java
new file mode 100644 (file)
index 0000000..74029e4
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.schema;
+
+import static org.opendaylight.yangtools.yang.common.YangConstants.BAD_ATTRIBUTE_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.BAD_ELEMENT_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.BAD_NAMESPACE_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.ERR_ELEMENT_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.MISSING_CHOICE_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.NON_UNIQUE_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.NOOP_ELEMENT_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.OK_ELEMENT_QNAME;
+import static org.opendaylight.yangtools.yang.common.YangConstants.SESSION_ID_QNAME;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.data.api.YangErrorInfo;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+
+@Beta
+@NonNullByDefault
+public final class YangErrorInfos {
+    private static final NodeIdentifier BAD_ATTRIBUTE_NODEID = NodeIdentifier.create(BAD_ATTRIBUTE_QNAME);
+    private static final NodeIdentifier BAD_ELEMENT_NODEID = NodeIdentifier.create(BAD_ELEMENT_QNAME);
+    private static final NodeIdentifier BAD_NAMESPACE_NODEID = NodeIdentifier.create(BAD_NAMESPACE_QNAME);
+    private static final NodeIdentifier MISSING_CHOICE_NODEID = NodeIdentifier.create(MISSING_CHOICE_QNAME);
+    private static final NodeIdentifier NON_UNIQUE_NODEID = NodeIdentifier.create(NON_UNIQUE_QNAME);
+    private static final NodeIdentifier SESSION_ID_NODEID = NodeIdentifier.create(SESSION_ID_QNAME);
+    @Deprecated(since = "RFC6241")
+    private static final NodeIdentifier ERR_ELEMENT_NODEID = NodeIdentifier.create(ERR_ELEMENT_QNAME);
+    @Deprecated(since = "RFC6241")
+    private static final NodeIdentifier NOOP_ELEMENT_NODEID = NodeIdentifier.create(NOOP_ELEMENT_QNAME);
+    @Deprecated(since = "RFC6241")
+    private static final NodeIdentifier OK_ELEMENT_NODEID = NodeIdentifier.create(OK_ELEMENT_QNAME);
+
+    private YangErrorInfos() {
+        // Hidden on purpose
+    }
+
+    public static YangErrorInfo of(final QName name, final Object value) {
+        return of(new NodeIdentifier(name), value);
+    }
+
+    public static YangErrorInfo of(final NodeIdentifier name, final Object value) {
+        return YangErrorInfo.of(ImmutableNodes.leafNode(name, value));
+    }
+
+    /**
+     * {@code bad-attribute} with the name of the attribute.
+     */
+    public static YangErrorInfo badAttribute(final QName attributeName) {
+        return of(BAD_ATTRIBUTE_NODEID, attributeName);
+    }
+
+    /**
+     * {@code bad-element} with the name of the element.
+     */
+    public static YangErrorInfo badElement(final QName elementName) {
+        return of(BAD_ELEMENT_NODEID, elementName);
+    }
+
+    /**
+     * {@code bad-namespace} with the name of the namespace.
+     */
+    public static YangErrorInfo badNamespace(final QName namespaceName) {
+        return of(BAD_NAMESPACE_NODEID, namespaceName);
+    }
+
+    /**
+     * {@code session-id} with the session identifier, as modeled in {@code SessionIdOrZero}.
+     */
+    public static YangErrorInfo sessionId(final Uint32 sessionId) {
+        return of(SESSION_ID_NODEID, sessionId);
+    }
+
+    /**
+     * {@code ok-element} with the name of the element.
+     *
+     * @deprecated This error-info specified by {@link ErrorTag#PARTIAL_OPERATION}.
+     */
+    @Deprecated(since = "RFC6241")
+    public static YangErrorInfo errElement(final QName elementName) {
+        return of(ERR_ELEMENT_NODEID, elementName);
+    }
+
+    /**
+     * {@code noop-element}, with the name of the element.
+     *
+     * @deprecated This error-info specified by {@link ErrorTag#PARTIAL_OPERATION}.
+     */
+    @Deprecated(since = "RFC6241")
+    public static YangErrorInfo noopElement(final QName elementName) {
+        return of(NOOP_ELEMENT_NODEID, elementName);
+    }
+
+    /**
+     * {@code ok-element}, with the name of the element.
+     *
+     * @deprecated This error-info specified by {@link ErrorTag#PARTIAL_OPERATION}.
+     */
+    @Deprecated(since = "RFC6241")
+    public static YangErrorInfo okElement(final QName elementName) {
+        return of(OK_ELEMENT_NODEID, elementName);
+    }
+
+    /**
+     * {@code non-unique} as defined in
+     * <a href="https://datatracker.ietf.org/doc/html/rfc6020#section-13.1">RFC6020, section 13.1</a>. Note this
+     * is only a prototype, which needs to be bound to a path representation type.
+     */
+    public static YangErrorInfo nonUnique(final YangInstanceIdentifier leafPath) {
+        return of(NON_UNIQUE_NODEID, leafPath);
+    }
+
+    /**
+     * {@code missing-choice} as defined in
+     * <a href="https://datatracker.ietf.org/doc/html/rfc6020#section-13.7">RFC6020, section 13.7</a>.
+     */
+    public static YangErrorInfo missingChoice(final NodeIdentifier choiceName) {
+        return of(MISSING_CHOICE_NODEID, choiceName);
+    }
+}