Bump to odlparent 3.1.3
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / AbstractNeutronNorthbound.java
index f1774c8935aa83662401469e206978d0828e45c0..2ebd7960e75f8c68879c18f2325de28f223335c2 100644 (file)
@@ -1,34 +1,53 @@
 /*
- * Copyright (c) 2015 Intel Corporation and others.  All rights reserved.
+ * Copyright (c) 2018 Intel Corporation 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
  */
-
 package org.opendaylight.neutron.northbound.api;
 
+import static org.opendaylight.neutron.spi.INeutronCRUD.Result.AlreadyExists;
+import static org.opendaylight.neutron.spi.INeutronCRUD.Result.DependencyMissing;
+import static org.opendaylight.neutron.spi.INeutronCRUD.Result.DoesNotExist;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.ParameterizedType;
 import java.net.HttpURLConnection;
 import java.util.List;
-
+import java.util.Objects;
+import javax.ws.rs.core.Response;
 import org.opendaylight.neutron.spi.INeutronCRUD;
+import org.opendaylight.neutron.spi.INeutronCRUD.Result;
 import org.opendaylight.neutron.spi.INeutronObject;
-
-import javax.ws.rs.core.Response;
-
+import org.opendaylight.yangtools.yang.common.OperationFailedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AbstractNeutronNorthbound<T extends INeutronObject, NeutronRequest extends INeutronRequest<T>, I extends INeutronCRUD<T>> {
-    private static final Logger LOGGER = LoggerFactory
-        .getLogger(AbstractNeutronNorthbound.class);
+public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, R extends INeutronRequest<T>,
+        I extends INeutronCRUD<T>> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractNeutronNorthbound.class);
+
+    // 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;
 
     protected static final int HTTP_OK_BOTTOM = 200;
     protected static final int HTTP_OK_TOP = 299;
+    private static final int HTTP_MISSING_DEPENDENCY = 442; // see NEUTRON-158 (also in neutron.e2etest.HttpUtils)
 
     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
 
+    private final I neutronCRUD;
+
+    protected AbstractNeutronNorthbound(I neutronCRUD) {
+        this.neutronCRUD = Objects.requireNonNull(neutronCRUD, "neutronCRUD");
+    }
+
     protected final String serviceUnavailable() {
         return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
     }
@@ -38,91 +57,152 @@ public abstract class AbstractNeutronNorthbound<T extends INeutronObject, Neutro
     }
 
     protected abstract String getResourceName();
