use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class PolatisOpticalUtility method toFlowRule.
/**
* Finds the FlowRule from flow rule store by the given ports and channel.
* Returns an extra flow to remove the flow by ONOS if not found.
* @param behaviour the parent driver handler
* @param inPort the input port
* @param outPort the output port
* @return the flow rule
*/
public static FlowRule toFlowRule(HandlerBehaviour behaviour, PortNumber inPort, PortNumber outPort) {
FlowRuleService service = behaviour.handler().get(FlowRuleService.class);
Iterable<FlowEntry> entries = service.getFlowEntries(behaviour.data().deviceId());
// Try to Find the flow from flow rule store.
for (FlowEntry entry : entries) {
Set<Criterion> criterions = entry.selector().criteria();
// input port
PortNumber ip = criterions.stream().filter(c -> c instanceof PortCriterion).map(c -> ((PortCriterion) c).port()).findAny().orElse(null);
// output port
PortNumber op = entry.treatment().immediate().stream().filter(c -> c instanceof Instructions.OutputInstruction).map(c -> ((Instructions.OutputInstruction) c).port()).findAny().orElse(null);
if (inPort.equals(ip) && outPort.equals(op)) {
// Find the flow.
return entry;
}
}
// Cannot find the flow from store. So report an extra flow to remove the flow by ONOS.
TrafficSelector selector = DefaultTrafficSelector.builder().matchInPort(inPort).build();
TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(outPort).build();
return DefaultFlowRule.builder().forDevice(behaviour.data().deviceId()).withSelector(selector).withTreatment(treatment).makePermanent().withPriority(DEFAULT_PRIORITY).fromApp(behaviour.handler().get(CoreService.class).getAppId(DEFAULT_APP)).build();
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class ServerHandshaker method probeReachability.
@Override
public CompletableFuture<Boolean> probeReachability() {
// Retrieve the device ID from the handler
DeviceId deviceId = super.getDeviceId();
checkNotNull(deviceId, MSG_DEVICE_ID_NULL);
// Probe the driver to ask for flow rule service
FlowRuleService flowService = getHandler().get(FlowRuleService.class);
List<TableStatisticsEntry> tableStats = Lists.newArrayList(flowService.getFlowTableStatistics(deviceId));
// If no statistics fetched, the server is not reachable
return completedFuture(tableStats.isEmpty() ? false : true);
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowsWebResource method getFlows.
/**
* Gets all flow entries. Returns array of all flow rules in the system.
*
* @return 200 OK with a collection of flows
* @onos.rsModel FlowEntries
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFlows() {
ObjectNode root = mapper().createObjectNode();
ArrayNode flowsNode = root.putArray(FLOWS);
FlowRuleService service = get(FlowRuleService.class);
Iterable<Device> devices = get(DeviceService.class).getDevices();
for (Device device : devices) {
Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id());
if (flowEntries != null) {
for (FlowEntry entry : flowEntries) {
flowsNode.add(codec(FlowEntry.class).encode(entry, this));
}
}
}
return ok(root).build();
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowsWebResource method createFlows.
/**
* Creates new flow rules. Creates and installs a new flow rules.<br>
* Flow rule criteria and instruction description:
* https://wiki.onosproject.org/display/ONOS/Flow+Rules
*
* @param appId application id
* @param stream flow rules JSON
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
* @onos.rsModel FlowsBatchPost
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createFlows(@QueryParam("appId") String appId, InputStream stream) {
FlowRuleService service = get(FlowRuleService.class);
ObjectNode root = mapper().createObjectNode();
ArrayNode flowsNode = root.putArray(FLOWS);
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
ArrayNode flowsArray = nullIsIllegal((ArrayNode) jsonTree.get(FLOWS), FLOW_ARRAY_REQUIRED);
if (appId != null) {
flowsArray.forEach(flowJson -> ((ObjectNode) flowJson).put("appId", appId));
}
List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
rules.forEach(flowRule -> {
ObjectNode flowNode = mapper().createObjectNode();
flowNode.put(DEVICE_ID, flowRule.deviceId().toString()).put(FLOW_ID, Long.toString(flowRule.id().value()));
flowsNode.add(flowNode);
});
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
return Response.ok(root).build();
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowsWebResource method deleteFlows.
/**
* Removes a batch of flow rules.
*
* @param stream stream for posted JSON
* @return 204 NO CONTENT
*/
@DELETE
public Response deleteFlows(InputStream stream) {
FlowRuleService service = get(FlowRuleService.class);
ListMultimap<DeviceId, Long> deviceMap = ArrayListMultimap.create();
List<FlowEntry> rulesToRemove = new ArrayList<>();
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), 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 -> flowId == entry.id().value()).forEach(rulesToRemove::add);
});
});
service.removeFlowRules(rulesToRemove.toArray(new FlowEntry[0]));
return Response.noContent().build();
}
Aggregations