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