Propagate @NonNull collection annotations
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import java.net.URI;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.concepts.Immutable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
26
27 /**
28  * The interface represents static view of compiled yang files,
29  * contains the methods for obtaining all the top level context
30  * data (data from all modules) like YANG notifications, extensions,
31  * operations...
32  * Instances MUST be immutable and thus usage in multi threaded
33  * environment is safe.
34  */
35 // FIXME: 7.0.0: ContainerLike is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
36 //               and possibly DataSchemaNode would reflect SchemaContext traits better.
37 // FIXME: 7.0.0: consider deprecating this class in favor of EffectiveModelContext
38 public interface SchemaContext extends ContainerLike, Immutable {
39     /**
40      * QName of NETCONF top-level data node.
41      */
42     // FIXME: YANGTOOLS-1074: we do not want this name
43     @NonNull QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
44
45     /**
46      * Returns data schema node instances which represents direct subnodes (like
47      * leaf, leaf-list, list, container) in all YANG modules in the context.
48      *
49      * @return set of <code>DataSchemaNode</code> instances which represents
50      *         YANG data nodes at the module top level
51      */
52     @NonNull Collection<? extends @NonNull DataSchemaNode> getDataDefinitions();
53
54     /**
55      * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
56      * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
57      * modules having the same attribute are encountered newest revision first.
58      *
59      * @return set of the modules which belong to the schema context
60      */
61     @NonNull Collection<? extends @NonNull Module> getModules();
62
63     /**
64      * Returns rpc definition instances which are defined as the direct
65      * subelements in all YANG modules in the context.
66      *
67      * @return set of <code>RpcDefinition</code> instances which represents
68      *         nodes defined via <code>rpc</code> YANG keyword
69      */
70     @NonNull Collection<? extends @NonNull RpcDefinition> getOperations();
71
72     /**
73      * Returns extension definition instances which are defined as the direct
74      * subelements in all YANG modules in the context.
75      *
76      * @return set of <code>ExtensionDefinition</code> instances which
77      *         represents nodes defined via <code>extension</code> YANG keyword
78      */
79     @NonNull Collection<? extends ExtensionDefinition> getExtensions();
80
81     /**
82      * Returns the module matching specified {@link QNameModule}, if present.
83      *
84      * @param qnameModule requested QNameModule
85      * @return Module, if present.
86      * @throws NullPointerException if qnameModule is null
87      */
88     Optional<Module> findModule(@NonNull QNameModule qnameModule);
89
90     /**
91      * Returns module instance (from the context) with specified namespace and no revision.
92      *
93      * @param namespace module namespace
94      * @return module instance which has name and revision the same as are the values specified in parameters
95      *         <code>namespace</code> and no revision.
96      */
97     default Optional<Module> findModule(final @NonNull URI namespace) {
98         return findModule(QNameModule.create(namespace));
99     }
100
101     /**
102      * Returns module instance (from the context) with specified namespace and revision.
103      *
104      * @param namespace module namespace
105      * @param revision module revision, may be null
106      * @return module instance which has name and revision the same as are the values specified in parameters
107      *         <code>namespace</code> and <code>revision</code>.
108      */
109     default Optional<Module> findModule(final @NonNull URI namespace, final @Nullable Revision revision) {
110         return findModule(QNameModule.create(namespace, revision));
111     }
112
113     /**
114      * Returns module instance (from the context) with specified namespace and revision.
115      *
116      * @param namespace module namespace
117      * @param revision 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 @NonNull URI namespace, final @NonNull 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<? extends 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<? extends Module> findModule(final String name, final @Nullable 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<? extends 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 @NonNull Collection<? extends @NonNull Module> findModules(final String name) {
174         return Collections2.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 @NonNull Collection<? extends @NonNull Module> findModules(final URI namespace) {
187         return Collections2.filter(getModules(), m -> namespace.equals(m.getNamespace()));
188     }
189
190     @Override
191     @Deprecated
192     default Collection<? extends ActionDefinition> getActions() {
193         return ImmutableSet.of();
194     }
195
196     @Override
197     @Deprecated
198     default Optional<ActionDefinition> findAction(final QName qname) {
199         requireNonNull(qname);
200         return Optional.empty();
201     }
202
203     @Override
204     default Optional<NotificationDefinition> findNotification(final QName qname) {
205         final Optional<Collection<? extends @NonNull NotificationDefinition>> defs = findModule(qname.getModule())
206                 .map(Module::getNotifications);
207         if (defs.isPresent()) {
208             for (NotificationDefinition def : defs.get()) {
209                 if (qname.equals(def.getQName())) {
210                     return Optional.of(def);
211                 }
212             }
213         }
214         return Optional.empty();
215     }
216
217     @Override
218     @Deprecated
219     default Optional<String> getDescription() {
220         return Optional.empty();
221     }
222
223     @Override
224     @Deprecated
225     default Optional<String> getReference() {
226         return Optional.empty();
227     }
228
229     @Override
230     @Deprecated
231     default Collection<? extends @NonNull MustDefinition> getMustConstraints() {
232         return ImmutableSet.of();
233     }
234
235     @Override
236     @Deprecated
237     default Optional<? extends QualifiedBound> getWhenCondition() {
238         return Optional.empty();
239     }
240
241     @Override
242     @Deprecated
243     default boolean isAugmenting() {
244         return false;
245     }
246
247     @Override
248     @Deprecated
249     default boolean isAddedByUses() {
250         return false;
251     }
252
253     @Override
254     @Deprecated
255     default boolean isConfiguration() {
256         return false;
257     }
258
259     @Override
260     @Deprecated
261     default QName getQName() {
262         // FIXME: YANGTOOLS-1074: we do not want this name
263         return NAME;
264     }
265
266     @Override
267     @Deprecated
268     default SchemaPath getPath() {
269         return SchemaPath.ROOT;
270     }
271
272     @Override
273     @Deprecated
274     default Status getStatus() {
275         return Status.CURRENT;
276     }
277
278     @Override
279     @Deprecated
280     default Collection<? extends UsesNode> getUses() {
281         return Collections.emptySet();
282     }
283
284     @Override
285     @Deprecated
286     default Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
287         return Collections.emptySet();
288     }
289
290     @Beta
291     @Override
292     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
293         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
294     }
295
296     /**
297      * Get identities derived from a selected identity.
298      *
299      * @param identity base identity
300      * @return collection of identities derived from this identity
301      * @throws NullPointerException if identity is null
302      * @throws IllegalArgumentException if the specified identity is not present in this context
303      */
304     @Beta
305     @NonNull Collection<? extends IdentitySchemaNode> getDerivedIdentities(IdentitySchemaNode identity);
306 }