Merge changes from topic 'northbound-refactor'
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVPNIPSECSiteConnectionsNorthbound.java
index 1a4b35555ce39185abe59355ea6f9a71bf47893d..73bcbe0fdcc825d877f1cf2c038ec00471e9df3f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright IBM Corporation, 2013.  All rights reserved.
+ * Copyright (c) 2013, 2015 IBM 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,
@@ -31,7 +31,6 @@ import javax.ws.rs.core.UriInfo;
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
 import org.codehaus.enunciate.jaxrs.TypeHint;
-import org.opendaylight.neutron.spi.INeutronVPNIKEPolicyAware;
 import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionAware;
 import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionsCRUD;
 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
@@ -56,25 +55,74 @@ import org.opendaylight.neutron.spi.NeutronVPNIPSECSiteConnection;
  */
 
 @Path("/vpn/ipsecsiteconnections")
-public class NeutronVPNIPSECSiteConnectionsNorthbound {
+public class NeutronVPNIPSECSiteConnectionsNorthbound
+    extends AbstractNeutronNorthbound<NeutronVPNIPSECSiteConnection, NeutronVPNIPSECSiteConnectionRequest, INeutronVPNIPSECSiteConnectionsCRUD, INeutronVPNIPSECSiteConnectionAware> {
 
-    private static final int HTTP_OK_BOTTOM = 200;
-    private static final int HTTP_OK_TOP = 299;
-    private static final String INTERFACE_NAME = "VPNIPSECSiteConnections CRUD Interface";
-    private static final String NO_PROVIDERS = "No providers registered.  Please try again later";
-    private static final String NO_PROVIDER_LIST = "Couldn't get providers list.  Please try again later";
+    private static final String RESOURCE_NAME = "VPNIPSECSiteConnections";
 
-    private NeutronVPNIPSECSiteConnection extractFields(NeutronVPNIPSECSiteConnection o, List<String> fields) {
+    @Override
+    protected String getResourceName() {
+        return RESOURCE_NAME;
+    }
+
+    @Override
+    protected NeutronVPNIPSECSiteConnection extractFields(NeutronVPNIPSECSiteConnection o, List<String> fields) {
         return o.extractFields(fields);
     }
 
-    private NeutronCRUDInterfaces getNeutronInterfaces() {
+    @Override
+    protected NeutronVPNIPSECSiteConnectionRequest newNeutronRequest(NeutronVPNIPSECSiteConnection o) {
+        return new NeutronVPNIPSECSiteConnectionRequest(o);
+    }
+
+    @Override
+    protected INeutronVPNIPSECSiteConnectionsCRUD getNeutronCRUD() {
         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronVPNIPSECSiteConnectionsCRUD(this);
         if (answer.getVPNIPSECSiteConnectionsInterface() == null) {
-            throw new ServiceUnavailableException(INTERFACE_NAME
-                + RestMessages.SERVICEUNAVAILABLE.toString());
+            throw new ServiceUnavailableException(serviceUnavailable());
         }
-        return answer;
+        return answer.getVPNIPSECSiteConnectionsInterface();
+    }
+
+    @Override
+    protected Object[] getInstances() {
+        return NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
+    }
+
+    @Override
+    protected int canCreate(Object instance, NeutronVPNIPSECSiteConnection singleton) {
+        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+        return service.canCreateNeutronVPNIPSECSiteConnection(singleton);
+    }
+
+    @Override
+    protected void created(Object instance, NeutronVPNIPSECSiteConnection singleton) {
+        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+        service.neutronVPNIPSECSiteConnectionCreated(singleton);
+    }
+
+    @Override
+    protected int canUpdate(Object instance, NeutronVPNIPSECSiteConnection delta, NeutronVPNIPSECSiteConnection original) {
+        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+        return service.canUpdateNeutronVPNIPSECSiteConnection(delta, original);
+    }
+
+    @Override
+    protected void updated(Object instance, NeutronVPNIPSECSiteConnection updated) {
+        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+        service.neutronVPNIPSECSiteConnectionUpdated(updated);
+    }
+
+    @Override
+    protected int canDelete(Object instance, NeutronVPNIPSECSiteConnection singleton) {
+        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+        return service.canDeleteNeutronVPNIPSECSiteConnection(singleton);
+    }
+
+    @Override
+    protected void deleted(Object instance, NeutronVPNIPSECSiteConnection singleton) {
+        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
+        service.neutronVPNIPSECSiteConnectionDeleted(singleton);
     }
 
     @Context
@@ -105,8 +153,7 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
             @QueryParam("vpnservice_id") String queryVpnServiceID
     // pagination and sorting are TODO
     ) {
-        INeutronVPNIPSECSiteConnectionsCRUD labelInterface = getNeutronInterfaces()
-                .getVPNIPSECSiteConnectionsInterface();
+        INeutronVPNIPSECSiteConnectionsCRUD labelInterface = getNeutronCRUD();
         List<NeutronVPNIPSECSiteConnection> allNeutronVPNIPSECSiteConnection = labelInterface
                 .getAllNeutronVPNIPSECSiteConnections();
         List<NeutronVPNIPSECSiteConnection> ans = new ArrayList<NeutronVPNIPSECSiteConnection>();
@@ -154,24 +201,10 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response showVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
+    public Response showVPNIPSECSiteConnection(@PathParam("connectionID") String connectionID,
     // return fields
             @QueryParam("fields") List<String> fields) {
-        INeutronVPNIPSECSiteConnectionsCRUD connectionInterface = getNeutronInterfaces()
-                .getVPNIPSECSiteConnectionsInterface();
-        if (!connectionInterface.neutronVPNIPSECSiteConnectionsExists(policyID)) {
-            throw new ResourceNotFoundException("NeutronVPNIPSECSiteConnections ID not found");
-        }
-        if (fields.size() > 0) {
-            NeutronVPNIPSECSiteConnection ans = connectionInterface.getNeutronVPNIPSECSiteConnections(policyID);
-            return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronVPNIPSECSiteConnectionRequest(extractFields(ans, fields)))
-                    .build();
-        } else {
-            return Response
-                    .status(HttpURLConnection.HTTP_OK)
-                    .entity(new NeutronVPNIPSECSiteConnectionRequest(connectionInterface
-                            .getNeutronVPNIPSECSiteConnections(policyID))).build();
-        }
+        return show(connectionID, fields);
     }
 
     /**
@@ -182,149 +215,35 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
     @Consumes({ MediaType.APPLICATION_JSON })
     @TypeHint(NeutronVPNIPSECSiteConnection.class)
     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
-            @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
-            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
-            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createVPNIPSECSiteConnection(final NeutronVPNIPSECSiteConnectionRequest input) {
-        INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
-                .getVPNIPSECSiteConnectionsInterface();
-        if (input.isSingleton()) {
-            NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
-            Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
-            if (instances != null) {
-                if (instances.length > 0) {
-                    for (Object instance : instances) {
-                        INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                        int status = service.canCreateNeutronVPNIPSECSiteConnection(singleton);
-                        if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                            return Response.status(status).build();
-                        }
-                    }
-                } else {
-                    throw new ServiceUnavailableException(NO_PROVIDERS);
-                }
-            } else {
-                throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-            }
-            /*
-             * add ipsecSiteConnections to the cache
-             */
-            ipsecSiteConnectionsInterface.addNeutronVPNIPSECSiteConnections(singleton);
-            if (instances != null) {
-                for (Object instance : instances) {
-                    INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                    service.neutronVPNIPSECSiteConnectionCreated(singleton);
-                }
-            }
-        } else {
-
-            /*
-             * only singleton ipsecSiteConnections creates supported
-             */
-            throw new BadRequestException("Only singleton ipsecSiteConnections creates supported");
-        }
-        return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
+        return create(input);
     }
 
     /**
      * Updates a VPN IPSEC SiteConnection
      */
-    @Path("{policyID}")
+    @Path("{connectionID}")
     @PUT
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-            @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
-            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
-            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
-            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response updateVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
+    public Response updateVPNIPSECSiteConnection(@PathParam("connectionID") String connectionID,
             final NeutronVPNIPSECSiteConnectionRequest input) {
-        INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
-                .getVPNIPSECSiteConnectionsInterface();
-
-        NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
-        NeutronVPNIPSECSiteConnection original = ipsecSiteConnectionsInterface
-                .getNeutronVPNIPSECSiteConnections(policyID);
-
-        Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
-        if (instances != null) {
-            if (instances.length > 0) {
-                for (Object instance : instances) {
-                    INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                    int status = service.canUpdateNeutronVPNIPSECSiteConnection(singleton, original);
-                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                        return Response.status(status).build();
-                    }
-                }
-            } else {
-                throw new ServiceUnavailableException(NO_PROVIDERS);
-            }
-        } else {
-            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-        }
-        /*
-         * update the ipsecSiteConnections entry and return the modified object
-         */
-        ipsecSiteConnectionsInterface.updateNeutronVPNIPSECSiteConnections(policyID, singleton);
-        NeutronVPNIPSECSiteConnection updatedVPNIKEPolicy = ipsecSiteConnectionsInterface
-                .getNeutronVPNIPSECSiteConnections(policyID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                service.neutronVPNIPSECSiteConnectionUpdated(updatedVPNIKEPolicy);
-            }
-        }
-        return Response
-                .status(HttpURLConnection.HTTP_OK)
-                .entity(new NeutronVPNIPSECSiteConnectionRequest(ipsecSiteConnectionsInterface
-                        .getNeutronVPNIPSECSiteConnections(policyID))).build();
+        return update(connectionID, input);
     }
 
     /**
      * Deletes a VPN IPSEC SiteConnection
      */
 
