Prince Pereira
Committed by Gerrit Code Review

Fix for ONOS-4834. Created rest api for removing the flows using appid, device id and flowid.

Change-Id: I7389fa017287f0daac299229914e7fbf9c60a91e
......@@ -301,6 +301,35 @@ public class FlowsWebResource extends AbstractWebResource {
}
/**
* Removes the specified flow rule based on application ID, device ID and flow ID.
*
* @param appId app identifier
* @param deviceId device identifier
* @param flowId flow rule identifier
* @return 204 NO CONTENT
*/
@DELETE
@Path("{appId}/{deviceId}/{flowId}")
public Response deleteFlowByAppIdDeviceIdAndFlowId(@PathParam("appId") String appId,
@PathParam("deviceId") String deviceId,
@PathParam("flowId") long flowId) {
final ApplicationService appService = get(ApplicationService.class);
final ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
final Iterable<FlowEntry> flowEntries =
service.getFlowEntries(DeviceId.deviceId(deviceId));
if (!flowEntries.iterator().hasNext()) {
throw new ItemNotFoundException(DEVICE_NOT_FOUND);
}
StreamSupport.stream(flowEntries.spliterator(), false)
.filter(entry -> (entry.id().value() == flowId) && (entry.appId() == idInstant.id()))
.forEach(service::removeFlowRules);
return Response.noContent().build();
}
/**
* Removes a batch of flow rules.
*
* @param stream stream for posted JSON
......@@ -343,4 +372,52 @@ public class FlowsWebResource extends AbstractWebResource {
service.removeFlowRules(rulesToRemove.toArray(new FlowEntry[0]));
return Response.noContent().build();
}
/**
* Removes collection of flow rules generated by the given application.
*
* @param appId app identifier
* @param stream stream for posted JSON
* @return 204 NO CONTENT
*/
@DELETE
@Path("{appId}")
public Response deleteFlowsByAppId(@PathParam("appId") String appId, InputStream stream) {
final ApplicationService appService = get(ApplicationService.class);
final ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
ListMultimap<DeviceId, Long> deviceMap = ArrayListMultimap.create();
List<FlowEntry> rulesToRemove = new ArrayList<>();
try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
JsonNode jsonFlows = jsonTree.get("flows");
jsonFlows.forEach(node -> {
DeviceId deviceId =
DeviceId.deviceId(
nullIsNotFound(node.get(DEVICE_ID),
DEVICE_NOT_FOUND).asText());
long flowId = nullIsNotFound(node.get(FLOW_ID),
FLOW_NOT_FOUND).asLong();
deviceMap.put(deviceId, flowId);
});
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
deviceMap.keySet().forEach(deviceId -> {
List<Long> flowIds = deviceMap.get(deviceId);
Iterable<FlowEntry> entries = service.getFlowEntries(deviceId);
flowIds.forEach(flowId -> {
StreamSupport.stream(entries.spliterator(), false)
.filter(entry -> (entry.id().value() == flowId) && (entry.appId() == idInstant.id()))
.forEach(rulesToRemove::add);
});
});
service.removeFlowRules(rulesToRemove.toArray(new FlowEntry[0]));
return Response.noContent().build();
}
}
......
......@@ -63,6 +63,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import static org.easymock.EasyMock.anyObject;
......@@ -182,7 +183,7 @@ public class FlowsResourceTest extends ResourceTest {
@Override
public short appId() {
return 2;
return (short) Objects.hash("foo");
}
@Override
......@@ -961,4 +962,28 @@ public class FlowsResourceTest extends ResourceTest {
assertThat(deleteResponse.getStatus(),
is(HttpURLConnection.HTTP_NO_CONTENT));
}
/**
* Tests the result of a rest api DELETE for application Id, device id and flow id.
*/
@Test
public void testRemoveFlowByAppIdDeviceIdAndFlowId() {
setupMockFlows();
expect(mockApplicationService.getId(anyObject())).andReturn(APP_ID).anyTimes();
replay(mockApplicationService);
mockFlowService.removeFlowRules(anyObject());
expectLastCall();
replay(mockFlowService);
WebTarget wt = target();
String location = "/flows/4/1/155";
Response deleteResponse = wt.path(location)
.request(MediaType.APPLICATION_JSON_TYPE)
.delete();
assertThat(deleteResponse.getStatus(),
is(HttpURLConnection.HTTP_NO_CONTENT));
}
}
......