Rework AugmentRuntimeType and Choice/Case linkage
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / BindingRuntimeTypesFactory.java
index a3b8807c62f8c1d12849108a1fae3659176af4cb..29df37b4041173fc2b01ff806bb55266e922880a 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;
@@ -26,6 +28,7 @@ import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultBindingRuntimeTyp
 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;
@@ -51,6 +54,8 @@ final class BindingRuntimeTypesFactory implements Mutable {
     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 +70,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.rpcInputs, factory.rpcOutputs, factory.choiceToCases);
     }
 
     private void indexModules(final Map<QNameModule, ModuleGenerator> moduleGens) {
@@ -111,6 +116,17 @@ final class BindingRuntimeTypesFactory implements Mutable {
                     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) {
+                        final var ifaces = ((GeneratedType) javaType).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(), (CaseRuntimeType) type);
+                    }
                 }
             }
             indexRuntimeTypes(gen);