-    protected abstract T extractFields(T o, List<String> fields);
-    protected abstract NeutronRequest newNeutronRequest(T o);
-    protected abstract I getNeutronCRUD();
-
-    protected Response show(String uuid,
-                            // return fields
-                            List<String> fields) {
-        I neutronCRUD = getNeutronCRUD();
-        T ans = neutronCRUD.get(uuid);
-        if (ans == null) {
-            throw new ResourceNotFoundException(uuidNoExist());
-        }
 
-        if (fields.size() > 0) {
-            return Response.status(HttpURLConnection.HTTP_OK).entity(
-                    newNeutronRequest(extractFields(ans, fields))).build();
-        } else {
-            return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
+    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 = getActualTypeArgument(NEUTRON_ARGUMENT_TYPE_INDEX);
+        // cls = NeturonRequest.class
+        Class<R> cls = getActualTypeArgument(NEUTRON_REQUEST_TYPE_INDEX);
+        try {
+            // 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 IllegalArgumentException(e);
         }
     }
 
-    protected Response create(final NeutronRequest input) {
-        I neutronCRUD = getNeutronCRUD();
-        if (input.isSingleton()) {
-            T singleton = input.getSingleton();
+    protected I getNeutronCRUD() {
+        return this.neutronCRUD;
+    }
+
+    protected Response show(String uuid, List<String> returnFields)
+            throws DatastoreOperationFailedWebApplicationException {
+        try {
+            T ans = neutronCRUD.get(uuid);
+            if (ans == null) {
+                throw new ResourceNotFoundException(uuidNoExist());
+            }
 
-            singleton.initDefaults();
-            neutronCRUD.add(singleton);
-        } else {
-            if (input.getBulk() == null) {
-                throw new BadRequestException("Invalid requests");
+            if (returnFields.size() > 0) {
+                return Response.status(HttpURLConnection.HTTP_OK)
+                        .entity(newNeutronRequest(ans.extractFields(returnFields))).build();
+            } else {
+                return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(ans)).build();
             }
-            for (T test : input.getBulk()) {
-                test.initDefaults();
-                neutronCRUD.add(test);
+        } catch (OperationFailedException e) {
+            LOG.warn("get failed due to datastore problem; uuid: {}", uuid);
+            throw new DatastoreOperationFailedWebApplicationException(e);
+        }
+    }
+
+    protected Response create(final R input) throws DatastoreOperationFailedWebApplicationException {
+        try {
+            if (input.isSingleton()) {
+                T singleton = input.getSingleton();
+
+                singleton.initDefaults();
+                Result result = neutronCRUD.add(singleton);
+                if (result.equals(DependencyMissing)) {
+                    LOG.warn("create failed due to input missing dependencies: {}", input);
+                    return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
+                } else if (result.equals(AlreadyExists)) {
+                    return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
+                }
+            } else {
+                if (input.getBulk() == null) {
+                    throw new BadRequestException("Invalid requests");
+                }
+                for (T test : input.getBulk()) {
+                    test.initDefaults();
+                    Result result = neutronCRUD.add(test);
+                    if (result.equals(DependencyMissing)) {
+                        LOG.warn("create failed due to input missing dependencies: {}", input);
+                        return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
+                    } else if (result.equals(AlreadyExists)) {
+                        return Response.status(HttpURLConnection.HTTP_CONFLICT).entity(input).build();
+                    }
+                }
             }
+            return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
+        } catch (OperationFailedException e) {
+            LOG.warn("create failed due to datastore problem (possibly missing required fields); input: {}", input);
+            throw new DatastoreOperationFailedWebApplicationException(e);
         }
-        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
     }
 
     protected void updateDelta(String uuid, T delta, T original) {
     }
 
-    protected Response update(String uuid, final NeutronRequest input) {
-        I neutronCRUD = getNeutronCRUD();
+    private boolean checkRevisionNumber(T original, T delta) {
+        // If new update is null ignore the original revision number
+        if (delta.getRevisionNumber() == null) {
+            return false;
+        }
+        // If what is stored is null no need for comparison
+        if (original.getRevisionNumber() == null) {
+            return false;
+        }
+        if (original.getRevisionNumber() > delta.getRevisionNumber()) {
+            return true;
+        }
+        return false;
+    }
+
+    protected Response update(String uuid, final R input) throws DatastoreOperationFailedWebApplicationException {
         if (!input.isSingleton()) {
             throw new BadRequestException("Only singleton edit supported");
         }
         T delta = input.getSingleton();
-        T original = neutronCRUD.get(uuid);
-        if (original == null) {
-            throw new ResourceNotFoundException(uuidNoExist());
-        }
-        updateDelta(uuid, delta, original);
-
-        /*
-         * update the object and return it
-         */
-        if (!neutronCRUD.update(uuid, delta)) {
-            throw new ResourceNotFoundException(uuidNoExist());
+        try {
+            T original = neutronCRUD.get(uuid);
+            if (original == null) {
+                throw new ResourceNotFoundException(uuidNoExist());
+            }
+            if (checkRevisionNumber(original, delta)) {
+                return Response.status(HttpURLConnection.HTTP_OK).build();
+            }
+            updateDelta(uuid, delta, original);
+            /*
+             * update the object and return it
+             */
+            Result updateResult = neutronCRUD.update(uuid, delta);
+            if (updateResult.equals(DoesNotExist)) {
+                throw new ResourceNotFoundException(uuidNoExist());
+            } else if (updateResult.equals(DependencyMissing)) {
+                LOG.warn("update failed due to missing dependencies; input: {}", input);
+                return Response.status(HTTP_MISSING_DEPENDENCY).entity(input).build();
+            }
+            T updated = neutronCRUD.get(uuid);
+            return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
+        } catch (OperationFailedException e) {
+            LOG.warn("update failed due to datastore problem (possibly missing required fields); input: {}", input);
+            throw new DatastoreOperationFailedWebApplicationException(e);
         }
-        T updated = neutronCRUD.get(uuid);
-        return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
     }
 
-    protected Response delete(String uuid) {
-        final I neutronCRUD = getNeutronCRUD();
-
-        /*
-         * remove it and return 204 status
-         */
-        final String resourceName = getResourceName();
-        boolean exist = false;
+    protected Response delete(String uuid) throws DatastoreOperationFailedWebApplicationException {
         try {
-            exist = neutronCRUD.remove(uuid);
-        } catch (Exception e) {
-            LOGGER.debug("exception during remove {} {} {}",
-                         resourceName, uuid, e);
-            throw new InternalServerErrorException("Could not delete " +
-                                                   resourceName);
-        }
-        if (!exist) {
-            throw new ResourceNotFoundException(uuidNoExist());
+            // remove it and return 204 status
+            if (!neutronCRUD.remove(uuid)) {
+                throw new ResourceNotFoundException(uuidNoExist());
+            } else {
+                return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+            }
+        } catch (OperationFailedException e) {
+            LOG.warn("delete failed due to datastore problem; uuid: {}", uuid);
+            throw new DatastoreOperationFailedWebApplicationException(e);
         }
-
-        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
     }
 }