+ @Path("/{containerName}/{subnetName}/delete")
+ @POST
+ @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @StatusCodes({
+ @ResponseCode(code = 202, condition = "Operation successful"),
+ @ResponseCode(code = 400, condition = "Invalid request"),
+ @ResponseCode(code = 404, condition = "The containerName or subnetName is not found"),
+ @ResponseCode(code = 500, condition = "Internal server error") })
+ public Response deleteNodePorts(
+ @PathParam("containerName") String containerName,
+ @PathParam("subnetName") String name,
+ @TypeHint(SubnetConfig.class) JAXBElement<SubnetConfig> subnetConfigData) {
+
+ SubnetConfig subnetConf = subnetConfigData.getValue();
+ return addOrDeletePorts(containerName, name, subnetConf, "delete");
+ }
+
+ /**
+ *
+ * Add/Delete ports to/from a subnet
+ *
+ * @param containerName
+ * Name of the Container
+ * @param name
+ * Name of the SubnetConfig to be modified
+ * @param subnetConfig
+ * the {@link SubnetConfig} structure
+ * @param action
+ * add or delete
+ * @return If the operation is successful or not
+ */
+ private Response addOrDeletePorts(String containerName, String name, SubnetConfig subnetConf, String action) {
+
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
+ this);
+ if (switchManager == null) {
+ throw new ResourceNotFoundException(RestMessages.NOCONTAINER.toString());
+ }
+
+ SubnetConfig existingConf = switchManager.getSubnetConfig(name);
+
+ // make sure that the name matches an existing subnet and we're not
+ // changing the name or subnet IP/mask
+ if (existingConf == null) {
+ // don't have a subnet by that name
+ return Response.status(Response.Status.NOT_FOUND).build();
+ } else if (!existingConf.getName().equals(subnetConf.getName())
+ || !existingConf.getSubnet().equals(subnetConf.getSubnet())) {
+ // can't change the name of a subnet
+ return Response.status(Response.Status.BAD_REQUEST).build();
+ } else {
+ Status st;
+ boolean successful = true;
+ List<String> ports = subnetConf.getNodePorts();
+
+ if (action.equals("add")) {
+ // add new ports
+ ports.removeAll(existingConf.getNodePorts());
+ for (String port : ports) {
+ st = switchManager.addPortsToSubnet(name, port);
+ successful = successful && st.isSuccess();
+ }
+ } else if (action.equals("delete")) {
+ // delete existing ports
+ ports.retainAll(existingConf.getNodePorts());
+ for (String port : ports) {
+ st = switchManager.removePortsFromSubnet(name, port);
+ successful = successful && st.isSuccess();
+ }
+ } else {
+ return Response.status(Response.Status.BAD_REQUEST).build();
+ }
+
+ if (successful) {
+ return Response.status(Response.Status.ACCEPTED).build();
+ } else {
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
+ }
+ }
+ }