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