BUG-4688: Disconnect Module and ModuleIdentifier
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SimpleSchemaContext.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
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;
17 import java.net.URI;
18 import java.util.Set;
19 import java.util.TreeMap;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21
22 /**
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.
26  */
27 @Beta
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;
32
33     protected SimpleSchemaContext(final Set<Module> modules) {
34         /*
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.
37          */
38         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules));
39
40         /*
41          * The most common lookup is from Namespace->Module.
42          *
43          * RESTCONF performs lookups based on module name only, where it wants
44          * to receive the latest revision
45          *
46          * Invest some quality time in building up lookup tables for both.
47          */
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);
55         }
56
57         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
58         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
59     }
60
61     /**
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.
64      */
65     public static SimpleSchemaContext forModules(final Set<Module> modules) {
66         return new SimpleSchemaContext(modules);
67     }
68
69     @Override
70     public final Set<Module> getModules() {
71         return modules;
72     }
73
74     @Override
75     public final String toString() {
76         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
77     }
78
79     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
80         return toStringHelper.add("modules", modules);
81     }
82
83
84     @Override
85     protected final SetMultimap<URI, Module> getNamespaceToModules() {
86         return namespaceToModules;
87     }
88
89     @Override
90     protected final SetMultimap<String, Module> getNameToModules() {
91         return nameToModules;
92     }
93 }