NEUTRON-208: BGPVPN network and router association
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / INeutronRequest.java
index 1172c02850be954c2779303500e55dbac9a3a50a..f03c00bf2f53fa336ff636ef5021d21e7eb74024 100644 (file)
@@ -8,12 +8,39 @@
 
 package org.opendaylight.neutron.northbound.api;
 
+import java.lang.reflect.Field;
+import java.util.List;
 import org.opendaylight.neutron.spi.INeutronObject;
 
-import java.util.List;
+public interface INeutronRequest<T extends INeutronObject<T>> {
+
+    default T getSingleton() {
+        // return this.singleton
+        Class<?> cls = getClass();
+        try {
+            Field field = cls.getDeclaredField("singleton");
+            @SuppressWarnings("unchecked")
+            T value = (T) field.get(this);
+            return value;
+        } catch (IllegalAccessException | NoSuchFieldException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    default boolean isSingleton() {
+        return getSingleton() != null;
+    }
 
-public interface INeutronRequest<T extends INeutronObject> {
-    T getSingleton();
-    boolean isSingleton();
-    List<T> getBulk();
+    default List<T> getBulk() {
+        // return this.bulkRequest
+        Class<?> cls = getClass();
+        try {
+            Field field = cls.getDeclaredField("bulkRequest");
+            @SuppressWarnings("unchecked")
+            List<T> value = (List<T>) field.get(this);
+            return value;
+        } catch (IllegalAccessException | NoSuchFieldException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
 }