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 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;
48 * Utility class for building up Binding mapping information. This class is NOT thread-safe.
50 public final class ModuleContext implements Mutable {
51 private static final Logger LOG = LoggerFactory.getLogger(ModuleContext.class);
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;
69 private final Map<JavaTypeName, SchemaNode> nameMapping = new HashMap<>();
71 private GeneratedTypeBuilder moduleNode;
72 private String modulePackageName;
74 ModuleContext(final Module module) {
75 this.module = requireNonNull(module);
82 String modulePackageName() {
83 String ret = modulePackageName;
85 modulePackageName = ret = BindingMapping.getRootPackageName(module.getQNameModule());
90 List<Type> getGeneratedTypes() {
91 List<Type> result = new ArrayList<>();
93 if (moduleNode != null) {
94 result.add(moduleNode.build());
97 for (GeneratedTOBuilder b : genTOs) {
98 result.add(b.build());
100 for (Type b : typedefs.values()) {
105 for (GeneratedTypeBuilder b : childNodes.values()) {
106 result.add(b.build());
108 for (GeneratedTypeBuilder b : groupings.values()) {
109 result.add(b.build());
111 for (GeneratedTypeBuilder b : cases.values()) {
112 result.add(b.build());
114 for (GeneratedTypeBuilder b : identities.values()) {
115 result.add(b.build());
117 for (GeneratedTypeBuilder b : topLevelNodes) {
118 result.add(b.build());
120 for (GeneratedTypeBuilder b : augmentations) {
121 result.add(b.build());
126 public Multimap<Type, Type> getChoiceToCases() {
127 return Multimaps.unmodifiableMultimap(choiceToCases);
130 public GeneratedTypeBuilder getModuleNode() {
134 public GeneratedTypeBuilder getChildNode(final SchemaPath path) {
135 return childNodes.get(path);
138 public GeneratedTypeBuilder getGrouping(final SchemaPath path) {
139 return groupings.get(path);
142 public GeneratedTypeBuilder getCase(final SchemaPath path) {
143 return cases.get(path);
146 public void addModuleNode(final GeneratedTypeBuilder newModuleNode) {
147 this.moduleNode = newModuleNode;
150 public void addGeneratedTOBuilder(final GeneratedTOBuilder builder) {
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);
160 public void addGroupingType(final GroupingDefinition def, final GeneratedTypeBuilder builder) {
161 checkNamingConflict(def, builder.getIdentifier());
162 groupings.put(def.getPath(), builder);
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);
173 // This seems to be fine
174 LOG.debug("GeneratedType conflict between {} and {} on {}", def, existingDef, name);
177 typedefs.put(def.getPath(), type);
180 public void addCaseType(final SchemaPath path, final GeneratedTypeBuilder builder) {
181 cases.put(path, builder);
184 public void addIdentityType(final IdentitySchemaNode def, final GeneratedTypeBuilder builder) {
185 checkNamingConflict(def, builder.getIdentifier());
186 identities.put(def.getQName(), builder);
189 public void addTopLevelNodeType(final GeneratedTypeBuilder builder) {
190 topLevelNodes.add(builder);
193 public void addAugmentType(final GeneratedTypeBuilder builder) {
194 augmentations.add(builder);
197 public Map<SchemaPath, Type> getTypedefs() {
201 public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
202 return Collections.unmodifiableMap(childNodes);
205 public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
206 return Collections.unmodifiableMap(groupings);
209 public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
210 return Collections.unmodifiableMap(cases);
213 public Map<QName, GeneratedTypeBuilder> getIdentities() {
214 return Collections.unmodifiableMap(identities);
217 public Set<GeneratedTypeBuilder> getTopLevelNodes() {
218 return Collections.unmodifiableSet(topLevelNodes);
221 public List<GeneratedTypeBuilder> getAugmentations() {
222 return Collections.unmodifiableList(augmentations);
225 public BiMap<Type, AugmentationSchemaNode> getTypeToAugmentation() {
226 return Maps.unmodifiableBiMap(typeToAugmentation);
229 public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchemaNode schema) {
230 typeToAugmentation.put(builder, schema);
231 typeToSchema.put(builder, schema);
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);
240 public BiMap<Type, CaseSchemaNode> getCaseTypeToSchemas() {
241 return Maps.unmodifiableBiMap(caseTypeToSchema);
245 * Returns mapping of type to its schema. Valid values are only instances of {@link DataSchemaNode}
246 * or {@link AugmentationSchemaNode}.
248 * @return Mapping from type to corresponding schema
250 public Map<Type, WithStatus> getTypeToSchema() {
251 return Collections.unmodifiableMap(typeToSchema);
254 protected void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
255 typeToSchema.put(type, typedef);
259 * Adds mapping between schema path and an inner type.
261 void addInnerTypedefType(final SchemaPath path, final Type type) {
262 innerTypes.put(path, type);
265 public Type getInnerType(final SchemaPath path) {
266 return innerTypes.get(path);
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());
278 throw resolveNamingConfict(existingDef, def, name);
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);
291 } else if (existing instanceof GroupingDefinition) {
292 if (incoming instanceof IdentitySchemaNode) {
293 return new RenameMappingException(name, incoming);
295 if (incoming instanceof TypeDefinition || incoming instanceof DataSchemaNode
296 || incoming instanceof NotificationDefinition || incoming instanceof OperationDefinition) {
297 return new RenameMappingException(name, existing);
299 } else if (existing instanceof TypeDefinition) {
300 if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode) {
301 return new RenameMappingException(name, incoming);
303 if (incoming instanceof DataSchemaNode || incoming instanceof NotificationDefinition
304 || incoming instanceof OperationDefinition) {
305 return new RenameMappingException(name, existing);
308 if (incoming instanceof GroupingDefinition || incoming instanceof IdentitySchemaNode
309 || incoming instanceof TypeDefinition) {
310 return new RenameMappingException(name, incoming);
314 return new IllegalStateException(String.format("Unhandled GeneratedType conflict between %s and %s on %s",
315 incoming, existing, name));