2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Supplier;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSetMultimap;
15 import com.google.common.collect.Multimaps;
16 import com.google.common.collect.SetMultimap;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.LinkedHashSet;
24 import java.util.List;
27 import java.util.TreeMap;
28 import javax.annotation.concurrent.Immutable;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
31 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
38 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
41 import org.opendaylight.yangtools.yang.model.api.Status;
42 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.UsesNode;
45 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
48 final class SchemaContextImpl implements SchemaContext {
49 private static final Supplier<HashSet<Module>> URI_SET_SUPPLIER = new Supplier<HashSet<Module>>() {
51 public HashSet<Module> get() {
52 return new HashSet<>();
56 private final ImmutableMap<ModuleIdentifier, String> identifiersToSources;
57 private final ImmutableSetMultimap<URI, Module> namespaceToModules;
58 private final ImmutableSet<Module> modules;
60 SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
61 this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
64 * Instead of doing this on each invocation of getModules(), pre-compute
65 * it once and keep it around -- better than the set we got in.
67 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
70 * The most common lookup is from Namespace->Module. Invest some quality time in
73 final SetMultimap<URI, Module> multimap = Multimaps.newSetMultimap(
74 new TreeMap<URI, Collection<Module>>(), URI_SET_SUPPLIER);
75 for (Module m : modules) {
76 multimap.put(m.getNamespace(), m);
79 namespaceToModules = ImmutableSetMultimap.copyOf(multimap);
83 public Set<DataSchemaNode> getDataDefinitions() {
84 final Set<DataSchemaNode> dataDefs = new HashSet<>();
85 for (Module m : modules) {
86 dataDefs.addAll(m.getChildNodes());
92 public Set<Module> getModules() {
97 public Set<NotificationDefinition> getNotifications() {
98 final Set<NotificationDefinition> notifications = new HashSet<>();
99 for (Module m : modules) {
100 notifications.addAll(m.getNotifications());
102 return notifications;
106 public Set<RpcDefinition> getOperations() {
107 final Set<RpcDefinition> rpcs = new HashSet<>();
108 for (Module m : modules) {
109 rpcs.addAll(m.getRpcs());
115 public Set<ExtensionDefinition> getExtensions() {
116 final Set<ExtensionDefinition> extensions = new HashSet<>();
117 for (Module m : modules) {
118 extensions.addAll(m.getExtensionSchemaNodes());
124 public Module findModuleByName(final String name, final Date revision) {
126 for (final Module module : modules) {
127 if (revision == null) {
128 if (module.getName().equals(name)) {
131 } else if (module.getName().equals(name) && module.getRevision().equals(revision)) {
140 public Set<Module> findModuleByNamespace(final URI namespace) {
141 final Set<Module> ret = namespaceToModules.get(namespace);
142 return ret == null ? Collections.<Module>emptySet() : ret;
146 public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
147 if (namespace == null) {
150 final Set<Module> modules = findModuleByNamespace(namespace);
151 if (modules.isEmpty()) {
155 if (revision == null) {
156 // FIXME: The ordering of modules in Multimap could just guarantee this...
157 TreeMap<Date, Module> map = new TreeMap<>();
158 for (Module module : modules) {
159 map.put(module.getRevision(), module);
164 return map.lastEntry().getValue();
166 for (Module module : modules) {
167 if (module.getRevision().equals(revision)) {
176 public boolean isAugmenting() {
181 public boolean isAddedByUses() {
186 public boolean isConfiguration() {
191 public ConstraintDefinition getConstraints() {
196 public QName getQName() {
197 return SchemaContext.NAME;
201 public SchemaPath getPath() {
206 public String getDescription() {
211 public String getReference() {
216 public Status getStatus() {
217 return Status.CURRENT;
221 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
222 final List<UnknownSchemaNode> result = new ArrayList<>();
223 for (Module module : modules) {
224 result.addAll(module.getUnknownSchemaNodes());
226 return Collections.unmodifiableList(result);
230 public Set<TypeDefinition<?>> getTypeDefinitions() {
231 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
232 for (Module module : modules) {
233 result.addAll(module.getTypeDefinitions());
235 return Collections.unmodifiableSet(result);
239 public Set<DataSchemaNode> getChildNodes() {
240 final Set<DataSchemaNode> result = new LinkedHashSet<>();
241 for (Module module : modules) {
242 result.addAll(module.getChildNodes());
244 return Collections.unmodifiableSet(result);
248 public Set<GroupingDefinition> getGroupings() {
249 final Set<GroupingDefinition> result = new LinkedHashSet<>();
250 for (Module module : modules) {
251 result.addAll(module.getGroupings());
253 return Collections.unmodifiableSet(result);
257 public DataSchemaNode getDataChildByName(final QName name) {
258 DataSchemaNode result = null;
259 for (Module module : modules) {
260 result = module.getDataChildByName(name);
261 if (result != null) {
269 public DataSchemaNode getDataChildByName(final String name) {
270 DataSchemaNode result = null;
271 for (Module module : modules) {
272 result = module.getDataChildByName(name);
273 if (result != null) {
281 public Set<UsesNode> getUses() {
282 return Collections.emptySet();
286 public boolean isPresenceContainer() {
291 public Set<AugmentationSchema> getAvailableAugmentations() {
292 return Collections.emptySet();
295 //FIXME: should work for submodules too
297 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
298 return identifiersToSources.keySet();
302 public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
303 String maybeSource = identifiersToSources.get(moduleIdentifier);
304 return Optional.fromNullable(maybeSource);
308 public String toString() {
309 return "SchemaContextImpl{" +
310 "modules=" + modules +