Lookup leaf key methods in parents
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / ModuleContext.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.mdsal.binding.generator.impl;
9
10 import com.google.common.collect.BiMap;
11 import com.google.common.collect.HashBiMap;
12 import com.google.common.collect.HashMultimap;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Multimap;
15 import com.google.common.collect.Multimaps;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
25 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
33
34 public final class ModuleContext {
35     private GeneratedTypeBuilder moduleNode;
36     private final List<GeneratedTOBuilder> genTOs = new ArrayList<>();
37     private final Map<SchemaPath, Type> typedefs = new HashMap<>();
38     private final Map<SchemaPath, GeneratedTypeBuilder> childNodes = new HashMap<>();
39     private final Map<SchemaPath, GeneratedTypeBuilder> groupings = new HashMap<>();
40     private final Map<SchemaPath, GeneratedTypeBuilder> cases = new HashMap<>();
41     private final Map<QName,GeneratedTOBuilder> identities = new HashMap<>();
42     private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
43     private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
44     private final BiMap<Type, AugmentationSchema> typeToAugmentation = HashBiMap.create();
45
46     private final Map<Type,Object> typeToSchema = new HashMap<>();
47
48
49     private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
50     private final BiMap<Type, ChoiceCaseNode> caseTypeToSchema = HashBiMap.create();
51
52     private final Map<SchemaPath, Type> innerTypes = new HashMap<>();
53
54     List<Type> getGeneratedTypes() {
55         List<Type> result = new ArrayList<>();
56
57         if (moduleNode != null) {
58             result.add(moduleNode.toInstance());
59         }
60
61         for (GeneratedTOBuilder b : genTOs) {
62             result.add(b.toInstance());
63         }
64         for (Type b : typedefs.values()) {
65             if (b != null) {
66                 result.add(b);
67             }
68         }
69         for (GeneratedTypeBuilder b : childNodes.values()) {
70             result.add(b.toInstance());
71         }
72         for (GeneratedTypeBuilder b : groupings.values()) {
73             result.add(b.toInstance());
74         }
75         for (GeneratedTypeBuilder b : cases.values()) {
76             result.add(b.toInstance());
77         }
78         for (GeneratedTOBuilder b : identities.values()) {
79             result.add(b.toInstance());
80         }
81         for (GeneratedTypeBuilder b : topLevelNodes) {
82             result.add(b.toInstance());
83         }
84         for (GeneratedTypeBuilder b : augmentations) {
85             result.add(b.toInstance());
86         }
87         return result;
88     }
89
90     public Multimap<Type, Type> getChoiceToCases() {
91         return Multimaps.unmodifiableMultimap(choiceToCases);
92     }
93
94     public GeneratedTypeBuilder getModuleNode() {
95         return moduleNode;
96     }
97
98     public GeneratedTypeBuilder getChildNode(final SchemaPath p) {
99         return childNodes.get(p);
100     }
101
102     public GeneratedTypeBuilder getGrouping(final SchemaPath p) {
103         return groupings.get(p);
104     }
105
106     public GeneratedTypeBuilder getCase(final SchemaPath p) {
107         return cases.get(p);
108     }
109
110     public void addModuleNode(final GeneratedTypeBuilder moduleNode) {
111         this.moduleNode = moduleNode;
112     }
113
114     public void addGeneratedTOBuilder(final GeneratedTOBuilder b) {
115         genTOs.add(b);
116     }
117
118     public void addChildNodeType(final SchemaNode p, final GeneratedTypeBuilder b) {
119         childNodes.put(p.getPath(), b);
120         typeToSchema.put(b,p);
121     }
122
123     public void addGroupingType(final SchemaPath p, final GeneratedTypeBuilder b) {
124         groupings.put(p, b);
125     }
126
127     public void addTypedefType(final SchemaPath p, final Type t) {
128         typedefs.put(p, t);
129     }
130
131     public void addCaseType(final SchemaPath p, final GeneratedTypeBuilder b) {
132         cases.put(p, b);
133     }
134
135     public void addIdentityType(final QName name,final GeneratedTOBuilder b) {
136         identities.put(name,b);
137     }
138
139     public void addTopLevelNodeType(final GeneratedTypeBuilder b) {
140         topLevelNodes.add(b);
141     }
142
143     public void addAugmentType(final GeneratedTypeBuilder b) {
144         augmentations.add(b);
145     }
146
147     public Map<SchemaPath, Type> getTypedefs() {
148         return typedefs;
149     }
150
151     public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
152         return Collections.unmodifiableMap(childNodes);
153     }
154
155     public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
156         return Collections.unmodifiableMap(groupings);
157     }
158
159     public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
160         return Collections.unmodifiableMap(cases);
161     }
162
163     public Map<QName,GeneratedTOBuilder> getIdentities() {
164         return Collections.unmodifiableMap(identities);
165     }
166
167     public Set<GeneratedTypeBuilder> getTopLevelNodes() {
168         return Collections.unmodifiableSet(topLevelNodes);
169     }
170
171     public List<GeneratedTypeBuilder> getAugmentations() {
172         return Collections.unmodifiableList(augmentations);
173     }
174
175     public BiMap<Type, AugmentationSchema> getTypeToAugmentation() {
176         return Maps.unmodifiableBiMap(typeToAugmentation);
177     }
178
179     public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchema schema) {
180         typeToAugmentation.put(builder, schema);
181         typeToSchema.put(builder, schema);
182     }
183
184     public void addChoiceToCaseMapping(final Type choiceType, final Type caseType, final ChoiceCaseNode schema) {
185         choiceToCases.put(choiceType, caseType);
186         caseTypeToSchema.put(caseType, schema);
187         typeToSchema.put(caseType, schema);
188     }
189
190     public BiMap<Type, ChoiceCaseNode> getCaseTypeToSchemas() {
191         return Maps.unmodifiableBiMap(caseTypeToSchema);
192     }
193
194     /**
195      *
196      * Returns mapping of type to its schema.
197      *
198      * Valid values are only instances of {@link DataSchemaNode} or {@link AugmentationSchema}
199      *
200      * @return Mapping from type to corresponding schema
201      */
202     public Map<Type, Object> getTypeToSchema() {
203         return Collections.unmodifiableMap(typeToSchema);
204     }
205
206     protected void addTypeToSchema(Type type, TypeDefinition<?> typedef) {
207         typeToSchema.put(type, typedef);
208     }
209
210     /**
211      * Adds mapping between schema path and an inner type.
212      *
213      * @param path
214      * @param type
215      */
216     void addInnerTypedefType(final SchemaPath path, final Type type) {
217         innerTypes.put(path, type);
218     }
219
220     public Type getInnerType(SchemaPath path) {
221         return innerTypes.get(path);
222     }
223
224 }