Bug 2366 - Effective statements impl for new yang parser.
[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         HashSet<Module> modules = new HashSet<Module>();
46         for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
47             if (rootEffectiveStatement instanceof Module) {
48                 Module module = (Module) rootEffectiveStatement;
49                 modules.add(module);
50             }
51         }
52         this.modules = ImmutableSet.copyOf(modules);
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 : modules) {
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
68         // this.identifiersToSources =
69         // ImmutableMap.copyOf(identifiersToSources);
70         this.identifiersToSources = null;
71
72     }
73
74     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
75         return rootDeclaredStatements;
76     }
77
78     public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
79         return rootEffectiveStatements;
80     }
81
82     @Override
83     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
84
85         return identifiersToSources;
86     }
87
88     @Override
89     public Set<Module> getModules() {
90
91         return modules;
92     }
93
94     @Override
95     protected SetMultimap<URI, Module> getNamespaceToModules() {
96
97         return namespaceToModules;
98     }
99
100     @Override
101     protected SetMultimap<String, Module> getNameToModules() {
102
103         return nameToModules;
104     }
105
106     @Override
107     public String toString() {
108
109         return String.format("SchemaContextImpl{modules=%s}", modules);
110     }
111 }