--- /dev/null
+/*\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.INeutronPortAware;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;\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 = NeutronNBInterfaces.getIfNBFloatingIPCRUD("default",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 = NeutronNBInterfaces.getIfNBFloatingIPCRUD("default",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 = NeutronNBInterfaces.getIfNBFloatingIPCRUD("default",this);\r
+ if (floatingIPInterface == null) {\r
+ throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ INeutronNetworkCRUD networkInterface = NeutronNBInterfaces.getIfNBNetworkCRUD("default", this);\r
+ if (networkInterface == null) {\r
+ throw new ServiceUnavailableException("Network CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ INeutronSubnetCRUD subnetInterface = NeutronNBInterfaces.getIfNBSubnetCRUD("default", this);\r
+ if (subnetInterface == null) {\r
+ throw new ServiceUnavailableException("Subnet CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ INeutronPortCRUD portInterface = NeutronNBInterfaces.getIfNBPortCRUD("default", 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 = NeutronNBInterfaces.getIfNBFloatingIPCRUD("default",this);\r
+ if (floatingIPInterface == null) {\r
+ throw new ServiceUnavailableException("Floating IP CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ INeutronNetworkCRUD networkInterface = NeutronNBInterfaces.getIfNBNetworkCRUD("default", this);\r
+ if (networkInterface == null) {\r
+ throw new ServiceUnavailableException("Network CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ INeutronSubnetCRUD subnetInterface = NeutronNBInterfaces.getIfNBSubnetCRUD("default", this);\r
+ if (subnetInterface == null) {\r
+ throw new ServiceUnavailableException("Subnet CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ INeutronPortCRUD portInterface = NeutronNBInterfaces.getIfNBPortCRUD("default", 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 = NeutronNBInterfaces.getIfNBFloatingIPCRUD("default",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