X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=northbound-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fneutron%2Fnorthbound%2Fapi%2FAbstractNeutronNorthbound.java;h=cad9f32bff528145225684678e54d441346c59f3;hb=f8778e026de414833ce6710e5bd80533433a9d1c;hp=a62a26884944a6ebc3da1687da89249302552669;hpb=7244b1ab3290f045f16fe4ac95ab41030862246b;p=neutron.git diff --git a/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/AbstractNeutronNorthbound.java b/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/AbstractNeutronNorthbound.java index a62a26884..cad9f32bf 100644 --- a/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/AbstractNeutronNorthbound.java +++ b/northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/AbstractNeutronNorthbound.java @@ -8,17 +8,28 @@ package org.opendaylight.neutron.northbound.api; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.ParameterizedType; import java.net.HttpURLConnection; import java.util.List; import javax.ws.rs.core.Response; import org.opendaylight.neutron.spi.INeutronCRUD; import org.opendaylight.neutron.spi.INeutronObject; +import org.opendaylight.neutron.spi.NeutronCRUDInterfaces; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class AbstractNeutronNorthbound, I extends INeutronCRUD> { - private static final Logger LOGGER = LoggerFactory - .getLogger(AbstractNeutronNorthbound.class); +public abstract class AbstractNeutronNorthbound, R extends INeutronRequest, + I extends INeutronCRUD> { + // T extends INeutronObject as 0th type argument + private static final int NEUTRON_ARGUMENT_TYPE_INDEX = 0; + // NeutronRequest extends INeutronRequest as 1st type argument + private static final int NEUTRON_REQUEST_TYPE_INDEX = 1; + // I extends INeutronCRUD 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; protected static final int HTTP_OK_TOP = 299; @@ -35,13 +46,45 @@ public abstract class AbstractNeutronNorthbound fields); - protected abstract NeutronRequest newNeutronRequest(T o); - protected abstract I getNeutronCRUD(); + + private Class getActualTypeArgument(final int typeIndex) { + ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); + @SuppressWarnings("unchecked") + Class cls = (Class) parameterizedType.getActualTypeArguments()[typeIndex]; + return cls; + } + + private R newNeutronRequest(T neutronObject) { + // return new R(neutronObject); + + // argumentClass = T.class + Class argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX); + // cls = NeturonRequest.class + Class cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX); + try { + // ctor = R constructor + Constructor ctor = cls.getDeclaredConstructor(argumentClass); + return ctor.newInstance(neutronObject); + } catch (NoSuchMethodException | InstantiationException + | IllegalAccessException | InvocationTargetException e) { + // This case shouldn't happen + throw new IllegalArgumentException(e); + } + } + + protected I getNeutronCRUD() { + // cls = I.class + Class cls = getActualTypeArgument(NEUTRON_CRUD_TYPE_INDEX); + I neutronCrud = NeutronCRUDInterfaces.fetchINeutronCRUD(cls, (Object) this); + if (neutronCrud == null) { + throw new ServiceUnavailableException(serviceUnavailable()); + } + return neutronCrud; + } protected Response show(String uuid, - // return fields - List fields) { + // return fields + List fields) { I neutronCRUD = getNeutronCRUD(); T ans = neutronCRUD.get(uuid); if (ans == null) { @@ -49,14 +92,14 @@ public abstract class AbstractNeutronNorthbound 0) { - return Response.status(HttpURLConnection.HTTP_OK).entity( - newNeutronRequest(extractFields(ans, fields))).build(); + return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans.extractFields(fields))) + .build(); } else { return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build(); } } - protected Response create(final NeutronRequest input) { + protected Response create(final R input) { I neutronCRUD = getNeutronCRUD(); if (input.isSingleton()) { T singleton = input.getSingleton(); @@ -78,7 +121,7 @@ public abstract class AbstractNeutronNorthbound