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