8546231c6250d278afe40de8eb348dd9450f29b7
[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 javax.annotation.concurrent.Immutable;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
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 @Immutable
33 // FIXME: 3.0.0: ContainerSchemaNode is far too broad. A combination of DataNodeContainer, NotificationNodeContainer
34 //               and possibly DataSchemaNode would reflect SchemaContext traits better.
35 public interface SchemaContext extends ContainerSchemaNode {
36     /**
37      * QName of NETCONF top-level data node.
38      */
39     @NonNull QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), "data").intern();
40
41     /**
42      * Returns data schema node instances which represents direct subnodes (like
43      * leaf, leaf-list, list, container) in all YANG modules in the context.
44      *
45      * @return set of <code>DataSchemaNode</code> instances which represents
46      *         YANG data nodes at the module top level
47      */
48     Set<DataSchemaNode> getDataDefinitions();
49
50     /**
51      * Returns modules which are part of the schema context. Returned set is required to have its iteration ordered
52      * by module revision, so that if modules are filtered by {@link Module#getName()} or {@link Module#getNamespace()},
53      * modules having the same attribute are encountered newest revision first.
54      *
55      * @return set of the modules which belong to the schema context
56      */
57     Set<Module> getModules();
58
59     /**
60      * Returns rpc definition instances which are defined as the direct
61      * subelements in all YANG modules in the context.
62      *
63      * @return set of <code>RpcDefinition</code> instances which represents
64      *         nodes defined via <code>rpc</code> YANG keyword
65      */
66     Set<RpcDefinition> getOperations();
67
68     /**
69      * Returns extension definition instances which are defined as the direct
70      * subelements in all YANG modules in the context.
71      *
72      * @return set of <code>ExtensionDefinition</code> instances which
73      *         represents nodes defined via <code>extension</code> YANG keyword
74      */
75     Set<ExtensionDefinition> getExtensions();
76
77     /**
78      * Returns the module matching specified {@link QNameModule}, if present.
79      *
80      * @param qnameModule requested QNameModule
81      * @return Module, if present.
82      * @throws NullPointerException if qnameModule is null
83      */
84     Optional<Module> findModule(@NonNull QNameModule qnameModule);
85
86     /**
87      * Returns module instance (from the context) with specified namespace and no revision.
88      *
89      * @param namespace module namespace
90      * @return module instance which has name and revision the same as are the values specified in parameters
91      *         <code>namespace</code> and no revision.
92      */
93     default Optional<Module> findModule(final @NonNull URI namespace) {
94         return findModule(QNameModule.create(namespace));
95     }
96
97     /**
98      * Returns module instance (from the context) with specified namespace and revision.
99      *
100      * @param namespace module namespace
101      * @param revision module revision, may be null
102      * @return module instance which has name and revision the same as are the values specified in parameters
103      *         <code>namespace</code> and <code>revision</code>.
104      */
105     default Optional<Module> findModule(final @NonNull URI namespace, final @Nullable Revision revision) {
106         return findModule(QNameModule.create(namespace, revision));
107     }
108
109     /**
110      * Returns module instance (from the context) with specified namespace and revision.
111      *
112      * @param namespace module namespace
113      * @param revision module revision, may be null
114      * @return module instance which has name and revision the same as are the values specified in parameters
115      *         <code>namespace</code> and <code>revision</code>.
116      */
117     default Optional<Module> findModule(final @NonNull URI namespace, final @NonNull Optional<Revision> revision) {
118         return findModule(QNameModule.create(namespace, revision));
119     }
120
121     /**
122      * Returns module instance (from the context) with specified name and an optional revision.
123      *
124      * @param name
125      *            string with the module name
126      * @param revision
127      *            date of the module revision
128      * @return module instance which has name and revision the same as are the values specified in parameters
129      *                <code>name</code> and <code>revision</code>.
130      */
131     default Optional<Module> findModule(final String name, final Optional<Revision> revision) {
132         return findModules(name).stream().filter(module -> revision.equals(module.getRevision())).findAny();
133     }
134
135     /**
136      * Returns module instance (from the context) with specified name and revision.
137      *
138      * @param name
139      *            string with the module name
140      * @param revision
141      *            date of the module revision, may be null
142      * @return module instance which has name and revision the same as are the values specified in parameters
143      *         <code>name</code> and <code>revision</code>.
144      */
145     default Optional<Module> findModule(final String name, final @Nullable Revision revision) {
146         return findModule(name, Optional.ofNullable(revision));
147     }
148
149     /**
150      * Returns module instance (from the context) with specified name and no revision.
151      *
152      * @param name string with the module name
153      * @return module instance which has name and revision the same as are the values specified in <code>name</code>
154      *                and no revision.
155      * @throws NullPointerException if name is null
156      */
157     default Optional<Module> findModule(final String name) {
158         return findModule(name, Optional.empty());
159     }
160
161     /**
162      * Returns module instances (from the context) with a concrete name. Returned Set is required to have its iteration
163      * order guarantee that the latest revision is encountered first.
164      *
165      * @param name
166      *            string with the module name
167      * @return set of module instances with specified name.
168      */
169     default Set<Module> findModules(final String name) {
170         return Sets.filter(getModules(), m -> name.equals(m.getName()));
171     }
172
173     /**
174      * Returns module instance (from the context) with concrete namespace. Returned Set is required to have its
175      * iteration order guarantee that the latest revision is encountered first.
176      *
177      * @param namespace
178      *            URI instance with specified namespace
179      * @return module instance which has namespace equal to the
180      *         <code>namespace</code> or <code>null</code> in other cases
181      */
182     default Set<Module> findModules(final URI namespace) {
183         return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace()));
184     }
185
186     @Override
187     default Set<ActionDefinition> getActions() {
188         return ImmutableSet.of();
189     }
190
191     @Override
192     default Optional<String> getDescription() {
193         return Optional.empty();
194     }
195
196     @Override
197     default Optional<String> getReference() {
198         return Optional.empty();
199     }
200
201     @Override
202     default Collection<MustDefinition> getMustConstraints() {
203         return ImmutableSet.of();
204     }
205
206     @Override
207     default Optional<RevisionAwareXPath> getWhenCondition() {
208         return Optional.empty();
209     }
210
211     @Beta
212     @Override
213     default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
214         return findModule(name.getModule()).flatMap(mod -> mod.findDataTreeChild(name));
215     }
216 }