Extend Immutable contract
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaContext.java
index c91e35e4ecde4609f9bfd382c6d4496777353439..2147d9bda5b8b9eae1570e0cc9b7078f7fc7038a 100644 (file)
@@ -7,14 +7,16 @@
  */
 package org.opendaylight.yangtools.yang.model.api;
 
+import com.google.common.annotations.Beta;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import java.net.URI;
 import java.util.Collection;
 import java.util.Optional;
 import java.util.Set;
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -27,12 +29,13 @@ import org.opendaylight.yangtools.yang.common.Revision;
  * Instances MUST be immutable and thus usage in multi threaded
  * environment is safe.
  */
-@Immutable
-public interface SchemaContext extends ContainerSchemaNode {
+// FIXME: 3.0.0: ContainerSchemaNode is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
+//               and possibly DataSchemaNode would reflect SchemaContext traits better.
+public interface SchemaContext extends ContainerSchemaNode, Immutable {
     /**
      * QName of NETCONF top-level data node.
      */
-    QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
+    @NonNull QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
 
     /**
      * Returns data schema node instances which represents direct subnodes (like
@@ -44,7 +47,9 @@ public interface SchemaContext extends ContainerSchemaNode {
     Set<DataSchemaNode> getDataDefinitions();
 
     /**
-     * Returns modules which are part of the schema context.
+     * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
+     * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
+     * modules having the same attribute are encountered newest revision first.
      *
      * @return set of the modules which belong to the schema context
      */
@@ -60,7 +65,7 @@ public interface SchemaContext extends ContainerSchemaNode {
     Set<RpcDefinition> getOperations();
 
     /**
-     * Returns extencion definition instances which are defined as the direct
+     * Returns extension definition instances which are defined as the direct
      * subelements in all YANG modules in the context.
      *
      * @return set of <code>ExtensionDefinition</code> instances which
@@ -69,31 +74,65 @@ public interface SchemaContext extends ContainerSchemaNode {
     Set<ExtensionDefinition> getExtensions();
 
     /**
-     * Returns module instance (from the context) with concrete name and revision date.
+     * Returns the module matching specified {@link QNameModule}, if present.
      *
-     * @param name
-     *            string with the module name
-     * @param revision
-     *            date of the module revision
+     * @param qnameModule requested QNameModule
+     * @return Module, if present.
+     * @throws NullPointerException if qnameModule is null
+     */
+    Optional<Module> findModule(@NonNull QNameModule qnameModule);
+
+    /**
+     * Returns module instance (from the context) with specified namespace and no revision.
+     *
+     * @param namespace module namespace
      * @return module instance which has name and revision the same as are the values specified in parameters
-     *         <code>name</code> and <code>revision</code>.
+     *         <code>namespace</code> and no revision.
+     */
+    default Optional<Module> findModule(final @NonNull URI namespace) {
+        return findModule(QNameModule.create(namespace));
+    }
+
+    /**
+     * Returns module instance (from the context) with specified namespace and revision.
+     *
+     * @param namespace module namespace
+     * @param revision module revision, may be null
+     * @return module instance which has name and revision the same as are the values specified in parameters
+     *         <code>namespace</code> and <code>revision</code>.
      */
-    Optional<Module> findModule(String name, Optional<Revision> revision);
+    default Optional<Module> findModule(final @NonNull URI namespace, final @Nullable Revision revision) {
+        return findModule(QNameModule.create(namespace, revision));
+    }
 
     /**
-     * Returns module instance (from the context) with concrete name and revision date.
+     * Returns module instance (from the context) with specified namespace and revision.
+     *
+     * @param namespace module namespace
+     * @param revision module revision, may be null
+     * @return module instance which has name and revision the same as are the values specified in parameters
+     *         <code>namespace</code> and <code>revision</code>.
+     */
+    default Optional<Module> findModule(final @NonNull URI namespace, final @NonNull Optional<Revision> revision) {
+        return findModule(QNameModule.create(namespace, revision));
+    }
+
+    /**
+     * Returns module instance (from the context) with specified name and an optional revision.
      *
      * @param name
      *            string with the module name
+     * @param revision
+     *            date of the module revision
      * @return module instance which has name and revision the same as are the values specified in parameters
-     *         <code>name</code> and <code>revision</code>.
+     *                <code>name</code> and <code>revision</code>.
      */
-    default Optional<Module> findModule(final String name) {
-        return findModule(name, Optional.empty());
+    default Optional<Module> findModule(final String name, final Optional<Revision> revision) {
+        return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
     }
 
     /**
-     * Returns module instance (from the context) with concrete name and revision date.
+     * Returns module instance (from the context) with specified name and revision.
      *
      * @param name
      *            string with the module name
@@ -102,28 +141,25 @@ public interface SchemaContext extends ContainerSchemaNode {
      * @return module instance which has name and revision the same as are the values specified in parameters
      *         <code>name</code> and <code>revision</code>.
      */
-    default Optional<Module> findModule(final String name, @Nullable final Revision revision) {
+    default Optional<Module> findModule(final String name, final @Nullable Revision revision) {
         return findModule(name, Optional.ofNullable(revision));
     }
 
-    default Optional<Module> findModule(final URI namespace) {
-        return findModule(QNameModule.create(namespace));
-    }
-
-    default Optional<Module> findModule(final URI namespace, @Nullable final Revision revision) {
-        return findModule(QNameModule.create(namespace, revision));
-    }
-
-    default Optional<Module> findModule(final URI namespace, final Optional<Revision> revision) {
-        return findModule(QNameModule.create(namespace, revision));
-    }
-
-    default Optional<Module> findModule(final QNameModule qnameModule) {
-        return getModules().stream().filter(m -> qnameModule.equals(m.getQNameModule())).findAny();
+    /**
+     * Returns module instance (from the context) with specified name and no revision.
+     *
+     * @param name string with the module name
+     * @return module instance which has name and revision the same as are the values specified in <code>name</code>
+     *                and no revision.
+     * @throws NullPointerException if name is null
+     */
+    default Optional<Module> findModule(final String name) {
+        return findModule(name, Optional.empty());
     }
 
     /**
-     * Returns module instances (from the context) with a concrete name.
+     * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
+     * order guarantee that the latest revision is encountered first.
      *
      * @param name
      *            string with the module name
@@ -134,7 +170,8 @@ public interface SchemaContext extends ContainerSchemaNode {
     }
 
     /**
-     * Returns module instance (from the context) with concrete namespace.
+     * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
+     * iteration order guarantee that the latest revision is encountered first.
      *
      * @param namespace
      *            URI instance with specified namespace
@@ -164,4 +201,15 @@ public interface SchemaContext extends ContainerSchemaNode {
     default Collection<MustDefinition> getMustConstraints() {
         return ImmutableSet.of();
     }
+
+    @Override
+    default Optional<RevisionAwareXPath> getWhenCondition() {
+        return Optional.empty();
+    }
+
+    @Beta
+    @Override
+    default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
+        return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
+    }
 }