Populate model/ hierarchy
[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.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(XMLNamespace.of("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 XMLNamespace 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 XMLNamespace 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 XMLNamespace namespace,
122             final @NonNull Optional<Revision> revision) {
123         return findModule(QNameModule.create(namespace, revision));
124     }
125
126     /**
127      * Returns module instance (from the context) with specified name and an optional revision.
128      *
129      * @param name
130      *            string with the module name
131      * @param revision
132      *            date of the module revision
133      * @return module instance which has name and revision the same as are the values specified in parameters
134      *                <code>name</code> and <code>revision</code>.
135      */
136     default Optional<? extends Module> findModule(final String name, final Optional<Revision> revision) {
137         return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
138     }
139
140     /**
141      * Returns module instance (from the context) with specified name and revision.
142      *
143      * @param name
144      *            string with the module name
145      * @param revision
146      *            date of the module revision, may be null
147      * @return module instance which has name and revision the same as are the values specified in parameters
148      *         <code>name</code> and <code>revision</code>.
149      */
150     default Optional<? extends Module> findModule(final String name, final @Nullable Revision revision) {
151         return findModule(name, Optional.ofNullable(revision));
152     }
153
154     /**
155      * Returns module instance (from the context) with specified name and no revision.
156      *
157      * @param name string with the module name
158      * @return module instance which has name and revision the same as are the values specified in <code>name</code>
159      *                and no revision.
160      * @throws NullPointerException if name is null
161      */
162     default Optional<? extends Module> findModule(final String name) {
163         return findModule(name, Optional.empty());
164     }
165
166     /**
167      * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
168      * order guarantee that the latest revision is encountered first.
169      *
170      * @param name 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 XMLNamespace instance with specified namespace
182      * @return module instance which has namespace equal to the {@code namespace} or {@code null} in other cases
183      */
184     default @NonNull Collection<? extends @NonNull Module> findModules(final XMLNamespace namespace) {
185         return Collections2.filter(getModules(), m -> namespace.equals(m.getNamespace()));
186     }
187
188     @Override
189     @Deprecated
190     default Collection<? extends ActionDefinition> getActions() {
191         return ImmutableSet.of();
192     }
193
194     @Override
195     @Deprecated
196     default Optional<ActionDefinition> findAction(final QName qname) {
197         requireNonNull(qname);
198         return Optional.empty();
199     }
200
201     @Override
202     default Optional<NotificationDefinition> findNotification(final QName qname) {
203         final Optional<Collection<? extends @NonNull NotificationDefinition>> defs = findModule(qname.getModule())
204                 .map(Module::getNotifications);
205         if (defs.isPresent()) {
206             for (NotificationDefinition def : defs.get()) {
207                 if (qname.equals(def.getQName())) {
208                     return Optional.of(def);
209                 }
210             }
211         }
212         return Optional.empty();
213     }
214
215     @Override
216     @Deprecated
217     default Optional<String> getDescription() {
218         return Optional.empty();
219     }
220
221     @Override
222     @Deprecated
223     default Optional<String> getReference() {
224         return Optional.empty();
225     }
226
227     @Override
228     @Deprecated
229     default Collection<? extends @NonNull MustDefinition> getMustConstraints() {
230         return ImmutableSet.of();
231     }
232
233     @Override
234     @Deprecated
235     default Optional<? extends QualifiedBound> getWhenCondition() {
236         return Optional.empty();
237     }
238
239     @Override
240     @Deprecated
241     default boolean isAugmenting() {
242         return false;
243     }
244
245     @Override
246     @Deprecated
247     default boolean isAddedByUses() {
248         return false;
249     }
250
251     @Override
252     @Deprecated
253     default Optional<Boolean> effectiveConfig() {
254         return Optional.empty();
255     }
256
257     @Override
258     @Deprecated
259     default QName getQName() {
260         // FIXME: YANGTOOLS-1074: we do not want this name
261         return NAME;
262     }
263
264     @Override
265     @Deprecated
266     default SchemaPath getPath() {
267         return SchemaPath.ROOT;
268     }
269
270     @Override
271     @Deprecated
272     default Status getStatus() {
273         return Status.CURRENT;
274     }
275
276     @Override
277     @Deprecated
278     default Collection<? extends UsesNode> getUses() {
279         return Collections.emptySet();
280     }
281
282     @Override
283     @Deprecated
284     default Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
285         return Collections.emptySet();
286     }
287
288     @Beta
289     @Override
290     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
291         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
292     }
293
294     /**
295      * Get identities derived from a selected identity.
296      *
297      * @param identity base identity
298      * @return collection of identities derived from this identity
299      * @throws NullPointerException if identity is null
300      * @throws IllegalArgumentException if the specified identity is not present in this context
301      */
302     @Beta
303     @NonNull Collection<? extends IdentitySchemaNode> getDerivedIdentities(IdentitySchemaNode identity);
304 }