BUG-7057: introduce UntrustedXML class
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / SchemaContextImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.parser.impl;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.ImmutableSetMultimap;
13 import com.google.common.collect.Multimaps;
14 import com.google.common.collect.SetMultimap;
15 import java.net.URI;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.TreeMap;
19 import javax.annotation.concurrent.Immutable;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
22 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
23 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
24
25 @Immutable
26 final class SchemaContextImpl extends AbstractSchemaContext {
27
28     private  final Map<ModuleIdentifier, String> identifiersToSources;
29     private  final SetMultimap<URI, Module> namespaceToModules;
30     private  final SetMultimap<String, Module> nameToModules;
31     private  final Set<Module> modules;
32
33     SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
34         this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
35
36          /*
37          * Instead of doing this on each invocation of getModules(), pre-compute
38          * it once and keep it around -- better than the set we got in.
39          */
40         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
41
42          /*
43          * The most common lookup is from Namespace->Module.
44          *
45          * RESTCONF performs lookups based on module name only, where it wants
46          * to receive the latest revision
47          *
48          * Invest some quality time in building up lookup tables for both.
49          */
50         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
51                 new TreeMap<>(), MODULE_SET_SUPPLIER);
52         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
53                 new TreeMap<>(), MODULE_SET_SUPPLIER);
54
55         for (Module m : modules) {
56             nameMap.put(m.getName(), m);
57             nsMap.put(m.getNamespace(), m);
58         }
59
60         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
61         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
62     }
63
64     @Override
65     protected Map<ModuleIdentifier, String> getIdentifiersToSources(){
66
67         return identifiersToSources;
68     }
69
70     @Override
71     public Set<Module> getModules(){
72
73         return modules;
74     }
75
76     @Override
77     protected SetMultimap<URI, Module> getNamespaceToModules() {
78
79         return namespaceToModules;
80     }
81
82     @Override
83     protected SetMultimap<String, Module> getNameToModules() {
84
85         return nameToModules;
86     }
87
88     @Override
89     public String toString() {
90
91         return String.format("SchemaContextImpl{modules=%s}", modules);
92     }
93 }