Add a few more utilities to EffectiveModelContext 88/97688/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 30 Sep 2021 14:02:06 +0000 (16:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 30 Sep 2021 14:02:51 +0000 (16:02 +0200)
Locating modules by name/namespace is useful, provide a simple bridge on
top of SchemaContext.findModules().

JIRA: YANGTOOLS-1337
Change-Id: I08968d6aebb1569ab8990cd69352c56ddcfe5cc3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/EffectiveModelContext.java

index fd57e659dd4cb51e6caea8aa9b28c3665e8ac1ee..ab71c68a7a646aca46f99d0abcea74762d34fe48 100644 (file)
@@ -11,11 +11,14 @@ import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
+import com.google.common.collect.Collections2;
+import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
@@ -43,6 +46,29 @@ public interface EffectiveModelContext extends SchemaContext, SchemaTreeRoot {
         return findModuleStatement(moduleName.getModule());
     }
 
+    /**
+     * Returns module instances (from the context) with a concrete name. Returned collection is required to have its
+     * iteration order guarantee that the latest revision is encountered first.
+     *
+     * @param name string with the module name
+     * @return set of module instances with specified name.
+     */
+    default @NonNull Collection<@NonNull ModuleEffectiveStatement> findModuleStatements(final String name) {
+        return Collections2.transform(findModules(name), Module::asEffectiveStatement);
+    }
+
+    /**
+     * Returns module instance (from the context) with concrete namespace. Returned collection is required to have its
+     * iteration order guarantee that the latest revision is encountered first.
+     *
+     * @param namespace XMLNamespace instance with specified namespace
+     * @return module instance which has namespace equal to the {@code namespace} or {@code null} in other cases
+     */
+    default @NonNull Collection<@NonNull ModuleEffectiveStatement> findModuleStatements(
+            final XMLNamespace namespace) {
+        return Collections2.transform(findModules(namespace), Module::asEffectiveStatement);
+    }
+
     default @NonNull ModuleEffectiveStatement getModuleStatement(final QNameModule moduleName) {
         return verifyNotNull(getModuleStatements().get(requireNonNull(moduleName)));
     }