Search in sources :

Example 31 with FlowRuleService

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();
}
Also used : TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Instructions(org.onosproject.net.flow.instructions.Instructions) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) CoreService(org.onosproject.core.CoreService) Instruction(org.onosproject.net.flow.instructions.Instruction) Range(com.google.common.collect.Range) PortNumber(org.onosproject.net.PortNumber) FlowEntry(org.onosproject.net.flow.FlowEntry) Set(java.util.Set) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) HandlerBehaviour(org.onosproject.net.driver.HandlerBehaviour) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) List(java.util.List) FlowRuleService(org.onosproject.net.flow.FlowRuleService) TrafficSelector(org.onosproject.net.flow.TrafficSelector) FlowRule(org.onosproject.net.flow.FlowRule) VariableBinding(org.snmp4j.smi.VariableBinding) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) Criterion(org.onosproject.net.flow.criteria.Criterion) Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) CoreService(org.onosproject.core.CoreService) Instructions(org.onosproject.net.flow.instructions.Instructions) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) Criterion(org.onosproject.net.flow.criteria.Criterion) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) FlowRuleService(org.onosproject.net.flow.FlowRuleService) PortNumber(org.onosproject.net.PortNumber) FlowEntry(org.onosproject.net.flow.FlowEntry)

Example 32 with FlowRuleService

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);
}
Also used : DeviceId(org.onosproject.net.DeviceId) FlowRuleService(org.onosproject.net.flow.FlowRuleService) TableStatisticsEntry(org.onosproject.net.flow.TableStatisticsEntry)

Example 33 with FlowRuleService

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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) FlowRuleService(org.onosproject.net.flow.FlowRuleService) FlowEntry(org.onosproject.net.flow.FlowEntry) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 34 with FlowRuleService

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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) FlowRule(org.onosproject.net.flow.FlowRule) IOException(java.io.IOException) FlowRuleService(org.onosproject.net.flow.FlowRuleService) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 35 with FlowRuleService

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();
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) PathParam(javax.ws.rs.PathParam) AbstractWebResource(org.onosproject.rest.AbstractWebResource) Produces(javax.ws.rs.Produces) ListMultimap(com.google.common.collect.ListMultimap) GET(javax.ws.rs.GET) DeviceService(org.onosproject.net.device.DeviceService) Path(javax.ws.rs.Path) FlowEntry(org.onosproject.net.flow.FlowEntry) ApplicationService(org.onosproject.app.ApplicationService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) FlowRuleService(org.onosproject.net.flow.FlowRuleService) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) ApplicationId(org.onosproject.core.ApplicationId) JsonNode(com.fasterxml.jackson.databind.JsonNode) UriBuilder(javax.ws.rs.core.UriBuilder) StreamSupport(java.util.stream.StreamSupport) Tools.nullIsIllegal(org.onlab.util.Tools.nullIsIllegal) DELETE(javax.ws.rs.DELETE) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) Tools.nullIsNotFound(org.onlab.util.Tools.nullIsNotFound) Device(org.onosproject.net.Device) IndexTableId(org.onosproject.net.flow.IndexTableId) IOException(java.io.IOException) ItemNotFoundException(org.onlab.util.ItemNotFoundException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) Response(javax.ws.rs.core.Response) FlowRule(org.onosproject.net.flow.FlowRule) UriInfo(javax.ws.rs.core.UriInfo) DeviceId(org.onosproject.net.DeviceId) Tools.readTreeFromStream(org.onlab.util.Tools.readTreeFromStream) InputStream(java.io.InputStream) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) FlowRuleService(org.onosproject.net.flow.FlowRuleService) FlowEntry(org.onosproject.net.flow.FlowEntry) DELETE(javax.ws.rs.DELETE)

Aggregations

FlowRuleService (org.onosproject.net.flow.FlowRuleService)51 FlowEntry (org.onosproject.net.flow.FlowEntry)23 CoreService (org.onosproject.core.CoreService)18 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)16 DeviceService (org.onosproject.net.device.DeviceService)16 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)14 Produces (javax.ws.rs.Produces)14 ApplicationId (org.onosproject.core.ApplicationId)13 Device (org.onosproject.net.Device)13 Path (javax.ws.rs.Path)12 FlowRule (org.onosproject.net.flow.FlowRule)12 GET (javax.ws.rs.GET)11 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)11 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)10 List (java.util.List)9 TrafficSelector (org.onosproject.net.flow.TrafficSelector)9 DeviceId (org.onosproject.net.DeviceId)8 DefaultFlowRule (org.onosproject.net.flow.DefaultFlowRule)7 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)7 IOException (java.io.IOException)6