northbound: code clean up
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronRoutersNorthbound.java
index 2c8d6487f5c24832ad742943942afa236fd2213a..60264b4dedcf0af271640149804810fc2eb19af2 100644 (file)
@@ -9,11 +9,9 @@
 package org.opendaylight.neutron.northbound.api;
 
 import java.net.HttpURLConnection;
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
@@ -25,19 +23,13 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
-
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
-import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
-import org.opendaylight.neutron.spi.INeutronPortCRUD;
-import org.opendaylight.neutron.spi.INeutronRouterAware;
 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
-import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronRouter;
 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
 
-
 /**
  * Neutron Northbound REST APIs.<br>
  * This class provides REST APIs for managing neutron routers
@@ -57,8 +49,8 @@ import org.opendaylight.neutron.spi.NeutronRouter_Interface;
  */
 
 @Path("/routers")
-public class NeutronRoutersNorthbound
-    extends AbstractNeutronNorthboundIAware<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD, INeutronRouterAware> {
+public final class NeutronRoutersNorthbound
+        extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
     static final String ROUTER_INTERFACE_STR = "network:router_interface";
     static final String ROUTER_GATEWAY_STR = "network:router_gateway";
     private static final String RESOURCE_NAME = "Router";
@@ -68,11 +60,6 @@ public class NeutronRoutersNorthbound
         return RESOURCE_NAME;
     }
 
