2147d9bda5b8b9eae1570e0cc9b7078f7fc7038a
[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 org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23
24 /**
25  * The interface represents static view of compiled yang files,
26  * contains the methods for obtaining all the top level context
27  * data (data from all modules) like YANG notifications, extensions,
28  * operations...
29  * Instances MUST be immutable and thus usage in multi threaded
30  * environment is safe.
31  */
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, Immutable {
35     /**
36      * QName of NETCONF top-level data node.
37      */
38     @NonNull 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(@NonNull QNameModule qnameModule);
84
85     /**
86      * Returns module instance (from the context) with specified namespace and no revision.
87      *
88      * @param namespace module namespace
89      * @return module instance which has name and revision the same as are the values specified in parameters
90      *         <code>namespace</code> and no revision.
91      */
92     default Optional<Module> findModule(final @NonNull URI namespace) {
93         return findModule(QNameModule.create(namespace));
94     }
95
96     /**
97      * Returns module instance (from the context) with specified namespace and revision.
98      *
99      * @param namespace module namespace
100      * @param revision module revision, may be null
101      * @return module instance which has name and revision the same as are the values specified in parameters
102      *         <code>namespace</code> and <code>revision</code>.
103      */
104     default Optional<Module> findModule(final @NonNull URI namespace, final @Nullable Revision revision) {
105         return findModule(QNameModule.create(namespace, revision));
106     }
107
108     /**
109      * Returns module instance (from the context) with specified namespace and revision.
110      *
111      * @param namespace module namespace
112      * @param revision module revision, may be null
113      * @return module instance which has name and revision the same as are the values specified in parameters
114      *         <code>namespace</code> and <code>revision</code>.
115      */
116     default Optional<Module> findModule(final @NonNull URI namespace, final @NonNull Optional<Revision> revision) {
117         return findModule(QNameModule.create(namespace, revision));
118     }
119
120     /**
121      * Returns module instance (from the context) with specified name and an optional revision.
122      *
123      * @param name
124      *            string with the module name
125      * @param revision
126      *            date of the module revision
127      * @return module instance which has name and revision the same as are the values specified in parameters
128      *                <code>name</code> and <code>revision</code>.
129      */
130     default Optional<Module> findModule(final String name, final Optional<Revision> revision) {
131         return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
132     }
133
134     /**
135      * Returns module instance (from the context) with specified name and revision.
136      *
137      * @param name
138      *            string with the module name
139      * @param revision
140      *            date of the module revision, may be null
141      * @return module instance which has name and revision the same as are the values specified in parameters
142      *         <code>name</code> and <code>revision</code>.
143      */
144     default Optional<Module> findModule(final String name, final @Nullable Revision revision) {
145         return findModule(name, Optional.ofNullable(revision));
146     }
147
148     /**
149      * Returns module instance (from the context) with specified name and no revision.
150      *
151      * @param name string with the module name
152      * @return module instance which has name and revision the same as are the values specified in <code>name</code>
153      *                and no revision.
154      * @throws NullPointerException if name is null
155      */
156     default Optional<Module> findModule(final String name) {
157         return findModule(name, Optional.empty());
158     }
159
160     /**
161      * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
162      * order guarantee that the latest revision is encountered first.
163      *
164      * @param name
165      *            string with the module name
166      * @return set of module instances with specified name.
167      */
168     default Set<Module> findModules(final String name) {
169         return Sets.filter(getModules(), m -> name.equals(m.getName()));
170     }
171
172     /**
173      * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
174      * iteration order guarantee that the latest revision is encountered first.
175      *
176      * @param namespace
177      *            URI instance with specified namespace
178      * @return module instance which has namespace equal to the
179      *         <code>namespace</code> or <code>null</code> in other cases
180      */
181     default Set<Module> findModules(final URI namespace) {
182         return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace()));
183     }
184
185     @Override
186     default Set<ActionDefinition> getActions() {
187         return ImmutableSet.of();
188     }
189
190     @Override
191     default Optional<String> getDescription() {
192         return Optional.empty();
193     }
194
195     @Override
196     default Optional<String> getReference() {
197         return Optional.empty();
198     }
199
200     @Override
201     default Collection<MustDefinition> getMustConstraints() {
202         return ImmutableSet.of();
203     }
204
205     @Override
206     default Optional<RevisionAwareXPath> getWhenCondition() {
207         return Optional.empty();
208     }
209
210     @Beta
211     @Override
212     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
213         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
214     }
215 }