Fix SchemaContextUtil RevisionAwareXPath resolution
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaContext.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.Sets;
13 import java.net.URI;
14 import java.util.Collection;
15 import java.util.Optional;
16 import java.util.Set;
17 import javax.annotation.Nullable;
18 import javax.annotation.concurrent.Immutable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.common.Revision;
22
23 /**
24  * The interface represents static view of compiled yang files,
25  * contains the methods for obtaining all the top level context
26  * data (data from all modules) like YANG notifications, extensions,
27  * operations...
28  * Instances MUST be immutable and thus usage in multi threaded
29  * environment is safe.
30  */
31 @Immutable
32 // FIXME: 3.0.0: ContainerSchemaNode is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
33 //               and possibly DataSchemaNode would reflect SchemaContext traits better.
34 public interface SchemaContext extends ContainerSchemaNode {
35     /**
36      * QName of NETCONF top-level data node.
37      */
38     QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
39
40     /**
41      * Returns data schema node instances which represents direct subnodes (like
42      * leaf, leaf-list, list, container) in all YANG modules in the context.
43      *
44      * @return set of <code>DataSchemaNode</code> instances which represents
45      *         YANG data nodes at the module top level
46      */
47     Set<DataSchemaNode> getDataDefinitions();
48
49     /**
50      * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
51      * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
52      * modules having the same attribute are encountered newest revision first.
53      *
54      * @return set of the modules which belong to the schema context
55      */
56     Set<Module> getModules();
57
58     /**
59      * Returns rpc definition instances which are defined as the direct
60      * subelements in all YANG modules in the context.
61      *
62      * @return set of <code>RpcDefinition</code> instances which represents
63      *         nodes defined via <code>rpc</code> YANG keyword
64      */
65     Set<RpcDefinition> getOperations();
66
67     /**
68      * Returns extension definition instances which are defined as the direct
69      * subelements in all YANG modules in the context.
70      *
71      * @return set of <code>ExtensionDefinition</code> instances which
72      *         represents nodes defined via <code>extension</code> YANG keyword
73      */
74     Set<ExtensionDefinition> getExtensions();
75
76     /**
77      * Returns the module matching specified {@link QNameModule}, if present.
78      *
79      * @param qnameModule requested QNameModule
80      * @return Module, if present.
81      * @throws NullPointerException if qnameModule is null
82      */
83     Optional<Module> findModule(QNameModule qnameModule);
84
85     /**
86      * Returns module instance (from the context) with specified namespace and no revision.
87      *
88      * @param namespace
89      *            module namespace
90      * @return module instance which has name and revision the same as are the values specified in parameters
91      *         <code>namespace</code> and no revision.
92      */
93     default Optional<Module> findModule(final URI namespace) {
94         return findModule(QNameModule.create(namespace));
95     }
96
97     /**
98      * Returns module instance (from the context) with specified namespace and revision.
99      *
100      * @param namespace
101      *            module namespace
102      * @param revision
103      *            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>.
106      */
107     default Optional<Module> findModule(final URI namespace, @Nullable final Revision revision) {
108         return findModule(QNameModule.create(namespace, revision));
109     }
110
111     /**
112      * Returns module instance (from the context) with specified namespace and revision.
113      *
114      * @param namespace
115      *            module namespace
116      * @param revision
117      *            module revision, may be null
118      * @return module instance which has name and revision the same as are the values specified in parameters
119      *         <code>namespace</code> and <code>revision</code>.
120      */
121     default Optional<Module> findModule(final URI namespace, final Optional<Revision> revision) {
122         return findModule(QNameModule.create(namespace, revision));
123     }
124
125     /**
126      * Returns module instance (from the context) with specified name and an optional revision.
127      *
128      * @param name
129      *            string with the module name
130      * @param revision
131      *            date of the module revision
132      * @return module instance which has name and revision the same as are the values specified in parameters
133      *                <code>name</code> and <code>revision</code>.
134      */
135     default Optional<Module> findModule(final String name, final Optional<Revision> revision) {
136         return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
137     }
138
139     /**
140      * Returns module instance (from the context) with specified name and revision.
141      *
142      * @param name
143      *            string with the module name
144      * @param revision
145      *            date of the module revision, may be null
146      * @return module instance which has name and revision the same as are the values specified in parameters
147      *         <code>name</code> and <code>revision</code>.
148      */
149     default Optional<Module> findModule(final String name, @Nullable final Revision revision) {
150         return findModule(name, Optional.ofNullable(revision));
151     }
152
153     /**
154      * Returns module instance (from the context) with specified name and no revision.
155      *
156      * @param name string with the module name
157      * @return module instance which has name and revision the same as are the values specified in <code>name</code>
158      *                and no revision.
159      * @throws NullPointerException if name is null
160      */
161     default Optional<Module> findModule(final String name) {
162         return findModule(name, Optional.empty());
163     }
164
165     /**
166      * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
167      * order guarantee that the latest revision is encountered first.
168      *
169      * @param name
170      *            string with the module name
171      * @return set of module instances with specified name.
172      */
173     default Set<Module> findModules(final String name) {
174         return Sets.filter(getModules(), m -> name.equals(m.getName()));
175     }
176
177     /**
178      * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
179      * iteration order guarantee that the latest revision is encountered first.
180      *
181      * @param namespace
182      *            URI instance with specified namespace
183      * @return module instance which has namespace equal to the
184      *         <code>namespace</code> or <code>null</code> in other cases
185      */
186     default Set<Module> findModules(final URI namespace) {
187         return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace()));
188     }
189
190     @Override
191     default Set<ActionDefinition> getActions() {
192         return ImmutableSet.of();
193     }
194
195     @Override
196     default Optional<String> getDescription() {
197         return Optional.empty();
198     }
199
200     @Override
201     default Optional<String> getReference() {
202         return Optional.empty();
203     }
204
205     @Override
206     default Collection<MustDefinition> getMustConstraints() {
207         return ImmutableSet.of();
208     }
209
210     @Override
211     default Optional<RevisionAwareXPath> getWhenCondition() {
212         return Optional.empty();
213     }
214
215     @Beta
216     @Override
217     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
218         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
219     }
220 }