X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-model-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Fapi%2FSchemaContext.java;h=4986eca713ee46e1440c4e53511584b5e3242d16;hb=862bc29f99617a37daecb59f3c2fa72c40dfa975;hp=f56e67ef0dba611642bcc4b963f273c4d69590a9;hpb=02d724c5ef8e51e7981a97f8418e4d6804aafe22;p=yangtools.git diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaContext.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaContext.java index f56e67ef0d..4986eca713 100644 --- a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaContext.java +++ b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaContext.java @@ -7,13 +7,19 @@ */ package org.opendaylight.yangtools.yang.model.api; -import com.google.common.base.Optional; +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.Date; +import java.util.Collection; +import java.util.Optional; import java.util.Set; -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; /** * The interface represents static view of compiled yang files, @@ -23,12 +29,14 @@ import org.opendaylight.yangtools.yang.common.QName; * 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. +// FIXME: 4.0.0: consider deprecating this class in favor of EffectiveModelContext +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"), null, "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 @@ -40,7 +48,9 @@ public interface SchemaContext extends ContainerSchemaNode { Set 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 */ @@ -56,7 +66,7 @@ public interface SchemaContext extends ContainerSchemaNode { Set 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 ExtensionDefinition instances which @@ -65,70 +75,142 @@ public interface SchemaContext extends ContainerSchemaNode { Set getExtensions(); /** - * Returns module instance (from the context) with concrete name and - * revision date. + * Returns the module matching specified {@link QNameModule}, if present. + * + * @param qnameModule requested QNameModule + * @return Module, if present. + * @throws NullPointerException if qnameModule is null + */ + Optional 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 + * namespace and no revision. + */ + default Optional 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 + * namespace and revision. + */ + default Optional 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 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 + * namespace and revision. + */ + default Optional findModule(final @NonNull URI namespace, final @NonNull Optional 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 (if specified) the - * same as are the values specified in parameters name - * and revision. In other cases the null - * value is returned. - * + * @return module instance which has name and revision the same as are the values specified in parameters + * name and revision. */ - Module findModuleByName(String name, Date revision); - - default Optional findAnyModuleByName(final String name) { - return Optional.fromNullable(findModuleByName(name, null)); + default Optional findModule(final String name, final Optional revision) { + return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny(); } /** - * Returns module instance (from the context) with concrete namespace. + * Returns module instance (from the context) with specified name and revision. * - * @param namespace - * URI instance with specified namespace - * @return module instance which has namespace equal to the - * namespace or null in other cases + * @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 + * name and revision. */ - default Set findModuleByNamespace(final URI namespace) { - return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace())); + default Optional findModule(final String name, final @Nullable Revision revision) { + return findModule(name, Optional.ofNullable(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 no revision. * - * @param namespace Module namespace, may be null - * @param revision Module revision, may be null - * @return Matching module or null if a match is not found + * @param name string with the module name + * @return module instance which has name and revision the same as are the values specified in name + * and no revision. + * @throws NullPointerException if name is null */ - default Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) { - if (namespace == null) { - return null; - } - for (Module module : findModuleByNamespace(namespace)) { - if (revision == null || revision.equals(module.getRevision())) { - return module; - } - } - return null; + default Optional findModule(final String name) { + return findModule(name, Optional.empty()); } /** - * Get yang source code represented as string for matching - * {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier}. + * 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 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. + * @param name + * string with the module name + * @return set of module instances with specified name. */ - Optional getModuleSource(ModuleIdentifier moduleIdentifier); + default Set findModules(final String name) { + return Sets.filter(getModules(), m -> name.equals(m.getName())); + } /** - * Get all module and submodule identifiers. + * 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 + * namespace or null in other cases */ - Set getAllModuleIdentifiers(); + default Set findModules(final URI namespace) { + return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace())); + } + + @Override + default Set getActions() { + return ImmutableSet.of(); + } + + @Override + default Optional getDescription() { + return Optional.empty(); + } + + @Override + default Optional getReference() { + return Optional.empty(); + } + + @Override + default Collection getMustConstraints() { + return ImmutableSet.of(); + } + + @Override + default Optional getWhenCondition() { + return Optional.empty(); + } + + @Beta + @Override + default Optional findDataTreeChild(final QName name) { + return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name)); + } }