Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveSchemaContext.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.stmt.rfc6020.effective;
9
10 import java.util.HashSet;
11 import com.google.common.collect.ImmutableList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
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.Collection;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.TreeMap;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
26
27 public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
28
29     private final Map<ModuleIdentifier, String> identifiersToSources;
30     private final SetMultimap<URI, Module> namespaceToModules;
31     private final SetMultimap<String, Module> nameToModules;
32     private final Set<Module> modules;
33
34     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
35     private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
36
37     public EffectiveSchemaContext(
38             List<DeclaredStatement<?>> rootDeclaredStatements,
39             List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
40         this.rootDeclaredStatements = ImmutableList
41                 .copyOf(rootDeclaredStatements);
42         this.rootEffectiveStatements = ImmutableList
43                 .copyOf(rootEffectiveStatements);
44
45         Set<Module> modulesInit = new HashSet<>();
46         for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
47             if (rootEffectiveStatement instanceof ModuleEffectiveStatementImpl) {
48                 Module module = (Module) rootEffectiveStatement;
49                 modulesInit.add(module);
50             }
51         }
52         this.modules = ImmutableSet.copyOf(modulesInit);
53
54         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
55                 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
56         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
57                 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
58
59         for (Module m : modulesInit) {
60             nameMap.put(m.getName(), m);
61             nsMap.put(m.getNamespace(), m);
62         }
63
64         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
65         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
66
67         // :TODO init identifiersToSources
68         this.identifiersToSources = null;
69
70     }
71
72     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
73         return rootDeclaredStatements;
74     }
75
76     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
77         return rootEffectiveStatements;
78     }
79
80     @Override
81     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
82
83         return identifiersToSources;
84     }
85
86     @Override
87     public Set<Module> getModules() {
88
89         return modules;
90     }
91
92     @Override
93     protected SetMultimap<URI, Module> getNamespaceToModules() {
94
95         return namespaceToModules;
96     }
97
98     @Override
99     protected SetMultimap<String, Module> getNameToModules() {
100
101         return nameToModules;
102     }
103
104     @Override
105     public String toString() {
106
107         return String.format("SchemaContextImpl{modules=%s}", modules);
108     }
109 }