BUG 2468 - IdentityValuesDTO to string class cast exception. 84/13684/2
authorJozef Gloncak <jgloncak@cisco.com>
Tue, 16 Dec 2014 15:49:40 +0000 (16:49 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 7 Jan 2015 07:06:09 +0000 (07:06 +0000)
If leaf had type which is definined in other module (imported) via typedef
and this type is leafref then it wasn't possible correctly resolve base
type of referenced type.

New method for this resolution was implemented and is now called.

Also test for this case was added

Merge precondition:
yangtools: https://git.opendaylight.org/gerrit/13683

Change-Id: If00db3a6297b5a0ea657acb469f4f49f350f8c63
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/augment-leafref-module [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json
opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module

index ded398a33d0f0390beab6f8e723412182a8172ce..e24500a76ce57ff5a7b607dec7a7e208990c35c3 100644 (file)
@@ -86,6 +86,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.EmptyType;
+import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
@@ -110,7 +111,15 @@ public class RestconfImpl implements RestconfService {
         }
     }
 
+    private static class TypeDef {
+        public final TypeDefinition<? extends Object> typedef;
+        public final QName qName;
 
+        TypeDef(final TypeDefinition<? extends Object> typedef, final QName qName) {
+            this.typedef = typedef;
+            this.qName = qName;
+        }
+    }
 
     private final static RestconfImpl INSTANCE = new RestconfImpl();
 
@@ -1322,11 +1331,16 @@ public class RestconfImpl implements RestconfService {
             final DOMMountPoint mountPoint) {
         final Object value = simpleNode.getValue();
         Object inputValue = value;
-        TypeDefinition<? extends Object> typeDefinition = this.typeDefinition(schema);
+        TypeDef typeDef = this.typeDefinition(schema);
+        TypeDefinition<? extends Object> typeDefinition = typeDef != null ? typeDef.typedef : null;
 
         // For leafrefs, extract the type it is pointing to
         if(typeDefinition instanceof LeafrefTypeDefinition) {
-            typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), schema);
+            if (schema.getQName().equals(typeDef.qName)) {
+                typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), schema);
+            } else {
+                typeDefinition = SchemaContextUtil.getBaseTypeForLeafRef(((LeafrefTypeDefinition) typeDefinition), mountPoint == null ? this.controllerContext.getGlobalSchema() : mountPoint.getSchemaContext(), typeDef.qName);
+            }
         }
 
         if (typeDefinition instanceof IdentityrefTypeDefinition) {
@@ -1498,29 +1512,25 @@ public class RestconfImpl implements RestconfService {
         }
     }
 
-    private TypeDefinition<? extends Object> _typeDefinition(final LeafSchemaNode node) {
-        TypeDefinition<?> baseType = node.getType();
+    private TypeDef typeDefinition(final TypeDefinition<?> type, final QName nodeQName) {
+        TypeDefinition<?> baseType = type;
+        QName qName = nodeQName;
         while (baseType.getBaseType() != null) {
+            if (baseType instanceof ExtendedType) {
+                qName = baseType.getQName();
+            }
             baseType = baseType.getBaseType();
         }
 
-        return baseType;
-    }
-
-    private TypeDefinition<? extends Object> typeDefinition(final LeafListSchemaNode node) {
-        TypeDefinition<?> baseType = node.getType();
-        while (baseType.getBaseType() != null) {
-            baseType = baseType.getBaseType();
-        }
+        return new TypeDef(baseType, qName);
 
-        return baseType;
     }
 
-    private TypeDefinition<? extends Object> typeDefinition(final DataSchemaNode node) {
+    private TypeDef typeDefinition(final DataSchemaNode node) {
         if (node instanceof LeafListSchemaNode) {
-            return typeDefinition((LeafListSchemaNode) node);
+            return typeDefinition(((LeafListSchemaNode)node).getType(), node.getQName());
         } else if (node instanceof LeafSchemaNode) {
-            return _typeDefinition((LeafSchemaNode) node);
+            return typeDefinition(((LeafSchemaNode)node).getType(), node.getQName());
         } else if (node instanceof AnyXmlSchemaNode) {
             return null;
         } else {
index bdd74e8f9617a634235814304f21a3362eefeb6f..c11c7dbbe7f9633b6fb14b57513a0f7bf5f997f8 100644 (file)
@@ -24,7 +24,7 @@ public class JsonLeafrefToCnSnTest extends YangAndXmlAndDataSchemaLoader {
 
     @BeforeClass
     public static void initialize() {
-        dataLoad("/json-to-cnsn/leafref");
+        dataLoad("/json-to-cnsn/leafref",2,"leafref-module","cont");
     }
 
     /**
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/augment-leafref-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/augment-leafref-module
new file mode 100644 (file)
index 0000000..766cc81
--- /dev/null
@@ -0,0 +1,21 @@
+module augment-leafref-module {
+  namespace "augment:leafref:module";  
+
+  prefix "auglfrfmo";
+  revision 2014-12-16 {    
+  }
+
+
+  typedef leafreftype {
+    type leafref {
+        path "/cont/lf3";
+    
+    }
+  }
+  
+  container cont {
+    leaf lf3 {
+        type string;
+    }
+  }
+}
\ No newline at end of file
index 235666eed43aa49a2ccc4ac04528ab69c2a0b332..a9d5d29b44a82cad849c098185abaed1ad15c5fd 100644 (file)
@@ -1,6 +1,7 @@
 {
     "cont":{
         "lf1":121,
-        "lf2":121
+        "lf2":121,
+        "lf4":"pcc://39.39.39.39"
     }
 }
\ No newline at end of file
index 8ca9f090968fa8ed25d4f69b5243f011f1f709c5..9b124a0fe6594e29c987d90773ef041fcfbe7f08 100644 (file)
@@ -2,6 +2,8 @@ module leafref-module {
   namespace "leafref:module";  
 
   prefix "lfrfmo";
+  
+  import augment-leafref-module {prefix augleafref; revision-date 2014-12-16;}
   revision 2013-11-18 {    
   }
 
@@ -14,6 +16,9 @@ module leafref-module {
                 path "/cont/lf1"; 
             }
         }
+        leaf lf4 {
+            type augleafref:leafreftype;
+        }
     }
   
 }
\ No newline at end of file