Cache YangModuleInfo type name
[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 com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.BiMap;
14 import com.google.common.collect.HashBiMap;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Multimap;
18 import com.google.common.collect.Multimaps;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
29 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
30 import org.opendaylight.mdsal.binding.model.api.Type;
31 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
32 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
33 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
34 import org.opendaylight.yangtools.concepts.Mutable;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
41 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
42 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
45 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Utility class for building up Binding mapping information. This class is NOT thread-safe.
54  */
55 public final class ModuleContext implements Mutable {
56     private static final Logger LOG = LoggerFactory.getLogger(ModuleContext.class);
57
58     private final BiMap<Type, AugmentationSchemaNode> typeToAugmentation = HashBiMap.create();
59     private final Map<SchemaPath, GeneratedTypeBuilder> childNodes = new HashMap<>();
60     private final Map<SchemaPath, GeneratedTypeBuilder> groupings = new HashMap<>();
61     private final BiMap<Type, CaseSchemaNode> caseTypeToSchema = HashBiMap.create();
62     private final Map<SchemaPath, GeneratedTypeBuilder> cases = new HashMap<>();
63     private final Map<QName, GeneratedTypeBuilder> identities = new HashMap<>();
64     private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
65     private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
66     private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
67     private final Map<SchemaPath, JavaTypeName> aliases = new HashMap<>();
68     private final Map<Type, WithStatus> typeToSchema = new HashMap<>();
69     private final List<GeneratedTOBuilder> genTOs = new ArrayList<>();
70     private final Map<SchemaPath, Type> innerTypes = new HashMap<>();
71     private final Map<SchemaPath, Type> typedefs = new HashMap<>();
72     private final Module module;
73
74     // Conflict mapping
75     private final Map<JavaTypeName, SchemaNode> nameMapping = new HashMap<>();
76
77     private GeneratedTypeBuilder moduleNode;
78     private JavaTypeName moduleInfoType;
79     private String modulePackageName;
80
81     ModuleContext(final Module module) {
82         this.module = requireNonNull(module);
83     }
84
85     Module module() {
86         return module;
87     }
88
89     @NonNull String modulePackageName() {
90         String ret = modulePackageName;
91         if (ret == null) {
92             modulePackageName = ret = BindingMapping.getRootPackageName(module.getQNameModule());
93         }
94         return ret;
95     }
96
97     @NonNull JavaTypeName moduleInfoType() {
98         JavaTypeName ret = moduleInfoType;
99         if (ret == null) {
100             moduleInfoType = ret = JavaTypeName.create(modulePackageName(), BindingMapping.MODULE_INFO_CLASS_NAME);
101         }
102         return ret;
103     }
104
105     List<Type> getGeneratedTypes() {
106         List<Type> result = new ArrayList<>();
107
108         if (moduleNode != null) {
109             result.add(moduleNode.build());
110         }
111
112         for (GeneratedTOBuilder b : genTOs) {
113             result.add(b.build());
114         }
115         for (Type b : typedefs.values()) {
116             if (b != null) {
117                 result.add(b);
118             }
119         }
120         for (GeneratedTypeBuilder b : childNodes.values()) {
121             result.add(b.build());
122         }
123         for (GeneratedTypeBuilder b : groupings.values()) {
124             result.add(b.build());
125         }
126         for (GeneratedTypeBuilder b : cases.values()) {
127             result.add(b.build());
128         }
129         for (GeneratedTypeBuilder b : identities.values()) {
130             result.add(b.build());
131         }
132         for (GeneratedTypeBuilder b : topLevelNodes) {
133             result.add(b.build());
134         }
135         for (GeneratedTypeBuilder b : augmentations) {
136             result.add(b.build());
137         }
138         return result;
139     }
140
141     public Multimap<Type, Type> getChoiceToCases() {
142         return Multimaps.unmodifiableMultimap(choiceToCases);
143     }
144
145     public GeneratedTypeBuilder getModuleNode() {
146         return moduleNode;
147     }
148
149     public GeneratedTypeBuilder getChildNode(final SchemaPath path) {
150         return childNodes.get(path);
151     }
152
153     public GeneratedTypeBuilder getGrouping(final SchemaPath path) {
154         return groupings.get(path);
155     }
156
157     public GeneratedTypeBuilder getCase(final SchemaPath path) {
158         return cases.get(path);
159     }
160
161     public void addModuleNode(final GeneratedTypeBuilder newModuleNode) {
162         this.moduleNode = newModuleNode;
163     }
164
165     public void addGeneratedTOBuilder(final GeneratedTOBuilder builder) {
166         genTOs.add(builder);
167     }
168
169     @NonNull GeneratedType addAliasType(final ModuleContext sourceContext, final ContainerSchemaNode source,
170             final ContainerSchemaNode alias) {
171         final GeneratedTypeBuilder builder = sourceContext.getChildNode(source.getPath());
172         checkState(builder != null, "Could not find builder for %s", source);
173
174         final JavaTypeName id = builder.getIdentifier();
175         final SchemaPath path = alias.getPath();
176         final JavaTypeName prev = aliases.putIfAbsent(path, id);
177         checkState(prev == null, "Type aliasing conflict on %s: %s vs %s", path, prev, id);
178
179         return builder.build();
180     }
181
182     @Nullable JavaTypeName getAlias(final SchemaPath path) {
183         return aliases.get(path);
184     }
185
186     public void addChildNodeType(final SchemaNode def, final GeneratedTypeBuilder builder) {
187         checkNamingConflict(def, builder.getIdentifier());
188         childNodes.put(def.getPath(), builder);
189         typeToSchema.put(builder, def);
190     }
191
192     public void addGroupingType(final GroupingDefinition def, final GeneratedTypeBuilder builder) {
193         checkNamingConflict(def, builder.getIdentifier());
194         groupings.put(def.getPath(), builder);
195     }
196
197     public void addTypedefType(final TypeDefinition<?> def, final Type type) {
198         final JavaTypeName name = type.getIdentifier();
199         final SchemaNode existingDef = nameMapping.putIfAbsent(name, def);
200         if (existingDef != null) {
201             if (!(existingDef instanceof TypeDefinition)) {
202                 throw resolveNamingConfict(existingDef, def, name);
203             }
204
205             // This seems to be fine
206             LOG.debug("GeneratedType conflict between {} and {} on {}", def, existingDef, name);
207         }
208
209         typedefs.put(def.getPath(), type);
210     }
211
212     public void addCaseType(final SchemaPath path, final GeneratedTypeBuilder builder) {
213         cases.put(path, builder);
214     }
215
216     public void addIdentityType(final IdentitySchemaNode def, final GeneratedTypeBuilder builder) {
217         checkNamingConflict(def, builder.getIdentifier());
218         identities.put(def.getQName(), builder);
219     }
220
221     public void addTopLevelNodeType(final GeneratedTypeBuilder builder) {
222         topLevelNodes.add(builder);
223     }
224
225     public void addAugmentType(final GeneratedTypeBuilder builder) {
226         augmentations.add(builder);
227     }
228
229     public Map<SchemaPath, Type> getTypedefs() {
230         return typedefs;
231     }
232
233     public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
234         return Collections.unmodifiableMap(childNodes);
235     }
236
237     public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
238         return Collections.unmodifiableMap(groupings);
239     }
240
241     public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
242         return Collections.unmodifiableMap(cases);
243     }
244
245     public Map<QName, GeneratedTypeBuilder> getIdentities() {
246         return Collections.unmodifiableMap(identities);
247     }
248
249     public Set<GeneratedTypeBuilder> getTopLevelNodes() {
250         return Collections.unmodifiableSet(topLevelNodes);
251     }
252
253     public List<GeneratedTypeBuilder> getAugmentations() {
254         return Collections.unmodifiableList(augmentations);
255     }
256
257     public BiMap<Type, AugmentationSchemaNode> getTypeToAugmentation() {
258         return Maps.unmodifiableBiMap(typeToAugmentation);
259     }
260
261     public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchemaNode schema) {
262         typeToAugmentation.put(builder, schema);
263         typeToSchema.put(builder, schema);
264     }
265
266     public void addChoiceToCaseMapping(final Type choiceType, final Type caseType, final CaseSchemaNode schema) {
267         choiceToCases.put(choiceType, caseType);
268         caseTypeToSchema.put(caseType, schema);
269         typeToSchema.put(caseType, schema);
270     }
271
272     public BiMap<Type, CaseSchemaNode> getCaseTypeToSchemas() {
273         return Maps.unmodifiableBiMap(caseTypeToSchema);
274     }
275
276     /**
277      * Returns mapping of type to its schema. Valid values are only instances of {@link DataSchemaNode}
278      * or {@link AugmentationSchemaNode}.
279      *
280      * @return Mapping from type to corresponding schema
281      */
282     public Map<Type, WithStatus> getTypeToSchema() {
283         return Collections.unmodifiableMap(typeToSchema);
284     }
285
286     protected void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
287         typeToSchema.put(type, typedef);
288     }
289
290     /**
291      * Adds mapping between schema path and an inner type.
292      */
293     void addInnerTypedefType(final SchemaPath path, final Type type) {
294         innerTypes.put(path, type);
295     }
296
297     public Type getInnerType(final SchemaPath path) {
298         return innerTypes.get(path);
299     }
300
301     private void checkNamingConflict(final SchemaNode def, final JavaTypeName name) {
302         final SchemaNode existingDef = nameMapping.putIfAbsent(name, def);
303         if (existingDef != null) {
304             if (def.equals(existingDef)) {
305                 if (LOG.isDebugEnabled()) {
306                     // TODO: this should not really be happening
307                     LOG.debug("Duplicate definition on {} as {}", name, def, new Throwable());
308                 }
309             } else {
310                 throw resolveNamingConfict(existingDef, def, name);
311             }
312         }
313     }
314
315     private static IllegalStateException resolveNamingConfict(final SchemaNode existing, final SchemaNode incoming,
316             final JavaTypeName name) {
317         if (existing instanceof IdentitySchemaNode) {
318             if (incoming instanceof GroupingDefinition || incoming instanceof TypeDefinition
319                     || incoming instanceof DataSchemaNode || incoming instanceof NotificationDefinition
320                     || incoming instanceof OperationDefinition) {
321                 return new RenameMappingException(name, existing);
322             }
323         } else if (existing instanceof GroupingDefinition) {
324             if (incoming instanceof IdentitySchemaNode) {
325                 return new RenameMappingException(name, incoming);
326             }
327             if (incoming instanceof TypeDefinition || incoming instanceof DataSchemaNode
328                     || incoming instanceof NotificationDefinition || incoming instanceof OperationDefinition) {
329                 return new RenameMappingException(name, existing);
330             }
331         } else if (existing instanceof TypeDefinition) {
332             if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode) {
333                 return new RenameMappingException(name, incoming);
334             }
335             if (incoming instanceof DataSchemaNode || incoming instanceof NotificationDefinition
336                     || incoming instanceof OperationDefinition) {
337                 return new RenameMappingException(name, existing);
338             }
339         } else {
340             if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode
341                     || incoming instanceof TypeDefinition) {
342                 return new RenameMappingException(name, incoming);
343             }
344         }
345
346         return new IllegalStateException(String.format("Unhandled GeneratedType conflict between %s and %s on %s",
347             incoming, existing, name));
348     }
349 }