Fix for bugs 446 and 456
[controller.git] / opendaylight / northbound / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / northbound / NeutronFloatingIPsNorthbound.java
index 2b0ad45629967ac7061d8961ff54c16155a17543..f93191220b407b43080595edfff2f771d7160aa4 100644 (file)
-/*\r
- * Copyright IBM Corporation, 2013.  All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.controller.networkconfig.neutron.northbound;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import javax.ws.rs.Consumes;\r
-import javax.ws.rs.DELETE;\r
-import javax.ws.rs.GET;\r
-import javax.ws.rs.POST;\r
-import javax.ws.rs.PUT;\r
-import javax.ws.rs.Path;\r
-import javax.ws.rs.PathParam;\r
-import javax.ws.rs.Produces;\r
-import javax.ws.rs.QueryParam;\r
-import javax.ws.rs.core.MediaType;\r
-import javax.ws.rs.core.Response;\r
-\r
-import org.codehaus.enunciate.jaxrs.ResponseCode;\r
-import org.codehaus.enunciate.jaxrs.StatusCodes;\r
-import org.opendaylight.controller.networkconfig.neutron.INeutronFloatingIPAware;\r
-import org.opendaylight.controller.networkconfig.neutron.INeutronFloatingIPCRUD;\r
-import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;\r
-import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;\r
-import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;\r
-import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;\r
-import org.opendaylight.controller.networkconfig.neutron.NeutronFloatingIP;\r
-import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;\r
-import org.opendaylight.controller.networkconfig.neutron.NeutronPort;\r
-import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;\r
-import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;\r
-import org.opendaylight.controller.northbound.commons.RestMessages;\r
-import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;\r
-import org.opendaylight.controller.sal.utils.ServiceHelper;\r
-\r
-/**\r
- * Open DOVE Northbound REST APIs.<br>\r
- * This class provides REST APIs for managing the open DOVE\r
- *\r
- * <br>\r
- * <br>\r
- * Authentication scheme : <b>HTTP Basic</b><br>\r
- * Authentication realm : <b>opendaylight</b><br>\r
- * Transport : <b>HTTP and HTTPS</b><br>\r
- * <br>\r
- * HTTPS Authentication is disabled by default. Administrator can enable it in\r
- * tomcat-server.xml after adding a proper keystore / SSL certificate from a\r
- * trusted authority.<br>\r
- * More info :\r
- * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration\r
- *\r
- */\r
-\r
-@Path("/floatingips")\r
-public class NeutronFloatingIPsNorthbound {\r
-\r
-    private NeutronFloatingIP extractFields(NeutronFloatingIP o, List<String> fields) {\r
-        return o.extractFields(fields);\r
-    }\r
-\r
-    /**\r
-     * Returns a list of all FloatingIPs */\r
-\r
-    @GET\r
-    @Produces({ MediaType.APPLICATION_JSON })\r
-    @StatusCodes({\r
-            @ResponseCode(code = 200, condition = "Operation successful"),\r
-            @ResponseCode(code = 401, condition = "Unauthorized"),\r
-            @ResponseCode(code = 501, condition = "Not Implemented") })\r
-    public Response listFloatingIPs(\r
-            // return fields\r
-            @QueryParam("fields") List<String> fields,\r
-            // note: openstack isn't clear about filtering on lists, so we aren't handling them\r
-            @QueryParam("id") String queryID,\r
-            @QueryParam("floating_network_id") String queryFloatingNetworkId,\r
-            @QueryParam("port_id") String queryPortId,\r
-            @QueryParam("fixed_ip_address") String queryFixedIPAddress,\r
-            @QueryParam("floating_ip_address") String queryFloatingIPAddress,\r
-            @QueryParam("tenant_id") String queryTenantID,\r
-            // pagination\r
-            @QueryParam("limit") String limit,\r
-            @QueryParam("marker") String marker,\r
-            @QueryParam("page_reverse") String pageReverse\r
-            // sorting not supported\r
-            ) {\r
-        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);\r
-        if (floatingIPInterface == null) {\r
-            throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        List<NeutronFloatingIP> allFloatingIPs = floatingIPInterface.getAllFloatingIPs();\r
-        List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();\r
-        Iterator<NeutronFloatingIP> i = allFloatingIPs.iterator();\r
-        while (i.hasNext()) {\r
-            NeutronFloatingIP oSS = i.next();\r
-            //match filters: TODO provider extension and router extension\r
-            if ((queryID == null || queryID.equals(oSS.getID())) &&\r
-                    (queryFloatingNetworkId == null || queryFloatingNetworkId.equals(oSS.getFloatingNetworkUUID())) &&\r
-                    (queryPortId == null || queryPortId.equals(oSS.getPortUUID())) &&\r
-                    (queryFixedIPAddress == null || queryFixedIPAddress.equals(oSS.getFixedIPAddress())) &&\r
-                    (queryFloatingIPAddress == null || queryFloatingIPAddress.equals(oSS.getFloatingIPAddress())) &&\r
-                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantUUID()))) {\r
-                if (fields.size() > 0)\r
-                    ans.add(extractFields(oSS,fields));\r
-                else\r
-                    ans.add(oSS);\r
-            }\r
-        }\r
-        //TODO: apply pagination to results\r
-        return Response.status(200).entity(\r
-                new NeutronFloatingIPRequest(ans)).build();\r
-    }\r
-\r
-    /**\r
-     * Returns a specific FloatingIP */\r
-\r
-    @Path("{floatingipUUID}")\r
-    @GET\r
-    @Produces({ MediaType.APPLICATION_JSON })\r
-    @StatusCodes({\r
-            @ResponseCode(code = 200, condition = "Operation successful"),\r
-            @ResponseCode(code = 401, condition = "Unauthorized"),\r
-            @ResponseCode(code = 404, condition = "Not Found"),\r
-            @ResponseCode(code = 501, condition = "Not Implemented") })\r
-    public Response showFloatingIP(\r
-            @PathParam("floatingipUUID") String floatingipUUID,\r
-            // return fields\r
-            @QueryParam("fields") List<String> fields ) {\r
-        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);\r
-        if (floatingIPInterface == null) {\r
-            throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        if (!floatingIPInterface.floatingIPExists(floatingipUUID))\r
-            return Response.status(404).build();\r
-        if (fields.size() > 0) {\r
-            NeutronFloatingIP ans = floatingIPInterface.getFloatingIP(floatingipUUID);\r
-            return Response.status(200).entity(\r
-                    new NeutronFloatingIPRequest(extractFields(ans, fields))).build();\r
-        } else\r
-            return Response.status(200).entity(\r
-                    new NeutronFloatingIPRequest(floatingIPInterface.getFloatingIP(floatingipUUID))).build();\r
-\r
-    }\r
-\r
-    /**\r
-     * Creates new FloatingIPs */\r
-\r
-    @POST\r
-    @Produces({ MediaType.APPLICATION_JSON })\r
-    @Consumes({ MediaType.APPLICATION_JSON })\r
-    @StatusCodes({\r
-        @ResponseCode(code = 201, condition = "Created"),\r
-        @ResponseCode(code = 400, condition = "Bad Request"),\r
-        @ResponseCode(code = 401, condition = "Unauthorized"),\r
-        @ResponseCode(code = 409, condition = "Conflict"),\r
-        @ResponseCode(code = 501, condition = "Not Implemented") })\r
-    public Response createFloatingIPs(final NeutronFloatingIPRequest input) {\r
-        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);\r
-        if (floatingIPInterface == null) {\r
-            throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);\r
-        if (networkInterface == null) {\r
-            throw new ServiceUnavailableException("Network CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);\r
-        if (subnetInterface == null) {\r
-            throw new ServiceUnavailableException("Subnet CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD( this);\r
-        if (portInterface == null) {\r
-            throw new ServiceUnavailableException("Port CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        if (input.isSingleton()) {\r
-            NeutronFloatingIP singleton = input.getSingleton();\r
-            // check existence of id in cache and return badrequest if exists\r
-            if (floatingIPInterface.floatingIPExists(singleton.getID()))\r
-                return Response.status(400).build();\r
-            // check if the external network is specified, exists, and is an external network\r
-            String externalNetworkUUID = singleton.getFloatingNetworkUUID();\r
-            if (externalNetworkUUID == null)\r
-                return Response.status(400).build();\r
-            if (!networkInterface.networkExists(externalNetworkUUID))\r
-                return Response.status(400).build();\r
-            NeutronNetwork externNetwork = networkInterface.getNetwork(externalNetworkUUID);\r
-            if (!externNetwork.isRouterExternal())\r
-                return Response.status(400).build();\r
-            // if floating IP is specified, make sure it can come from the network\r
-            String floatingIP = singleton.getFloatingIPAddress();\r
-            if (floatingIP != null) {\r
-                if (externNetwork.getSubnets().size() > 1)\r
-                    return Response.status(400).build();\r
-                NeutronSubnet externSubnet = subnetInterface.getSubnet(externNetwork.getSubnets().get(0));\r
-                if (!externSubnet.isValidIP(floatingIP))\r
-                    return Response.status(400).build();\r
-                if (externSubnet.isIPInUse(floatingIP))\r
-                    return Response.status(409).build();\r
-            }\r
-            // if port_id is specified, then check that the port exists and has at least one IP\r
-            String port_id = singleton.getPortUUID();\r
-            if (port_id != null) {\r
-                String fixedIP = null;        // used for the fixedIP calculation\r
-                if (!portInterface.portExists(port_id))\r
-                    return Response.status(404).build();\r
-                NeutronPort port = portInterface.getPort(port_id);\r
-                if (port.getFixedIPs().size() < 1)\r
-                    return Response.status(400).build();\r
-                // if there is more than one fixed IP then check for fixed_ip_address\r
-                // and that it is in the list of port addresses\r
-                if (port.getFixedIPs().size() > 1) {\r
-                    fixedIP = singleton.getFixedIPAddress();\r
-                    if (fixedIP == null)\r
-                        return Response.status(400).build();\r
-                    Iterator<Neutron_IPs> i = port.getFixedIPs().iterator();\r
-                    boolean validFixedIP = false;\r
-                    while (i.hasNext() && !validFixedIP) {\r
-                        Neutron_IPs ip = i.next();\r
-                        if (ip.getIpAddress().equals(fixedIP))\r
-                            validFixedIP = true;\r
-                    }\r
-                    if (!validFixedIP)\r
-                        return Response.status(400).build();\r
-                } else {\r
-                    fixedIP = port.getFixedIPs().get(0).getIpAddress();\r
-                    if (singleton.getFixedIPAddress() != null && !fixedIP.equalsIgnoreCase(singleton.getFixedIPAddress()))\r
-                        return Response.status(400).build();\r
-                }\r
-                //lastly check that this fixed IP address isn't already used\r
-                if (port.isBoundToFloatingIP(fixedIP))\r
-                    return Response.status(409).build();\r
-                singleton.setFixedIPAddress(fixedIP);\r
-            }\r
-            Object[] instances = ServiceHelper.getGlobalInstances(INeutronFloatingIPAware.class, this, null);\r
-            if (instances != null) {\r
-                for (Object instance : instances) {\r
-                    INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;\r
-                    int status = service.canCreateFloatingIP(singleton);\r
-                    if (status < 200 || status > 299)\r
-                        return Response.status(status).build();\r
-                }\r
-            }\r
-            floatingIPInterface.addFloatingIP(singleton);\r
-            if (instances != null) {\r
-                for (Object instance : instances) {\r
-                    INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;\r
-                    service.neutronFloatingIPCreated(singleton);\r
-                }\r
-            }\r
-        } else {\r
-            return Response.status(400).build();\r
-        }\r
-        return Response.status(201).entity(input).build();\r
-    }\r
-\r
-    /**\r
-     * Updates a FloatingIP */\r
-\r
-    @Path("{floatingipUUID}")\r
-    @PUT\r
-    @Produces({ MediaType.APPLICATION_JSON })\r
-    @Consumes({ MediaType.APPLICATION_JSON })\r
-    @StatusCodes({\r
-            @ResponseCode(code = 200, condition = "Operation successful"),\r
-            @ResponseCode(code = 400, condition = "Bad Request"),\r
-            @ResponseCode(code = 401, condition = "Unauthorized"),\r
-            @ResponseCode(code = 404, condition = "Not Found"),\r
-            @ResponseCode(code = 409, condition = "Conflict"),\r
-            @ResponseCode(code = 501, condition = "Not Implemented") })\r
-    public Response updateFloatingIP(\r
-            @PathParam("floatingipUUID") String floatingipUUID,\r
-            NeutronFloatingIPRequest input\r
-            ) {\r
-        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);\r
-        if (floatingIPInterface == null) {\r
-            throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);\r
-        if (networkInterface == null) {\r
-            throw new ServiceUnavailableException("Network CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);\r
-        if (subnetInterface == null) {\r
-            throw new ServiceUnavailableException("Subnet CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD( this);\r
-        if (portInterface == null) {\r
-            throw new ServiceUnavailableException("Port CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        if (!floatingIPInterface.floatingIPExists(floatingipUUID))\r
-            return Response.status(404).build();\r
-\r
-        NeutronFloatingIP sourceFloatingIP = floatingIPInterface.getFloatingIP(floatingipUUID);\r
-        if (!input.isSingleton())\r
-            return Response.status(400).build();\r
-        NeutronFloatingIP singleton = input.getSingleton();\r
-        if (singleton.getID() != null)\r
-            return Response.status(400).build();\r
-\r
-        NeutronNetwork externNetwork = networkInterface.getNetwork(\r
-                sourceFloatingIP.getFloatingNetworkUUID());\r
-\r
-        // if floating IP is specified, make sure it can come from the network\r
-        String floatingIP = singleton.getFloatingIPAddress();\r
-        if (floatingIP != null) {\r
-            if (externNetwork.getSubnets().size() > 1)\r
-                return Response.status(400).build();\r
-            NeutronSubnet externSubnet = subnetInterface.getSubnet(externNetwork.getSubnets().get(0));\r
-            if (!externSubnet.isValidIP(floatingIP))\r
-                return Response.status(400).build();\r
-            if (externSubnet.isIPInUse(floatingIP))\r
-                return Response.status(409).build();\r
-        }\r
-\r
-        // if port_id is specified, then check that the port exists and has at least one IP\r
-        String port_id = singleton.getPortUUID();\r
-        if (port_id != null) {\r
-            String fixedIP = null;        // used for the fixedIP calculation\r
-            if (!portInterface.portExists(port_id))\r
-                return Response.status(404).build();\r
-            NeutronPort port = portInterface.getPort(port_id);\r
-            if (port.getFixedIPs().size() < 1)\r
-                return Response.status(400).build();\r
-            // if there is more than one fixed IP then check for fixed_ip_address\r
-            // and that it is in the list of port addresses\r
-            if (port.getFixedIPs().size() > 1) {\r
-                fixedIP = singleton.getFixedIPAddress();\r
-                if (fixedIP == null)\r
-                    return Response.status(400).build();\r
-                Iterator<Neutron_IPs> i = port.getFixedIPs().iterator();\r
-                boolean validFixedIP = false;\r
-                while (i.hasNext() && !validFixedIP) {\r
-                    Neutron_IPs ip = i.next();\r
-                    if (ip.getIpAddress().equals(fixedIP))\r
-                        validFixedIP = true;\r
-                }\r
-                if (!validFixedIP)\r
-                    return Response.status(400).build();\r
-            } else {\r
-                fixedIP = port.getFixedIPs().get(0).getIpAddress();\r
-                if (singleton.getFixedIPAddress() != null &&\r
-                        !fixedIP.equalsIgnoreCase(singleton.getFixedIPAddress()))\r
-                    return Response.status(400).build();\r
-            }\r
-            //lastly check that this fixed IP address isn't already used\r
-            if (port.isBoundToFloatingIP(fixedIP))\r
-                return Response.status(409).build();\r
-            singleton.setFixedIPAddress(fixedIP);\r
-        }\r
-        NeutronFloatingIP target = floatingIPInterface.getFloatingIP(floatingipUUID);\r
-        Object[] instances = ServiceHelper.getGlobalInstances(INeutronFloatingIPAware.class, this, null);\r
-        if (instances != null) {\r
-            for (Object instance : instances) {\r
-                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;\r
-                int status = service.canUpdateFloatingIP(singleton, target);\r
-                if (status < 200 || status > 299)\r
-                    return Response.status(status).build();\r
-            }\r
-        }\r
-        floatingIPInterface.updateFloatingIP(floatingipUUID, singleton);\r
-        target = floatingIPInterface.getFloatingIP(floatingipUUID);\r
-        if (instances != null) {\r
-            for (Object instance : instances) {\r
-                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;\r
-                service.neutronFloatingIPUpdated(target);\r
-            }\r
-        }\r
-        return Response.status(200).entity(\r
-                new NeutronFloatingIPRequest(target)).build();\r
-\r
-    }\r
-\r
-    /**\r
-     * Deletes a FloatingIP */\r
-\r
-    @Path("{floatingipUUID}")\r
-    @DELETE\r
-    @StatusCodes({\r
-            @ResponseCode(code = 204, condition = "No Content"),\r
-            @ResponseCode(code = 401, condition = "Unauthorized"),\r
-            @ResponseCode(code = 404, condition = "Not Found"),\r
-            @ResponseCode(code = 501, condition = "Not Implemented") })\r
-    public Response deleteFloatingIP(\r
-            @PathParam("floatingipUUID") String floatingipUUID) {\r
-        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);\r
-        if (floatingIPInterface == null) {\r
-            throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
-                    + RestMessages.SERVICEUNAVAILABLE.toString());\r
-        }\r
-        if (!floatingIPInterface.floatingIPExists(floatingipUUID))\r
-            return Response.status(404).build();\r
-        // TODO: need to undo port association if it exists\r
-        NeutronFloatingIP singleton = floatingIPInterface.getFloatingIP(floatingipUUID);\r
-        Object[] instances = ServiceHelper.getGlobalInstances(INeutronFloatingIPAware.class, this, null);\r
-        if (instances != null) {\r
-            for (Object instance : instances) {\r
-                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;\r
-                int status = service.canDeleteFloatingIP(singleton);\r
-                if (status < 200 || status > 299)\r
-                    return Response.status(status).build();\r
-            }\r
-        }\r
-        floatingIPInterface.removeFloatingIP(floatingipUUID);\r
-        if (instances != null) {\r
-            for (Object instance : instances) {\r
-                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;\r
-                service.neutronFloatingIPDeleted(singleton);\r
-            }\r
-        }\r
-        return Response.status(204).build();\r
-    }\r
-}\r
+/*
+ * Copyright IBM Corporation, 2013.  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.controller.networkconfig.neutron.northbound;
+
+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;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+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.controller.networkconfig.neutron.INeutronFloatingIPAware;
+import org.opendaylight.controller.networkconfig.neutron.INeutronFloatingIPCRUD;
+import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
+import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
+import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
+import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
+import org.opendaylight.controller.networkconfig.neutron.NeutronFloatingIP;
+import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
+import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
+import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
+import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
+import org.opendaylight.controller.northbound.commons.RestMessages;
+import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
+import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
+import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
+import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
+import org.opendaylight.controller.sal.utils.ServiceHelper;
+
+/**
+ * Neutron Northbound REST APIs.<br>
+ * This class provides REST APIs for managing Neutron Floating IPs
+ *
+ * <br>
+ * <br>
+ * Authentication scheme : <b>HTTP Basic</b><br>
+ * Authentication realm : <b>opendaylight</b><br>
+ * Transport : <b>HTTP and HTTPS</b><br>
+ * <br>
+ * HTTPS Authentication is disabled by default. Administrator can enable it in
+ * tomcat-server.xml after adding a proper keystore / SSL certificate from a
+ * trusted authority.<br>
+ * More info :
+ * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
+ *
+ */
+
+@Path("/floatingips")
+public class NeutronFloatingIPsNorthbound {
+
+    private NeutronFloatingIP extractFields(NeutronFloatingIP o, List<String> fields) {
+        return o.extractFields(fields);
+    }
+
+    /**
+     * Returns a list of all FloatingIPs */
+
+    @GET
+    @Produces({ MediaType.APPLICATION_JSON })
+    @StatusCodes({
+            @ResponseCode(code = 200, condition = "Operation successful"),
+            @ResponseCode(code = 401, condition = "Unauthorized"),
+            @ResponseCode(code = 501, condition = "Not Implemented") })
+    public Response listFloatingIPs(
+            // return fields
+            @QueryParam("fields") List<String> fields,
+            // note: openstack isn't clear about filtering on lists, so we aren't handling them
+            @QueryParam("id") String queryID,
+            @QueryParam("floating_network_id") String queryFloatingNetworkId,
+            @QueryParam("port_id") String queryPortId,
+            @QueryParam("fixed_ip_address") String queryFixedIPAddress,
+            @QueryParam("floating_ip_address") String queryFloatingIPAddress,
+            @QueryParam("tenant_id") String queryTenantID,
+            // pagination
+            @QueryParam("limit") String limit,
+            @QueryParam("marker") String marker,
+            @QueryParam("page_reverse") String pageReverse
+            // sorting not supported
+            ) {
+        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);
+        if (floatingIPInterface == null) {
+            throw new ServiceUnavailableException("Floating IP CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        List<NeutronFloatingIP> allFloatingIPs = floatingIPInterface.getAllFloatingIPs();
+        List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
+        Iterator<NeutronFloatingIP> i = allFloatingIPs.iterator();
+        while (i.hasNext()) {
+            NeutronFloatingIP oSS = i.next();
+            //match filters: TODO provider extension and router extension
+            if ((queryID == null || queryID.equals(oSS.getID())) &&
+                    (queryFloatingNetworkId == null || queryFloatingNetworkId.equals(oSS.getFloatingNetworkUUID())) &&
+                    (queryPortId == null || queryPortId.equals(oSS.getPortUUID())) &&
+                    (queryFixedIPAddress == null || queryFixedIPAddress.equals(oSS.getFixedIPAddress())) &&
+                    (queryFloatingIPAddress == null || queryFloatingIPAddress.equals(oSS.getFloatingIPAddress())) &&
+                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantUUID()))) {
+                if (fields.size() > 0)
+                    ans.add(extractFields(oSS,fields));
+                else
+                    ans.add(oSS);
+            }
+        }
+        //TODO: apply pagination to results
+        return Response.status(200).entity(
+                new NeutronFloatingIPRequest(ans)).build();
+    }
+
+    /**
+     * Returns a specific FloatingIP */
+
+    @Path("{floatingipUUID}")
+    @GET
+    @Produces({ MediaType.APPLICATION_JSON })
+    @StatusCodes({
+            @ResponseCode(code = 200, condition = "Operation successful"),
+            @ResponseCode(code = 401, condition = "Unauthorized"),
+            @ResponseCode(code = 404, condition = "Not Found"),
+            @ResponseCode(code = 501, condition = "Not Implemented") })
+    public Response showFloatingIP(
+            @PathParam("floatingipUUID") String floatingipUUID,
+            // return fields
+            @QueryParam("fields") List<String> fields ) {
+        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);
+        if (floatingIPInterface == null) {
+            throw new ServiceUnavailableException("Floating IP CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (!floatingIPInterface.floatingIPExists(floatingipUUID))
+            throw new ResourceNotFoundException("Floating IP UUID doesn't exist.");
+        if (fields.size() > 0) {
+            NeutronFloatingIP ans = floatingIPInterface.getFloatingIP(floatingipUUID);
+            return Response.status(200).entity(
+                    new NeutronFloatingIPRequest(extractFields(ans, fields))).build();
+        } else
+            return Response.status(200).entity(
+                    new NeutronFloatingIPRequest(floatingIPInterface.getFloatingIP(floatingipUUID))).build();
+
+    }
+
+    /**
+     * Creates new FloatingIPs */
+
+    @POST
+    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes({ MediaType.APPLICATION_JSON })
+    @StatusCodes({
+        @ResponseCode(code = 201, condition = "Created"),
+        @ResponseCode(code = 400, condition = "Bad Request"),
+        @ResponseCode(code = 401, condition = "Unauthorized"),
+        @ResponseCode(code = 409, condition = "Conflict"),
+        @ResponseCode(code = 501, condition = "Not Implemented") })
+    public Response createFloatingIPs(final NeutronFloatingIPRequest input) {
+        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);
+        if (floatingIPInterface == null) {
+            throw new ServiceUnavailableException("Floating IP CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
+        if (networkInterface == null) {
+            throw new ServiceUnavailableException("Network CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
+        if (subnetInterface == null) {
+            throw new ServiceUnavailableException("Subnet CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD( this);
+        if (portInterface == null) {
+            throw new ServiceUnavailableException("Port CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (input.isSingleton()) {
+            NeutronFloatingIP singleton = input.getSingleton();
+            // check existence of id in cache and return badrequest if exists
+            if (floatingIPInterface.floatingIPExists(singleton.getID()))
+                throw new BadRequestException("Floating IP UUID already exists.");
+            // check if the external network is specified, exists, and is an external network
+            String externalNetworkUUID = singleton.getFloatingNetworkUUID();
+            if (externalNetworkUUID == null)
+                throw new BadRequestException("external network UUID doesn't exist.");
+            if (!networkInterface.networkExists(externalNetworkUUID))
+                throw new BadRequestException("external network UUID doesn't exist.");
+            NeutronNetwork externNetwork = networkInterface.getNetwork(externalNetworkUUID);
+            if (!externNetwork.isRouterExternal())
+                throw new BadRequestException("external network isn't marked router:external");
+            // if floating IP is specified, make sure it can come from the network
+            String floatingIP = singleton.getFloatingIPAddress();
+            if (floatingIP != null) {
+                if (externNetwork.getSubnets().size() > 1)
+                    throw new BadRequestException("external network doesn't have a subnet");
+                NeutronSubnet externSubnet = subnetInterface.getSubnet(externNetwork.getSubnets().get(0));
+                if (!externSubnet.isValidIP(floatingIP))
+                    throw new BadRequestException("external IP isn't valid for the specified subnet.");
+                if (externSubnet.isIPInUse(floatingIP))
+                    throw new ResourceConflictException("floating IP is in use.");
+            }
+            // if port_id is specified, then check that the port exists and has at least one IP
+            String port_id = singleton.getPortUUID();
+            if (port_id != null) {
+                String fixedIP = null;        // used for the fixedIP calculation
+                if (!portInterface.portExists(port_id))
+                    throw new ResourceNotFoundException("Port UUID doesn't exist.");
+                NeutronPort port = portInterface.getPort(port_id);
+                if (port.getFixedIPs().size() < 1)
+                    throw new BadRequestException("port UUID doesn't have an IP address.");
+                // if there is more than one fixed IP then check for fixed_ip_address
+                // and that it is in the list of port addresses
+                if (port.getFixedIPs().size() > 1) {
+                    fixedIP = singleton.getFixedIPAddress();
+                    if (fixedIP == null)
+                        throw new BadRequestException("fixed IP address doesn't exist.");
+                    Iterator<Neutron_IPs> i = port.getFixedIPs().iterator();
+                    boolean validFixedIP = false;
+                    while (i.hasNext() && !validFixedIP) {
+                        Neutron_IPs ip = i.next();
+                        if (ip.getIpAddress().equals(fixedIP))
+                            validFixedIP = true;
+                    }
+                    if (!validFixedIP)
+                        throw new BadRequestException("can't find a valid fixed IP address");
+                } else {
+                    fixedIP = port.getFixedIPs().get(0).getIpAddress();
+                    if (singleton.getFixedIPAddress() != null && !fixedIP.equalsIgnoreCase(singleton.getFixedIPAddress()))
+                        throw new BadRequestException("mismatched fixed IP address in request");
+                }
+                //lastly check that this fixed IP address isn't already used
+                if (port.isBoundToFloatingIP(fixedIP))
+                    throw new ResourceConflictException("fixed IP is in use.");
+                singleton.setFixedIPAddress(fixedIP);
+            }
+            Object[] instances = ServiceHelper.getGlobalInstances(INeutronFloatingIPAware.class, this, null);
+            if (instances != null) {
+                for (Object instance : instances) {
+                    INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;
+                    int status = service.canCreateFloatingIP(singleton);
+                    if (status < 200 || status > 299)
+                        return Response.status(status).build();
+                }
+            }
+            floatingIPInterface.addFloatingIP(singleton);
+            if (instances != null) {
+                for (Object instance : instances) {
+                    INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;
+                    service.neutronFloatingIPCreated(singleton);
+                }
+            }
+        } else {
+            throw new BadRequestException("only singleton requests allowed.");
+        }
+        return Response.status(201).entity(input).build();
+    }
+
+    /**
+     * Updates a FloatingIP */
+
+    @Path("{floatingipUUID}")
+    @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 = 409, condition = "Conflict"),
+            @ResponseCode(code = 501, condition = "Not Implemented") })
+    public Response updateFloatingIP(
+            @PathParam("floatingipUUID") String floatingipUUID,
+            NeutronFloatingIPRequest input
+            ) {
+        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);
+        if (floatingIPInterface == null) {
+            throw new ServiceUnavailableException("Floating IP CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
+        if (networkInterface == null) {
+            throw new ServiceUnavailableException("Network CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
+        if (subnetInterface == null) {
+            throw new ServiceUnavailableException("Subnet CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD( this);
+        if (portInterface == null) {
+            throw new ServiceUnavailableException("Port CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (!floatingIPInterface.floatingIPExists(floatingipUUID))
+            throw new ResourceNotFoundException("Floating IP UUID doesn't exist.");
+
+        NeutronFloatingIP sourceFloatingIP = floatingIPInterface.getFloatingIP(floatingipUUID);
+        if (!input.isSingleton())
+            throw new BadRequestException("only singleton requests allowed.");
+        NeutronFloatingIP singleton = input.getSingleton();
+        if (singleton.getID() != null)
+            throw new BadRequestException("singleton UUID doesn't exist.");
+
+        NeutronNetwork externNetwork = networkInterface.getNetwork(
+                sourceFloatingIP.getFloatingNetworkUUID());
+
+        // if floating IP is specified, make sure it can come from the network
+        String floatingIP = singleton.getFloatingIPAddress();
+        if (floatingIP != null) {
+            if (externNetwork.getSubnets().size() > 1)
+                throw new BadRequestException("external network doesn't have a subnet.");
+            NeutronSubnet externSubnet = subnetInterface.getSubnet(externNetwork.getSubnets().get(0));
+            if (!externSubnet.isValidIP(floatingIP))
+                throw new BadRequestException("floating IP not valid for external subnet");
+            if (externSubnet.isIPInUse(floatingIP))
+                throw new ResourceConflictException("floating IP is in use.");
+        }
+
+        // if port_id is specified, then check that the port exists and has at least one IP
+        String port_id = singleton.getPortUUID();
+        if (port_id != null) {
+            String fixedIP = null;        // used for the fixedIP calculation
+            if (!portInterface.portExists(port_id))
+                throw new ResourceNotFoundException("Port UUID doesn't exist.");
+            NeutronPort port = portInterface.getPort(port_id);
+            if (port.getFixedIPs().size() < 1)
+                throw new BadRequestException("port ID doesn't have a fixed IP address.");
+            // if there is more than one fixed IP then check for fixed_ip_address
+            // and that it is in the list of port addresses
+            if (port.getFixedIPs().size() > 1) {
+                fixedIP = singleton.getFixedIPAddress();
+                if (fixedIP == null)
+                    throw new BadRequestException("request doesn't have a fixed IP address");
+                Iterator<Neutron_IPs> i = port.getFixedIPs().iterator();
+                boolean validFixedIP = false;
+                while (i.hasNext() && !validFixedIP) {
+                    Neutron_IPs ip = i.next();
+                    if (ip.getIpAddress().equals(fixedIP))
+                        validFixedIP = true;
+                }
+                if (!validFixedIP)
+                    throw new BadRequestException("couldn't find a valid fixed IP address");
+            } else {
+                fixedIP = port.getFixedIPs().get(0).getIpAddress();
+                if (singleton.getFixedIPAddress() != null &&
+                        !fixedIP.equalsIgnoreCase(singleton.getFixedIPAddress()))
+                    throw new BadRequestException("mismatch in fixed IP addresses");
+            }
+            //lastly check that this fixed IP address isn't already used
+            if (port.isBoundToFloatingIP(fixedIP))
+                throw new ResourceConflictException("fixed IP is in use.");
+            singleton.setFixedIPAddress(fixedIP);
+        }
+        NeutronFloatingIP target = floatingIPInterface.getFloatingIP(floatingipUUID);
+        Object[] instances = ServiceHelper.getGlobalInstances(INeutronFloatingIPAware.class, this, null);
+        if (instances != null) {
+            for (Object instance : instances) {
+                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;
+                int status = service.canUpdateFloatingIP(singleton, target);
+                if (status < 200 || status > 299)
+                    return Response.status(status).build();
+            }
+        }
+        floatingIPInterface.updateFloatingIP(floatingipUUID, singleton);
+        target = floatingIPInterface.getFloatingIP(floatingipUUID);
+        if (instances != null) {
+            for (Object instance : instances) {
+                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;
+                service.neutronFloatingIPUpdated(target);
+            }
+        }
+        return Response.status(200).entity(
+                new NeutronFloatingIPRequest(target)).build();
+
+    }
+
+    /**
+     * Deletes a FloatingIP */
+
+    @Path("{floatingipUUID}")
+    @DELETE
+    @StatusCodes({
+            @ResponseCode(code = 204, condition = "No Content"),
+            @ResponseCode(code = 401, condition = "Unauthorized"),
+            @ResponseCode(code = 404, condition = "Not Found"),
+            @ResponseCode(code = 501, condition = "Not Implemented") })
+    public Response deleteFloatingIP(
+            @PathParam("floatingipUUID") String floatingipUUID) {
+        INeutronFloatingIPCRUD floatingIPInterface = NeutronCRUDInterfaces.getINeutronFloatingIPCRUD(this);
+        if (floatingIPInterface == null) {
+            throw new ServiceUnavailableException("Floating IP CRUD Interface "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+        if (!floatingIPInterface.floatingIPExists(floatingipUUID))
+            throw new ResourceNotFoundException("Floating IP UUID doesn't exist.");
+        // TODO: need to undo port association if it exists
+        NeutronFloatingIP singleton = floatingIPInterface.getFloatingIP(floatingipUUID);
+        Object[] instances = ServiceHelper.getGlobalInstances(INeutronFloatingIPAware.class, this, null);
+        if (instances != null) {
+            for (Object instance : instances) {
+                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;
+                int status = service.canDeleteFloatingIP(singleton);
+                if (status < 200 || status > 299)
+                    return Response.status(status).build();
+            }
+        }
+        floatingIPInterface.removeFloatingIP(floatingipUUID);
+        if (instances != null) {
+            for (Object instance : instances) {
+                INeutronFloatingIPAware service = (INeutronFloatingIPAware) instance;
+                service.neutronFloatingIPDeleted(singleton);
+            }
+        }
+        return Response.status(204).build();
+    }
+}