dbc2c8e17a07db84e596d476a00ad5236892898f
[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.base.Optional;
11 import com.google.common.collect.Sets;
12 import java.net.URI;
13 import java.util.Date;
14 import java.util.Set;
15 import javax.annotation.concurrent.Immutable;
16 import org.opendaylight.yangtools.yang.common.QName;
17
18 /**
19  * The interface represents static view of compiled yang files,
20  * contains the methods for obtaining all the top level context
21  * data (data from all modules) like YANG notifications, extensions,
22  * operations...
23  * Instances MUST be immutable and thus usage in multi threaded
24  * environment is safe.
25  */
26 @Immutable
27 public interface SchemaContext extends ContainerSchemaNode {
28     /**
29      * QName of NETCONF top-level data node.
30      */
31     QName NAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "data").intern();
32
33     /**
34      * Returns data schema node instances which represents direct subnodes (like
35      * leaf, leaf-list, list, container) in all YANG modules in the context.
36      *
37      * @return set of <code>DataSchemaNode</code> instances which represents
38      *         YANG data nodes at the module top level
39      */
40     Set<DataSchemaNode> getDataDefinitions();
41
42     /**
43      * Returns modules which are part of the schema context.
44      *
45      * @return set of the modules which belong to the schema context
46      */
47     Set<Module> getModules();
48
49     /**
50      * Returns rpc definition instances which are defined as the direct
51      * subelements in all YANG modules in the context.
52      *
53      * @return set of <code>RpcDefinition</code> instances which represents
54      *         nodes defined via <code>rpc</code> YANG keyword
55      */
56     Set<RpcDefinition> getOperations();
57
58     /**
59      * Returns extencion definition instances which are defined as the direct
60      * subelements in all YANG modules in the context.
61      *
62      * @return set of <code>ExtensionDefinition</code> instances which
63      *         represents nodes defined via <code>extension</code> YANG keyword
64      */
65     Set<ExtensionDefinition> getExtensions();
66
67     /**
68      * Returns module instance (from the context) with concrete name and
69      * revision date.
70      *
71      * @param name
72      *            string with the module name
73      * @param revision
74      *            date of the module revision
75      * @return module instance which has name and revision (if specified) the
76      *         same as are the values specified in parameters <code>name</code>
77      *         and <code>revision</code>. In other cases the <code>null</code>
78      *         value is returned.
79      *
80      */
81     Module findModuleByName(String name, Date revision);
82
83     /**
84      * Returns module instance (from the context) with concrete namespace.
85      *
86      * @param namespace
87      *            URI instance with specified namespace
88      * @return module instance which has namespace equal to the
89      *         <code>namespace</code> or <code>null</code> in other cases
90      */
91     default Set<Module> findModuleByNamespace(final URI namespace) {
92         return Sets.filter(getModules(), m -> namespace.equals(m.getNamespace()));
93     }
94
95     /**
96      * Returns module instance based on given namespace and revision. If
97      * revision is not specified, returns module with newest revision.
98      *
99      * @param namespace Module namespace, may be null
100      * @param revision Module revision, may be null
101      * @return Matching module or null if a match is not found
102      */
103     default Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
104         if (namespace == null) {
105             return null;
106         }
107         for (Module module : findModuleByNamespace(namespace)) {
108             if (revision == null || revision.equals(module.getRevision())) {
109                 return module;
110             }
111         }
112         return null;
113     }
114
115     /**
116      * Get yang source code represented as string for matching
117      * {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier}.
118      *
119      * @param moduleIdentifier must provide a non-null
120      *        {@link org.opendaylight.yangtools.yang.model.api.ModuleIdentifier#getName()}, other methods might return
121      *        null.
122      * @return value iif matching module is found in schema context.
123      */
124     Optional<String> getModuleSource(ModuleIdentifier moduleIdentifier);
125
126     /**
127      * Get all module and submodule identifiers.
128      */
129     Set<ModuleIdentifier> getAllModuleIdentifiers();
130 }