872529db7557bb5b849c190b5e33fbc964406d4f
[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 p) {
133         return childNodes.get(p);
134     }
135
136     public GeneratedTypeBuilder getGrouping(final SchemaPath p) {
137         return groupings.get(p);
138     }
139
140     public GeneratedTypeBuilder getCase(final SchemaPath p) {
141         return cases.get(p);
142     }
143
144     public void addModuleNode(final GeneratedTypeBuilder moduleNode) {
145         this.moduleNode = moduleNode;
146     }
147
148     public void addGeneratedTOBuilder(final GeneratedTOBuilder b) {
149         genTOs.add(b);
150     }
151
152     public void addChildNodeType(final SchemaNode def, final GeneratedTypeBuilder b) {
153         checkNamingConflict(def, b.getIdentifier());
154         childNodes.put(def.getPath(), b);
155         typeToSchema.put(b, def);
156     }
157
158     public void addGroupingType(final GroupingDefinition def, final GeneratedTypeBuilder b) {
159         checkNamingConflict(def, b.getIdentifier());
160         groupings.put(def.getPath(), b);
161     }
162
163     public void addTypedefType(final TypeDefinition<?> def, final Type t) {
164         final JavaTypeName name = t.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(), t);
176     }
177
178     public void addCaseType(final SchemaPath p, final GeneratedTypeBuilder b) {
179         cases.put(p, b);
180     }
181
182     public void addIdentityType(final IdentitySchemaNode def, final GeneratedTypeBuilder b) {
183         checkNamingConflict(def, b.getIdentifier());
184         identities.put(def.getQName(), b);
185     }
186
187     public void addTopLevelNodeType(final GeneratedTypeBuilder b) {
188         topLevelNodes.add(b);
189     }
190
191     public void addAugmentType(final GeneratedTypeBuilder b) {
192         augmentations.add(b);
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.
244      *
245      * Valid values are only instances of {@link DataSchemaNode} or {@link AugmentationSchemaNode}
246      *
247      * @return Mapping from type to corresponding schema
248      */
249     public Map<Type, WithStatus> getTypeToSchema() {
250         return Collections.unmodifiableMap(typeToSchema);
251     }
252
253     protected void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
254         typeToSchema.put(type, typedef);
255     }
256
257     /**
258      * Adds mapping between schema path and an inner type.
259      *
260      * @param path
261      * @param type
262      */
263     void addInnerTypedefType(final SchemaPath path, final Type type) {
264         innerTypes.put(path, type);
265     }
266
267     public Type getInnerType(final SchemaPath path) {
268         return innerTypes.get(path);
269     }
270
271     private void checkNamingConflict(final SchemaNode def, final JavaTypeName name) {
272         final SchemaNode existingDef = nameMapping.putIfAbsent(name, def);
273         if (existingDef != null) {
274             if (def.equals(existingDef)) {
275                 if (LOG.isDebugEnabled()) {
276                     // TODO: this should not really be happening
277                     LOG.debug("Duplicate definition on {} as {}", name, def, new Throwable());
278                 }
279             } else {
280                 throw resolveNamingConfict(existingDef, def, name);
281             }
282         }
283     }
284
285     private static IllegalStateException resolveNamingConfict(final SchemaNode existing, final SchemaNode incoming,
286             final JavaTypeName name) {
287         if (existing instanceof IdentitySchemaNode) {
288             if (incoming instanceof GroupingDefinition || incoming instanceof TypeDefinition
289                     || incoming instanceof DataSchemaNode || incoming instanceof NotificationDefinition
290                     || incoming instanceof OperationDefinition) {
291                 return new RenameMappingException(name, existing);
292             }
293         } else if (existing instanceof GroupingDefinition) {
294             if (incoming instanceof IdentitySchemaNode) {
295                 return new RenameMappingException(name, incoming);
296             }
297             if (incoming instanceof TypeDefinition || incoming instanceof DataSchemaNode
298                     || incoming instanceof NotificationDefinition || incoming instanceof OperationDefinition) {
299                 return new RenameMappingException(name, existing);
300             }
301         } else if (existing instanceof TypeDefinition) {
302             if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode) {
303                 return new RenameMappingException(name, incoming);
304             }
305             if (incoming instanceof DataSchemaNode || incoming instanceof NotificationDefinition
306                     || incoming instanceof OperationDefinition) {
307                 return new RenameMappingException(name, existing);
308             }
309         } else {
310             if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode
311                     || incoming instanceof TypeDefinition) {
312                 return new RenameMappingException(name, incoming);
313             }
314         }
315
316         return new IllegalStateException(String.format("Unhandled GeneratedType conflict between %s and %s on %s",
317             incoming, existing, name));
318     }
319 }