Merge "Add additional subnet E2E tests."
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / AbstractNeutronInterface.java
index 58f52898a8f15841d9bac26d4da4b8673b0317ef..31b20dc9df29458ff1e674ed850d56c570359d14 100644 (file)
@@ -1,5 +1,7 @@
 package org.opendaylight.neutron.transcriber;
 
+import java.lang.reflect.Method;
+
 import java.util.concurrent.ExecutionException;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -106,6 +108,33 @@ public abstract class AbstractNeutronInterface<T extends DataObject,S> implement
         return result;
     }
 
+    // this method uses reflection to update an object from it's delta.
+
+    protected boolean overwrite(Object target, Object delta) {
+        Method[] methods = target.getClass().getMethods();
+
+        for(Method toMethod: methods){
+            if(toMethod.getDeclaringClass().equals(target.getClass())
+                    && toMethod.getName().startsWith("set")){
+
+                String toName = toMethod.getName();
+                String fromName = toName.replace("set", "get");
+
+                try {
+                    Method fromMethod = delta.getClass().getMethod(fromName);
+                    Object value = fromMethod.invoke(delta, (Object[])null);
+                    if(value != null){
+                        toMethod.invoke(target, value);
+                    }
+                } catch (Exception e) {
+                    LOGGER.error("Error in overwrite", e);
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
     @Override
     public void close() throws Exception {
         // TODO Auto-generated method stub