Simplify leaf access
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / SchemaRootCodecContext.java
index 5eacddfe0cc7c1cf381c7de69eebc5ab64db5b93..e438ed54dd090c16d5a7f52207071f556bbb1ae4 100644 (file)
@@ -8,7 +8,9 @@
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.base.Verify.verify;
+import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.Throwables;
 import com.google.common.base.Verify;
@@ -16,9 +18,14 @@ import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.common.util.concurrent.UncheckedExecutionException;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.util.ClassLoaderUtils;
@@ -27,6 +34,7 @@ import org.opendaylight.yangtools.yang.binding.ChoiceIn;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedListAction;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.binding.RpcInput;
 import org.opendaylight.yangtools.yang.binding.RpcOutput;
@@ -37,101 +45,120 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ContainerLike;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
-import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
-
-final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCodecContext<D,SchemaContext> {
-
-    private final LoadingCache<Class<?>, DataContainerCodecContext<?,?>> childrenByClass = CacheBuilder.newBuilder()
-            .build(new CacheLoader<Class<?>, DataContainerCodecContext<?,?>>() {
-                @Override
-                public DataContainerCodecContext<?,?> load(final Class<?> key) {
-                    return createDataTreeChildContext(key);
-                }
-            });
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 
-    private final LoadingCache<Class<? extends Action<?, ?, ?>>, ActionCodecContext> actionsByClass = CacheBuilder
-            .newBuilder().build(new CacheLoader<Class<? extends Action<?, ?, ?>>, ActionCodecContext>() {
-                @Override
-                public ActionCodecContext load(final Class<? extends Action<?, ?, ?>> key) {
-                    return createActionContext(key);
-                }
-            });
+final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCodecContext<D, EffectiveModelContext> {
 
-    private final LoadingCache<Class<?>, ContainerNodeCodecContext<?>> rpcDataByClass = CacheBuilder.newBuilder().build(
-            new CacheLoader<Class<?>, ContainerNodeCodecContext<?>>() {
-                @Override
-                public ContainerNodeCodecContext<?> load(final Class<?> key) {
-                    return createRpcDataContext(key);
+    private final LoadingCache<Class<? extends DataObject>, DataContainerCodecContext<?, ?>> childrenByClass =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public DataContainerCodecContext<?, ?> load(final Class<? extends DataObject> key) {
+                if (Notification.class.isAssignableFrom(key)) {
+                    checkArgument(key.isInterface(), "Supplied class must be interface.");
+                    final QName qname = BindingReflections.findQName(key);
+                    final NotificationDefinition schema = getSchema().findNotification(qname).orElseThrow(
+                        () -> new IllegalArgumentException("Supplied " + key + " is not valid notification"));
+                    return new NotificationCodecContext<>(key, schema, factory());
                 }
-            });
+                if (RpcInput.class.isAssignableFrom(key) || RpcOutput.class.isAssignableFrom(key)) {
+                    final QName qname = BindingReflections.findQName(key);
+                    final QNameModule qnameModule = qname.getModule();
+                    final Module module = getSchema().findModule(qnameModule).orElseThrow(
+                        () -> new IllegalArgumentException("Failed to find module for " + qnameModule));
+                    final String className = BindingMapping.getClassName(qname);
+
+                    for (final RpcDefinition potential : module.getRpcs()) {
+                        final QName potentialQName = potential.getQName();
+                        /*
+                         * Check if rpc and class represents data from same module and then checks if rpc local name
+                         * produces same class name as class name appended with Input/Output based on QName associated
+                         * with binding class.
+                         *
+                         * FIXME: Rework this to have more precise logic regarding Binding Specification.
+                         */
+                        if (key.getSimpleName().equals(BindingMapping.getClassName(potentialQName) + className)) {
+                            final ContainerLike schema = getRpcDataSchema(potential, qname);
+                            checkArgument(schema != null, "Schema for %s does not define input / output.",
+                                potentialQName);
+                            return DataContainerCodecPrototype.from(key, schema, factory()).get();
+                        }
+                    }
 
-    private final LoadingCache<Class<?>, NotificationCodecContext<?>> notificationsByClass = CacheBuilder.newBuilder()
-            .build(new CacheLoader<Class<?>, NotificationCodecContext<?>>() {
-                @Override
-                public NotificationCodecContext<?> load(final Class<?> key) {
-                    return createNotificationDataContext(key);
+                    throw new IllegalArgumentException("Supplied class " + key + " is not valid RPC class.");
                 }
-            });
+                return createDataTreeChildContext(key);
+            }
+        });
+
+    private final LoadingCache<Class<? extends Action<?, ?, ?>>, ActionCodecContext> actionsByClass =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public ActionCodecContext load(final Class<? extends Action<?, ?, ?>> key) {
+                return createActionContext(key);
+            }
+        });
 
     private final LoadingCache<Class<? extends DataObject>, ChoiceNodeCodecContext<?>> choicesByClass =
