Merge "Bug 7848: Allow neutron port create with security disabled."
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / AbstractNeutronNorthbound.java
index 9ff8078dd7a3b0da43c9ed758f654c93293439d9..cad9f32bff528145225684678e54d441346c59f3 100644 (file)
@@ -20,8 +20,15 @@ import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, NeutronRequest extends INeutronRequest<T>,
+public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
         I extends INeutronCRUD<T>> {
+    // T extends INeutronObject<T> as 0th type argument
+    private static final int NEUTRON_ARGUMENT_TYPE_INDEX = 0;
+    // NeutronRequest extends INeutronRequest<T> as 1st type argument
+    private static final int NEUTRON_REQUEST_TYPE_INDEX = 1;
+    // I extends INeutronCRUD<T> as 2nd type argument
+    private static final int NEUTRON_CRUD_TYPE_INDEX = 2;
+
     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
 
     protected static final int HTTP_OK_BOTTOM = 200;
@@ -40,18 +47,24 @@ public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, Neu
 
     protected abstract String getResourceName();
 
-    private NeutronRequest newNeutronRequest(T o) {
-        // return new NeutronRequest(o);
-
+    private <K> Class<K> getActualTypeArgument(final int typeIndex) {
         ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
+        @SuppressWarnings("unchecked")
+        Class<K> cls = (Class<K>) parameterizedType.getActualTypeArguments()[typeIndex];
+        return cls;
+    }
+
+    private R newNeutronRequest(T neutronObject) {
+        // return new R(neutronObject);
+
         // argumentClass = T.class
-        Class<T> argumentClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
+        Class<T> argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
         // cls = NeturonRequest.class
-        Class<NeutronRequest> cls = (Class<NeutronRequest>) parameterizedType.getActualTypeArguments()[1];
+        Class<R> cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX);
         try {
-            // ctor = NeutronRequest constructor
-            Constructor<NeutronRequest> ctor = cls.getDeclaredConstructor(argumentClass);
-            return ctor.newInstance(o);
+            // ctor = R constructor
+            Constructor<R> ctor = cls.getDeclaredConstructor(argumentClass);
+            return ctor.newInstance(neutronObject);
         } catch (NoSuchMethodException | InstantiationException
                  | IllegalAccessException | InvocationTargetException e) {
             // This case shouldn't happen
@@ -60,9 +73,8 @@ public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, Neu
     }
 
     protected I getNeutronCRUD() {
-        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
         // cls = I.class
-        Class<I> cls = (Class<I>) parameterizedType.getActualTypeArguments()[2];
+        Class<I> cls = getActualTypeArgument(NEUTRON_CRUD_TYPE_INDEX);
         I neutronCrud = NeutronCRUDInterfaces.fetchINeutronCRUD(cls, (Object) this);
         if (neutronCrud == null) {
             throw new ServiceUnavailableException(serviceUnavailable());
@@ -87,7 +99,7 @@ public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, Neu
         }
     }
 
-    protected Response create(final NeutronRequest input) {
+    protected Response create(final R input) {
         I neutronCRUD = getNeutronCRUD();
         if (input.isSingleton()) {
             T singleton = input.getSingleton();
@@ -109,7 +121,7 @@ public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, Neu
     protected void updateDelta(String uuid, T delta, T original) {
     }
 
-    protected Response update(String uuid, final NeutronRequest input) {
+    protected Response update(String uuid, final R input) {
         I neutronCRUD = getNeutronCRUD();
         if (!input.isSingleton()) {
             throw new BadRequestException("Only singleton edit supported");