- * the {@link SubnetConfig} structure in request body
- * parameter
- * @return If the operation is successful or not
- *
- * <pre>
- * Example:
- *
- * Request URL: http://localhost:8080/controller/nb/v2/subnet/default/subnet/subnet1/node-ports
- *
- * Request in XML:
- * <subnetConfig>
- * <name>subnet1</name>
- * <subnet>30.0.0.1/24</subnet>
- * <nodePorts>1/1</nodePorts>
- * <nodePorts>1/2</nodePorts>
- * <nodePorts>1/3</nodePorts>
- * </subnetConfig>
- *
- * Request in JSON:
- * {
- * "name":"subnet1",
- * "subnet":"30.0.0.1/24",
- * "nodePorts":["1/1","1/2","1/3"]
- * }
- * </pre>
- */
- @Path("/{containerName}/subnet/{subnetName}/node-ports")
- @PUT
- @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
- @StatusCodes({
- @ResponseCode(code = 200, condition = "Ports replaced successfully"),
- @ResponseCode(code = 400, condition = "Invalid request to change subnet name or invalid node ports passed"),
- @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
- @ResponseCode(code = 409, condition = "Subnet name in url conflicts with name in request body"),
- @ResponseCode(code = 404, condition = "The containerName or subnetName is not found"),
- @ResponseCode(code = 500, condition = "Internal server error: Modify ports failed"),
- @ResponseCode(code = 503, condition = "Service unavailable") })
- public Response modifySubnet(@PathParam("containerName") String containerName,
- @PathParam("subnetName") String subnetName,
- @TypeHint(SubnetConfig.class) JAXBElement<SubnetConfig> subnetConfigData) {
-
- handleContainerDoesNotExist(containerName);
-
- if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
- throw new UnauthorizedException("User is not authorized to perform this operation on container "
- + containerName);
- }
- handleNameMismatch(subnetConfigData.getValue().getName(), subnetName);
-
- ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
- this);
- if (switchManager == null) {
- throw new ServiceUnavailableException("SwitchManager " + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- SubnetConfig subnetConf = subnetConfigData.getValue();
- SubnetConfig existingConf = switchManager.getSubnetConfig(subnetName);
-
- boolean successful = true;
-
- // 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
- Response.status(Response.Status.BAD_REQUEST).build();
- } else {
- // create a set for fast lookups
- Set<String> newPorts = new HashSet<String>(subnetConf.getNodePorts());
-
- // go through the current ports and (1) remove ports that aren't
- // there anymore and (2) remove ports that are still there from the
- // set of ports to add
- for (String s : existingConf.getNodePorts()) {
- if (newPorts.contains(s)) {
- newPorts.remove(s);
- } else {
- Status st = switchManager.removePortsFromSubnet(subnetName, s);
- successful = successful && st.isSuccess();
- }
- }
-
- // add any remaining ports
- for (String s : newPorts) {
- Status st = switchManager.addPortsToSubnet(subnetName, s);
- successful = successful && st.isSuccess();
- if (successful) {
- NorthboundUtils.auditlog("Subnet Gateway", username, "added", s + " to " + subnetName,
- containerName);
- }
- }
- }
-
- if (successful) {
- return Response.status(Response.Status.OK).build();
- }
- throw new InternalServerErrorException(RestMessages.INTERNALERROR.toString());
- }
-
- /**
- * Add ports to a subnet in the container
- *
- * @param containerName
- * name of the container that has the subnet to which node ports
- * need to be added
- * @param subnetName
- * name of subnet to which node ports need to be added
- * @param SubnetConfig
- * the {@link SubnetConfig} structure in request body