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.SchemaNode;
36 final class RuntimeTypeGenerator extends AbstractTypeGenerator {
37 RuntimeTypeGenerator(final EffectiveModelContext context, final Map<SchemaNode, JavaTypeName> renames) {
38 super(context, new RuntimeTypeProvider(context, renames), renames);
41 @NonNull BindingRuntimeTypes toTypeMapping() {
42 final Map<Type, AugmentationSchemaNode> augmentationToSchema = new HashMap<>();
43 final BiMap<Type, WithStatus> typeToDefiningSchema = HashBiMap.create();
44 final Multimap<Type, Type> choiceToCases = HashMultimap.create();
45 final Map<QName, Type> identities = new HashMap<>();
48 * Fun parts are here. ModuleContext maps have Builders in them, we want plan types. We may encounter each
49 * builder multiple times, hence we keep a builder->instance cache.
51 final Map<Type, Type> builderToType = new IdentityHashMap<>();
52 for (final ModuleContext ctx : moduleContexts()) {
53 for (Entry<Type, AugmentationSchemaNode> e : ctx.getTypeToAugmentation().entrySet()) {
54 augmentationToSchema.put(builtType(builderToType, e.getKey()), e.getValue());
56 for (Entry<Type, WithStatus> e : ctx.getTypeToSchema().entrySet()) {
57 typeToDefiningSchema.put(builtType(builderToType, e.getKey()), e.getValue());
59 for (Entry<Type, Type> e : ctx.getChoiceToCases().entries()) {
60 choiceToCases.put(builtType(builderToType, e.getKey()), builtType(builderToType, e.getValue()));
62 for (Entry<QName, GeneratedTypeBuilder> e : ctx.getIdentities().entrySet()) {
63 identities.put(e.getKey(), builtType(builderToType, e.getValue()));
67 return new BindingRuntimeTypes(schemaContext(), augmentationToSchema, typeToDefiningSchema, choiceToCases,
71 private static Type builtType(final Map<Type, Type> knownTypes, final Type type) {
72 if (type instanceof Builder) {
73 final Type existing = knownTypes.get(type);
74 if (existing != null) {
78 final Type built = (Type) ((Builder<?>)type).build();
79 knownTypes.put(type, built);
86 void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module) {
91 void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module, final SchemaNode node) {
96 void addCodegenInformation(final GeneratedTypeBuilder interfaceBuilder, final Module module, final String string,
97 final Collection<? extends SchemaNode> nodes) {
102 void addComment(final TypeMemberBuilder<?> genType, final DocumentedNode node) {