Deprecate Clustered(DOM)DataTreeChangeListener
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / CurrentAdapterSerializer.java
index 14b2fc278f23e83c0aac7f42399b6392afff2697..432a4a4aafe9f01123852a6fc2b95290c0d8d205 100644 (file)
@@ -7,37 +7,47 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter;
 
-import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.VerifyException;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
-import com.google.common.collect.ImmutableBiMap;
-import java.lang.reflect.Method;
 import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.api.ActionSpec;
 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.InstanceNotificationSpec;
 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
 import org.opendaylight.mdsal.binding.dom.codec.spi.ForwardingBindingDOMCodecServices;
-import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
-import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
+import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
-import org.opendaylight.yangtools.yang.binding.Action;
+import org.opendaylight.yangtools.yang.binding.BindingContract;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -54,6 +64,7 @@ public final class CurrentAdapterSerializer extends ForwardingBindingDOMCodecSer
                 }
             });
 
+    private final ConcurrentMap<JavaTypeName, ContextReferenceExtractor> extractors = new ConcurrentHashMap<>();
     private final @NonNull BindingDOMCodecServices delegate;
 
     public CurrentAdapterSerializer(final BindingDOMCodecServices delegate) {
@@ -74,67 +85,94 @@ public final class CurrentAdapterSerializer extends ForwardingBindingDOMCodecSer
     }
 
     DOMDataTreeIdentifier toDOMDataTreeIdentifier(final DataTreeIdentifier<?> path) {
-        return new DOMDataTreeIdentifier(path.getDatastoreType(), toYangInstanceIdentifier(path.getRootIdentifier()));
+        return DOMDataTreeIdentifier.of(path.datastore(), toYangInstanceIdentifier(path.path()));
     }
 
     Collection<DOMDataTreeIdentifier> toDOMDataTreeIdentifiers(final Collection<DataTreeIdentifier<?>> subtrees) {
         return subtrees.stream().map(this::toDOMDataTreeIdentifier).collect(Collectors.toSet());
     }
 
