Fix RPC method name conflict with JLS
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / AbstractTypeGenerator.java
index bace161c8f6971f4b114196ad5606ac43eca4581..58e53a5022e9d5084088e3c6652c929542b8fa87 100644 (file)
@@ -29,7 +29,9 @@ 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.instanceNotification;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.keyedListAction;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.keyedListNotification;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.opaqueObject;
 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.rpcResult;
 import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
@@ -51,6 +53,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -108,6 +111,7 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
@@ -301,6 +305,7 @@ abstract class AbstractTypeGenerator {
             constructGetter(parent, genType, node);
             resolveDataSchemaNodes(context, genType, genType, node.getChildNodes(), inGrouping);
             actionsToGenType(context, genType, node, null, inGrouping);
+            notificationsToGenType(context, genType, node, null, inGrouping);
         }
     }
 
@@ -308,35 +313,39 @@ abstract class AbstractTypeGenerator {
             final Type baseInterface, final ListSchemaNode node, final boolean inGrouping) {
         final GeneratedTypeBuilder genType = processDataSchemaNode(context, baseInterface, node, inGrouping);
         if (genType != null) {
+            final List<String> listKeys = listKeys(node);
+            final GeneratedTOBuilder keyTypeBuilder;
+            if (!listKeys.isEmpty()) {
+                keyTypeBuilder = typeProvider.newGeneratedTOBuilder(JavaTypeName.create(
+                    packageNameForGeneratedType(context.modulePackageName(), node.getPath()),
+                    BindingMapping.getClassName(node.getQName().getLocalName() + "Key")))
+                        .addImplementsType(identifier(genType));
+                genType.addImplementsType(identifiable(keyTypeBuilder));
+            } else {
+                keyTypeBuilder = null;
+            }
+
             final ParameterizedType listType = listTypeFor(genType);
             constructGetter(parent, listType, node);
             constructNonnull(parent, listType, node);
 
-            final List<String> listKeys = listKeys(node);
-            final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(context, node);
-            if (genTOBuilder != null) {
-                final Type identifierMarker = identifier(genType);
-                final Type identifiableMarker = identifiable(genTOBuilder);
-                genTOBuilder.addImplementsType(identifierMarker);
-                genType.addImplementsType(identifiableMarker);
-
-            }
-            actionsToGenType(context, genType, node, genTOBuilder, inGrouping);
+            actionsToGenType(context, genType, node, keyTypeBuilder, inGrouping);
+            notificationsToGenType(context, genType, node, keyTypeBuilder, inGrouping);
 
             for (final DataSchemaNode schemaNode : node.getChildNodes()) {
                 if (!schemaNode.isAugmenting()) {
-                    addSchemaNodeToListBuilders(context, schemaNode, genType, genTOBuilder, listKeys, inGrouping);
+                    addSchemaNodeToListBuilders(context, schemaNode, genType, keyTypeBuilder, listKeys, inGrouping);
                 }
             }
 
             // serialVersionUID
-            if (genTOBuilder != null) {
+            if (keyTypeBuilder != null) {
                 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
-                prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
-                genTOBuilder.setSUID(prop);
+                prop.setValue(Long.toString(computeDefaultSUID(keyTypeBuilder)));
+                keyTypeBuilder.setSUID(prop);
             }
 
-            typeBuildersToGenTypes(context, genType, genTOBuilder);
+            typeBuildersToGenTypes(context, genType, keyTypeBuilder);
         }
     }
 
@@ -513,7 +522,7 @@ abstract class AbstractTypeGenerator {
         for (final RpcDefinition rpc : rpcDefinitions) {
             if (rpc != null) {
                 final String rpcName = BindingMapping.getClassName(rpc.getQName());
-                final String rpcMethodName = BindingMapping.getPropertyName(rpcName);
+                final String rpcMethodName = BindingMapping.getRpcMethodName(rpc.getQName());
                 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
 
                 // Do not refer to annotation class, as it may not be available at runtime
@@ -597,6 +606,40 @@ abstract class AbstractTypeGenerator {
         context.addTopLevelNodeType(listenerInterface);
     }
 
+    private <T extends DataNodeContainer & NotificationNodeContainer> void notificationsToGenType(
+            final ModuleContext context, final Type parent, final T parentSchema, final Type keyType,
+            final boolean inGrouping) {
+        final Set<NotificationDefinition> notifications = parentSchema.getNotifications();
+        if (notifications.isEmpty()) {
+            return;
+        }
+
+        for (NotificationDefinition notif : notifications) {
+            if (notif.isAugmenting()) {
+                continue;
+            }
+            if (parentSchema instanceof GroupingDefinition) {
+                // Notifications cannot be really established, as they lack instantiation context, which would be
+                // completely described by an InstanceIdentifier -- hence we cannot create a binding class
+                continue;
+            }
+
+            processUsesAugments(notif, context, false);
+
+            final GeneratedTypeBuilder notifInterface = addDefaultInterfaceDefinition(
+                packageNameForGeneratedType(context.modulePackageName(), notif.getPath()), notif, DATA_OBJECT, context);
+            defaultImplementedInterace(notifInterface);
+            annotateDeprecatedIfNecessary(notif, notifInterface);
+
+            notifInterface.addImplementsType(keyType != null ? keyedListNotification(notifInterface, parent, keyType)
+                    : instanceNotification(notifInterface, parent));
+            context.addChildNodeType(notif, notifInterface);
+
+            // Notification object
+            resolveDataSchemaNodes(context, notifInterface, notifInterface, notif.getChildNodes(), false);
+        }
+    }
+
     /**
      * Converts all <b>identities</b> of the module to the list of
      * <code>Type</code> objects.
@@ -705,6 +748,7 @@ abstract class AbstractTypeGenerator {
             groupingsToGenTypes(context, grouping.getGroupings());
             processUsesAugments(grouping, context, true);
             actionsToGenType(context, genType, grouping, null, true);
+            notificationsToGenType(context, genType, grouping, null, true);
         }
     }
 
@@ -935,6 +979,8 @@ abstract class AbstractTypeGenerator {
 
         augSchemaNodeToMethods(context, augTypeBuilder, augSchema.getChildNodes(), inGrouping);
         actionsToGenType(context, augTypeBuilder, augSchema, null, inGrouping);
+        notificationsToGenType(context, augTypeBuilder, augSchema, null, inGrouping);
+
         augmentBuilders.put(augTypeName, augTypeBuilder);
 
         if (!augSchema.getChildNodes().isEmpty()) {
@@ -1788,32 +1834,19 @@ abstract class AbstractTypeGenerator {
      *         an empty list is returned.
      */
     private static List<String> listKeys(final ListSchemaNode list) {
-        final List<String> listKeys = new ArrayList<>();
-
         final List<QName> keyDefinition = list.getKeyDefinition();
-        if (keyDefinition != null) {
-            for (final QName keyDef : keyDefinition) {
-                listKeys.add(keyDef.getLocalName());
-            }
-        }
-        return listKeys;
-    }
-
-    /**
-     * Generates for the <code>list</code> which contains any list keys special generated TO builder.
-     *
-     * @param packageName string with package name to which the list belongs
-     * @param list list schema node which is source of data about the list name
-     * @return generated TO builder which represents the keys of the <code>list</code> or null if <code>list</code> is
-     *         null or list of key definitions is null or empty.
-     */
-    private GeneratedTOBuilder resolveListKeyTOBuilder(final ModuleContext context, final ListSchemaNode list) {
-        if (list.getKeyDefinition() != null && !list.getKeyDefinition().isEmpty()) {
-            return typeProvider.newGeneratedTOBuilder(JavaTypeName.create(
-                packageNameForGeneratedType(context.modulePackageName(), list.getPath()),
-                BindingMapping.getClassName(list.getQName().getLocalName() + "Key")));
+        switch (keyDefinition.size()) {
+            case 0:
+                return Collections.emptyList();
+            case 1:
+                return Collections.singletonList(keyDefinition.get(0).getLocalName());
+            default:
+                final List<String> listKeys = new ArrayList<>(keyDefinition.size());
+                for (final QName keyDef : keyDefinition) {
+                    listKeys.add(keyDef.getLocalName());
+                }
+                return listKeys;
         }
-        return null;
     }
 
     /**