-            CacheBuilder.newBuilder().build(new CacheLoader<Class<? extends DataObject>, ChoiceNodeCodecContext<?>>() {
-                @Override
-                public ChoiceNodeCodecContext<?> load(final Class<? extends DataObject> key) {
-                    return createChoiceDataContext(key);
-                }
-            });
-
-    private final LoadingCache<QName, DataContainerCodecContext<?,?>> childrenByQName = CacheBuilder.newBuilder().build(
-            new CacheLoader<QName, DataContainerCodecContext<?,?>>() {
-                @Override
-                public DataContainerCodecContext<?,?> load(final QName qname) {
-                    final DataSchemaNode childSchema = getSchema().getDataChildByName(qname);
-                    childNonNull(childSchema, qname,"Argument %s is not valid child of %s", qname,getSchema());
-                    if (childSchema instanceof DataNodeContainer || childSchema instanceof ChoiceSchemaNode) {
-                        @SuppressWarnings("unchecked")
-                        final Class<? extends DataObject> childCls = (Class<? extends DataObject>)
-                                factory().getRuntimeContext().getClassForSchema(childSchema);
-                        return streamChild(childCls);
-                    }
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public ChoiceNodeCodecContext<?> load(final Class<? extends DataObject> key) {
+                return createChoiceDataContext(key);
+            }
+        });
 
-                    throw new UnsupportedOperationException("Unsupported child type " + childSchema.getClass());
+    private final LoadingCache<QName, DataContainerCodecContext<?,?>> childrenByQName =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public DataContainerCodecContext<?, ?> load(final QName qname) {
+                final DataSchemaNode childSchema = getSchema().dataChildByName(qname);
+                childNonNull(childSchema, qname, "Argument %s is not valid child of %s", qname, getSchema());
+                if (childSchema instanceof DataNodeContainer || childSchema instanceof ChoiceSchemaNode) {
+                    @SuppressWarnings("unchecked")
+                    final Class<? extends DataObject> childCls = (Class<? extends DataObject>)
+                        factory().getRuntimeContext().getClassForSchema(childSchema);
+                    return streamChild(childCls);
                 }
-            });
 
-    private final LoadingCache<SchemaPath, RpcInputCodec<?>> rpcDataByPath = CacheBuilder.newBuilder().build(
-        new CacheLoader<SchemaPath, RpcInputCodec<?>>() {
+                throw new UnsupportedOperationException("Unsupported child type " + childSchema.getClass());
+            }
+        });
+
+    private final LoadingCache<Absolute, RpcInputCodec<?>> rpcDataByPath =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
             @Override
-            public RpcInputCodec<?> load(final SchemaPath key) {
-                final ContainerSchemaNode schema = SchemaContextUtil.getRpcDataSchema(getSchema(), key);
+            public RpcInputCodec<?> load(final Absolute key) {
+                final ContainerLike schema = getRpcDataSchema(getSchema(), key);
                 @SuppressWarnings("unchecked")
                 final Class<? extends DataContainer> cls = (Class<? extends DataContainer>)
-                        factory().getRuntimeContext().getClassForSchema(schema);
+                    factory().getRuntimeContext().getClassForSchema(schema);
                 return getRpc(cls);
             }
         });
 
