Add input()/output() utility methods 08/103308/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Nov 2022 15:58:35 +0000 (16:58 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 21 Nov 2022 16:22:02 +0000 (17:22 +0100)
RpcDefinition and ActionDefinition provide direct access to their,
always present, input and output substatements. Expose equivalent
functionality from {Action,Rpc}EffectiveStatement.

JIRA: YANGTOOLS-1466
Change-Id: I836bc03f6215eec98c66502619c51bf361cc77b2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/ActionEffectiveStatement.java
model/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/DefaultMethodHelpers.java
model/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/RpcEffectiveStatement.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1262Test.java

index 6ab4b7990a5cac3f65566a8f52d1634d9c33f92a..b29900024ebd1f6acf4ccc2ae36c3171ee5d2ead 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.model.api.stmt;
 
+import com.google.common.base.VerifyException;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 
 /**
- * Effective representation of a {@code action} statement.
+ * Effective representation of a {@code action} statement. The effective view always defines an {@code input} and an
+ * {@code output} substatement, both of which are available through {@link #input()} and {@link #output()} methods.
  */
 public interface ActionEffectiveStatement extends SchemaTreeEffectiveStatement<ActionStatement>,
         DataTreeAwareEffectiveStatement<QName, ActionStatement>,
@@ -21,4 +24,28 @@ public interface ActionEffectiveStatement extends SchemaTreeEffectiveStatement<A
     default StatementDefinition statementDefinition() {
         return YangStmtMapping.ACTION;
     }
+
+    /**
+     * Return this statement's {@code input} substatement.
+     *
+     * @implSpec
+     *      Default implementation uses {@link #findFirstEffectiveSubstatement(Class)} and throws a
+     *      {@link VerifyException} if a matching substatement is not found.
+     * @return An {@link InputEffectiveStatement}
+     */
+    default @NonNull InputEffectiveStatement input() {
+        return DefaultMethodHelpers.verifyInputSubstatement(this);
+    }
+
+    /**
+     * Return this statement's {@code output} substatement.
+     *
+     * @implSpec
+     *      Default implementation uses {@link #findFirstEffectiveSubstatement(Class)} and throws a
+     *      {@link VerifyException} if a matching substatement is not found.
+     * @return An {@link OutputEffectiveStatement}
+     */
+    default @NonNull OutputEffectiveStatement output() {
+        return DefaultMethodHelpers.verifyOutputSubstatement(this);
+    }
 }
index 8132145e352d2fad399fcb2c0b6254816d7cf5fb..93a525a6b996d76770371d25478ecf0ca8962cd3 100644 (file)
@@ -7,8 +7,10 @@
  */
 package org.opendaylight.yangtools.yang.model.api.stmt;
 
+import com.google.common.base.VerifyException;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 
 /**
  * Simple helper methods used in default implementations.
@@ -21,4 +23,18 @@ final class DefaultMethodHelpers {
     static <E> @NonNull Optional<E> filterOptional(final @NonNull Optional<?> optional, final @NonNull Class<E> type) {
         return optional.filter(type::isInstance).map(type::cast);
     }
+
+    static @NonNull InputEffectiveStatement verifyInputSubstatement(final EffectiveStatement<?, ?> stmt) {
+        return verifySubstatement(stmt, InputEffectiveStatement.class);
+    }
+
+    static @NonNull OutputEffectiveStatement verifyOutputSubstatement(final EffectiveStatement<?, ?> stmt) {
+        return verifySubstatement(stmt, OutputEffectiveStatement.class);
+    }
+
+    private static <T extends EffectiveStatement<?, ?>> @NonNull T verifySubstatement(
+            final EffectiveStatement<?, ?> stmt, final Class<T> type) {
+        return stmt.findFirstEffectiveSubstatement(type).orElseThrow(
+            () -> new VerifyException(stmt + " does not define a " + type.getSimpleName() + " substatement"));
+    }
 }
index 601a7743d906115967ae7890d771523ff42cbf09..7819b982ea7357a89e036369a88de2503863a5df 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.model.api.stmt;
 
+import com.google.common.base.VerifyException;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 
 /**
- * Effective representation of a {@code rpc} statement.
+ * Effective representation of a {@code rpc} statement. The effective view always defines an {@code input} and an
+ * {@code output} substatement, both of which are available through {@link #input()} and {@link #output()} methods.
  */
 public interface RpcEffectiveStatement extends SchemaTreeEffectiveStatement<RpcStatement>,
         DataTreeAwareEffectiveStatement<QName, RpcStatement>, TypedefAwareEffectiveStatement<QName, RpcStatement> {
@@ -20,4 +23,28 @@ public interface RpcEffectiveStatement extends SchemaTreeEffectiveStatement<RpcS
     default StatementDefinition statementDefinition() {
         return YangStmtMapping.RPC;
     }
+
+    /**
+     * Return this statement's {@code input} substatement.
+     *
+     * @implSpec
+     *      Default implementation uses {@link #findFirstEffectiveSubstatement(Class)} and throws a
+     *      {@link VerifyException} if a matching substatement is not found.
+     * @return An {@link InputEffectiveStatement}
+     */
+    default @NonNull InputEffectiveStatement input() {
+        return DefaultMethodHelpers.verifyInputSubstatement(this);
+    }
+
+    /**
+     * Return this statement's {@code output} substatement.
+     *
+     * @implSpec
+     *      Default implementation uses {@link #findFirstEffectiveSubstatement(Class)} and throws a
+     *      {@link VerifyException} if a matching substatement is not found.
+     * @return An {@link OutputEffectiveStatement}
+     */
+    default @NonNull OutputEffectiveStatement output() {
+        return DefaultMethodHelpers.verifyOutputSubstatement(this);
+    }
 }
index 754f2bce4732d787e2db7ba00fe8e9238abbffcc..539777bd893ec9d4b46377cdc459637cda32dd24 100644 (file)
@@ -19,10 +19,8 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefAwareEffectiveStatement;
 
@@ -44,8 +42,8 @@ public class YT1262Test extends AbstractYangTest {
 
         final var action = container.findFirstEffectiveSubstatement(ActionEffectiveStatement.class).orElseThrow();
         assertTypedef(action, "adef");
-        assertTypedef(action.findFirstEffectiveSubstatement(InputEffectiveStatement.class).orElseThrow(), "idef");
-        assertTypedef(action.findFirstEffectiveSubstatement(OutputEffectiveStatement.class).orElseThrow(), "odef");
+        assertTypedef(action.input(), "idef");
+        assertTypedef(action.output(), "odef");
     }
 
     private static void assertTypedef(final EffectiveStatement<?, ?> parent, final String typedefName) {