Remove IdentitySchemaNode.getDerivedIdentities()
[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.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
25 /**
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,
29  * operations...
30  * Instances MUST be immutable and thus usage in multi threaded
31  * environment is safe.
32  */
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 {
37     /**
38      * QName of NETCONF top-level data node.
39      */
40     @NonNull QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
41
42     /**
43      * Returns data schema node instances which represents direct subnodes (like
44      * leaf, leaf-list, list, container) in all YANG modules in the context.
45      *
46      * @return set of <code>DataSchemaNode</code> instances which represents
47      *         YANG data nodes at the module top level
48      */
49     Collection<? extends DataSchemaNode> getDataDefinitions();
50
51     /**
52      * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
53      * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
54      * modules having the same attribute are encountered newest revision first.
55      *
56      * @return set of the modules which belong to the schema context
57      */
58     Collection<? extends Module> getModules();
59
60     /**
61      * Returns rpc definition instances which are defined as the direct
62      * subelements in all YANG modules in the context.
63      *
64      * @return set of <code>RpcDefinition</code> instances which represents
65      *         nodes defined via <code>rpc</code> YANG keyword
66      */
67     Collection<? extends RpcDefinition> getOperations();
68
69     /**
70      * Returns extension definition instances which are defined as the direct
71      * subelements in all YANG modules in the context.
72      *
73      * @return set of <code>ExtensionDefinition</code> instances which
74      *         represents nodes defined via <code>extension</code> YANG keyword
75      */
76     Collection<? extends ExtensionDefinition> getExtensions();
77
78     /**
79      * Returns the module matching specified {@link QNameModule}, if present.
80      *
81      * @param qnameModule requested QNameModule
82      * @return Module, if present.
83      * @throws NullPointerException if qnameModule is null
84      */
85     Optional<Module> findModule(@NonNull QNameModule qnameModule);
86
87     /**
88      * Returns module instance (from the context) with specified namespace and no revision.
89      *
90      * @param namespace module namespace
91      * @return module instance which has name and revision the same as are the values specified in parameters
92      *         <code>namespace</code> and no revision.
93      */
94     default Optional<Module> findModule(final @NonNull URI namespace) {
95         return findModule(QNameModule.create(namespace));
96     }
97
98     /**
99      * Returns module instance (from the context) with specified namespace and revision.
100      *
101      * @param namespace module namespace
102      * @param revision module revision, may be null
103      * @return module instance which has name and revision the same as are the values specified in parameters
104      *         <code>namespace</code> and <code>revision</code>.
105      */
106     default Optional<Module> findModule(final @NonNull URI namespace, final @Nullable Revision revision) {
107         return findModule(QNameModule.create(namespace, revision));
108     }
109
110     /**
111      * Returns module instance (from the context) with specified namespace and revision.
112      *
113      * @param namespace module namespace
114      * @param revision module revision, may be null
115      * @return module instance which has name and revision the same as are the values specified in parameters
116      *         <code>namespace</code> and <code>revision</code>.
117      */
118     default Optional<Module> findModule(final @NonNull URI namespace, final @NonNull Optional<Revision> revision) {
119         return findModule(QNameModule.create(namespace, revision));
120     }
121
122     /**
123      * Returns module instance (from the context) with specified name and an optional revision.
124      *
125      * @param name
126      *            string with the module name
127      * @param revision
128      *            date of the module revision
129      * @return module instance which has name and revision the same as are the values specified in parameters
130      *                <code>name</code> and <code>revision</code>.
131      */
132     default Optional<? extends Module> findModule(final String name, final Optional<Revision> revision) {
133         return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
134     }
135
136     /**
137      * Returns module instance (from the context) with specified name and revision.
138      *
139      * @param name
140      *            string with the module name
141      * @param revision
142      *            date of the module revision, may be null
143      * @return module instance which has name and revision the same as are the values specified in parameters
144      *         <code>name</code> and <code>revision</code>.
145      */
146     default Optional<? extends Module> findModule(final String name, final @Nullable Revision revision) {
147         return findModule(name, Optional.ofNullable(revision));
148     }
149
150     /**
151      * Returns module instance (from the context) with specified name and no revision.
152      *
153      * @param name string with the module name
154      * @return module instance which has name and revision the same as are the values specified in <code>name</code>
155      *                and no revision.
156      * @throws NullPointerException if name is null
157      */
158     default Optional<? extends Module> findModule(final String name) {
159         return findModule(name, Optional.empty());
160     }
161
162     /**
163      * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
164      * order guarantee that the latest revision is encountered first.
165      *
166      * @param name
167      *            string with the module name
168      * @return set of module instances with specified name.
169      */
170     default Collection<? extends Module> findModules(final String name) {
171         return Collections2.filter(getModules(), m -> name.equals(m.getName()));
172     }
173
174     /**
175      * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
176      * iteration order guarantee that the latest revision is encountered first.
177      *
178      * @param namespace
179      *            URI instance with specified namespace
180      * @return module instance which has namespace equal to the
181      *         <code>namespace</code> or <code>null</code> in other cases
182      */
183     default Collection<? extends Module> findModules(final URI namespace) {
184         return Collections2.filter(getModules(), m -> namespace.equals(m.getNamespace()));
185     }
186
187     @Override
188     default Collection<? extends ActionDefinition> getActions() {
189         return ImmutableSet.of();
190     }
191
192     @Override
193     default Optional<ActionDefinition> findAction(final QName qname) {
194         requireNonNull(qname);
195         return Optional.empty();
196     }
197
198     @Override
199     default Optional<NotificationDefinition> findNotification(final QName qname) {
200         final Optional<Collection<? extends NotificationDefinition>> defs = findModule(qname.getModule())
201                 .map(Module::getNotifications);
202         if (defs.isPresent()) {
203             for (NotificationDefinition def : defs.get()) {
204                 if (qname.equals(def.getQName())) {
205                     return Optional.of(def);
206                 }
207             }
208         }
209         return Optional.empty();
210     }
211
212     @Override
213     default Optional<String> getDescription() {
214         return Optional.empty();
215     }
216
217     @Override
218     default Optional<String> getReference() {
219         return Optional.empty();
220     }
221
222     @Override
223     default Collection<? extends MustDefinition> getMustConstraints() {
224         return ImmutableSet.of();
225     }
226
227     @Override
228     default Optional<RevisionAwareXPath> getWhenCondition() {
229         return Optional.empty();
230     }
231
232     @Beta
233     @Override
234     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
235         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
236     }
237
238     /**
239      * Get identities derived from a selected identity.
240      *
241      * @return collection of identities derived from this identity
242      * @throws NullPointerException if identity is null
243      * @throws IllegalArgumentException if the specified identity is not present in this context
244      */
245     @Beta
246     @NonNull Collection<? extends IdentitySchemaNode> getDerivedIdentities(IdentitySchemaNode identity);
247 }