2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl;
10 import static java.util.Objects.requireNonNull;
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;
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;
48 public final class ModuleContext {
49 private static final Logger LOG = LoggerFactory.getLogger(ModuleContext.class);
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;
67 private final Map<JavaTypeName, SchemaNode> nameMapping = new HashMap<>();
69 private GeneratedTypeBuilder moduleNode;
70 private String modulePackageName;
72 ModuleContext(final Module module) {
73 this.module = requireNonNull(module);
80 String modulePackageName() {
81 String ret = modulePackageName;
83 modulePackageName = ret = BindingMapping.getRootPackageName(module.getQNameModule());
88 List<Type> getGeneratedTypes() {
89 List<Type> result = new ArrayList<>();
91 if (moduleNode != null) {
92 result.add(moduleNode.build());
95 for (GeneratedTOBuilder b : genTOs) {
96 result.add(b.build());
98 for (Type b : typedefs.values()) {
103 for (GeneratedTypeBuilder b : childNodes.values()) {
104 result.add(b.build());
106 for (GeneratedTypeBuilder b : groupings.values()) {
107 result.add(b.build());
109 for (GeneratedTypeBuilder b : cases.values()) {
110 result.add(b.build());
112 for (GeneratedTypeBuilder b : identities.values()) {
113 result.add(b.build());
115 for (GeneratedTypeBuilder b : topLevelNodes) {
116 result.add(b.build());
118 for (GeneratedTypeBuilder b : augmentations) {
119 result.add(b.build());
124 public Multimap<Type, Type> getChoiceToCases() {
125 return Multimaps.unmodifiableMultimap(choiceToCases);
128 public GeneratedTypeBuilder getModuleNode() {
132 public GeneratedTypeBuilder getChildNode(final SchemaPath path) {
133 return childNodes.get(path);
136 public GeneratedTypeBuilder getGrouping(final SchemaPath path) {
137 return groupings.get(path);
140 public GeneratedTypeBuilder getCase(final SchemaPath path) {
141 return cases.get(path);
144 public void addModuleNode(final GeneratedTypeBuilder newModuleNode) {
145 this.moduleNode = newModuleNode;
148 public void addGeneratedTOBuilder(final GeneratedTOBuilder builder) {
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);
158 public void addGroupingType(final GroupingDefinition def, final GeneratedTypeBuilder builder) {
159 checkNamingConflict(def, builder.getIdentifier());
160 groupings.put(def.getPath(), builder);
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);
171 // This seems to be fine
172 LOG.debug("GeneratedType conflict between {} and {} on {}", def, existingDef, name);
175 typedefs.put(def.getPath(), type);
178 public void addCaseType(final SchemaPath path, final GeneratedTypeBuilder builder) {
179 cases.put(path, builder);
182 public void addIdentityType(final IdentitySchemaNode def, final GeneratedTypeBuilder builder) {
183 checkNamingConflict(def, builder.getIdentifier());
184 identities.put(def.getQName(), builder);
187 public void addTopLevelNodeType(final GeneratedTypeBuilder builder) {
188 topLevelNodes.add(builder);
191 public void addAugmentType(final GeneratedTypeBuilder builder) {
192 augmentations.add(builder);
195 public Map<SchemaPath, Type> getTypedefs() {
199 public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
200 return Collections.unmodifiableMap(childNodes);
203 public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
204 return Collections.unmodifiableMap(groupings);
207 public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
208 return Collections.unmodifiableMap(cases);
211 public Map<QName, GeneratedTypeBuilder> getIdentities() {
212 return Collections.unmodifiableMap(identities);
215 public Set<GeneratedTypeBuilder> getTopLevelNodes() {
216 return Collections.unmodifiableSet(topLevelNodes);
219 public List<GeneratedTypeBuilder> getAugmentations() {
220 return Collections.unmodifiableList(augmentations);
223 public BiMap<Type, AugmentationSchemaNode> getTypeToAugmentation() {
224 return Maps.unmodifiableBiMap(typeToAugmentation);
227 public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchemaNode schema) {
228 typeToAugmentation.put(builder, schema);
229 typeToSchema.put(builder, schema);
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);
238 public BiMap<Type, CaseSchemaNode> getCaseTypeToSchemas() {
239 return Maps.unmodifiableBiMap(caseTypeToSchema);
243 * Returns mapping of type to its schema. Valid values are only instances of {@link DataSchemaNode}
244 * or {@link AugmentationSchemaNode}.
246 * @return Mapping from type to corresponding schema
248 public Map<Type, WithStatus> getTypeToSchema() {
249 return Collections.unmodifiableMap(typeToSchema);
252 protected void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
253 typeToSchema.put(type, typedef);
257 * Adds mapping between schema path and an inner type.
259 void addInnerTypedefType(final SchemaPath path, final Type type) {
260 innerTypes.put(path, type);
263 public Type getInnerType(final SchemaPath path) {
264 return innerTypes.get(path);
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());
276 throw resolveNamingConfict(existingDef, def, name);
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);
289 } else if (existing instanceof GroupingDefinition) {
290 if (incoming instanceof IdentitySchemaNode) {
291 return new RenameMappingException(name, incoming);
293 if (incoming instanceof TypeDefinition || incoming instanceof DataSchemaNode
294 || incoming instanceof NotificationDefinition || incoming instanceof OperationDefinition) {
295 return new RenameMappingException(name, existing);
297 } else if (existing instanceof TypeDefinition) {
298 if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode) {
299 return new RenameMappingException(name, incoming);
301 if (incoming instanceof DataSchemaNode || incoming instanceof NotificationDefinition
302 || incoming instanceof OperationDefinition) {
303 return new RenameMappingException(name, existing);
306 if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode
307 || incoming instanceof TypeDefinition) {
308 return new RenameMappingException(name, incoming);
312 return new IllegalStateException(String.format("Unhandled GeneratedType conflict between %s and %s on %s",
313 incoming, existing, name));