--- /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
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\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.INeutronNetworkAware;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetAware;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;\r
+import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;\r
+import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;\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 open DOVE internals related to Subnets\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("/subnets")\r
+public class NeutronSubnetsNorthbound {\r
+\r
+ private NeutronSubnet extractFields(NeutronSubnet o, List<String> fields) {\r
+ return o.extractFields(fields);\r
+ }\r
+\r
+\r
+ /**\r
+ * Returns a list of all Subnets */\r
+ @GET\r
+ @Produces({ MediaType.APPLICATION_JSON })\r
+ //@TypeHint(OpenStackSubnets.class)\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 listSubnets(\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("network_id") String queryNetworkID,\r
+ @QueryParam("name") String queryName,\r
+ @QueryParam("ip_version") String queryIPVersion,\r
+ @QueryParam("cidr") String queryCIDR,\r
+ @QueryParam("gateway_ip") String queryGatewayIP,\r
+ @QueryParam("enable_dhcp") String queryEnableDHCP,\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
+ INeutronSubnetCRUD subnetInterface = NeutronNBInterfaces.getIfNBSubnetCRUD("default", this);\r
+ if (subnetInterface == null) {\r
+ throw new ServiceUnavailableException("Subnet CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ List<NeutronSubnet> allNetworks = subnetInterface.getAllSubnets();\r
+ List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();\r
+ Iterator<NeutronSubnet> i = allNetworks.iterator();\r
+ while (i.hasNext()) {\r
+ NeutronSubnet oSS = i.next();\r
+ if ((queryID == null || queryID.equals(oSS.getID())) &&\r
+ (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&\r
+ (queryName == null || queryName.equals(oSS.getName())) &&\r
+ (queryIPVersion == null || queryIPVersion.equals(oSS.getIpVersion())) &&\r
+ (queryCIDR == null || queryCIDR.equals(oSS.getCidr())) &&\r
+ (queryGatewayIP == null || queryGatewayIP.equals(oSS.getGatewayIP())) &&\r
+ (queryEnableDHCP == null || queryEnableDHCP.equals(oSS.getEnableDHCP())) &&\r
+ (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {\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 NeutronSubnetRequest(ans)).build();\r
+ }\r
+\r
+ /**\r
+ * Returns a specific Subnet */\r
+\r
+ @Path("{subnetUUID}")\r
+ @GET\r
+ @Produces({ MediaType.APPLICATION_JSON })\r
+ //@TypeHint(OpenStackSubnets.class)\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 showSubnet(\r
+ @PathParam("subnetUUID") String subnetUUID,\r
+ // return fields\r
+ @QueryParam("fields") List<String> fields) {\r
+ INeutronSubnetCRUD subnetInterface = NeutronNBInterfaces.getIfNBSubnetCRUD("default",this);\r
+ if (subnetInterface == null) {\r
+ throw new ServiceUnavailableException("Subnet CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+ if (!subnetInterface.subnetExists(subnetUUID))\r
+ return Response.status(404).build();\r
+ if (fields.size() > 0) {\r
+ NeutronSubnet ans = subnetInterface.getSubnet(subnetUUID);\r
+ return Response.status(200).entity(\r
+ new NeutronSubnetRequest(extractFields(ans, fields))).build();\r
+ } else\r
+ return Response.status(200).entity(\r
+ new NeutronSubnetRequest(subnetInterface.getSubnet(subnetUUID))).build();\r
+ }\r
+\r
+ /**\r
+ * Creates new Subnets */\r
+\r
+ @POST\r
+ @Produces({ MediaType.APPLICATION_JSON })\r
+ @Consumes({ MediaType.APPLICATION_JSON })\r
+ //@TypeHint(OpenStackSubnets.class)\r
+ @StatusCodes({\r
+ @ResponseCode(code = 201, condition = "Created"),\r
+ @ResponseCode(code = 400, condition = "Bad Request"),\r
+ @ResponseCode(code = 401, condition = "Unauthorized"),\r
+ @ResponseCode(code = 403, condition = "Forbidden"),\r
+ @ResponseCode(code = 404, condition = "Not Found"),\r
+ @ResponseCode(code = 409, condition = "Conflict"),\r
+ @ResponseCode(code = 501, condition = "Not Implemented") })\r
+ public Response createSubnets(final NeutronSubnetRequest input) {\r
+ INeutronSubnetCRUD subnetInterface = NeutronNBInterfaces.getIfNBSubnetCRUD("default",this);\r
+ if (subnetInterface == null) {\r
+ throw new ServiceUnavailableException("Subnet 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
+ if (input.isSingleton()) {\r
+ NeutronSubnet singleton = input.getSingleton();\r
+\r
+ /*\r
+ * Verify that the subnet doesn't already exist (Issue: is a deeper check necessary?)\r
+ * the specified network exists, the subnet has a valid network address,\r
+ * and that the gateway IP doesn't overlap with the allocation pools\r
+ * *then* add the subnet to the cache\r
+ */\r
+ if (subnetInterface.subnetExists(singleton.getID()))\r
+ return Response.status(400).build();\r
+ if (!networkInterface.networkExists(singleton.getNetworkUUID()))\r
+ return Response.status(404).build();\r
+ if (!singleton.isValidCIDR())\r
+ return Response.status(400).build();\r
+ singleton.initDefaults();\r
+ if (singleton.gatewayIP_Pool_overlap())\r
+ return Response.status(409).build();\r
+ Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ int status = service.canCreateSubnet(singleton);\r
+ if (status < 200 || status > 299)\r
+ return Response.status(status).build();\r
+ }\r
+ }\r
+ subnetInterface.addSubnet(singleton);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ service.neutronSubnetCreated(singleton);\r
+ }\r
+ }\r
+ } else {\r
+ List<NeutronSubnet> bulk = input.getBulk();\r
+ Iterator<NeutronSubnet> i = bulk.iterator();\r
+ HashMap<String, NeutronSubnet> testMap = new HashMap<String, NeutronSubnet>();\r
+ Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
+ while (i.hasNext()) {\r
+ NeutronSubnet test = i.next();\r
+\r
+ /*\r
+ * Verify that the subnet doesn't already exist (Issue: is a deeper check necessary?)\r
+ * the specified network exists, the subnet has a valid network address,\r
+ * and that the gateway IP doesn't overlap with the allocation pools,\r
+ * and that the bulk request doesn't already contain a subnet with this id\r
+ */\r
+\r
+ test.initDefaults();\r
+ if (subnetInterface.subnetExists(test.getID()))\r
+ return Response.status(400).build();\r
+ if (testMap.containsKey(test.getID()))\r
+ return Response.status(400).build();\r
+ testMap.put(test.getID(), test);\r
+ if (!networkInterface.networkExists(test.getNetworkUUID()))\r
+ return Response.status(404).build();\r
+ if (!test.isValidCIDR())\r
+ return Response.status(400).build();\r
+ if (test.gatewayIP_Pool_overlap())\r
+ return Response.status(409).build();\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ int status = service.canCreateSubnet(test);\r
+ if (status < 200 || status > 299)\r
+ return Response.status(status).build();\r
+ }\r
+ }\r
+ }\r
+\r
+ /*\r
+ * now, each element of the bulk request can be added to the cache\r
+ */\r
+ i = bulk.iterator();\r
+ while (i.hasNext()) {\r
+ NeutronSubnet test = i.next();\r
+ subnetInterface.addSubnet(test);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ service.neutronSubnetCreated(test);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ return Response.status(201).entity(input).build();\r
+ }\r
+\r
+ /**\r
+ * Updates a Subnet */\r
+\r
+ @Path("{subnetUUID}")\r
+ @PUT\r
+ @Produces({ MediaType.APPLICATION_JSON })\r
+ @Consumes({ MediaType.APPLICATION_JSON })\r
+ //@TypeHint(OpenStackSubnets.class)\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 = 403, condition = "Forbidden"),\r
+ @ResponseCode(code = 404, condition = "Not Found"),\r
+ @ResponseCode(code = 501, condition = "Not Implemented") })\r
+ public Response updateSubnet(\r
+ @PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input\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
+\r
+ /*\r
+ * verify the subnet exists and there is only one delta provided\r
+ */\r
+ if (!subnetInterface.subnetExists(subnetUUID))\r
+ return Response.status(404).build();\r
+ if (!input.isSingleton())\r
+ return Response.status(400).build();\r
+ NeutronSubnet delta = input.getSingleton();\r
+ NeutronSubnet original = subnetInterface.getSubnet(subnetUUID);\r
+\r
+ /*\r
+ * updates restricted by Neutron\r
+ */\r
+ if (delta.getID() != null || delta.getTenantID() != null ||\r
+ delta.getIpVersion() != null || delta.getCidr() != null ||\r
+ delta.getAllocationPools() != null)\r
+ return Response.status(400).build();\r
+\r
+ Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ int status = service.canUpdateSubnet(delta, original);\r
+ if (status < 200 || status > 299)\r
+ return Response.status(status).build();\r
+ }\r
+ }\r
+\r
+ /*\r
+ * update the object and return it\r
+ */\r
+ subnetInterface.updateSubnet(subnetUUID, delta);\r
+ NeutronSubnet updatedSubnet = subnetInterface.getSubnet(subnetUUID);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ service.neutronSubnetUpdated(updatedSubnet);\r
+ }\r
+ }\r
+ return Response.status(200).entity(\r
+ new NeutronSubnetRequest(subnetInterface.getSubnet(subnetUUID))).build();\r
+ }\r
+\r
+ /**\r
+ * Deletes a Subnet */\r
+\r
+ @Path("{subnetUUID}")\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 = 409, condition = "Conflict"),\r
+ @ResponseCode(code = 501, condition = "Not Implemented") })\r
+ public Response deleteSubnet(\r
+ @PathParam("subnetUUID") String subnetUUID) {\r
+ INeutronSubnetCRUD subnetInterface = NeutronNBInterfaces.getIfNBSubnetCRUD("default", this);\r
+ if (subnetInterface == null) {\r
+ throw new ServiceUnavailableException("Network CRUD Interface "\r
+ + RestMessages.SERVICEUNAVAILABLE.toString());\r
+ }\r
+\r
+ /*\r
+ * verify the subnet exists and it isn't currently in use\r
+ */\r
+ if (!subnetInterface.subnetExists(subnetUUID))\r
+ return Response.status(404).build();\r
+ if (subnetInterface.subnetInUse(subnetUUID))\r
+ return Response.status(409).build();\r
+ NeutronSubnet singleton = subnetInterface.getSubnet(subnetUUID);\r
+ Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ int status = service.canDeleteSubnet(singleton);\r
+ if (status < 200 || status > 299)\r
+ return Response.status(status).build();\r
+ }\r
+ }\r
+\r
+ /*\r
+ * remove it and return 204 status\r
+ */\r
+ subnetInterface.removeSubnet(subnetUUID);\r
+ if (instances != null) {\r
+ for (Object instance : instances) {\r
+ INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
+ service.neutronSubnetDeleted(singleton);\r
+ }\r
+ }\r
+ return Response.status(204).build();\r
+ }\r
+}\r