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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.ImmutableSetMultimap;
17 import com.google.common.collect.Multimaps;
18 import com.google.common.collect.SetMultimap;
20 import java.util.ArrayList;
21 import java.util.List;
24 import java.util.TreeMap;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.model.api.Module;
29 * Simple subclass of {@link AbstractSchemaContext} which performs some amount of indexing to speed up common
30 * SchemaContext operations. This implementation assumes input modules are consistent and does not perform
31 * any extensive analysis to ensure the resulting object complies to SchemaContext interface.
34 public class SimpleSchemaContext extends AbstractSchemaContext {
35 private final SetMultimap<URI, Module> namespaceToModules;
36 private final SetMultimap<String, Module> nameToModules;
37 private final Map<QNameModule, Module> moduleMap;
38 private final Set<Module> modules;
40 protected SimpleSchemaContext(final Set<Module> modules) {
42 * Instead of doing this on each invocation of getModules(), pre-compute it once and keep it around -- better
43 * than the set we got in.
45 * Note we are performing two sort operations: the dependency sort takes care of detecting multiple imports,
46 * performing sorting as a side-effect, but we really want the modules sorted to comply with getModules().
48 final List<Module> sortedModules = new ArrayList<>(ModuleDependencySort.sort(modules));
49 sortedModules.sort(NAME_REVISION_COMPARATOR);
50 this.modules = ImmutableSet.copyOf(sortedModules);
53 * The most common lookup is from Namespace->Module.
55 * RESTCONF performs lookups based on module name only, where it wants
56 * to receive the latest revision
58 * Invest some quality time in building up lookup tables for both.
60 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<>(),
61 AbstractSchemaContext::createModuleSet);
62 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(),
63 AbstractSchemaContext::createModuleSet);
64 final Builder<QNameModule, Module> moduleMapBuilder = ImmutableMap.builder();
65 for (Module m : modules) {
66 nameMap.put(m.getName(), m);
67 nsMap.put(m.getNamespace(), m);
68 moduleMapBuilder.put(m.getQNameModule(), m);
71 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
72 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
73 moduleMap = moduleMapBuilder.build();
77 * Create a new instance from specified modules. Note that no module validation is done and hence the consistency
78 * of the resulting SchemaContext is completely in hands of the caller.
80 public static SimpleSchemaContext forModules(final Set<Module> modules) {
81 return new SimpleSchemaContext(modules);
85 public final Set<Module> getModules() {
90 protected Map<QNameModule, Module> getModuleMap() {
95 protected final SetMultimap<URI, Module> getNamespaceToModules() {
96 return namespaceToModules;
100 protected final SetMultimap<String, Module> getNameToModules() {
101 return nameToModules;
105 public final String toString() {
106 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
109 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
110 return toStringHelper.add("modules", modules);