2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.model.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.ImmutableSetMultimap;
19 import com.google.common.collect.Multimaps;
20 import com.google.common.collect.SetMultimap;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
26 import java.util.Optional;
28 import java.util.TreeMap;
29 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode;
30 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNodeAwareSchemaContext;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.model.api.Module;
36 * Simple subclass of {@link AbstractSchemaContext} which performs some amount of indexing to speed up common
37 * SchemaContext operations. This implementation assumes input modules are consistent and does not perform
38 * any extensive analysis to ensure the resulting object complies to SchemaContext interface.
41 public class SimpleSchemaContext extends AbstractSchemaContext implements AnnotationSchemaNodeAwareSchemaContext {
42 private final ImmutableSetMultimap<URI, Module> namespaceToModules;
43 private final ImmutableSetMultimap<String, Module> nameToModules;
44 private final ImmutableMap<QNameModule, Module> moduleMap;
45 private final ImmutableSet<Module> modules;
46 private final ImmutableMap<QName, AnnotationSchemaNode> annotations;
48 protected SimpleSchemaContext(final Collection<? extends Module> modules) {
50 * Instead of doing this on each invocation of getModules(), pre-compute it once and keep it around -- better
51 * than the set we got in.
53 * Note we are performing two sort operations: the dependency sort takes care of detecting multiple imports,
54 * performing sorting as a side-effect, but we really want the modules sorted to comply with getModules().
56 final List<Module> sortedModules = new ArrayList<>(ModuleDependencySort.sort(modules));
57 sortedModules.sort(NAME_REVISION_COMPARATOR);
58 this.modules = ImmutableSet.copyOf(sortedModules);
61 * The most common lookup is from Namespace->Module.
63 * RESTCONF performs lookups based on module name only, where it wants
64 * to receive the latest revision
66 * Invest some quality time in building up lookup tables for both.
68 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<>(),
69 AbstractSchemaContext::createModuleSet);
70 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(),
71 AbstractSchemaContext::createModuleSet);
72 final Builder<QNameModule, Module> moduleMapBuilder = ImmutableMap.builder();
73 for (Module m : modules) {
74 nameMap.put(m.getName(), m);
75 nsMap.put(m.getNamespace(), m);
76 moduleMapBuilder.put(m.getQNameModule(), m);
79 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
80 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
81 moduleMap = moduleMapBuilder.build();
82 annotations = ImmutableMap.copyOf(AnnotationSchemaNode.findAll(this));
86 * Create a new instance from specified modules. Note that no module validation is done and hence the consistency
87 * of the resulting SchemaContext is completely in hands of the caller.
89 public static SimpleSchemaContext forModules(final Collection<? extends Module> modules) {
90 return new SimpleSchemaContext(modules);
94 public final Set<Module> getModules() {
99 public final Optional<AnnotationSchemaNode> findAnnotation(final QName qname) {
100 return Optional.ofNullable(annotations.get(requireNonNull(qname)));
104 protected Map<QNameModule, Module> getModuleMap() {
109 protected final SetMultimap<URI, Module> getNamespaceToModules() {
110 return namespaceToModules;
114 protected final SetMultimap<String, Module> getNameToModules() {
115 return nameToModules;
119 public final String toString() {
120 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
123 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
124 return toStringHelper.add("modules", modules);