-    @Override
-    protected NeutronRouter extractFields(NeutronRouter o, List<String> fields) {
-        return o.extractFields(fields);
-    }
-
     private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
         if (answer.getRouterInterface() == null) {
@@ -81,8 +68,8 @@ public class NeutronRoutersNorthbound
         if (flag) {
             answer = answer.fetchINeutronNetworkCRUD(this);
             if (answer.getNetworkInterface() == null) {
-                throw new ServiceUnavailableException("Network CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
+                throw new ServiceUnavailableException(
+                        "Network CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
             }
         }
         return answer;
@@ -93,77 +80,13 @@ public class NeutronRoutersNorthbound
         return getNeutronInterfaces(false).getRouterInterface();
     }
 
-    private NeutronCRUDInterfaces getAttachInterfaces() {
-        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
-        if (answer.getRouterInterface() == null) {
-            throw new ServiceUnavailableException(serviceUnavailable());
-        }
-        answer = answer.fetchINeutronPortCRUD(this).fetchINeutronSubnetCRUD(this);
-        if (answer.getPortInterface() == null) {
-            throw new ServiceUnavailableException("Port CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        if (answer.getSubnetInterface() == null) {
-            throw new ServiceUnavailableException("Subnet CRUD Interface "
-                    + RestMessages.SERVICEUNAVAILABLE.toString());
-        }
-        return answer;
-    }
-
-    @Override
-    protected NeutronRouterRequest newNeutronRequest(NeutronRouter o) {
-        return new NeutronRouterRequest(o);
-    }
-
-    @Override
-    protected Object[] getInstances() {
-        return NeutronUtil.getInstances(INeutronRouterAware.class, this);
-    }
-
-    @Override
-    protected int canCreate(Object instance, NeutronRouter singleton) {
-        INeutronRouterAware service = (INeutronRouterAware) instance;
-        return service.canCreateRouter(singleton);
-    }
-
-    @Override
-    protected void created(Object instance, NeutronRouter singleton) {
-        INeutronRouterAware service = (INeutronRouterAware) instance;
-        service.neutronRouterCreated(singleton);
-    }
-
-    @Override
-    protected int canUpdate(Object instance, NeutronRouter delta, NeutronRouter original) {
-        INeutronRouterAware service = (INeutronRouterAware) instance;
-        return service.canUpdateRouter(delta, original);
-    }
-
-    @Override
-    protected void updated(Object instance, NeutronRouter updated) {
-        INeutronRouterAware service = (INeutronRouterAware) instance;
-        service.neutronRouterUpdated(updated);
-    }
-
-    @Override
-    protected int canDelete(Object instance, NeutronRouter singleton) {
-        INeutronRouterAware service = (INeutronRouterAware) instance;
-        return service.canDeleteRouter(singleton);
-    }
-
-    @Override
-    protected void deleted(Object instance, NeutronRouter singleton) {
-        INeutronRouterAware service = (INeutronRouterAware) instance;
-        service.neutronRouterDeleted(singleton);
-    }
-
     /**
      * Returns a list of all Routers */
 
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+    @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") })
@@ -181,33 +104,33 @@ public class NeutronRoutersNorthbound
             @QueryParam("limit") String limit,
             @QueryParam("marker") String marker,
             @QueryParam("page_reverse") String pageReverse
-            // sorting not supported
-            ) {
+    // sorting not supported
+    ) {
         INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
         if (routerInterface == null) {
             throw new ServiceUnavailableException(serviceUnavailable());
         }
-        List<NeutronRouter> allRouters = routerInterface.getAllRouters();
-        List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
+        List<NeutronRouter> allRouters = routerInterface.getAll();
+        List<NeutronRouter> ans = new ArrayList<>();
         Iterator<NeutronRouter> i = allRouters.iterator();
         while (i.hasNext()) {
             NeutronRouter oSS = i.next();
-            if ((queryID == null || queryID.equals(oSS.getID())) &&
-                    (queryName == null || queryName.equals(oSS.getName())) &&
-                    (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
-                    (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
-                    (queryExternalGatewayInfo == null || queryExternalGatewayInfo.equals(oSS.getExternalGatewayInfo())) &&
-                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
+            if ((queryID == null || queryID.equals(oSS.getID()))
+                    && (queryName == null || queryName.equals(oSS.getName()))
+                    && (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp()))
+                    && (queryStatus == null || queryStatus.equals(oSS.getStatus()))
+                    && (queryExternalGatewayInfo == null
+                            || queryExternalGatewayInfo.equals(oSS.getExternalGatewayInfo()))
+                    && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
                 if (fields.size() > 0) {
-                    ans.add(extractFields(oSS,fields));
+                    ans.add(oSS.extractFields(fields));
                 } else {
                     ans.add(oSS);
                 }
             }
         }
         //TODO: apply pagination to results
-        return Response.status(HttpURLConnection.HTTP_OK).entity(
-                new NeutronRouterRequest(ans)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronRouterRequest(ans)).build();
     }
 
     /**
@@ -217,15 +140,13 @@ public class NeutronRoutersNorthbound
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+    @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 showRouter(
-            @PathParam("routerUUID") String routerUUID,
+    public Response showRouter(@PathParam("routerUUID") String routerUUID,
             // return fields
             @QueryParam("fields") List<String> fields) {
         return show(routerUUID, fields);
@@ -238,8 +159,7 @@ public class NeutronRoutersNorthbound
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createRouters(final NeutronRouterRequest input) {
         getNeutronInterfaces(true); // ensure that network service is loaded
@@ -260,14 +180,10 @@ public class NeutronRoutersNorthbound
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouters.class)
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response updateRouter(
-            @PathParam("routerUUID") String routerUUID,
-            NeutronRouterRequest input
-            ) {
+    public Response updateRouter(@PathParam("routerUUID") String routerUUID, NeutronRouterRequest input) {
         getNeutronInterfaces(true); // ensure that network service is loaded
         return update(routerUUID, input);
     }
@@ -277,12 +193,10 @@ public class NeutronRoutersNorthbound
 
     @Path("{routerUUID}")
     @DELETE
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response deleteRouter(
-            @PathParam("routerUUID") String routerUUID) {
+    public Response deleteRouter(@PathParam("routerUUID") String routerUUID) {
         return delete(routerUUID);
     }
 
@@ -294,87 +208,20 @@ public class NeutronRoutersNorthbound
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouterInterfaces.class)
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response addRouterInterface(
-            @PathParam("routerUUID") String routerUUID,
-            NeutronRouter_Interface input
-            ) {
-        NeutronCRUDInterfaces interfaces = getAttachInterfaces();
-        INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
-
-        NeutronRouter target = routerInterface.getRouter(routerUUID);
-        Object[] instances = getInstances();
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronRouterAware service = (INeutronRouterAware) instance;
-                int status = service.canAttachInterface(target, input);
-                if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                    return Response.status(status).build();
-                }
-            }
-        }
-
-        target.addInterface(input.getPortUUID(), input);
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronRouterAware service = (INeutronRouterAware) instance;
-                service.neutronRouterInterfaceAttached(target, input);
-            }
-        }
-
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
+    public Response addRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouter_Interface input) {
+        // Do nothing. Keep this interface for compatibility
         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
     }
 
-
-    private int checkDownstreamDetach(NeutronRouter target, NeutronRouter_Interface input) {
-        Object[] instances = getInstances();
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronRouterAware service = (INeutronRouterAware) instance;
-                int status = service.canDetachInterface(target, input);
-                if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
-                    return status;
-                }
-            }
-        }
-        return HTTP_OK_BOTTOM;
-    }
-
-    /**
-     * Removes an interface to a router */
-
     @Path("{routerUUID}/remove_router_interface")
     @PUT
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackRouterInterfaces.class)
-    @StatusCodes({
-            @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response removeRouterInterface(
-            @PathParam("routerUUID") String routerUUID,
-            NeutronRouter_Interface input
-            ) {
-        NeutronCRUDInterfaces interfaces = getAttachInterfaces();
-        INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
-        Object[] instances = getInstances();
-
-        NeutronRouter target = routerInterface.getRouter(routerUUID);
-        input.setID(target.getID());
-        input.setTenantID(target.getTenantID());
-        int status = checkDownstreamDetach(target, input);
-        if (status != HTTP_OK_BOTTOM) {
-            return Response.status(status).build();
-        }
-        target.removeInterface(input.getPortUUID());
-        if (instances != null) {
-            for (Object instance : instances) {
-                INeutronRouterAware service = (INeutronRouterAware) instance;
-                service.neutronRouterInterfaceDetached(target, input);
-            }
-        }
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
+    public Response removeRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouter_Interface input) {
+        // Do nothing. Keep this interface for compatibility
         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
     }
 }