Fix actions in keyed lists 55/74555/6
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Jul 2018 01:10:33 +0000 (03:10 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Jul 2018 09:21:42 +0000 (11:21 +0200)
When we encounter a keyed list we end up generating an action for
the key, not for the list, which leads to a compilation error.

Change-Id: Iad9a5b449778704a3209d43011060f4a7942b9a6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-api/src/main/java/org/opendaylight/mdsal/binding/api/ActionProviderService.java
binding/mdsal-binding-api/src/main/java/org/opendaylight/mdsal/binding/api/ActionService.java
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/ActionServiceAdapter.java
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/generator/impl/AbstractTypeGenerator.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/ActionsTest.java
binding/mdsal-binding-generator-impl/src/test/resources/actions.yang
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/BindingTypes.java
binding/mdsal-binding-test-model/src/main/yang/actions.yang
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Action.java
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedListAction.java [new file with mode: 0644]

index 6de0801deff8fdb0f3d655ecdacb67add9726db7..3f4e737fd133cbe636270eabe0f8cddb59668564 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
 import org.opendaylight.yangtools.yang.binding.Action;
 import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 /**
  * Registration interface used by {@code action} implementations. Each action is defined in a YANG model,
@@ -40,17 +41,18 @@ public interface ActionProviderService extends BindingService {
      * @throws IllegalArgumentException if any of the {@code validNodes} does not match {@code datastore}
      * @throws UnsupportedOperationException if this service cannot handle requested datastore
      */
-    <O extends DataObject, T extends Action<O, ?, ?>, S extends T> ObjectRegistration<S> registerImplementation(
-            Class<T> actionInterface, S implementation, LogicalDatastoreType datastore,
-            Set<DataTreeIdentifier<O>> validNodes);
+    <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>, S extends T>
+        ObjectRegistration<S> registerImplementation(Class<T> actionInterface, S implementation,
+                LogicalDatastoreType datastore, Set<DataTreeIdentifier<O>> validNodes);
 
-    default <O extends DataObject, T extends Action<O, ?, ?>, S extends T> ObjectRegistration<S> registerImplementation(
-            final Class<T> actionInterface, final S implementation, final LogicalDatastoreType datastore) {
+    default <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>, S extends T>
+        ObjectRegistration<S> registerImplementation(final Class<T> actionInterface, final S implementation,
+                final LogicalDatastoreType datastore) {
         return registerImplementation(actionInterface, implementation, datastore, ImmutableSet.of());
     }
 
-    default <O extends DataObject, T extends Action<O, ?, ?>, S extends T> ObjectRegistration<S> registerImplementation(
-            final Class<T> actionInterface, final S implementation) {
+    default <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>, S extends T>
+        ObjectRegistration<S> registerImplementation(final Class<T> actionInterface, final S implementation) {
         return registerImplementation(actionInterface, implementation, LogicalDatastoreType.OPERATIONAL);
     }
 }
index 7b272ba101f09f6f2763757bdcf0f7f5fb9c614a..6642c34e6e7de0e6575cc1cdc1154a9d29ab541e 100644 (file)
@@ -56,22 +56,23 @@ public interface ActionService extends BindingService {
     <O extends DataObject, T extends Action<?, ?, ?>> T getActionHandle(Class<T> actionInterface,
             Set<DataTreeIdentifier<O>> validNodes);
 
-    default <O extends DataObject, T extends Action<O, ?, ?>> T getActionHandle(final Class<T> actionInterface) {
+    default <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>> T getActionHandle(
+            final Class<T> actionInterface) {
         return getActionHandle(actionInterface, ImmutableSet.of());
     }
 
-    default <O extends DataObject, T extends Action<O, ?, ?>> T getActionHandle(final Class<T> actionInterface,
-            final LogicalDatastoreType dataStore, final InstanceIdentifier<O> path) {
+    default <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>> T getActionHandle(
+            final Class<T> actionInterface, final LogicalDatastoreType dataStore, final P path) {
         return getActionHandle(actionInterface, ImmutableSet.of(DataTreeIdentifier.create(dataStore, path)));
     }
 
-    default <O extends DataObject, T extends Action<O, ?, ?>> T getActionHandle(final Class<T> actionInterface,
-            final InstanceIdentifier<O> path) {
+    default <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>> T getActionHandle(
+            final Class<T> actionInterface, final P path) {
         return getActionHandle(actionInterface, LogicalDatastoreType.OPERATIONAL, path);
     }
 
-    default <O extends DataObject, T extends Action<O, ?, ?>> T getActionHandle(final Class<T> actionInterface,
-            @SuppressWarnings("unchecked") final DataTreeIdentifier<O>... nodes) {
+    default <O extends DataObject, P extends InstanceIdentifier<O>, T extends Action<P, ?, ?>> T getActionHandle(
+            final Class<T> actionInterface, @SuppressWarnings("unchecked") final DataTreeIdentifier<O>... nodes) {
         return getActionHandle(actionInterface, ImmutableSet.copyOf(nodes));
     }
 }
index 04c74e630e620fc408025ae433c7dd207b212350..513db38d2baa6f7d8eb8d8fded5d5e437984df45 100644 (file)
@@ -35,8 +35,8 @@ final class ActionServiceAdapter
         extends AbstractBindingLoadingAdapter<DOMOperationService, Class<? extends Action<?, ?, ?>>, ActionAdapter>
         implements ActionService {
     private static final class ConstrainedAction implements Delegator<Action<?, ?, ?>>,
-            Action<DataObject, RpcInput, RpcOutput> {
-        private final Action<DataObject, RpcInput, RpcOutput> delegate;
+            Action<InstanceIdentifier<?>, RpcInput, RpcOutput> {
+        private final Action<InstanceIdentifier<?>, RpcInput, RpcOutput> delegate;
         private final Set<? extends DataTreeIdentifier<?>> nodes;
 
         ConstrainedAction(final Action<?, ?, ?> delegate, final Set<? extends DataTreeIdentifier<?>> nodes) {
@@ -45,8 +45,7 @@ final class ActionServiceAdapter
         }
 
         @Override
-        public FluentFuture<RpcResult<RpcOutput>> invoke(final InstanceIdentifier<DataObject> path,
-                final RpcInput input) {
+        public FluentFuture<RpcResult<RpcOutput>> invoke(final InstanceIdentifier<?> path, final RpcInput input) {
             checkState(nodes.contains(path), "Cannot service %s", path);
             return delegate.invoke(path, input);
         }
index 907eba6d20938d1346b0c21f505a6f3f73e64033..a70b7cf95951ae83ac301c6ae8c7fcf234f14a13 100644 (file)
@@ -29,6 +29,7 @@ import static org.opendaylight.mdsal.binding.model.util.BindingTypes.childOf;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.choiceIn;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.identifiable;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.identifier;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.keyedListAction;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.rpcResult;
 import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
 import static org.opendaylight.mdsal.binding.model.util.Types.listTypeFor;
@@ -285,7 +286,7 @@ abstract class AbstractTypeGenerator {
         if (genType != null) {
             constructGetter(parent, genType, node);
             resolveDataSchemaNodes(context, genType, genType, node.getChildNodes());
-            actionsToGenType(context, genType, node);
+            actionsToGenType(context, genType, node, null);
         }
     }
 
@@ -303,8 +304,8 @@ abstract class AbstractTypeGenerator {
                 genTOBuilder.addImplementsType(identifierMarker);
                 genType.addImplementsType(identifiableMarker);
 
-                actionsToGenType(context, genTOBuilder, node);
             }
+            actionsToGenType(context, genType, node, genTOBuilder);
 
             for (final DataSchemaNode schemaNode : node.getChildNodes()) {
                 if (!schemaNode.isAugmenting()) {
@@ -404,7 +405,7 @@ abstract class AbstractTypeGenerator {
     }
 
     private <T extends DataNodeContainer & ActionNodeContainer> void actionsToGenType(final ModuleContext context,
-            final Type parent, final T parentSchema) {
+            final Type parent, final T parentSchema, final Type keyType) {
         for (final ActionDefinition action : parentSchema.getActions()) {
             final GeneratedType input;
             final GeneratedType output;
@@ -429,7 +430,8 @@ abstract class AbstractTypeGenerator {
                     BindingMapping.MODULE_INFO_CLASS_NAME), qname.getLocalName());
 
                 annotateDeprecatedIfNecessary(action.getStatus(), builder);
-                builder.addImplementsType(action(parent, input, output));
+                builder.addImplementsType(keyType != null ? keyedListAction(parent, keyType, input, output)
+                        : action(parent, input, output));
 
                 addCodegenInformation(builder, context.module(), action);
                 context.addChildNodeType(action, builder);
@@ -669,7 +671,7 @@ abstract class AbstractTypeGenerator {
             resolveDataSchemaNodes(context, genType, genType, grouping.getChildNodes());
             groupingsToGenTypes(context, grouping.getGroupings());
             processUsesAugments(grouping, context);
-            actionsToGenType(context, genType, grouping);
+            actionsToGenType(context, genType, grouping, null);
         }
     }
 
index fd1ffda7b641a0cc6a37b8cdf19ad0ebd287b5e8..3dfd3cfe6edfd21fe623704c55e8f49ce63befd4 100644 (file)
@@ -23,6 +23,6 @@ public class ActionsTest {
 
         List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
         assertNotNull(generateTypes);
-        assertEquals(13, generateTypes.size());
+        assertEquals(21, generateTypes.size());
     }
 }
index 4ea4a546dc7f0c6396350bba9cc4821819b1cece..171b8cb5ed41a1b61a2faf99a32bc7ff389d5404 100644 (file)
@@ -5,7 +5,17 @@ module actions {
 
     container cont {
         action foo {
-        
+
+        }
+    }
+
+    list lst {
+        key key;
+        leaf key {
+            type string;
+        }
+        action foo {
+
         }
     }
 
@@ -23,6 +33,14 @@ module actions {
         uses grp;
     }
 
+    list grplst {
+        key key;
+        leaf key {
+            type string;
+        }
+        uses grp;
+    }
+
     container othercont {
         uses other;
     }
index 5afd0cdcb6cbc9b10e8a7a05011f1a91ccd9dff4..117276462d717e86f55dc74f503f9d4a3570aab0 100644 (file)
@@ -27,6 +27,7 @@ import org.opendaylight.yangtools.yang.binding.DataRoot;
 import org.opendaylight.yangtools.yang.binding.Identifiable;
 import org.opendaylight.yangtools.yang.binding.Identifier;
 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.NotificationListener;
 import org.opendaylight.yangtools.yang.binding.RpcInput;
@@ -64,6 +65,7 @@ public final class BindingTypes {
     private static final ConcreteType ACTION = typeForClass(Action.class);
     private static final ConcreteType CHILD_OF = typeForClass(ChildOf.class);
     private static final ConcreteType CHOICE_IN = typeForClass(ChoiceIn.class);
+    private static final ConcreteType KEYED_LIST_ACTION = typeForClass(KeyedListAction.class);
     private static final ConcreteType RPC_RESULT = typeForClass(RpcResult.class);
 
     private BindingTypes() {
@@ -80,7 +82,22 @@ public final class BindingTypes {
      * @throws NullPointerException if any argument is is null
      */
     public static ParameterizedType action(final Type parent, final Type input, final Type output) {
-        return parameterizedTypeFor(ACTION, parent, input, output);
+        return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
+    }
+
+    /**
+     * Type specializing {@link KeyedListAction} for a particular type.
+     *
+     * @param parent Type of parent defining the action
+     * @param keyType Type of parent's key
+     * @param input Type input type
+     * @param output Type output type
+     * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
+     * @throws NullPointerException if any argument is is null
+     */
+    public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
+            final Type output) {
+        return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
     }
 
     /**
index 32210d9fb2b36e9c74c41321ba1422d1e8bc36ba..5f2eff6431c86e6610f0de5ca4ea5560be19b461 100644 (file)
@@ -13,6 +13,16 @@ module actions {
         }
     }
 
+    list lst {
+        key key;
+        leaf key {
+            type string;
+        }
+        action foo {
+
+        }
+    }
+
     grouping grp {
         action bar {
             output {
@@ -31,6 +41,14 @@ module actions {
         uses grp;
     }
 
+    list grplst {
+        key key;
+        leaf key {
+            type string;
+        }
+        uses grp;
+    }
+
     container othercont {
         uses other;
     }
index 0b60c9c835ccf29129605b274ddfcc7a4014b746..e77d65800bdfc82e22c86f954d097e214d9ad0a8 100644 (file)
@@ -20,7 +20,7 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
  */
 @Beta
 @FunctionalInterface
-public interface Action<P extends DataObject, I extends RpcInput, O extends RpcOutput> {
+public interface Action<P extends InstanceIdentifier<?>, I extends RpcInput, O extends RpcOutput> {
     /**
      * Invoke the action.
      *
@@ -30,5 +30,5 @@ public interface Action<P extends DataObject, I extends RpcInput, O extends RpcO
      * @throws NullPointerException if any of the arguments are null
      */
     @CheckReturnValue
-    @NonNull FluentFuture<@NonNull RpcResult<@NonNull O>> invoke(@NonNull InstanceIdentifier<P> path, @NonNull I input);
+    @NonNull FluentFuture<@NonNull RpcResult<@NonNull O>> invoke(@NonNull P path, @NonNull I input);
 }
diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedListAction.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/KeyedListAction.java
new file mode 100644 (file)
index 0000000..f7c31d0
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.binding;
+
+import com.google.common.annotations.Beta;
+import com.google.common.util.concurrent.FluentFuture;
+import javax.annotation.CheckReturnValue;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+/**
+ * Interface extended by all interfaces generated for a YANG {@code action} instantiated in keyed lists.
+ *
+ * @author Robert Varga
+ */
+@Beta
+@FunctionalInterface
+public interface KeyedListAction<K extends Identifier<T>, T extends DataObject & Identifiable<K>,
+        I extends RpcInput, O extends RpcOutput> extends Action<KeyedInstanceIdentifier<T, K>, I, O> {
+    @Override
+    @CheckReturnValue
+    FluentFuture<RpcResult<O>> invoke(KeyedInstanceIdentifier<T, K> path, I input);
+}