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