-    @NonNull Absolute getActionPath(final @NonNull Class<? extends Action<?, ?, ?>> type) {
-        final Absolute identifier = getRuntimeContext().getActionIdentifier(type);
-        checkArgument(identifier != null, "Failed to find schema for %s", type);
-        return identifier;
+    @NonNull Absolute getActionPath(final @NonNull ActionSpec<?, ?> spec) {
+        return getSchemaNodeIdentifier(spec.path(), spec.type(), ActionRuntimeType.class,
+            ActionEffectiveStatement.class);
     }
 
-    // FIXME: This should be probably part of Binding Runtime context
-    ImmutableBiMap<Method, RpcDefinition> getRpcMethodToSchema(final Class<? extends RpcService> key) {
-        final Module module = getModule(key);
-        final ImmutableBiMap.Builder<Method, RpcDefinition> ret = ImmutableBiMap.builder();
+    @NonNull Absolute getNotificationPath(final @NonNull InstanceNotificationSpec<?, ?> spec) {
+        return getSchemaNodeIdentifier(spec.path(), spec.type(), NotificationRuntimeType.class,
+            NotificationEffectiveStatement.class);
+    }
+
+    private <T extends RuntimeType> @NonNull Absolute getSchemaNodeIdentifier(final @NonNull InstanceIdentifier<?> path,
+            final @NonNull Class<? extends BindingContract<?>> type, final @NonNull Class<T> expectedRuntime,
+            final @NonNull Class<? extends SchemaTreeEffectiveStatement<?>> expectedStatement) {
+        final var typeName = JavaTypeName.create(type);
+        final var runtimeType = getRuntimeContext().getTypes().findSchema(typeName)
+            .orElseThrow(() -> new IllegalArgumentException(typeName + " is not known"));
+        final T casted;
         try {
-            for (final RpcDefinition rpcDef : module.getRpcs()) {
-                final Method method = findRpcMethod(key, rpcDef);
-                ret.put(method, rpcDef);
-            }
-        } catch (final NoSuchMethodException e) {
-            throw new IllegalStateException("Rpc defined in model does not have representation in generated class.", e);
+            casted = expectedRuntime.cast(runtimeType);
+        } catch (ClassCastException e) {
+            throw new IllegalArgumentException(typeName + " resolved to unexpected " + runtimeType, e);
         }
-        return ret.build();
+        final var qname = expectedStatement.cast(casted.statement()).argument();
+
+        final var entry = resolvePath(path);
+        final var stack = entry.getKey();
+        final var stmt = stack.enterSchemaTree(qname.bindTo(entry.getValue()));
+        if (expectedStatement.isInstance(stmt)) {
+            return stack.toSchemaNodeIdentifier();
+        }
+        throw new VerifyException(path + " child " + typeName + " resolved to unexpected statement" + stmt);
     }
 
-    // FIXME: This should be probably part of Binding Runtime context
-    ImmutableBiMap<Method, QName> getRpcMethodToSchemaPath(final Class<? extends RpcService> key) {
-        final Module module = getModule(key);
-        final ImmutableBiMap.Builder<Method, QName> ret = ImmutableBiMap.builder();
+    @Nullable ContextReferenceExtractor findExtractor(final @NonNull InputRuntimeType inputType) {
+        final var inputName = inputType.getIdentifier();
+        final var cached = extractors.get(inputName);
+        if (cached != null) {
+            return cached;
+        }
+
+        // Load the class
+        final Class<?> inputClass;
         try {
-            for (final RpcDefinition rpcDef : module.getRpcs()) {
-                final Method method = findRpcMethod(key, rpcDef);
-                ret.put(method,rpcDef.getQName());
-            }
-        } catch (final NoSuchMethodException e) {
-            throw new IllegalStateException("Rpc defined in model does not have representation in generated class.", e);
+            inputClass = getRuntimeContext().loadClass(inputName);
+        } catch (ClassNotFoundException e) {
+            throw new IllegalArgumentException("Failed to load class for " + inputType, e);
         }
-        return ret.build();
-    }
 
-    private Module getModule(final Class<?> modeledClass) {
-        final QNameModule moduleName = BindingReflections.getQNameModule(modeledClass);
-        final BindingRuntimeContext localRuntimeContext = getRuntimeContext();
-        final Module module = localRuntimeContext.getEffectiveModelContext().findModule(moduleName).orElse(null);
-        if (module != null) {
-            return module;
+        // Check if there is an extractor at all
+        final var created = ContextReferenceExtractor.of(inputClass);
+        if (created == null) {
+            return null;
         }
 
-        LOG.trace("Schema for {} is not available; expected module name: {}; BindingRuntimeContext: {}",
-                modeledClass, moduleName, localRuntimeContext);
-        throw new IllegalStateException(String.format("Schema for %s is not available; expected module name: %s; "
-                + "full BindingRuntimeContext available in trace log", modeledClass, moduleName));
+        // Reconcile with cache
+        final var raced = extractors.putIfAbsent(inputName, created);
+        return raced != null ? raced : created;
     }
 
-    private Method findRpcMethod(final Class<? extends RpcService> key, final RpcDefinition rpcDef)
-            throws NoSuchMethodException {
-        final String methodName = BindingMapping.getRpcMethodName(rpcDef.getQName());
-        final Class<?> inputClz = getRuntimeContext().getClassForSchema(rpcDef.getInput());
-        return key.getMethod(methodName, inputClz);
+    private @NonNull Entry<SchemaInferenceStack, QNameModule> resolvePath(final @NonNull InstanceIdentifier<?> path) {
+        final var stack = SchemaInferenceStack.of(getRuntimeContext().modelContext());
+        final var it = toYangInstanceIdentifier(path).getPathArguments().iterator();
+        verify(it.hasNext(), "Unexpected empty instance identifier for %s", path);
+
+        QNameModule lastNamespace;
+        do {
+            final var arg = it.next();
+            final var qname = arg.getNodeType();
+            final var stmt = stack.enterDataTree(qname);
+            lastNamespace = qname.getModule();
+            if (stmt instanceof ListEffectiveStatement) {
+                // Lists have two steps
+                verify(it.hasNext(), "Unexpected list termination at %s in %s", stmt, path);
+                // Verify just to make sure we are doing the right thing
+                final var skipped = it.next();
+                verify(skipped instanceof NodeIdentifier, "Unexpected skipped list entry item %s in %s", skipped, path);
+                verify(stmt.argument().equals(skipped.getNodeType()), "Mismatched list entry item %s in %s", skipped,
+                    path);
+            }
+        } while (it.hasNext());
+
+        return Map.entry(stack, lastNamespace);
     }
 }