Merge changes from topic 'northbound-refactor'
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVPNIPSECSiteConnectionsNorthbound.java
index 205d01b0a05267a32f09e608832510567037c59c..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,
@@ -8,14 +8,14 @@
 
 package org.opendaylight.neutron.northbound.api;
 
+import java.net.HttpURLConnection;
+
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
-import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
@@ -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,12 +55,76 @@ import org.opendaylight.neutron.spi.NeutronVPNIPSECSiteConnection;
  */
 
 @Path("/vpn/ipsecsiteconnections")
-public class NeutronVPNIPSECSiteConnectionsNorthbound {
+public class NeutronVPNIPSECSiteConnectionsNorthbound
+    extends AbstractNeutronNorthbound<NeutronVPNIPSECSiteConnection, NeutronVPNIPSECSiteConnectionRequest, INeutronVPNIPSECSiteConnectionsCRUD, INeutronVPNIPSECSiteConnectionAware> {
+
+    private static final String RESOURCE_NAME = "VPNIPSECSiteConnections";
+
+    @Override
+    protected String getResourceName() {
+        return RESOURCE_NAME;
+    }
 
-    private NeutronVPNIPSECSiteConnection extractFields(NeutronVPNIPSECSiteConnection o, List<String> fields) {
+    @Override
+    protected NeutronVPNIPSECSiteConnection extractFields(NeutronVPNIPSECSiteConnection o, List<String> fields) {
         return o.extractFields(fields);
     }
 
+    @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(serviceUnavailable());
+        }
+        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
     UriInfo uriInfo;
 
@@ -71,10 +134,10 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
 
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
-    @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @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 listVPNIPSECSiteConnections(
             // return fields
             @QueryParam("fields") List<String> fields,
@@ -90,12 +153,7 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
             @QueryParam("vpnservice_id") String queryVpnServiceID
     // pagination and sorting are TODO
     ) {
-        INeutronVPNIPSECSiteConnectionsCRUD labelInterface = NeutronCRUDInterfaces
-                .getINeutronVPNIPSECSiteConnectionsCRUD(this);
-        if (labelInterface == null) {
-            throw new ServiceUnavailableException("NeutronVPNIPSECSiteConnections CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
+        INeutronVPNIPSECSiteConnectionsCRUD labelInterface = getNeutronCRUD();
         List<NeutronVPNIPSECSiteConnection> allNeutronVPNIPSECSiteConnection = labelInterface
                 .getAllNeutronVPNIPSECSiteConnections();
         List<NeutronVPNIPSECSiteConnection> ans = new ArrayList<NeutronVPNIPSECSiteConnection>();
@@ -127,7 +185,7 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
         }
 
         // TODO: apply pagination to results
-        return Response.status(200).entity(new NeutronVPNIPSECSiteConnectionRequest(ans)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronVPNIPSECSiteConnectionRequest(ans)).build();
     }
 
     /**
@@ -137,33 +195,16 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
     @Path("{connectionID}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
-    @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
-            @ResponseCode(code = 401, condition = "Unauthorized"), @ResponseCode(code = 403, condition = "Forbidden"),
-            @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
-    public Response showVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
+            @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("connectionID") String connectionID,
     // return fields
             @QueryParam("fields") List<String> fields) {
-        INeutronVPNIPSECSiteConnectionsCRUD connectionInterface = NeutronCRUDInterfaces
-                .getINeutronVPNIPSECSiteConnectionsCRUD(this);
-        if (connectionInterface == null) {
-            throw new ServiceUnavailableException("NeutronVPNIPSECSiteConnections CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        if (!connectionInterface.neutronVPNIPSECSiteConnectionsExists(policyID)) {
-            throw new ResourceNotFoundException("NeutronVPNIPSECSiteConnections ID not found");
-        }
-        if (fields.size() > 0) {
-            NeutronVPNIPSECSiteConnection ans = connectionInterface.getNeutronVPNIPSECSiteConnections(policyID);
-            return Response.status(200).entity(new NeutronVPNIPSECSiteConnectionRequest(extractFields(ans, fields)))
-                    .build();
-        } else {
-            return Response
-                    .status(200)
-                    .entity(new NeutronVPNIPSECSiteConnectionRequest(connectionInterface
-                            .getNeutronVPNIPSECSiteConnections(policyID))).build();
-        }
+        return show(connectionID, fields);
     }
 
     /**
@@ -173,193 +214,36 @@ public class NeutronVPNIPSECSiteConnectionsNorthbound {
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     @TypeHint(NeutronVPNIPSECSiteConnection.class)
-    @StatusCodes({ @ResponseCode(code = 201, condition = "Created"),
-            @ResponseCode(code = 400, condition = "Bad Request"),
-            @ResponseCode(code = 401, condition = "Unauthorized"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createVPNIPSECSiteConnection(final NeutronVPNIPSECSiteConnectionRequest input) {
-        INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = NeutronCRUDInterfaces
-                .getINeutronVPNIPSECSiteConnectionsCRUD(this);
-        if (ipsecSiteConnectionsInterface == null) {
-            throw new ServiceUnavailableException("VPNIPSECSiteConnections CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        if (input.isSingleton()) {
-            NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
-
-            /*
-             * verify that the ipsec site connection doesn't already exist (issue: is deeper
-             * inspection necessary?)
-             */
-            if (ipsecSiteConnectionsInterface.neutronVPNIPSECSiteConnectionsExists(singleton.getID())) {
-                throw new BadRequestException("VPNIPSECSiteConnections ID already exists");
-            }
-            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 < 200 || status > 299) {
-                            return Response.status(status).build();
-                        }
-                    }
-                } else {
-                    throw new ServiceUnavailableException("No providers registered.  Please try again later");
-                }
-            } else {
-                throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-            }
-            /*
-             * 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(201).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 = 200, condition = "Operation successful"),
-            @ResponseCode(code = 400, condition = "Bad Request"),
-            @ResponseCode(code = 401, condition = "Unauthorized"), @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
-    public Response updateVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    public Response updateVPNIPSECSiteConnection(@PathParam("connectionID") String connectionID,
             final NeutronVPNIPSECSiteConnectionRequest input) {
-        INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = NeutronCRUDInterfaces
-                .getINeutronVPNIPSECSiteConnectionsCRUD(this);
-
-        if (ipsecSiteConnectionsInterface == null) {
-            throw new ServiceUnavailableException("VPNIPSECSiteConnections CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-
-        /*
-         * ipsecSiteConnection has to exist and only a single delta can be
-         * supplied
-         */
-        if (!ipsecSiteConnectionsInterface.neutronVPNIPSECSiteConnectionsExists(policyID)) {
-            throw new ResourceNotFoundException("VPNIPSECSiteConnections ID not found");
-        }
-        if (!input.isSingleton()) {
-            throw new BadRequestException("Only singleton deltas supported");
-        }
-        NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
-        NeutronVPNIPSECSiteConnection original = ipsecSiteConnectionsInterface
-                .getNeutronVPNIPSECSiteConnections(policyID);
-
-        /*
-         * attribute changes blocked by Neutron
-         */
-        if (singleton.getID() != null || singleton.getTenantID() != null) {
-            throw new BadRequestException("Request attribute change not allowed");
-        }
-
-        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 < 200 || status > 299) {
-                        return Response.status(status).build();
-                    }
-                }
-            } else {
-                throw new ServiceUnavailableException("No providers registered.  Please try again later");
-            }
-        } else {
-            throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-        }
-        /*
-         * 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(200)
-                .entity(new NeutronVPNIPSECSiteConnectionRequest(ipsecSiteConnectionsInterface
-                        .getNeutronVPNIPSECSiteConnections(policyID))).build();
+        return update(connectionID, input);
     }
 
     /**
      * Deletes a VPN IPSEC SiteConnection
      */
 
