Remove BindingRuntimeTypes.findRpc{Input,Output}
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / BindingRuntimeTypesFactory.java
index a3b8807c62f8c1d12849108a1fae3659176af4cb..9a88b9a0cc9a083327a2a60b6788a2b9e3d21e90 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.mdsal.binding.generator.impl;
 import static com.google.common.base.Verify.verify;
 
 import com.google.common.base.Stopwatch;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.SetMultimap;
 import java.util.HashMap;
 import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
@@ -18,18 +20,14 @@ import org.opendaylight.mdsal.binding.generator.impl.reactor.Generator;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.GeneratorReactor;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.IdentityGenerator;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.ModuleGenerator;
-import org.opendaylight.mdsal.binding.generator.impl.reactor.RpcGenerator;
-import org.opendaylight.mdsal.binding.generator.impl.reactor.RpcInputGenerator;
-import org.opendaylight.mdsal.binding.generator.impl.reactor.RpcOutputGenerator;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultBindingRuntimeTypes;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeTypes;
+import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
-import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.ModuleRuntimeType;
-import org.opendaylight.mdsal.binding.runtime.api.OutputRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
 import org.opendaylight.yangtools.concepts.Mutable;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -47,10 +45,8 @@ final class BindingRuntimeTypesFactory implements Mutable {
     private final Map<QName, IdentityRuntimeType> identities = new HashMap<>();
     // All known types, indexed by their JavaTypeName
     private final Map<JavaTypeName, RuntimeType> allTypes = new HashMap<>();
-    // All RpcOutputs, indexed by their RPC's QName
-    private final Map<QName, OutputRuntimeType> rpcOutputs = new HashMap<>();
-    // All RpcInputs, indexed by their RPC's QName
-    private final Map<QName, InputRuntimeType> rpcInputs = new HashMap<>();
+    // All known 'choice's to their corresponding cases
+    private final SetMultimap<JavaTypeName, CaseRuntimeType> choiceToCases = HashMultimap.create();
 
     private BindingRuntimeTypesFactory() {
         // Hidden on purpose
@@ -65,7 +61,7 @@ final class BindingRuntimeTypesFactory implements Mutable {
         LOG.debug("Indexed {} generators in {}", moduleGens.size(), sw);
 
         return new DefaultBindingRuntimeTypes(context, factory.modules, factory.allTypes, factory.identities,
-            factory.rpcInputs, factory.rpcOutputs);
+            factory.choiceToCases);
     }
 
     private void indexModules(final Map<QNameModule, ModuleGenerator> moduleGens) {
@@ -77,24 +73,11 @@ final class BindingRuntimeTypesFactory implements Mutable {
 
             // index module's identities and RPC input/outputs
             for (var gen : modGen) {
-                if (gen instanceof IdentityGenerator) {
-                    ((IdentityGenerator) gen).runtimeType().ifPresent(identity -> {
+                if (gen instanceof IdentityGenerator idGen) {
+                    idGen.runtimeType().ifPresent(identity -> {
                         safePut(identities, "identities", identity.statement().argument(), identity);
                     });
                 }
-                // FIXME: do not collect these once we they generate a proper RuntimeType
-                if (gen instanceof RpcGenerator) {
-                    final QName rpcName = ((RpcGenerator) gen).statement().argument();
-                    for (var subgen : gen) {
-                        if (subgen instanceof RpcInputGenerator) {
-                            ((RpcInputGenerator) subgen).runtimeType()
-                                .ifPresent(input -> rpcInputs.put(rpcName, input));
-                        } else if (subgen instanceof RpcOutputGenerator) {
-                            ((RpcOutputGenerator) subgen).runtimeType()
-                                .ifPresent(output -> rpcOutputs.put(rpcName, output));
-                        }
-                    }
-                }
             }
         }
 
@@ -103,14 +86,24 @@ final class BindingRuntimeTypesFactory implements Mutable {
 
     private void indexRuntimeTypes(final Iterable<? extends Generator> generators) {
         for (Generator gen : generators) {
-            if (gen instanceof AbstractExplicitGenerator && gen.generatedType().isPresent()) {
-                final var type = ((AbstractExplicitGenerator<?, ?>) gen).runtimeType().orElseThrow();
-                final var javaType = type.javaType();
-                if (javaType instanceof GeneratedType) {
-                    final var name = javaType.getIdentifier();
+            if (gen instanceof AbstractExplicitGenerator<?, ?> explicitGen && gen.generatedType().isPresent()) {
+                final var type = explicitGen.runtimeType().orElseThrow();
+                if (type.javaType() instanceof GeneratedType genType) {
+                    final var name = genType.getIdentifier();
                     final var prev = allTypes.put(name, type);
                     verify(prev == null || prev == type, "Conflict on runtime type mapping of %s between %s and %s",
                         name, prev, type);
+
+                    // Global indexing of cases generated for a particular choice. We look at the Generated type
+                    // and make assumptions about its shape -- which works just fine without touching the
+                    // ChoiceRuntimeType for cases.
+                    if (type instanceof CaseRuntimeType caseType) {
+                        final var ifaces = genType.getImplements();
+                        // The appropriate choice and DataObject at the very least. The choice interface is the first
+                        // one mentioned.
+                        verify(ifaces.size() >= 2, "Unexpected implemented interfaces %s", ifaces);
+                        choiceToCases.put(ifaces.get(0).getIdentifier(), caseType);
+                    }
                 }
             }
             indexRuntimeTypes(gen);