Bug 4647: Binding Codec does not provide leaf default value for identityref 58/31758/1
authorIgor Foltin <ifoltin@cisco.com>
Tue, 22 Dec 2015 07:25:29 +0000 (08:25 +0100)
committerIgor Foltin <ifoltin@cisco.com>
Tue, 22 Dec 2015 07:25:29 +0000 (08:25 +0100)
Fixed handling of leaf default value for identityref type.

Also removed obsolete code.

Change-Id: Id442f25512f445e0fff5c9e9aef314813e2c35d4
Signed-off-by: Igor Foltin <ifoltin@cisco.com>
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/LeafDefaultValueTest.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/impl/LeafNodeCodecContext.java
binding/mdsal-binding-test-model/src/main/yang/opendaylight-default-value-test.yang

index c6bb24fb3d320779ff2d1beef3c9c3c59daad5c5..d81be9ccc008a4485758708e1197c9c566c77949 100644 (file)
@@ -403,5 +403,8 @@ public class LeafDefaultValueTest extends AbstractDataBrokerTest {
 
         IdentityrefContainer idrefCont = identityrefContainerNode.get();
         assertNull(idrefCont.getIdentityrefLeaf());
+        assertEquals("MyDerivedIdentity", idrefCont.getIdentityrefLeaf2().getSimpleName());
+        assertEquals("MyDerivedIdentity", idrefCont.getIdentityrefLeaf3().getSimpleName());
+        assertEquals("MyDerivedIdentity2", idrefCont.getIdentityrefLeaf4().getSimpleName());
     }
 }
index 40c706e354a5b0c5c226dd4cd46dc90f7d329c0e..7bc317d7162df2f7381ffea033d36e462c46f506 100644 (file)
@@ -10,8 +10,6 @@ package org.opendaylight.yangtools.binding.data.codec.impl;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -23,6 +21,7 @@ import org.opendaylight.yangtools.concepts.Codec;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
@@ -31,9 +30,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 
 final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<D> implements NodeContextSupplier {
 
@@ -57,31 +56,21 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
             Object defaultValue = ((LeafSchemaNode) schema).getDefault();
             TypeDefinition<?> type = ((LeafSchemaNode) schema).getType();
             if (defaultValue != null) {
+                if (type instanceof IdentityrefTypeDefinition) {
+                    return qnameDomValueFromString(codec, schema, defaultValue);
+                }
                 return domValueFromString(codec, type, defaultValue);
-            }
-            else {
+            } else {
                 while (type.getBaseType() != null && type.getDefaultValue() == null) {
                     type = type.getBaseType();
                 }
 
                 defaultValue = type.getDefaultValue();
                 if (defaultValue != null) {
-                    if (defaultValue instanceof Boolean) {
-                        return codec.deserialize(defaultValue);
+                    if (type instanceof IdentityrefTypeDefinition) {
+                        return qnameDomValueFromString(codec, schema, defaultValue);
                     }
 
-                    if (defaultValue instanceof IdentitySchemaNode) {
-                        defaultValue = ((IdentitySchemaNode) defaultValue).getQName();
-                        return codec.deserialize(defaultValue);
-                    }
-
-                    if (defaultValue instanceof ImmutableList) {
-                        return codec.deserialize(ImmutableSet.copyOf((ImmutableList) defaultValue));
-                    }
-
-                    if (defaultValue instanceof List) {
-                        return codec.deserialize(defaultValue);
-                    }
                     return domValueFromString(codec, type, defaultValue);
                 }
             }
@@ -89,6 +78,12 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
         return null;
     }
 
+    private static Object qnameDomValueFromString(final Codec<Object, Object> codec, final DataSchemaNode schema,
+                                               final Object defaultValue) {
+        Object qname = QName.create(schema.getQName(), (String) defaultValue);
+        return codec.deserialize(qname);
+    }
+
     private static Object domValueFromString(final Codec<Object, Object> codec, final TypeDefinition<?> type,
     Object defaultValue) {
         TypeDefinitionAwareCodec typeDefAwareCodec = TypeDefinitionAwareCodec.from(type);
index 9a41e0c35ea0d0a3bdcad1fe03d026982cc920d0..c567e7e26119511572411fa9e5491cf9311c36df 100644 (file)
@@ -257,7 +257,17 @@ module opendaylight-default-value-test {
     }
 
     identity my-identity {
-        description "identity for testing purposes";
+        description "parent identity for testing purposes";
+    }
+
+    identity my-derived-identity {
+        base my-identity;
+        description "child identity for testing purposes";
+    }
+
+    identity my-derived-identity2 {
+        base my-derived-identity;
+        description "another child identity for testing purposes";
     }
 
     typedef my-identityref {
@@ -266,6 +276,13 @@ module opendaylight-default-value-test {
         }
     }
 
+    typedef my-identityref2 {
+        type identityref {
+            base my-identity;
+        }
+        default "my-derived-identity";
+    }
+
     container tiny-int-container {
         presence "presence container";
 
@@ -622,5 +639,19 @@ module opendaylight-default-value-test {
         leaf identityref-leaf {
             type my-identityref;
         }
+
+        leaf identityref-leaf2 {
+            type my-identityref;
+            default "my-derived-identity";
+        }
+
+        leaf identityref-leaf3 {
+            type my-identityref2;
+        }
+
+        leaf identityref-leaf4 {
+            type my-identityref;
+            default "my-derived-identity2";
+        }
     }
 }
\ No newline at end of file