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