2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 com.google.common.collect.HashMultimap;
11 import com.google.common.collect.Multimap;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.IdentityHashMap;
16 import java.util.Map.Entry;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
19 import org.opendaylight.mdsal.binding.model.api.Type;
20 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
21 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder;
23 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
24 import org.opendaylight.mdsal.binding.yang.types.RuntimeTypeProvider;
25 import org.opendaylight.yangtools.concepts.Builder;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
29 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 final class RuntimeTypeGenerator extends AbstractTypeGenerator {
36 RuntimeTypeGenerator(final EffectiveModelContext context, final Map<SchemaNode, JavaTypeName> renames) {
37 super(context, new RuntimeTypeProvider(context, renames), renames);
40 @NonNull BindingRuntimeTypes toTypeMapping() {
41 final Map<Type, AugmentationSchemaNode> augmentationToSchema = new HashMap<>();
42 final Map<Type, WithStatus> typeToSchema = new HashMap<>();
43 final Multimap<Type, Type> choiceToCases = HashMultimap.create();
44 final Map<QName, Type> identities = new HashMap<>();
46 // Note: we are keying through WithStatus, but these nodes compare on semantics, so equivalent schema nodes
47 // can result in two distinct types. We certainly need to keep them separate.
48 final Map<WithStatus, Type> schemaToType = new IdentityHashMap<>();
51 * Fun parts are here. ModuleContext maps have Builders in them, we want plain types. We may encounter each
52 * builder multiple times, hence we keep a builder->instance cache.
54 final Map<Type, Type> builderToType = new IdentityHashMap<>();
56 for (final ModuleContext ctx : moduleContexts()) {
57 for (Entry<Type, AugmentationSchemaNode> e : ctx.getTypeToAugmentation().entrySet()) {
58 augmentationToSchema.put(builtType(builderToType, e.getKey()), e.getValue());
60 for (Entry<Type, WithStatus> e : ctx.getTypeToSchema().entrySet()) {
61 final Type type = builtType(builderToType, e.getKey());
62 typeToSchema.put(type, e.getValue());
63 schemaToType.put(e.getValue(), type);
65 for (Entry<Type, Type> e : ctx.getChoiceToCases().entries()) {
66 choiceToCases.put(builtType(builderToType, e.getKey()), builtType(builderToType, e.getValue()));
68 for (Entry<QName, GeneratedTypeBuilder> e : ctx.getIdentities().entrySet()) {
69 identities.put(e.getKey(), builtType(builderToType, e.getValue()));
73 return new BindingRuntimeTypes(schemaContext(), augmentationToSchema, typeToSchema, schemaToType, choiceToCases,
77 private static Type builtType(final Map<Type, Type> knownTypes, final Type type) {
78 if (type instanceof Builder) {
79 final Type existing = knownTypes.get(type);
80 if (existing != null) {
84 final Type built = (Type) ((Builder<?>)type).build();
85 knownTypes.put(type, built);
92 void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module) {
97 void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module, final SchemaNode node) {
102 void addCodegenInformation(final GeneratedTypeBuilder interfaceBuilder, final Module module, final String string,
103 final Collection<? extends SchemaNode> nodes) {
108 void addComment(final TypeMemberBuilder<?> genType, final DocumentedNode node) {
113 void addRpcMethodComment(final TypeMemberBuilder<?> genType, final RpcDefinition node) {