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;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Date;
15 import java.util.HashSet;
16 import java.util.LinkedHashSet;
17 import java.util.List;
20 import java.util.TreeMap;
22 import javax.annotation.concurrent.Immutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.opendaylight.yangtools.yang.model.api.Status;
37 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.UsesNode;
40 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
42 import com.google.common.base.Optional;
43 import com.google.common.base.Supplier;
44 import com.google.common.collect.ImmutableMap;
45 import com.google.common.collect.ImmutableSet;
46 import com.google.common.collect.ImmutableSetMultimap;
47 import com.google.common.collect.Multimaps;
48 import com.google.common.collect.SetMultimap;
51 final class SchemaContextImpl implements SchemaContext {
52 private static final Supplier<HashSet<Module>> URI_SET_SUPPLIER = new Supplier<HashSet<Module>>() {
54 public HashSet<Module> get() {
55 return new HashSet<>();
59 private final ImmutableMap<ModuleIdentifier, String> identifiersToSources;
60 private final ImmutableSetMultimap<URI, Module> namespaceToModules;
61 private final ImmutableSet<Module> modules;
63 SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
64 this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
67 * Instead of doing this on each invocation of getModules(), pre-compute
68 * it once and keep it around -- better than the set we got in.
70 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
73 * The most common lookup is from Namespace->Module. Invest some quality time in
76 final SetMultimap<URI, Module> multimap = Multimaps.newSetMultimap(
77 new TreeMap<URI, Collection<Module>>(), URI_SET_SUPPLIER);
78 for (Module m : modules) {
79 multimap.put(m.getNamespace(), m);
82 namespaceToModules = ImmutableSetMultimap.copyOf(multimap);
86 public Set<DataSchemaNode> getDataDefinitions() {
87 final Set<DataSchemaNode> dataDefs = new HashSet<>();
88 for (Module m : modules) {
89 dataDefs.addAll(m.getChildNodes());
95 public Set<Module> getModules() {
100 public Set<NotificationDefinition> getNotifications() {
101 final Set<NotificationDefinition> notifications = new HashSet<>();
102 for (Module m : modules) {
103 notifications.addAll(m.getNotifications());
105 return notifications;
109 public Set<RpcDefinition> getOperations() {
110 final Set<RpcDefinition> rpcs = new HashSet<>();
111 for (Module m : modules) {
112 rpcs.addAll(m.getRpcs());
118 public Set<ExtensionDefinition> getExtensions() {
119 final Set<ExtensionDefinition> extensions = new HashSet<>();
120 for (Module m : modules) {
121 extensions.addAll(m.getExtensionSchemaNodes());
127 public Module findModuleByName(final String name, final Date revision) {
129 for (final Module module : modules) {
130 if (revision == null) {
131 if (module.getName().equals(name)) {
134 } else if (module.getName().equals(name) && module.getRevision().equals(revision)) {
143 public Set<Module> findModuleByNamespace(final URI namespace) {
144 final Set<Module> ret = namespaceToModules.get(namespace);
145 return ret == null ? Collections.<Module>emptySet() : ret;
149 public Module findModuleByNamespaceAndRevision(final URI namespace, final Date revision) {
150 if (namespace == null) {
153 final Set<Module> modules = findModuleByNamespace(namespace);
154 if (modules.isEmpty()) {
158 if (revision == null) {
159 // FIXME: The ordering of modules in Multimap could just guarantee this...
160 TreeMap<Date, Module> map = new TreeMap<>();
161 for (Module module : modules) {
162 map.put(module.getRevision(), module);
167 return map.lastEntry().getValue();
169 for (Module module : modules) {
170 if (module.getRevision().equals(revision)) {
179 public boolean isAugmenting() {
184 public boolean isAddedByUses() {
189 public boolean isConfiguration() {
194 public ConstraintDefinition getConstraints() {
199 public QName getQName() {
200 return SchemaContext.NAME;
204 public SchemaPath getPath() {
209 public String getDescription() {
214 public String getReference() {
219 public Status getStatus() {
220 return Status.CURRENT;
224 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
225 final List<UnknownSchemaNode> result = new ArrayList<>();
226 for (Module module : modules) {
227 result.addAll(module.getUnknownSchemaNodes());
229 return Collections.unmodifiableList(result);
233 public Set<TypeDefinition<?>> getTypeDefinitions() {
234 final Set<TypeDefinition<?>> result = new LinkedHashSet<>();
235 for (Module module : modules) {
236 result.addAll(module.getTypeDefinitions());
238 return Collections.unmodifiableSet(result);
242 public Set<DataSchemaNode> getChildNodes() {
243 final Set<DataSchemaNode> result = new LinkedHashSet<>();
244 for (Module module : modules) {
245 result.addAll(module.getChildNodes());
247 return Collections.unmodifiableSet(result);
251 public Set<GroupingDefinition> getGroupings() {
252 final Set<GroupingDefinition> result = new LinkedHashSet<>();
253 for (Module module : modules) {
254 result.addAll(module.getGroupings());
256 return Collections.unmodifiableSet(result);
260 public DataSchemaNode getDataChildByName(final QName name) {
261 DataSchemaNode result = null;
262 for (Module module : modules) {
263 result = module.getDataChildByName(name);
264 if (result != null) {
272 public DataSchemaNode getDataChildByName(final String name) {
273 DataSchemaNode result = null;
274 for (Module module : modules) {
275 result = module.getDataChildByName(name);
276 if (result != null) {
284 public Set<UsesNode> getUses() {
285 return Collections.emptySet();
289 public boolean isPresenceContainer() {
294 public Set<AugmentationSchema> getAvailableAugmentations() {
295 return Collections.emptySet();
298 //FIXME: should work for submodules too
300 public Set<ModuleIdentifier> getAllModuleIdentifiers() {
301 return identifiersToSources.keySet();
305 public Optional<String> getModuleSource(final ModuleIdentifier moduleIdentifier) {
306 String maybeSource = identifiersToSources.get(moduleIdentifier);
307 return Optional.fromNullable(maybeSource);
311 public String toString() {
312 return "SchemaContextImpl{" +
313 "modules=" + modules +