Mark typedef types as TypeObject 92/79892/6
authorJie Han <han.jie@zte.com.cn>
Fri, 25 Jan 2019 07:29:04 +0000 (15:29 +0800)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 19 Feb 2019 14:21:51 +0000 (15:21 +0100)
Make binding class of typedef implement TypeObject. In order to not
break compatibility, make sure we disregard marker interfaces when
computing serialVersionUID.

JIRA: MDSAL-406
Change-Id: I8e8bf31d3b3c43a2c843e7dd9914def12c52fee3
Signed-off-by: Jie Han <han.jie@zte.com.cn>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/yang/types/AbstractTypeProvider.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal406TypeObjectTest.java [new file with mode: 0644]
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/BindingGeneratorUtil.java
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/BindingTypes.java

index fa3457fbd544d6800bf75f3c2e7acf3516d40a2e..39f5931d9ae8876a19bcb579157bc7231759cebb 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.mdsal.binding.yang.types;
 
 import static java.util.Objects.requireNonNull;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.TYPE_OBJECT;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNodeForRelativeXPath;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
@@ -834,6 +835,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         genTOBuilder.addEqualsIdentity(genPropBuilder);
         genTOBuilder.addHashIdentity(genPropBuilder);
         genTOBuilder.addToStringProperty(genPropBuilder);
+        genTOBuilder.addImplementsType(TYPE_OBJECT);
         if (typedef.getStatus() == Status.DEPRECATED) {
             genTOBuilder.addAnnotation("java.lang", "Deprecated");
         }
@@ -892,6 +894,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         unionGenTOBuilder.setIsUnion(true);
         unionGenTOBuilder.setSchemaPath(typedef.getPath());
         unionGenTOBuilder.setModuleName(module.getName());
+        unionGenTOBuilder.addImplementsType(TYPE_OBJECT);
         addCodegenInformation(unionGenTOBuilder, typedef);
         generatedTOBuilders.add(unionGenTOBuilder);
 
@@ -1106,6 +1109,7 @@ public abstract class AbstractTypeProvider implements TypeProvider {
         genTOBuilder.setSchemaPath(typeDef.getPath());
         genTOBuilder.setModuleName(moduleName);
         genTOBuilder.setBaseType(typeDef);
+        genTOBuilder.addImplementsType(TYPE_OBJECT);
         addCodegenInformation(genTOBuilder, typeDef);
 
         final List<Bit> bitList = typeDef.getBits();
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal406TypeObjectTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal406TypeObjectTest.java
new file mode 100644 (file)
index 0000000..f43e055
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.mdsal.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.opendaylight.mdsal.binding.model.util.BindingTypes.TYPE_OBJECT;
+
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal406TypeObjectTest {
+
+    @Test
+    public void typeObjectTest() {
+        final SchemaContext context = YangParserTestUtils.parseYangResources(ExtendedTypedefTest.class,
+                "/type-provider/test.yang", "/ietf/ietf-inet-types.yang");
+
+        final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
+        assertNotNull(generateTypes);
+
+        final Type typedefType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
+            .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.MyBinary")).findFirst().get();
+
+        assertTrue(typedefType instanceof GeneratedType);
+        assertNotNull(((GeneratedType)  typedefType).getImplements());
+        Type objectType = ((GeneratedType)  typedefType).getImplements().stream()
+                .filter(type -> type.getFullyQualifiedName()
+                .equals("org.opendaylight.yangtools.yang.binding.TypeObject")).findAny().get();
+        assertEquals(TYPE_OBJECT, objectType);
+    }
+
+    @Test
+    public void typeObjectForBitsTypedefTest() {
+        final SchemaContext context = YangParserTestUtils.parseYangResources(ExtendedTypedefTest.class,
+                "/type-provider/test.yang", "/ietf/ietf-inet-types.yang");
+
+        final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
+        assertNotNull(generateTypes);
+
+        final Type typedefType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
+                .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.MyBits")).findFirst().get();
+
+        assertTrue(typedefType instanceof GeneratedType);
+        assertNotNull(((GeneratedType)  typedefType).getImplements());
+        Type objectType = ((GeneratedType)  typedefType).getImplements().stream()
+                .filter(type -> type.getFullyQualifiedName()
+                        .equals("org.opendaylight.yangtools.yang.binding.TypeObject")).findAny().get();
+        assertEquals(TYPE_OBJECT, objectType);
+    }
+
+    @Test
+    public void typeObjectForUnionTypedefTest() {
+        final SchemaContext context = YangParserTestUtils.parseYangResources(ExtendedTypedefTest.class,
+                "/type-provider/test.yang", "/ietf/ietf-inet-types.yang");
+
+        final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
+        assertNotNull(generateTypes);
+
+        final Type typedefType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
+                .equals("org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.MyUnion")).findFirst().get();
+
+        assertTrue(typedefType instanceof GeneratedType);
+        assertNotNull(((GeneratedType)  typedefType).getImplements());
+        Type objectType = ((GeneratedType)  typedefType).getImplements().stream()
+                .filter(type -> type.getFullyQualifiedName()
+                        .equals("org.opendaylight.yangtools.yang.binding.TypeObject")).findAny().get();
+        assertEquals(TYPE_OBJECT, objectType);
+    }
+}
index 8b23035eb97bbb03b9f0c13f179d4cbd16ed3201..3bd9a045a2afadd4008a7ce1b5474d6fa5619896 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.mdsal.binding.model.util;
 
 import com.google.common.base.CharMatcher;
+import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableList.Builder;
 import com.google.common.collect.Iterables;
@@ -186,7 +187,7 @@ public final class BindingGeneratorUtil {
             dout.writeUTF(to.getName());
             dout.writeInt(to.isAbstract() ? 3 : 7);
 
-            for (final Type ifc : sortedCollection(SUID_NAME_COMPARATOR, to.getImplementsTypes())) {
+            for (final Type ifc : sortedCollection(SUID_NAME_COMPARATOR, filteredImplementsTypes(to))) {
                 dout.writeUTF(ifc.getFullyQualifiedName());
             }
 
@@ -214,6 +215,10 @@ public final class BindingGeneratorUtil {
         return hash;
     }
 
+    private static Collection<Type> filteredImplementsTypes(final GeneratedTypeBuilderBase<?> to) {
+        return Collections2.filter(to.getImplementsTypes(), item -> !BindingTypes.TYPE_OBJECT.equals(item));
+    }
+
     private static <T extends Optional<?>> T currentOrEmpty(final T current, final T base) {
         return current.equals(base) ? (T)Optional.empty() : current;
     }
index 117276462d717e86f55dc74f503f9d4a3570aab0..46d487a9c485f28c68c0b9e96aa2bc379a06e42c 100644 (file)
@@ -33,6 +33,7 @@ import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.opendaylight.yangtools.yang.binding.RpcInput;
 import org.opendaylight.yangtools.yang.binding.RpcOutput;
 import org.opendaylight.yangtools.yang.binding.RpcService;
+import org.opendaylight.yangtools.yang.binding.TypeObject;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
@@ -41,6 +42,7 @@ public final class BindingTypes {
     public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
     public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
     public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.class);
+    public static final ConcreteType TYPE_OBJECT = typeForClass(TypeObject.class);
     public static final ConcreteType DATA_ROOT = typeForClass(DataRoot.class);
     public static final ConcreteType NOTIFICATION = typeForClass(Notification.class);
     public static final ConcreteType NOTIFICATION_LISTENER = typeForClass(NotificationListener.class);