Bug 2366 - new parser API - implementation of declared statements
[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
35     private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
36     private final ImmutableList<EffectiveStatement<?,?>> rootEffectiveStatements;
37
38     public EffectiveSchemaContext(List<DeclaredStatement<?>> rootDeclaredStatements, List<EffectiveStatement<?,?>> rootEffectiveStatements) {
39         this.rootDeclaredStatements = ImmutableList.copyOf(rootDeclaredStatements);
40         this.rootEffectiveStatements = ImmutableList.copyOf(rootEffectiveStatements);
41
42         HashSet<Module> modules = new HashSet<Module>();
43         for (EffectiveStatement<?, ?> rootEffectiveStatement : rootEffectiveStatements) {
44             if(rootEffectiveStatement instanceof Module) {
45                 Module module = (Module) rootEffectiveStatement;
46                 modules.add(module);
47             }
48         }
49         this.modules = ImmutableSet.copyOf(modules);
50
51        final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
52                new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
53        final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
54                new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
55
56        for (Module m : modules) {
57            nameMap.put(m.getName(), m);
58            nsMap.put(m.getNamespace(), m);
59        }
60
61        namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
62        nameToModules = ImmutableSetMultimap.copyOf(nameMap);
63
64        //:TODO
65        //this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
66        this.identifiersToSources = null;
67
68     }
69
70     public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
71         return rootDeclaredStatements;
72     }
73
74     public ImmutableList<EffectiveStatement<?,?>> getRootEffectiveStatements() {
75         return rootEffectiveStatements;
76     }
77
78     @Override
79     protected Map<ModuleIdentifier, String> getIdentifiersToSources(){
80
81         return identifiersToSources;
82     }
83
84     @Override
85     public Set<Module> getModules(){
86
87         return modules;
88     }
89
90     @Override
91     protected SetMultimap<URI, Module> getNamespaceToModules() {
92
93         return namespaceToModules;
94     }
95
96     @Override
97     protected SetMultimap<String, Module> getNameToModules() {
98
99         return nameToModules;
100     }
101
102     @Override
103     public String toString() {
104
105         return String.format("SchemaContextImpl{modules=%s}", modules);
106     }
107 }