-    private final LoadingCache<SchemaPath, NotificationCodecContext<?>> notificationsByPath = CacheBuilder.newBuilder()
-            .build(new CacheLoader<SchemaPath, NotificationCodecContext<?>>() {
-                @Override
-                public NotificationCodecContext<?> load(final SchemaPath key) {
-                    final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(getSchema(), key);
-                    @SuppressWarnings("unchecked")
-                    final Class<? extends Notification> clz = (Class<? extends Notification>)
-                            factory().getRuntimeContext().getClassForSchema(schema);
-                    return getNotification(clz);
-                }
-            });
+    private final LoadingCache<Absolute, NotificationCodecContext<?>> notificationsByPath =
+        CacheBuilder.newBuilder().build(new CacheLoader<>() {
+            @Override
+            public NotificationCodecContext<?> load(final Absolute key) {
+                final SchemaTreeEffectiveStatement<?> stmt = getSchema().findSchemaTreeNode(key)
+                    .orElseThrow(() -> new IllegalArgumentException("Cannot find statement at " + key));
+                checkArgument(stmt instanceof NotificationDefinition, "Statement %s is not a notification", stmt);
+
+                @SuppressWarnings("unchecked")
+                final Class<? extends Notification<?>> clz = (Class<? extends Notification<?>>)
+                    factory().getRuntimeContext().getClassForSchema((NotificationDefinition) stmt);
+                return getNotification(clz);
+            }
+        });
 
-    private SchemaRootCodecContext(final DataContainerCodecPrototype<SchemaContext> dataPrototype) {
+    private SchemaRootCodecContext(final DataContainerCodecPrototype<EffectiveModelContext> dataPrototype) {
         super(dataPrototype);
     }
 
@@ -143,23 +170,13 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
      * @return A new root node
      */
     static SchemaRootCodecContext<?> create(final CodecContextFactory factory) {
-        final DataContainerCodecPrototype<SchemaContext> prototype = DataContainerCodecPrototype.rootPrototype(factory);
-        return new SchemaRootCodecContext<>(prototype);
+        return new SchemaRootCodecContext<>(DataContainerCodecPrototype.rootPrototype(factory));
     }
 
-
     @SuppressWarnings("unchecked")
     @Override
     public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
-        /* FIXME: This is still not solved for RPCs
-         * TODO: Probably performance wise RPC, Data and Notification loading cache
-         *       should be merge for performance resons. Needs microbenchmark to
-         *       determine which is faster (keeping them separate or in same cache).
-         */
-        if (Notification.class.isAssignableFrom(childClass)) {
-            return (DataContainerCodecContext<C, ?>) getNotification((Class<? extends Notification>)childClass);
-        }
-        return (DataContainerCodecContext<C, ?>) getOrRethrow(childrenByClass,childClass);
+        return (DataContainerCodecContext<C, ?>) getOrRethrow(childrenByClass, childClass);
     }
 
     @Override
@@ -174,7 +191,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     }
 
     @Override
-    public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
+    public D deserialize(final NormalizedNode normalizedNode) {
         throw new UnsupportedOperationException("Could not create Binding data representation for root");
     }
 
@@ -182,37 +199,52 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
         return getOrRethrow(actionsByClass, action);
     }
 