-    @Path("{policyID}")
+    @Path("{connectionID}")
     @DELETE
     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
-            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
-            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
-            @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
-            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response deleteVPNIPSECSiteConnection(@PathParam("policyID") String policyID) {
-        INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
-                .getVPNIPSECSiteConnectionsInterface();
-
-        NeutronVPNIPSECSiteConnection singleton = ipsecSiteConnectionsInterface
-                .getNeutronVPNIPSECSiteConnections(policyID);
-        Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
-        if (instances != null) {
-            if (instances.length > 0) {
-                for (Object instance : instances) {
-                    INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                    int status = service.canDeleteNeutronVPNIPSECSiteConnection(singleton);
-                    if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                        return Response.status(status).build();
-                    }
-                }
-            } else {
-                throw new ServiceUnavailableException(NO_PROVIDERS);
-            }
-        } else {
-            throw new ServiceUnavailableException(NO_PROVIDER_LIST);
-        }
-        ipsecSiteConnectionsInterface.removeNeutronVPNIPSECSiteConnections(policyID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                service.neutronVPNIPSECSiteConnectionDeleted(singleton);
-            }
-        }
-        return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
+    public Response deleteVPNIPSECSiteConnection(@PathParam("connectionID") String connectionID) {
+        return delete(connectionID);
     }
 
 }