- if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
- throw new UnauthorizedException("User is not authorized to perform this operation on container "
- + containerName);
- }
- handleNameMismatch(subnetConfigData.getName(), subnetName);
-
- SubnetConfig subnetConf = subnetConfigData;
-
- ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
- this);
- if (switchManager == null) {
- throw new ServiceUnavailableException("SwitchManager " + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- SubnetConfig existingConf = switchManager.getSubnetConfig(subnetName);
-
- // 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();
- }
- Status st;
- boolean successful = true;
- Set<String> ports = subnetConf.getNodePorts();
-
- if (ports == null || ports.isEmpty()) {
- throw new BadRequestException(RestMessages.INVALIDDATA.toString());
- }
-
- // add new ports only
- if (existingConf.getNodePorts() != null) {
- ports.removeAll(existingConf.getNodePorts());
- }
- for (String port : ports) {
- st = switchManager.addPortsToSubnet(subnetName, port);
- successful = successful && st.isSuccess();
- if (successful) {
- NorthboundUtils.auditlog("Subnet Gateway", username, "added", st + " to " + subnetName, containerName);
- }
- }
- if (successful) {
- return Response.status(Response.Status.OK).build();
- }
- throw new InternalServerErrorException(RestMessages.INTERNALERROR.toString());
- }
-
- /**
- * Delete ports from a subnet in the container
- *
- * @param containerName
- * name of the container that has the subnet from which node
- * ports need to be deleted
- * @param subnetName
- * name of subnet from which node ports need to be deleted
- * @param subnetConfigData
- * SubnetConfig object to be deleted
- * @return Response as dictated by the HTTP Response Status code
- *
- * <pre>
- * Example:
- * Request URL: http://localhost:8080/controller/nb/v2/subnetservice/default/subnet/subnet1/nodePorts
- *
- * Request XML:
- * <subnetConfig>
- * <name>subnet3</name>
- * <subnet>30.0.0.1/24</subnet>
- * <nodePorts>1/1,1/2,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}/nodePorts")
- @DELETE
- @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
- @StatusCodes({
- @ResponseCode(code = 204, condition = "No content"),
- @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 = 404, condition = "The containerName or subnet is not found"),
- @ResponseCode(code = 409, condition = "Subnet name in url conflicts with name in request body"),
- @ResponseCode(code = 500, condition = "Internal server error : Delete node ports failed"),
- @ResponseCode(code = 503, condition = "Service unavailable") })
- public Response deleteNodePorts(@PathParam("containerName") String containerName,
- @PathParam("subnetName") String subnetName,
- @TypeHint(SubnetConfig.class) 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.getName(), subnetName);
-
- SubnetConfig subnetConf = subnetConfigData;
-
- if (subnetConf.getNodePorts() == null || subnetConf.getNodePorts().isEmpty()) {
- throw new BadRequestException(RestMessages.INVALIDDATA.toString() + " : invalid node ports");
- }
-
- ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
- this);
- if (switchManager == null) {
- throw new ServiceUnavailableException("SwitchManager " + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- SubnetConfig existingConf = switchManager.getSubnetConfig(subnetName);
-
- // 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();
- }
- Status st;
- boolean successful = true;
- Set<String> ports = subnetConf.getNodePorts();
-
- // delete existing ports
- ports.retainAll(existingConf.getNodePorts());
- for (String port : ports) {
- st = switchManager.removePortsFromSubnet(subnetName, port);
- successful = successful && st.isSuccess();
- if (successful) {
- NorthboundUtils.auditlog("Subnet Gateway", username, "removed", st + " from " + subnetName,
- containerName);