Bug 2366 - new parser API - implementation of declared statements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveSchemaContext.java
1 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
2
3 import com.google.common.base.Optional;
4 import com.google.common.base.Supplier;
5 import com.google.common.collect.SetMultimap;
6 import java.net.URI;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Comparator;
10 import java.util.Date;
11 import java.util.HashSet;
12 import java.util.LinkedHashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.TreeSet;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
26 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.model.api.Status;
30 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.UsesNode;
33
34
35 public abstract class AbstractEffectiveSchemaContext implements SchemaContext {
36
37     protected static final Supplier<TreeSet<Module>> MODULE_SET_SUPPLIER = new Supplier<TreeSet<Module>>() {
38         @Override
39         public TreeSet<Module> get() {
40             return new TreeSet<>(REVISION_COMPARATOR);
41         }
42     };
43
44     protected static final Comparator<Module> REVISION_COMPARATOR = new Comparator<Module>() {
45         @Override
46         public int compare(final Module o1, final Module o2) {
47             if (o2.getRevision() == null) {
48                 return -1;
49             }
50
51             return o2.getRevision().compareTo(o1.getRevision());
52         }
53     };
54
55     /**
56      * @return yang sources where key is ModuleIdentifier
57      */
58     protected abstract Map<ModuleIdentifier, String> getIdentifiersToSources();
59
60     /**
61      * @return Map of modules where key is namespace
62      */
63     protected abstract SetMultimap<URI, Module> getNamespaceToModules();
64
65     /**
66      * @return Map of modules where key is name of module
67      */
68     protected abstract SetMultimap<String, Module> getNameToModules();
69
70     @Override
71     public Set<DataSchemaNode> getDataDefinitions() {
72         final Set<DataSchemaNode> dataDefs = new HashSet<>();
73         for (Module m : getModules()) {
74             dataDefs.addAll(m.getChildNodes());
75         }
76         return dataDefs;
77     }
78
79     @Override
80     public Set<NotificationDefinition> getNotifications() {
81         final Set<NotificationDefinition> notifications = new HashSet<>();
82         for (Module m : getModules()) {
83             notifications.addAll(m.getNotifications());
84         }
85         return notifications;
86     }
87
88     @Override
89     public Set<RpcDefinition> getOperations() {
90         final Set<RpcDefinition> rpcs = new HashSet<>();
91         for (Module m : getModules()) {
92             rpcs.addAll(m.getRpcs());
93         }
94         return rpcs;
95     }
96
97     @Override
98     public Set<ExtensionDefinition> getExtensions() {
99         final Set<ExtensionDefinition> extensions = new HashSet<>();
100         for (Module m : getModules()) {
101             extensions.addAll(m.getExtensionSchemaNodes());
102         }
103         return extensions;
104     }
105
106     @Override
107     public Module findModuleByName(final String name, final Date revision) {
108         for (final Module module : getNameToModules().get(name)) {
109             if (revision == null || revision.equals(module.getRevision())) {
110                 return module;
111             }
112         }
113
114         return null;
115     }
116
117     @Override
118     public Set<Module> findModuleByNamespace(final URI namespace) {
119         final Set<Module> ret = getNamespaceToModules().get(namespace);
120         return ret == null ? Collections.<Module>emptySet() : ret;
121     }
122
123     @Override
124     public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
125         if (namespace == null) {
126             return null;
127         }
128         for (Module module : findModuleByNamespace(namespace)) {
129             if (revision == null || revision.equals(module.getRevision())) {
130                 return module;
131             }
132         }
133         return null;
134     }
135
136     @Override
137     public boolean isAugmenting() {
138         return false;
139     }
140
141     @Override
142     public boolean isAddedByUses() {
143         return false;
144     }
145
146     @Override
147     public boolean isConfiguration() {
148         return false;
149     }
150
151     @Override
152     public ConstraintDefinition getConstraints() {
153         return null;
154     }
155
156     @Override
157     public QName getQName() {
158         return SchemaContext.NAME;
159     }
160
161     @Override
162     public SchemaPath getPath() {
163         return SchemaPath.ROOT;
164     }
165
166     @Override
167     public String getDescription() {
168         return null;
169     }
170
171     @Override
172     public String getReference() {
173         return null;
174     }
175
176     @Override
177     public Status getStatus() {
178         return Status.CURRENT;
179     }
180
181     @Override
182     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
183         final List<UnknownSchemaNode> result = new ArrayList<>();
184         for (Module module : getModules()) {
185             result.addAll(module.getUnknownSchemaNodes());
186         }
187         return Collections.unmodifiableList(result);
188     }
189
190     @Override
191     public Set<TypeDefinition<?>> getTypeDefinitions() {
192         final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
193         for (Module module : getModules()) {
194             result.addAll(module.getTypeDefinitions());
195         }
196         return Collections.unmodifiableSet(result);
197     }
198
199     @Override
200     public Set<DataSchemaNode> getChildNodes() {
201         final Set<DataSchemaNode> result = new LinkedHashSet<>();
202         for (Module module : getModules()) {
203             result.addAll(module.getChildNodes());
204         }
205         return Collections.unmodifiableSet(result);
206     }
207
208     @Override
209     public Set<GroupingDefinition> getGroupings() {
210         final Set<GroupingDefinition> result = new LinkedHashSet<>();
211         for (Module module : getModules()) {
212             result.addAll(module.getGroupings());
213         }
214         return Collections.unmodifiableSet(result);
215     }
216
217     @Override
218     public DataSchemaNode getDataChildByName(final QName name) {
219         for (Module module : getModules()) {
220             final DataSchemaNode result = module.getDataChildByName(name);
221             if (result != null) {
222                 return result;
223             }
224         }
225         return null;
226     }
227
228     @Override
229     public DataSchemaNode getDataChildByName(final String name) {
230         for (Module module : getModules()) {
231             final DataSchemaNode result = module.getDataChildByName(name);
232             if (result != null) {
233                 return result;
234             }
235         }
236         return null;
237     }
238
239     @Override
240     public Set<UsesNode> getUses() {
241         return Collections.emptySet();
242     }
243
244     @Override
245     public boolean isPresenceContainer() {
246         return false;
247     }
248
249     @Override
250     public Set<AugmentationSchema> getAvailableAugmentations() {
251         return Collections.emptySet();
252     }
253
254     //FIXME: should work for submodules too
255     @Override
256     public Set<ModuleIdentifier> getAllModuleIdentifiers() {
257         return getIdentifiersToSources().keySet();
258     }
259
260     @Override
261     public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
262         String maybeSource = getIdentifiersToSources().get(moduleIdentifier);
263         return Optional.fromNullable(maybeSource);
264     }
265
266 }