Merge branch 'master' of ../controller
[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 static java.util.Objects.requireNonNull;
11
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;
21 import java.net.URI;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.TreeMap;
28 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode;
29 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNodeAwareSchemaContext;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33
34 /**
35  * Simple subclass of {@link AbstractSchemaContext} which performs some amount of indexing to speed up common
36  * SchemaContext operations. This implementation assumes input modules are consistent and does not perform
37  * any extensive analysis to ensure the resulting object complies to SchemaContext interface.
38  */
39 @Beta
40 public class SimpleSchemaContext extends AbstractSchemaContext implements AnnotationSchemaNodeAwareSchemaContext {
41     private final ImmutableSetMultimap<URI, Module> namespaceToModules;
42     private final ImmutableSetMultimap<String, Module> nameToModules;
43     private final ImmutableMap<QNameModule, Module> moduleMap;
44     private final ImmutableSet<Module> modules;
45     private final ImmutableMap<QName, AnnotationSchemaNode> annotations;
46
47     protected SimpleSchemaContext(final Set<Module> modules) {
48         /*
49          * Instead of doing this on each invocation of getModules(), pre-compute it once and keep it around -- better
50          * than the set we got in.
51          *
52          * Note we are performing two sort operations: the dependency sort takes care of detecting multiple imports,
53          * performing sorting as a side-effect, but we really want the modules sorted to comply with getModules().
54          */
55         final List<Module> sortedModules = new ArrayList<>(ModuleDependencySort.sort(modules));
56         sortedModules.sort(NAME_REVISION_COMPARATOR);
57         this.modules = ImmutableSet.copyOf(sortedModules);
58
59         /*
60          * The most common lookup is from Namespace->Module.
61          *
62          * RESTCONF performs lookups based on module name only, where it wants
63          * to receive the latest revision
64          *
65          * Invest some quality time in building up lookup tables for both.
66          */
67         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<>(),
68             AbstractSchemaContext::createModuleSet);
69         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(),
70             AbstractSchemaContext::createModuleSet);
71         final Builder<QNameModule, Module> moduleMapBuilder = ImmutableMap.builder();
72         for (Module m : modules) {
73             nameMap.put(m.getName(), m);
74             nsMap.put(m.getNamespace(), m);
75             moduleMapBuilder.put(m.getQNameModule(), m);
76         }
77
78         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
79         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
80         moduleMap = moduleMapBuilder.build();
81         annotations = ImmutableMap.copyOf(AnnotationSchemaNode.findAll(this));
82     }
83
84     /**
85      * Create a new instance from specified modules. Note that no module validation is done and hence the consistency
86      * of the resulting SchemaContext is completely in hands of the caller.
87      */
88     public static SimpleSchemaContext forModules(final Set<Module> modules) {
89         return new SimpleSchemaContext(modules);
90     }
91
92     @Override
93     public final Set<Module> getModules() {
94         return modules;
95     }
96
97     @Override
98     public final Optional<AnnotationSchemaNode> findAnnotation(final QName qname) {
99         return Optional.ofNullable(annotations.get(requireNonNull(qname)));
100     }
101
102     @Override
103     protected Map<QNameModule, Module> getModuleMap() {
104         return moduleMap;
105     }
106
107     @Override
108     protected final SetMultimap<URI, Module> getNamespaceToModules() {
109         return namespaceToModules;
110     }
111
112     @Override
113     protected final SetMultimap<String, Module> getNameToModules() {
114         return nameToModules;
115     }
116
117     @Override
118     public final String toString() {
119         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
120     }
121
122     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
123         return toStringHelper.add("modules", modules);
124     }
125 }