-    @Path("{policyID}")
+    @Path("{connectionID}")
     @DELETE
-    @StatusCodes({ @ResponseCode(code = 204, condition = "No Content"),
-            @ResponseCode(code = 401, condition = "Unauthorized"), @ResponseCode(code = 404, condition = "Not Found"),
-            @ResponseCode(code = 409, condition = "Conflict"),
-            @ResponseCode(code = 501, condition = "Not Implemented"),
-            @ResponseCode(code = 503, condition = "No providers available") })
-    public Response deleteVPNIPSECSiteConnection(@PathParam("policyID") String policyID) {
-        INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = NeutronCRUDInterfaces
-                .getINeutronVPNIPSECSiteConnectionsCRUD(this);
-        if (ipsecSiteConnectionsInterface == null) {
-            throw new ServiceUnavailableException("NeutronVPNIPSECSiteConnections CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-
-        /*
-         * verify that the iSiteConnections exists and is not in use before
-         * removing it
-         */
-        if (!ipsecSiteConnectionsInterface.neutronVPNIPSECSiteConnectionsExists(policyID)) {
-            throw new ResourceNotFoundException("VPNIPSECSiteConnections ID not found");
-        }
-        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 < 200 || status > 299) {
-                        return Response.status(status).build();
-                    }
-                }
-            } else {
-                throw new ServiceUnavailableException("No providers registered.  Please try again later");
-            }
-        } else {
-            throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
-        }
-        ipsecSiteConnectionsInterface.removeNeutronVPNIPSECSiteConnections(policyID);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
-                service.neutronVPNIPSECSiteConnectionDeleted(singleton);
-            }
-        }
-        return Response.status(204).build();
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    public Response deleteVPNIPSECSiteConnection(@PathParam("connectionID") String connectionID) {
+        return delete(connectionID);
     }
 
 }