*/
package org.opendaylight.yangtools.yang.model.api;
-import com.google.common.base.Optional;
-import javax.annotation.concurrent.Immutable;
-import org.opendaylight.yangtools.yang.common.QName;
+import static java.util.Objects.requireNonNull;
+import com.google.common.annotations.Beta;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableSet;
import java.net.URI;
-import java.util.Date;
-import java.util.Set;
+import java.util.Collection;
+import java.util.Optional;
+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;
/**
* The interface represents static view of compiled yang files,
* Instances MUST be immutable and thus usage in multi threaded
* environment is safe.
*/
-@Immutable
-public interface SchemaContext extends ContainerSchemaNode {
-
- public static final QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "data");
+// FIXME: 5.0.0: ContainerSchemaNode is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
+// and possibly DataSchemaNode would reflect SchemaContext traits better.
+// FIXME: 5.0.0: consider deprecating this class in favor of EffectiveModelContext
+public interface SchemaContext extends ContainerSchemaNode, Immutable {
+ /**
+ * QName of NETCONF top-level data node.
+ */
+ @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
* @return set of <code>DataSchemaNode</code> instances which represents
* YANG data nodes at the module top level
*/
- Set<DataSchemaNode> getDataDefinitions();
+ Collection<? extends 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
*/
- Set<Module> getModules();
-
- /**
- *
- * Returns notification definition instances which are defined as the direct
- * subelements in all YANG modules in the context.
- *
- * @return set of <code>NotificationDefinition</code> instances which
- * represents nodes defined via <code>notification</code> YANG
- * keyword
- */
- Set<NotificationDefinition> getNotifications();
+ Collection<? extends Module> getModules();
/**
* Returns rpc definition instances which are defined as the direct
* @return set of <code>RpcDefinition</code> instances which represents
* nodes defined via <code>rpc</code> YANG keyword
*/
- Set<RpcDefinition> getOperations();
+ Collection<? extends RpcDefinition> getOperations();
/**
- * Returns extencion definition instances which are defined as the direct
- * subelements in all YANG modules in the context
+ * 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
* represents nodes defined via <code>extension</code> YANG keyword
*/
- Set<ExtensionDefinition> getExtensions();
+ Collection<? extends 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
- * @return module instance which has name and revision (if specified) the
- * same as are the values specified in parameters <code>name</code>
- * and <code>revision</code>. In other cases the <code>null</code>
- * value is returned.
+ * @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>namespace</code> and no revision.
*/
- Module findModuleByName(final String name, final Date 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.
*
- * Returns module instance (from the context) with concrete namespace.
+ * @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 @Nullable Revision revision) {
+ return findModule(QNameModule.create(namespace, revision));
+ }
+
+ /**
+ * Returns module instance (from the context) with specified namespace and revision.
*
- * @param namespace
- * URI instance with specified namespace
- * @return module instance which has namespace equal to the
- * <code>namespace</code> or <code>null</code> in other cases
+ * @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>.
*/
- Set<Module> findModuleByNamespace(final URI namespace);
+ default Optional<Module> findModule(final @NonNull URI namespace, final @NonNull Optional<Revision> revision) {
+ return findModule(QNameModule.create(namespace, revision));
+ }
/**
- * Returns module instance based on given namespace and revision. If
- * revision is not specified, returns module with newest revision.
+ * Returns module instance (from the context) with specified name and an optional revision.
*
- * @param namespace
+ * @param name
+ * string with the module name
* @param revision
- * @return
+ * 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>.
*/
- Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision);
+ default Optional<? extends 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 specified name and revision.
+ *
+ * @param name
+ * string with the module name
+ * @param revision
+ * date of the module revision, may be null
+ * @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<? extends Module> findModule(final String name, final @Nullable Revision revision) {
+ return findModule(name, Optional.ofNullable(revision));
+ }
/**
- * Get yang source code represented as string for matching
- * {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier}.
- * @param moduleIdentifier must provide a non-null
- * {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier#getName()},
- * other methods might return null.
- * @return value iif matching module is found in schema context.
+ * 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
*/
- Optional<String> getModuleSource(ModuleIdentifier moduleIdentifier);
+ default Optional<? extends Module> findModule(final String name) {
+ return findModule(name, Optional.empty());
+ }
/**
- * Get all module and submodule identifiers.
+ * 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
+ * @return set of module instances with specified name.
*/
- Set<ModuleIdentifier> getAllModuleIdentifiers();
+ default Collection<? extends Module> findModules(final String name) {
+ return Collections2.filter(getModules(), m -> name.equals(m.getName()));
+ }
+ /**
+ * 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
+ * @return module instance which has namespace equal to the
+ * <code>namespace</code> or <code>null</code> in other cases
+ */
+ default Collection<? extends Module> findModules(final URI namespace) {
+ return Collections2.filter(getModules(), m -> namespace.equals(m.getNamespace()));
+ }
+
+ @Override
+ default Collection<? extends ActionDefinition> getActions() {
+ return ImmutableSet.of();
+ }
+
+ @Override
+ default Optional<ActionDefinition> findAction(final QName qname) {
+ requireNonNull(qname);
+ return Optional.empty();
+ }
+
+ @Override
+ default Optional<NotificationDefinition> findNotification(final QName qname) {
+ final Optional<Collection<? extends NotificationDefinition>> defs = findModule(qname.getModule())
+ .map(Module::getNotifications);
+ if (defs.isPresent()) {
+ for (NotificationDefinition def : defs.get()) {
+ if (qname.equals(def.getQName())) {
+ return Optional.of(def);
+ }
+ }
+ }
+ return Optional.empty();
+ }
+
+ @Override
+ default Optional<String> getDescription() {
+ return Optional.empty();
+ }
+
+ @Override
+ default Optional<String> getReference() {
+ return Optional.empty();
+ }
+
+ @Override
+ default Collection<? extends 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));
+ }
}