import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
resolveDataSchemaNodes(context, notificationInterface, notificationInterface,
notification.getChildNodes(), false);
- addComment(listenerInterface.addMethod("on" + notificationInterface.getName())
+ final MethodSignatureBuilder notificationMethod =
+ listenerInterface.addMethod("on" + notificationInterface.getName())
.setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
- .setReturnType(primitiveVoidType()), notification);
+ .setReturnType(primitiveVoidType());
+
+ annotateDeprecatedIfNecessary(notification, notificationMethod);
+ if (notification.getStatus().equals(Status.OBSOLETE)) {
+ notificationMethod.setDefault(true);
+ }
+ addComment(notificationMethod, notification);
}
}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech, 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.mdsal.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
+import org.opendaylight.mdsal.binding.model.api.MethodSignature;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal554Test {
+ @Test
+ public void builderTemplateGenerateListenerMethodsTest() {
+ final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/mdsal554.yang");
+
+ final Set<Module> toGenModules = new HashSet<>();
+ for (final Module module : context.getModules()) {
+ toGenModules.add(module);
+ }
+
+ final List<Type> genTypes = DefaultBindingGenerator.generateFor(context, toGenModules);
+
+ final GeneratedType type = (GeneratedType) genTypes.get(3);
+
+ final List<MethodSignature> methods = type.getMethodDefinitions();
+
+ assertEquals(3, methods.size());
+ assertEquals(methods.get(0).getName(), "onDeprecatedNotification");
+ assertEquals(methods.get(0).isDefault(), false);
+ assertEquals(methods.get(0).getAnnotations().size(), 1);
+ assertEquals(methods.get(0).getAnnotations().get(0).getIdentifier(), JavaTypeName.create(Deprecated.class));
+ assertEquals(methods.get(0).getAnnotations().get(0).getParameters().size(), 0);
+
+ assertEquals(methods.get(1).getName(), "onObsoleteNotification");
+ assertEquals(methods.get(1).isDefault(), true);
+ assertEquals(methods.get(1).getAnnotations().size(), 1);
+ assertEquals(methods.get(1).getAnnotations().get(0).getIdentifier(), JavaTypeName.create(Deprecated.class));
+ assertEquals(methods.get(1).getAnnotations().get(0).getParameters().size(), 1);
+ assertEquals(methods.get(1).getAnnotations().get(0).getParameters().get(0).getName(), "forRemoval");
+ assertEquals(methods.get(1).getAnnotations().get(0).getParameters().get(0).getValue(), "true");
+
+ assertEquals(methods.get(2).getName(), "onTestNotification");
+ assertEquals(methods.get(2).isDefault(), false);
+ assertEquals(methods.get(2).getAnnotations().size(), 0);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech, 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
+ */
+module test-listener {
+ yang-version 1;
+ namespace "urn:opendaylight:test-listener";
+ prefix "t";
+
+ revision "2020-10-22" {
+ }
+
+ notification deprecated-notification {
+ description "Just a testing deprecated notification.";
+ status deprecated;
+ }
+
+ notification obsolete-notification {
+ description "Just a testing obsolete notification.";
+ status obsolete;
+ }
+
+ notification test-notification {
+ description "Just a testing notification.";
+ }
+}
import org.opendaylight.mdsal.binding.model.api.MethodSignature
import org.opendaylight.mdsal.binding.model.api.Restrictions
import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.api.TypeMember
import org.opendaylight.mdsal.binding.model.api.TypeMemberComment
import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Single
import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition.Multiple
import org.opendaylight.mdsal.binding.model.api.Type
import org.opendaylight.mdsal.binding.model.util.Types
import org.opendaylight.mdsal.binding.model.util.TypeConstants
-import org.opendaylight.mdsal.binding.model.api.TypeMember
/**
* Template for generating JAVA interfaces.
} else {
switch method.name {
case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
+ default :
+ if (VOID == method.returnType.identifier) {
+ generateNoopVoidInterfaceMethod(method)
+ }
}
}
}
«method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
'''
+ def private generateNoopVoidInterfaceMethod(MethodSignature method) '''
+ «method.comment.asJavadoc»
+ «method.annotations.generateAnnotations»
+ default «VOID.importedName» «method.name»(«method.parameters.generateParameters») {
+ // No-op
+ }
+ '''
+
def private static accessorJavadoc(MethodSignature method, String orString) {
val reference = method.comment?.referenceDescription
val propReturn = method.propertyNameFromGetter + ", or " + orString + " if it is not present."
*/
static final @NonNull JavaTypeName OVERRIDE = JavaTypeName.create(Override.class);
/**
- * {@code java.lang.SuppressWarnings} as a JavaTypeName.
+ * {@code java.lang.void} as a JavaTypeName.
*/
static final @NonNull JavaTypeName SUPPRESS_WARNINGS = JavaTypeName.create(SuppressWarnings.class);
+ /**
+ * {@code java.lang.SuppressWarnings} as a JavaTypeName.
+ */
+ static final @NonNull JavaTypeName VOID = JavaTypeName.create(void.class);
/**
* {@code java.util.Arrays} as a JavaTypeName.
package org.opendaylight.mdsal.binding.java.api.generator;
import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
+import java.util.List;
import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.AnnotationType;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
+import org.opendaylight.mdsal.binding.model.api.MethodSignature;
import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.util.Types;
public class InterfaceGeneratorTest {
+ private static final String TEST = "test";
+ private static final JavaTypeName TYPE_NAME = JavaTypeName.create(TEST, TEST);
+
@Test
- public void basicTest() throws Exception {
+ public void basicTest() {
assertEquals("", new InterfaceGenerator().generate(mock(Type.class)));
}
+
+ @Test
+ public void builderTemplateListenerMethodTest() {
+ final MethodSignature methSign = mockMethSign("on" + TEST);
+ final GeneratedType genType = mockGenType(methSign);
+
+ String expected = String.join(System.lineSeparator(),
+ "package test;",
+ "",
+ "public interface test",
+ "{",
+ "",
+ "",
+ "",
+ "",
+ " void ontest();",
+ "",
+ "}",
+ "",
+ ""
+ );
+ assertEquals(expected, new InterfaceGenerator().generate(genType));
+ }
+
+ @Test
+ public void builderTemplateDeprecatedListenerMethodTest() {
+ final MethodSignature methSign = mockMethSign("on" + TEST);
+ addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
+ final GeneratedType genType = mockGenType(methSign);
+
+ String expected = String.join(System.lineSeparator(),
+ "package test;",
+ "import java.lang.Deprecated;",
+ "",
+ "public interface test",
+ "{",
+ "",
+ "",
+ "",
+ "",
+ " @Deprecated",
+ " void ontest();",
+ "",
+ "}",
+ "",
+ ""
+ );
+ assertEquals(expected, new InterfaceGenerator().generate(genType));
+ }
+
+ @Test
+ public void builderTemplateGenerateObsoleteListenerMethodTest() {
+ final MethodSignature methSign = mockMethSign("on" + TEST);
+ addMethodStatus(methSign, JavaTypeName.create(Deprecated.class));
+ doReturn(true).when(methSign).isDefault();
+ final GeneratedType genType = mockGenType(methSign);
+
+ String expected = String.join(System.lineSeparator(),
+ "package test;",
+ "import java.lang.Deprecated;",
+ "",
+ "public interface test",
+ "{",
+ "",
+ "",
+ "",
+ "",
+ " @Deprecated",
+ " default void ontest() {",
+ " // No-op",
+ " }",
+ "",
+ "}",
+ "",
+ ""
+ );
+ assertEquals(expected, new InterfaceGenerator().generate(genType));
+ }
+
+ private static GeneratedType mockGenType(final MethodSignature methSign) {
+ final GeneratedType genType = spy(GeneratedType.class);
+ doReturn(TYPE_NAME).when(genType).getIdentifier();
+ doReturn(TEST).when(genType).getName();
+ doReturn(TEST).when(genType).getPackageName();
+ final List<MethodSignature> listMethodSign = new ArrayList<>();
+ listMethodSign.add(methSign);
+ doReturn(listMethodSign).when(genType).getMethodDefinitions();
+
+ final List<Type> impls = new ArrayList<>();
+ doReturn(impls).when(genType).getImplements();
+ return genType;
+ }
+
+ private static MethodSignature mockMethSign(final String methodeName) {
+ final MethodSignature methSign = mock(MethodSignature.class);
+ doReturn(methodeName).when(methSign).getName();
+ final Type methType = Types.typeForClass(void.class);
+ doReturn(methType).when(methSign).getReturnType();
+ doReturn(MethodSignature.ValueMechanics.NORMAL).when(methSign).getMechanics();
+ return methSign;
+ }
+
+ private static void addMethodStatus(MethodSignature methSign, JavaTypeName annotationJavaType) {
+ final AnnotationType annotationType = mock(AnnotationType.class);
+ doReturn(annotationJavaType).when(annotationType).getIdentifier();
+ doReturn(ImmutableList.of(annotationType)).when(methSign).getAnnotations();
+ }
}
\ No newline at end of file