Fixed bug when converting augmentation in Instance Identifier 28/4128/1
authorTony Tkacik <ttkacik@cisco.com>
Fri, 10 Jan 2014 09:02:10 +0000 (10:02 +0100)
committerTony Tkacik <ttkacik@cisco.com>
Fri, 10 Jan 2014 09:04:50 +0000 (10:04 +0100)
QName for augmentation s not present in compile-time generated
classes, but Instance Identifier codec did not explicitly checked
that.

Changed implementation in a way, that now it does not uses
reflection, but runtime generated Augmentation codec to
provide valid QName.

Change-Id: If24e9c6bc67975440239dd20a6ec5025977aab58
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/dom/serializer/api/AugmentationCodec.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/dom/serializer/api/CodecRegistry.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/dom/serializer/impl/InstanceIdentifierCodecImpl.xtend
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/dom/serializer/impl/LazyGeneratedCodecRegistry.java

index cdddec7..970e077 100644 (file)
@@ -1,6 +1,7 @@
 package org.opendaylight.controller.sal.binding.dom.serializer.api;
 
 import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.Node;
 
@@ -12,4 +13,6 @@ public interface AugmentationCodec<A extends Augmentation<?>> extends DomCodec<A
     
     @Override
     public ValueWithQName<A> deserialize(Node<?> input);
+
+    public QName getAugmentationQName();
 }
index b9a4fe8..4565b54 100644 (file)
@@ -10,6 +10,7 @@ import java.util.List;
 
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.controller.sal.binding.dom.serializer.api.IdentifierCodec;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 
 
 public interface CodecRegistry {
@@ -36,4 +37,6 @@ public interface CodecRegistry {
     void bindingClassEncountered(Class<?> cls);
 
     void putPathToClass(List<QName> names, Class<?> cls);
+
+    public abstract QName getQNameForAugmentation(Class<?> cls);
 }
index 150d0f1..f84503b 100644 (file)
@@ -26,13 +26,13 @@ import org.opendaylight.yangtools.yang.binding.Augmentable
 import com.google.common.collect.ImmutableList
 import org.opendaylight.yangtools.yang.binding.Augmentation
 import java.util.concurrent.ConcurrentHashMap
+import org.opendaylight.yangtools.yang.binding.util.BindingReflections
 
 class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec {
     
     private static val LOG = LoggerFactory.getLogger(InstanceIdentifierCodecImpl);
     val CodecRegistry codecRegistry;
     
-    val Map<Class<?>,QName> classToQName = new WeakHashMap;
     val Map<Class<?>, Map<List<QName>, Class<?>>> classToPreviousAugment = new WeakHashMap;
     
     public new(CodecRegistry registry) {
@@ -106,7 +106,7 @@ class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec {
                 
                 previousAugmentation = null;
             } else {
-                previousQName = resolveQname(baArg.type);
+                previousQName = codecRegistry.getQNameForAugmentation(baArg.type as Class);
                 previousAugmentation = baArg.type;
             }
         }
@@ -122,7 +122,7 @@ class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec {
     
     private def dispatch PathArgument serializePathArgument(Item argument, QName previousQname) {
         val type = argument.type;
-        val qname = resolveQname(type);
+        val qname = BindingReflections.findQName(type);
         if(previousQname == null) {
             return new NodeIdentifier(qname);
         }
@@ -134,7 +134,7 @@ class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec {
         val Map<QName,Object> predicates = new HashMap();
         val type = argument.type;
         val keyCodec = codecRegistry.getIdentifierCodecForIdentifiable(type);
-        val qname = resolveQname(type);
+        val qname = BindingReflections.findQName(type);
         val combinedInput =  new ValueWithQName(previousQname,argument.key)
         val compositeOutput = keyCodec.serialize(combinedInput as ValueWithQName);
         for(outputValue :compositeOutput.value) {
@@ -145,15 +145,4 @@ class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec {
         }
         return new NodeIdentifierWithPredicates(QName.create(previousQname,qname.localName),predicates);
     }
-    
-    def resolveQname(Class<?> class1) {
-        val qname = classToQName.get(class1);
-        if(qname !== null) {
-            return qname;
-        }
-        val qnameField = class1.getField("QNAME");
-        val qnameValue = qnameField.get(null) as QName;
-        classToQName.put(class1,qnameValue);
-        return qnameValue;
-    }
 }
\ No newline at end of file
index cabb1bc..89d9b49 100644 (file)
@@ -44,6 +44,7 @@ import org.opendaylight.yangtools.yang.binding.BindingCodec;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.Node;
@@ -153,6 +154,12 @@ public class LazyGeneratedCodecRegistry implements //
         }
         return codec;
     }
+    
+    @Override
+    public QName getQNameForAugmentation(Class<?> cls) {
+        checkArgument(Augmentation.class.isAssignableFrom(cls));
+        return getCodecForAugmentation((Class<? extends Augmentation>)cls).getAugmentationQName();
+    }
 
     private static Class<? extends Augmentable<?>> getAugmentableArgumentFrom(
             final Class<? extends Augmentation<?>> augmentation) {
@@ -909,9 +916,11 @@ public class LazyGeneratedCodecRegistry implements //
             Delegator<BindingCodec> {
 
         private BindingCodec delegate;
+        private QName augmentationQName;
 
         public AugmentationCodecWrapper(BindingCodec<Map<QName, Object>, Object> rawCodec) {
             this.delegate = rawCodec;
+            this.augmentationQName = BindingReflections.findQName(rawCodec.getClass());
         }
 
         @Override
@@ -936,6 +945,11 @@ public class LazyGeneratedCodecRegistry implements //
             Object rawCodecValue = getDelegate().deserialize((Map<QName, Object>) input);
             return new ValueWithQName<T>(input.getNodeType(), (T) rawCodecValue);
         }
+        
+        @Override
+        public QName getAugmentationQName() {
+            return augmentationQName;
+        }
     }
 
     private class IdentityCompositeCodec implements IdentitityCodec {