NEUTRON-208: BGPVPN network and router association
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / INeutronRequest.java
index f8145c5c7706d65941d6dc5d4a3ce9860a57964d..f03c00bf2f53fa336ff636ef5021d21e7eb74024 100644 (file)
@@ -1,21 +1,46 @@
 /*
- * Copyright (C) 2014 Red Hat, Inc.
+ * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
- *  Authors : Dave Tucker
  */
 
 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);
+        }
+    }
 }