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