Improve RpcRoutingStrategyTest
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / RuntimeTypeGenerator.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
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;
15 import java.util.Map;
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;
34
35 final class RuntimeTypeGenerator extends AbstractTypeGenerator {
36     RuntimeTypeGenerator(final EffectiveModelContext context, final Map<SchemaNode, JavaTypeName> renames) {
37         super(context, new RuntimeTypeProvider(context, renames), renames);
38     }
39
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<>();
45
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<>();
49
50         /*
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.
53          */
54         final Map<Type, Type> builderToType = new IdentityHashMap<>();
55
56         for (final ModuleContext ctx : moduleContexts()) {
57             for (Entry<Type, AugmentationSchemaNode> e : ctx.getTypeToAugmentation().entrySet()) {
58                 augmentationToSchema.put(builtType(builderToType, e.getKey()), e.getValue());
59             }
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);
64             }
65             for (Entry<Type, Type> e : ctx.getChoiceToCases().entries()) {
66                 choiceToCases.put(builtType(builderToType, e.getKey()), builtType(builderToType, e.getValue()));
67             }
68             for (Entry<QName, GeneratedTypeBuilder> e : ctx.getIdentities().entrySet()) {
69                 identities.put(e.getKey(), builtType(builderToType, e.getValue()));
70             }
71         }
72
73         return new BindingRuntimeTypes(schemaContext(), augmentationToSchema, typeToSchema, schemaToType, choiceToCases,
74             identities);
75     }
76
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) {
81                 return existing;
82             }
83
84             final Type built = (Type) ((Builder<?>)type).build();
85             knownTypes.put(type, built);
86             return built;
87         }
88         return type;
89     }
90
91     @Override
92     void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module) {
93         // No-op
94     }
95
96     @Override
97     void addCodegenInformation(final GeneratedTypeBuilderBase<?> genType, final Module module, final SchemaNode node) {
98         // No-op
99     }
100
101     @Override
102     void addCodegenInformation(final GeneratedTypeBuilder interfaceBuilder, final Module module, final String string,
103             final Collection<? extends SchemaNode> nodes) {
104         // No-op
105     }
106
107     @Override
108     void addComment(final TypeMemberBuilder<?> genType, final DocumentedNode node) {
109         // No-op
110     }
111
112     @Override
113     void addRpcMethodComment(final TypeMemberBuilder<?> genType, final RpcDefinition node) {
114         // No-op
115     }
116 }