89774deb0d8384e9cddf4bdebc8b898b169f5540
[yangtools.git] / model / 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.util.Collection;
16 import java.util.Collections;
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;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.common.YangConstants;
26 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
27
28 /**
29  * The interface represents static view of compiled yang files,
30  * contains the methods for obtaining all the top level context
31  * data (data from all modules) like YANG notifications, extensions,
32  * operations...
33  * Instances MUST be immutable and thus usage in multi threaded
34  * environment is safe.
35  */
36 // FIXME: 7.0.0: ContainerLike is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
37 //               and possibly DataSchemaNode would reflect SchemaContext traits better.
38 // FIXME: 7.0.0: consider deprecating this class in favor of EffectiveModelContext
39 public interface SchemaContext extends ContainerLike, Immutable {
40     /**
41      * QName of NETCONF top-level data node.
42      */
43     // FIXME: YANGTOOLS-1074: we do not want this name
44     @NonNull QName NAME = QName.create(YangConstants.NETCONF_NAMESPACE, "data").intern();
45
46     /**
47      * Returns data schema node instances which represents direct subnodes (like
48      * leaf, leaf-list, list, container) in all YANG modules in the context.
49      *
50      * @return set of <code>DataSchemaNode</code> instances which represents
51      *         YANG data nodes at the module top level
52      */
53     @NonNull Collection<? extends @NonNull DataSchemaNode> getDataDefinitions();
54
55     /**
56      * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
57      * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
58      * modules having the same attribute are encountered newest revision first.
59      *
60      * @return set of the modules which belong to the schema context
61      */
62     @NonNull Collection<? extends @NonNull Module> getModules();
63
64     /**
65      * Returns rpc definition instances which are defined as the direct
66      * subelements in all YANG modules in the context.
67      *
68      * @return set of <code>RpcDefinition</code> instances which represents
69      *         nodes defined via <code>rpc</code> YANG keyword
70      */
71     @NonNull Collection<? extends @NonNull RpcDefinition> getOperations();
72
73     /**
74      * Returns extension definition instances which are defined as the direct
75      * subelements in all YANG modules in the context.
76      *
77      * @return set of <code>ExtensionDefinition</code> instances which
78      *         represents nodes defined via <code>extension</code> YANG keyword
79      */
80     @NonNull Collection<? extends ExtensionDefinition> getExtensions();
81
82     /**
83      * Returns the module matching specified {@link QNameModule}, if present.
84      *
85      * @param qnameModule requested QNameModule
86      * @return Module, if present.
87      * @throws NullPointerException if qnameModule is null
88      */
89     Optional<Module> findModule(@NonNull QNameModule qnameModule);
90
91     /**
92      * Returns module instance (from the context) with specified namespace and no revision.
93      *
94      * @param namespace module namespace
95      * @return module instance which has name and revision the same as are the values specified in parameters
96      *         <code>namespace</code> and no revision.
97      */
98     default Optional<Module> findModule(final @NonNull XMLNamespace namespace) {
99         return findModule(QNameModule.create(namespace));
100     }
101
102     /**
103      * Returns module instance (from the context) with specified namespace and revision.
104      *
105      * @param namespace module namespace
106      * @param revision module revision, may be null
107      * @return module instance which has name and revision the same as are the values specified in parameters
108      *         <code>namespace</code> and <code>revision</code>.
109      */
110     default Optional<Module> findModule(final @NonNull XMLNamespace namespace, final @Nullable Revision revision) {
111         return findModule(QNameModule.create(namespace, revision));
112     }
113
114     /**
115      * Returns module instance (from the context) with specified namespace and revision.
116      *
117      * @param namespace module namespace
118      * @param revision module revision, may be null
119      * @return module instance which has name and revision the same as are the values specified in parameters
120      *         <code>namespace</code> and <code>revision</code>.
121      */
122     default Optional<Module> findModule(final @NonNull XMLNamespace namespace,
123             final @NonNull Optional<Revision> revision) {
124         return findModule(QNameModule.create(namespace, revision));
125     }
126
127     /**
128      * Returns module instance (from the context) with specified name and an optional revision.
129      *
130      * @param name
131      *            string with the module name
132      * @param revision
133      *            date of the module revision
134      * @return module instance which has name and revision the same as are the values specified in parameters
135      *                <code>name</code> and <code>revision</code>.
136      */
137     default Optional<? extends Module> findModule(final String name, final Optional<Revision> revision) {
138         return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
139     }
140
141     /**
142      * Returns module instance (from the context) with specified name and revision.
143      *
144      * @param name
145      *            string with the module name
146      * @param revision
147      *            date of the module revision, may be null
148      * @return module instance which has name and revision the same as are the values specified in parameters
149      *         <code>name</code> and <code>revision</code>.
150      */
151     default Optional<? extends Module> findModule(final String name, final @Nullable Revision revision) {
152         return findModule(name, Optional.ofNullable(revision));
153     }
154
155     /**
156      * Returns module instance (from the context) with specified name and no revision.
157      *
158      * @param name string with the module name
159      * @return module instance which has name and revision the same as are the values specified in <code>name</code>
160      *                and no revision.
161      * @throws NullPointerException if name is null
162      */
163     default Optional<? extends Module> findModule(final String name) {
164         return findModule(name, Optional.empty());
165     }
166
167     /**
168      * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
169      * order guarantee that the latest revision is encountered first.
170      *
171      * @param name string with the module name
172      * @return set of module instances with specified name.
173      */
174     default @NonNull Collection<? extends @NonNull Module> findModules(final String name) {
175         return Collections2.filter(getModules(), m -> name.equals(m.getName()));
176     }
177
178     /**
179      * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
180      * iteration order guarantee that the latest revision is encountered first.
181      *
182      * @param namespace XMLNamespace instance with specified namespace
183      * @return module instance which has namespace equal to the {@code namespace} or {@code null} in other cases
184      */
185     default @NonNull Collection<? extends @NonNull Module> findModules(final XMLNamespace namespace) {
186         return Collections2.filter(getModules(), m -> namespace.equals(m.getNamespace()));
187     }
188
189     @Override
190     @Deprecated
191     default Collection<? extends ActionDefinition> getActions() {
192         return ImmutableSet.of();
193     }
194
195     @Override
196     @Deprecated
197     default Optional<ActionDefinition> findAction(final QName qname) {
198         requireNonNull(qname);
199         return Optional.empty();
200     }
201
202     @Override
203     default Optional<NotificationDefinition> findNotification(final QName qname) {
204         final Optional<Collection<? extends @NonNull NotificationDefinition>> defs = findModule(qname.getModule())
205                 .map(Module::getNotifications);
206         if (defs.isPresent()) {
207             for (NotificationDefinition def : defs.get()) {
208                 if (qname.equals(def.getQName())) {
209                     return Optional.of(def);
210                 }
211             }
212         }
213         return Optional.empty();
214     }
215
216     @Override
217     @Deprecated
218     default Optional<String> getDescription() {
219         return Optional.empty();
220     }
221
222     @Override
223     @Deprecated
224     default Optional<String> getReference() {
225         return Optional.empty();
226     }
227
228     @Override
229     @Deprecated
230     default Collection<? extends @NonNull MustDefinition> getMustConstraints() {
231         return ImmutableSet.of();
232     }
233
234     @Override
235     @Deprecated
236     default Optional<? extends QualifiedBound> getWhenCondition() {
237         return Optional.empty();
238     }
239
240     @Override
241     @Deprecated
242     default boolean isAugmenting() {
243         return false;
244     }
245
246     @Override
247     @Deprecated
248     default boolean isAddedByUses() {
249         return false;
250     }
251
252     @Override
253     @Deprecated
254     default Optional<Boolean> effectiveConfig() {
255         return Optional.empty();
256     }
257
258     @Override
259     @Deprecated
260     default QName getQName() {
261         // FIXME: YANGTOOLS-1074: we do not want this name
262         return NAME;
263     }
264
265     @Override
266     @Deprecated
267     default SchemaPath getPath() {
268         return SchemaPath.ROOT;
269     }
270
271     @Override
272     @Deprecated
273     default Status getStatus() {
274         return Status.CURRENT;
275     }
276
277     @Override
278     @Deprecated
279     default Collection<? extends UsesNode> getUses() {
280         return Collections.emptySet();
281     }
282
283     @Override
284     @Deprecated
285     default Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
286         return Collections.emptySet();
287     }
288
289     @Beta
290     @Override
291     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
292         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
293     }
294
295     /**
296      * Get identities derived from a selected identity.
297      *
298      * @param identity base identity
299      * @return collection of identities derived from this identity
300      * @throws NullPointerException if identity is null
301      * @throws IllegalArgumentException if the specified identity is not present in this context
302      */
303     @Beta
304     @NonNull Collection<? extends IdentitySchemaNode> getDerivedIdentities(IdentitySchemaNode identity);
305 }