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