2 * Copyright (c) 2013 Cisco Systems, Inc. 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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
16 import java.util.Collection;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
26 * The interface represents static view of compiled yang files,
27 * contains the methods for obtaining all the top level context
28 * data (data from all modules) like YANG notifications, extensions,
30 * Instances MUST be immutable and thus usage in multi threaded
31 * environment is safe.
33 // FIXME: 5.0.0: ContainerSchemaNode is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
34 // and possibly DataSchemaNode would reflect SchemaContext traits better.
35 // FIXME: 5.0.0: consider deprecating this class in favor of EffectiveModelContext
36 public interface SchemaContext extends ContainerSchemaNode, Immutable {
38 * QName of NETCONF top-level data node.
40 // FIXME: YANGTOOLS-1074: we do not want this name
41 @NonNull QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
44 * Returns data schema node instances which represents direct subnodes (like
45 * leaf, leaf-list, list, container) in all YANG modules in the context.
47 * @return set of <code>DataSchemaNode</code> instances which represents
48 * YANG data nodes at the module top level
50 Collection<? extends DataSchemaNode> getDataDefinitions();
53 * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
54 * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
55 * modules having the same attribute are encountered newest revision first.
57 * @return set of the modules which belong to the schema context
59 Collection<? extends Module> getModules();
62 * Returns rpc definition instances which are defined as the direct
63 * subelements in all YANG modules in the context.
65 * @return set of <code>RpcDefinition</code> instances which represents
66 * nodes defined via <code>rpc</code> YANG keyword
68 Collection<? extends RpcDefinition> getOperations();
71 * Returns extension definition instances which are defined as the direct
72 * subelements in all YANG modules in the context.
74 * @return set of <code>ExtensionDefinition</code> instances which
75 * represents nodes defined via <code>extension</code> YANG keyword
77 Collection<? extends ExtensionDefinition> getExtensions();
80 * Returns the module matching specified {@link QNameModule}, if present.
82 * @param qnameModule requested QNameModule
83 * @return Module, if present.
84 * @throws NullPointerException if qnameModule is null
86 Optional<Module> findModule(@NonNull QNameModule qnameModule);
89 * Returns module instance (from the context) with specified namespace and no revision.
91 * @param namespace module namespace
92 * @return module instance which has name and revision the same as are the values specified in parameters
93 * <code>namespace</code> and no revision.
95 default Optional<Module> findModule(final @NonNull URI namespace) {
96 return findModule(QNameModule.create(namespace));
100 * Returns module instance (from the context) with specified namespace and revision.
102 * @param namespace module namespace
103 * @param revision module revision, may be null
104 * @return module instance which has name and revision the same as are the values specified in parameters
105 * <code>namespace</code> and <code>revision</code>.
107 default Optional<Module> findModule(final @NonNull URI namespace, final @Nullable Revision revision) {
108 return findModule(QNameModule.create(namespace, revision));
112 * Returns module instance (from the context) with specified namespace and revision.
114 * @param namespace module namespace
115 * @param revision module revision, may be null
116 * @return module instance which has name and revision the same as are the values specified in parameters
117 * <code>namespace</code> and <code>revision</code>.
119 default Optional<Module> findModule(final @NonNull URI namespace, final @NonNull Optional<Revision> revision) {
120 return findModule(QNameModule.create(namespace, revision));
124 * Returns module instance (from the context) with specified name and an optional revision.
127 * string with the module name
129 * date of the module revision
130 * @return module instance which has name and revision the same as are the values specified in parameters
131 * <code>name</code> and <code>revision</code>.
133 default Optional<? extends Module> findModule(final String name, final Optional<Revision> revision) {
134 return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
138 * Returns module instance (from the context) with specified name and revision.
141 * string with the module name
143 * date of the module revision, may be null
144 * @return module instance which has name and revision the same as are the values specified in parameters
145 * <code>name</code> and <code>revision</code>.
147 default Optional<? extends Module> findModule(final String name, final @Nullable Revision revision) {
148 return findModule(name, Optional.ofNullable(revision));
152 * Returns module instance (from the context) with specified name and no revision.
154 * @param name string with the module name
155 * @return module instance which has name and revision the same as are the values specified in <code>name</code>
157 * @throws NullPointerException if name is null
159 default Optional<? extends Module> findModule(final String name) {
160 return findModule(name, Optional.empty());
164 * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
165 * order guarantee that the latest revision is encountered first.
168 * string with the module name
169 * @return set of module instances with specified name.
171 default Collection<? extends Module> findModules(final String name) {
172 return Collections2.filter(getModules(), m -> name.equals(m.getName()));
176 * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
177 * iteration order guarantee that the latest revision is encountered first.
180 * URI instance with specified namespace
181 * @return module instance which has namespace equal to the
182 * <code>namespace</code> or <code>null</code> in other cases
184 default Collection<? extends Module> findModules(final URI namespace) {
185 return Collections2.filter(getModules(), m -> namespace.equals(m.getNamespace()));
189 default Collection<? extends ActionDefinition> getActions() {
190 return ImmutableSet.of();
194 default Optional<ActionDefinition> findAction(final QName qname) {
195 requireNonNull(qname);
196 return Optional.empty();
200 default Optional<NotificationDefinition> findNotification(final QName qname) {
201 final Optional<Collection<? extends NotificationDefinition>> defs = findModule(qname.getModule())
202 .map(Module::getNotifications);
203 if (defs.isPresent()) {
204 for (NotificationDefinition def : defs.get()) {
205 if (qname.equals(def.getQName())) {
206 return Optional.of(def);
210 return Optional.empty();
214 default Optional<String> getDescription() {
215 return Optional.empty();
219 default Optional<String> getReference() {
220 return Optional.empty();
224 default Collection<? extends MustDefinition> getMustConstraints() {
225 return ImmutableSet.of();
229 default Optional<RevisionAwareXPath> getWhenCondition() {
230 return Optional.empty();
235 default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
236 return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
240 * Get identities derived from a selected identity.
242 * @return collection of identities derived from this identity
243 * @throws NullPointerException if identity is null
244 * @throws IllegalArgumentException if the specified identity is not present in this context
247 @NonNull Collection<? extends IdentitySchemaNode> getDerivedIdentities(IdentitySchemaNode identity);