Do not fake an errorMessage for pattern constraints
[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.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;
19 import java.net.URI;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.TreeMap;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27
28 /**
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.
32  */
33 @Beta
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;
39
40     protected SimpleSchemaContext(final Set<Module> modules) {
41         /*
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.
44          *
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().
47          */
48         final List<Module> sortedModules = new ArrayList<>(ModuleDependencySort.sort(modules));
49         sortedModules.sort(NAME_REVISION_COMPARATOR);
50         this.modules = ImmutableSet.copyOf(sortedModules);
51
52         /*
53          * The most common lookup is from Namespace->Module.
54          *
55          * RESTCONF performs lookups based on module name only, where it wants
56          * to receive the latest revision
57          *
58          * Invest some quality time in building up lookup tables for both.
59          */
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);
69         }
70
71         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
72         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
73         moduleMap = moduleMapBuilder.build();
74     }
75
76     /**
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.
79      */
80     public static SimpleSchemaContext forModules(final Set<Module> modules) {
81         return new SimpleSchemaContext(modules);
82     }
83
84     @Override
85     public final Set<Module> getModules() {
86         return modules;
87     }
88
89     @Override
90     protected Map<QNameModule, Module> getModuleMap() {
91         return moduleMap;
92     }
93
94     @Override
95     protected final SetMultimap<URI, Module> getNamespaceToModules() {
96         return namespaceToModules;
97     }
98
99     @Override
100     protected final SetMultimap<String, Module> getNameToModules() {
101         return nameToModules;
102     }
103
104     @Override
105     public final String toString() {
106         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
107     }
108
109     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
110         return toStringHelper.add("modules", modules);
111     }
112 }