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;
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) parameterizedType.getActualTypeArguments()[0];
+ Class<T> argumentClass = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
// cls = NeturonRequest.class
- Class<NeutronRequest> cls = (Class) 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
- throw new RuntimeException(e);
+ throw new IllegalArgumentException(e);
}
}
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());
}
}
- protected Response create(final NeutronRequest input) {
+ protected Response create(final R input) {
I neutronCRUD = getNeutronCRUD();
if (input.isSingleton()) {
T singleton = input.getSingleton();
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");
/*
* remove it and return 204 status
*/
- boolean exist = false;
- try {
- exist = neutronCRUD.remove(uuid);
- } catch (Exception e) {
- final String resourceName = getResourceName();
- LOGGER.debug("exception during remove {} {} {}", resourceName, uuid, e);
- throw new InternalServerErrorException("Could not delete " + resourceName);
- }
- if (!exist) {
+ if (!neutronCRUD.remove(uuid)) {
throw new ResourceNotFoundException(uuidNoExist());
}