Checkstyle Import issues fix (SPI tests,Northbound API)
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / AbstractNeutronNorthbound.java
index 292d7af9ebeee47d0b7585af76dac09d2049269d..a62a26884944a6ebc3da1687da89249302552669 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * Copyright (c) 2015 Intel Corporation  All rights reserved.
- * Copyright (c) 2015 Isaku Yamahata  All rights reserved.
+ * Copyright (c) 2015 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,
@@ -9,12 +8,15 @@
 
 package org.opendaylight.neutron.northbound.api;
 
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class AbstractNeutronNorthbound {
+public abstract class AbstractNeutronNorthbound<T extends INeutronObject, NeutronRequest extends INeutronRequest<T>, I extends INeutronCRUD<T>> {
     private static final Logger LOGGER = LoggerFactory
         .getLogger(AbstractNeutronNorthbound.class);
 
@@ -23,24 +25,91 @@ public abstract class AbstractNeutronNorthbound {
 
     private static final String INTERFACE_NAME_BASE = " CRUD Interface";
     private static final String UUID_NO_EXIST_BASE = " UUID does not exist.";
-    protected static final String NO_PROVIDERS = "No providers registered.  Please try again later";
-    protected static final String NO_PROVIDER_LIST = "Couldn't get providers list.  Please try again later";
 
-    protected static final String serviceUnavailable(String resourceName) {
-        return resourceName + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
+    protected final String serviceUnavailable() {
+        return getResourceName() + INTERFACE_NAME_BASE + RestMessages.SERVICEUNAVAILABLE.toString();
+    }
+
+    protected final String uuidNoExist() {
+        return getResourceName() + UUID_NO_EXIST_BASE;
+    }
+
+    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();
+        }
+    }
+
+    protected Response create(final NeutronRequest input) {
+        I neutronCRUD = getNeutronCRUD();
+        if (input.isSingleton()) {
+            T singleton = input.getSingleton();
+
+            singleton.initDefaults();
+            neutronCRUD.add(singleton);
+        } else {
+            if (input.getBulk() == null) {
+                throw new BadRequestException("Invalid requests");
+            }
+            for (T test : input.getBulk()) {
+                test.initDefaults();
+                neutronCRUD.add(test);
+            }
+        }
+        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
     }
-    protected static final String uuidNoExist(String resourceName) {
-        return resourceName + UUID_NO_EXIST_BASE;
+
+    protected void updateDelta(String uuid, T delta, T original) {
     }
 
-    protected interface Remover {
-        abstract public boolean remove(String uuid);
+    protected Response update(String uuid, final NeutronRequest input) {
+        I neutronCRUD = getNeutronCRUD();
+        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());
+        }
+        T updated = neutronCRUD.get(uuid);
+        return Response.status(HttpURLConnection.HTTP_OK).entity(newNeutronRequest(updated)).build();
     }
 
-    public void deleteUuid(String resourceName, String uuid, Remover remover) {
+    protected Response delete(String uuid) {
+        final I neutronCRUD = getNeutronCRUD();
+
+        /*
+         * remove it and return 204 status
+         */
+        final String resourceName = getResourceName();
         boolean exist = false;
         try {
-            exist = remover.remove(uuid);
+            exist = neutronCRUD.remove(uuid);
         } catch (Exception e) {
             LOGGER.debug("exception during remove {} {} {}",
                          resourceName, uuid, e);
@@ -48,7 +117,9 @@ public abstract class AbstractNeutronNorthbound {
                                                    resourceName);
         }
         if (!exist) {
-            throw new ResourceNotFoundException(uuidNoExist(resourceName));
+            throw new ResourceNotFoundException(uuidNoExist());
         }
+
+        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
     }
 }