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.ImmutableSet;
14 import com.google.common.collect.ImmutableSetMultimap;
15 import com.google.common.collect.Multimaps;
16 import com.google.common.collect.SetMultimap;
19 import java.util.TreeMap;
20 import org.opendaylight.yangtools.yang.model.api.Module;
23 * Simple subclass of {@link AbstractSchemaContext} which performs some amount of indexing to speed up common
24 * SchemaContext operations. This implementation assumes input modules are consistent and does not perform
25 * any extensive analysis to ensure the resulting object complies to SchemaContext interface.
28 public class SimpleSchemaContext extends AbstractSchemaContext {
29 private final SetMultimap<URI, Module> namespaceToModules;
30 private final SetMultimap<String, Module> nameToModules;
31 private final Set<Module> modules;
33 protected SimpleSchemaContext(final Set<Module> modules) {
35 * Instead of doing this on each invocation of getModules(), pre-compute
36 * it once and keep it around -- better than the set we got in.
38 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules));
41 * The most common lookup is from Namespace->Module.
43 * RESTCONF performs lookups based on module name only, where it wants
44 * to receive the latest revision
46 * Invest some quality time in building up lookup tables for both.
48 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<>(),
49 AbstractSchemaContext::createModuleSet);
50 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(),
51 AbstractSchemaContext::createModuleSet);
52 for (Module m : modules) {
53 nameMap.put(m.getName(), m);
54 nsMap.put(m.getNamespace(), m);
57 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
58 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
62 * Create a new instance from specified modules. Note that no module validation is done and hence the consistency
63 * of the resulting SchemaContext is completely in hands of the caller.
65 public static SimpleSchemaContext forModules(final Set<Module> modules) {
66 return new SimpleSchemaContext(modules);
70 public final Set<Module> getModules() {
75 public final String toString() {
76 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
79 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
80 return toStringHelper.add("modules", modules);
85 protected final SetMultimap<URI, Module> getNamespaceToModules() {
86 return namespaceToModules;
90 protected final SetMultimap<String, Module> getNameToModules() {