BUG-4688: Rework SchemaContext module lookups
[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.Sets;
11 import java.net.URI;
12 import java.util.Date;
13 import java.util.Optional;
14 import java.util.Set;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.Immutable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19
20 /**
21  * The interface represents static view of compiled yang files,
22  * contains the methods for obtaining all the top level context
23  * data (data from all modules) like YANG notifications, extensions,
24  * operations...
25  * Instances MUST be immutable and thus usage in multi threaded
26  * environment is safe.
27  */
28 @Immutable
29 public interface SchemaContext extends ContainerSchemaNode {
30     /**
31      * QName of NETCONF top-level data node.
32      */
33     QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "data").intern();
34
35     /**
36      * Returns data schema node instances which represents direct subnodes (like
37      * leaf, leaf-list, list, container) in all YANG modules in the context.
38      *
39      * @return set of <code>DataSchemaNode</code> instances which represents
40      *         YANG data nodes at the module top level
41      */
42     Set<DataSchemaNode> getDataDefinitions();
43
44     /**
45      * Returns modules which are part of the schema context.
46      *
47      * @return set of the modules which belong to the schema context
48      */
49     Set<Module> getModules();
50
51     /**
52      * Returns rpc definition instances which are defined as the direct
53      * subelements in all YANG modules in the context.
54      *
55      * @return set of <code>RpcDefinition</code> instances which represents
56      *         nodes defined via <code>rpc</code> YANG keyword
57      */
58     Set<RpcDefinition> getOperations();
59
60     /**
61      * Returns extencion definition instances which are defined as the direct
62      * subelements in all YANG modules in the context.
63      *
64      * @return set of <code>ExtensionDefinition</code> instances which
65      *         represents nodes defined via <code>extension</code> YANG keyword
66      */
67     Set<ExtensionDefinition> getExtensions();
68
69     /**
70      * Returns module instance (from the context) with concrete name and revision date.
71      *
72      * @param name
73      *            string with the module name
74      * @param revision
75      *            date of the module revision, may be null
76      * @return module instance which has name and revision the same as are the values specified in parameters
77      *         <code>name</code> and <code>revision</code>.
78      */
79     Optional<Module> findModule(String name, @Nullable Date revision);
80
81     default Optional<Module> findModule(final URI namespace, @Nullable final Date revision) {
82         return findModule(QNameModule.create(namespace, revision));
83     }
84
85     default Optional<Module> findModule(final QNameModule qnameModule) {
86         return getModules().stream().filter(m -> qnameModule.equals(m.getQNameModule())).findAny();
87     }
88
89     /**
90      * Returns module instances (from the context) with a concrete name.
91      *
92      * @param name
93      *            string with the module name
94      * @return set of module instances with specified name.
95      */
96     default Set<Module> findModules(final String name) {
97         return Sets.filter(getModules(), m -> name.equals(m.getName()));
98     }
99
100     /**
101      * Returns module instance (from the context) with concrete namespace.
102      *
103      * @param namespace
104      *            URI instance with specified namespace
105      * @return module instance which has namespace equal to the
106      *         <code>namespace</code> or <code>null</code> in other cases
107      */
108     default Set<Module> findModules(final URI namespace) {
109         return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace()));
110     }
111 }