2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.model.api;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.Collections2;
15 import java.util.Collection;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.XMLNamespace;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeRoot;
29 * {@link EffectiveStatement}-based result of YANG parser compilation. Unlike a SchemaContext, which it extends,
30 * it gives access to individual {@link ModuleEffectiveStatement}s that comprise it. It also supports resolution of
31 * schema node identifiers via {@link #findSchemaTreeNode(SchemaNodeIdentifier)}.
33 * @author Robert Varga
36 // FIXME: 8.0.0: evaluate if we still need to extend SchemaContext here
37 public interface EffectiveModelContext extends SchemaContext, SchemaTreeRoot {
39 @NonNull Map<QNameModule, ModuleEffectiveStatement> getModuleStatements();
41 default @NonNull Optional<ModuleEffectiveStatement> findModuleStatement(final QNameModule moduleName) {
42 return Optional.ofNullable(getModuleStatements().get(requireNonNull(moduleName)));
45 default @NonNull Optional<ModuleEffectiveStatement> findModuleStatement(final QName moduleName) {
46 return findModuleStatement(moduleName.getModule());
50 * Returns module instances (from the context) with a concrete name. Returned collection is required to have its
51 * iteration order guarantee that the latest revision is encountered first.
53 * @param name string with the module name
54 * @return set of module instances with specified name.
56 default @NonNull Collection<@NonNull ModuleEffectiveStatement> findModuleStatements(final String name) {
57 return Collections2.transform(findModules(name), Module::asEffectiveStatement);
61 * Returns module instance (from the context) with concrete namespace. Returned collection is required to have its
62 * iteration order guarantee that the latest revision is encountered first.
64 * @param namespace XMLNamespace instance with specified namespace
65 * @return module instance which has namespace equal to the {@code namespace} or {@code null} in other cases
67 default @NonNull Collection<@NonNull ModuleEffectiveStatement> findModuleStatements(
68 final XMLNamespace namespace) {
69 return Collections2.transform(findModules(namespace), Module::asEffectiveStatement);
72 default @NonNull ModuleEffectiveStatement getModuleStatement(final QNameModule moduleName) {
73 return verifyNotNull(getModuleStatements().get(requireNonNull(moduleName)));
76 default @NonNull ModuleEffectiveStatement getModuleStatement(final QName moduleName) {
77 return getModuleStatement(moduleName.getModule());
84 * Default implementation defers locates the module corresponding to the first element of path and then defers
85 * to {@link ModuleEffectiveStatement#findSchemaTreeNode(SchemaNodeIdentifier)}.
88 default Optional<SchemaTreeEffectiveStatement<?>> findSchemaTreeNode(final SchemaNodeIdentifier path) {
89 return findModuleStatement(path.firstNodeIdentifier().getModule())
90 .flatMap(module -> module.findSchemaTreeNode(path));