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.BiMap;
11 import com.google.common.collect.HashBiMap;
12 import com.google.common.collect.HashMultimap;
13 import com.google.common.collect.Multimap;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.IdentityHashMap;
18 import java.util.Map.Entry;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.TypeMemberBuilder;
25 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
26 import org.opendaylight.mdsal.binding.yang.types.RuntimeTypeProvider;
27 import org.opendaylight.yangtools.concepts.Builder;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
31 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 final class RuntimeTypeGenerator extends AbstractTypeGenerator {
38 RuntimeTypeGenerator(final EffectiveModelContext context, final Map<SchemaNode, JavaTypeName> renames) {
39 super(context, new RuntimeTypeProvider(context, renames), renames);
42 @NonNull BindingRuntimeTypes toTypeMapping() {
43 final Map<Type, AugmentationSchemaNode> augmentationToSchema = new HashMap<>();
44 final BiMap<Type, WithStatus> typeToDefiningSchema = HashBiMap.create();
45 final Multimap<Type, Type> choiceToCases = HashMultimap.create();
46 final Map<QName, Type> identities = new HashMap<>();
49 * Fun parts are here. ModuleContext maps have Builders in them, we want plan types. We may encounter each
50 * builder multiple times, hence we keep a builder->instance cache.
52 final Map<Type, Type> builderToType = new IdentityHashMap<>();
53 for (final ModuleContext ctx : moduleContexts()) {
54 for (Entry<Type, AugmentationSchemaNode> e : ctx.getTypeToAugmentation().entrySet()) {
55 augmentationToSchema.put(builtType(builderToType, e.getKey()), e.getValue());
57 for (Entry<Type, WithStatus> e : ctx.getTypeToSchema().entrySet()) {
58 typeToDefiningSchema.put(builtType(builderToType, e.getKey()), e.getValue());
60 for (Entry<Type, Type> e : ctx.getChoiceToCases().entries()) {
61 choiceToCases.put(builtType(builderToType, e.getKey()), builtType(builderToType, e.getValue()));
63 for (Entry<QName, GeneratedTypeBuilder> e : ctx.getIdentities().entrySet()) {
64 identities.put(e.getKey(), builtType(builderToType, e.getValue()));
68 return new BindingRuntimeTypes(schemaContext(), augmentationToSchema, typeToDefiningSchema, choiceToCases,
72 private static Type builtType(final Map<Type, Type> knownTypes, final Type type) {
73 if (type instanceof Builder) {
74 final Type existing = knownTypes.get(type);
75 if (existing != null) {
79 final Type built = (Type) ((Builder<?>)type).build();
80 knownTypes.put(type, built);
87 void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module) {
92 void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module, final SchemaNode node) {
97 void addCodegenInformation(final GeneratedTypeBuilder interfaceBuilder, final Module module, final String string,
98 final Collection<? extends SchemaNode> nodes) {
103 void addComment(final TypeMemberBuilder<?> genType, final DocumentedNode node) {
108 void addRpcMethodComment(final TypeMemberBuilder<?> genType, final RpcDefinition node) {