-    NotificationCodecContext<?> getNotification(final Class<? extends Notification> notification) {
-        return getOrRethrow(notificationsByClass, notification);
+    NotificationCodecContext<?> getNotification(final Class<? extends Notification<?>> notification) {
+        return (NotificationCodecContext<?>) streamChild((Class<? extends DataObject>)notification);
     }
 
-    NotificationCodecContext<?> getNotification(final SchemaPath notification) {
+    NotificationCodecContext<?> getNotification(final Absolute notification) {
         return getOrRethrow(notificationsByPath, notification);
     }
 
     ContainerNodeCodecContext<?> getRpc(final Class<? extends DataContainer> rpcInputOrOutput) {
-        return getOrRethrow(rpcDataByClass, rpcInputOrOutput);
+        return (ContainerNodeCodecContext<?>) streamChild((Class<? extends DataObject>)rpcInputOrOutput);
     }
 
-    RpcInputCodec<?> getRpc(final SchemaPath notification) {
-        return getOrRethrow(rpcDataByPath, notification);
+    RpcInputCodec<?> getRpc(final Absolute containerPath) {
+        return getOrRethrow(rpcDataByPath, containerPath);
     }
 
-    DataContainerCodecContext<?,?> createDataTreeChildContext(final Class<?> key) {
+    DataContainerCodecContext<?, ?> createDataTreeChildContext(final Class<? extends DataObject> key) {
         final QName qname = BindingReflections.findQName(key);
-        final DataSchemaNode childSchema = childNonNull(getSchema().getDataChildByName(qname), key,
+        final DataSchemaNode childSchema = childNonNull(getSchema().dataChildByName(qname), key,
             "%s is not top-level item.", key);
         return DataContainerCodecPrototype.from(key, childSchema, factory()).get();
     }
 
     ActionCodecContext createActionContext(final Class<? extends Action<?, ?, ?>> action) {
-        final Type[] args = ClassLoaderUtils.findParameterizedType(action, Action.class).getActualTypeArguments();
-        checkArgument(args.length == 3, "Unexpected (%s) Action generatic arguments", args.length);
+        if (KeyedListAction.class.isAssignableFrom(action)) {
+            return prepareActionContext(2, 3, 4, action, KeyedListAction.class);
+        } else if (Action.class.isAssignableFrom(action)) {
+            return prepareActionContext(1, 2, 3, action, Action.class);
+        }
+        throw new IllegalArgumentException("The specific action type does not exist for action " + action.getName());
+    }
 
+    private ActionCodecContext prepareActionContext(final int inputOffset, final int outputOffset,
+            final int expectedArgsLength, final Class<? extends Action<?, ?, ?>> action, final Class<?> actionType) {
+        final Optional<ParameterizedType> optParamType = ClassLoaderUtils.findParameterizedType(action, actionType);
+        checkState(optParamType.isPresent(), "%s does not specialize %s", action, actionType);
+
+        final ParameterizedType paramType = optParamType.get();
+        final Type[] args = paramType.getActualTypeArguments();
+        checkArgument(args.length == expectedArgsLength, "Unexpected (%s) Action generatic arguments", args.length);
         final ActionDefinition schema = factory().getRuntimeContext().getActionDefinition(action);
         return new ActionCodecContext(
-            DataContainerCodecPrototype.from(asClass(args[1], RpcInput.class), schema.getInput(), factory()).get(),
-            DataContainerCodecPrototype.from(asClass(args[2], RpcOutput.class), schema.getOutput(), factory()).get());
+                DataContainerCodecPrototype.from(asClass(args[inputOffset], RpcInput.class), schema.getInput(),
+                        factory()).get(),
+                DataContainerCodecPrototype.from(asClass(args[outputOffset], RpcOutput.class), schema.getOutput(),
+                        factory()).get());
     }
 
     private static <T extends DataObject> Class<? extends T> asClass(final Type type, final Class<T> target) {
@@ -220,50 +252,51 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
         return ((Class<?>) type).asSubclass(target);
     }
 
-    ContainerNodeCodecContext<?> createRpcDataContext(final Class<?> key) {
-        checkArgument(DataContainer.class.isAssignableFrom(key));
-        final QName qname = BindingReflections.findQName(key);
-        final QNameModule qnameModule = qname.getModule();
-        final Module module = getSchema().findModule(qnameModule)
-                .orElseThrow(() -> new IllegalArgumentException("Failed to find module for " + qnameModule));
-        final String className = BindingMapping.getClassName(qname);
-
-        RpcDefinition rpc = null;
-        for (final RpcDefinition potential : module.getRpcs()) {
-            final QName potentialQName = potential.getQName();
-            /*
-             * Check if rpc and class represents data from same module and then
-             * checks if rpc local name produces same class name as class name
-             * appended with Input/Output based on QName associated with bidning
-             * class.
-             *
-             * FIXME: Rework this to have more precise logic regarding Binding
-             * Specification.
-             */
-            if (key.getSimpleName().equals(BindingMapping.getClassName(potentialQName) + className)) {
-                rpc = potential;
-                break;
-            }
+    /**
+     * Returns RPC input or output schema based on supplied QName.
+     *
+     * @param rpc RPC Definition
+     * @param qname input or output QName with namespace same as RPC
+     * @return input or output schema. Returns null if RPC does not have input/output specified.
+     */
+    private static @Nullable ContainerLike getRpcDataSchema(final @NonNull RpcDefinition rpc,
+            final @NonNull QName qname) {
+        requireNonNull(rpc, "Rpc Schema must not be null");
+        switch (requireNonNull(qname, "QName must not be null").getLocalName()) {
+            case "input":
+                return rpc.getInput();
+            case "output":
+                return rpc.getOutput();
+            default:
+                throw new IllegalArgumentException("Supplied qname " + qname
+                        + " does not represent rpc input or output.");
         }
-        checkArgument(rpc != null, "Supplied class %s is not valid RPC class.", key);
-        final ContainerSchemaNode schema = SchemaNodeUtils.getRpcDataSchema(rpc, qname);
-        checkArgument(schema != null, "Schema for %s does not define input / output.", rpc.getQName());
-        return (ContainerNodeCodecContext<?>) DataContainerCodecPrototype.from(key, schema, factory()).get();
     }
 
-    NotificationCodecContext<?> createNotificationDataContext(final Class<?> notificationType) {
-        checkArgument(Notification.class.isAssignableFrom(notificationType));
-        checkArgument(notificationType.isInterface(), "Supplied class must be interface.");
-        final QName qname = BindingReflections.findQName(notificationType);
-        /**
-         *  FIXME: After Lithium cleanup of yang-model-api, use direct call on schema context
-         *  to retrieve notification via index.
-         */
-        final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(getSchema(),
-                SchemaPath.create(true, qname));
-        checkArgument(schema != null, "Supplied %s is not valid notification", notificationType);
-
-        return new NotificationCodecContext<>(notificationType, schema, factory());
+    /**
+     * Returns RPC Input or Output Data container from RPC definition.
+     *
+     * @param schema SchemaContext in which lookup should be performed.
+     * @param path Schema path of RPC input/output data container
+     * @return Notification schema or null, if notification is not present in schema context.
+     */
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+        justification = "https://github.com/spotbugs/spotbugs/issues/811")
+    private static @Nullable ContainerLike getRpcDataSchema(final @NonNull EffectiveModelContext schema,
+            final @NonNull Absolute path) {
+        requireNonNull(schema, "Schema context must not be null.");
+        requireNonNull(path, "Schema path must not be null.");
+        final Iterator<QName> it = path.getNodeIdentifiers().iterator();
+        checkArgument(it.hasNext(), "Rpc must have QName.");
+        final QName rpcName = it.next();
+        checkArgument(it.hasNext(), "input or output must be part of path.");
+        final QName inOrOut = it.next();
+        for (final RpcDefinition potential : schema.getOperations()) {
+            if (rpcName.equals(potential.getQName())) {
+                return getRpcDataSchema(potential, inOrOut);
+            }
+        }
+        return null;
     }
 
     ChoiceNodeCodecContext<?> createChoiceDataContext(final Class<? extends DataObject> caseType) {
@@ -279,7 +312,7 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     }
 
     @Override
-    protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
+    protected Object deserializeObject(final NormalizedNode normalizedNode) {
         throw new UnsupportedOperationException("Unable to deserialize root");
     }
 
@@ -298,9 +331,9 @@ final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCo
     @Override
     public DataContainerCodecContext<?, ?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
             final List<PathArgument> builder) {
-        final java.util.Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
+        final Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
         if (caseType.isPresent()) {
-            final Class<? extends DataObject> type = caseType.get();
+            final @NonNull Class<? extends DataObject> type = caseType.orElseThrow();
             final ChoiceNodeCodecContext<?> choice = choicesByClass.getUnchecked(type);
             choice.addYangPathArgument(arg, builder);
             final DataContainerCodecContext<?, ?> caze = choice.streamChild(type);