Search in sources :

Example 11 with FlowRuleService

use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.

the class TelemetryVflowListCommand method doExecute.

@Override
protected void doExecute() {
    CoreService coreService = get(CoreService.class);
    FlowRuleService flowService = get(FlowRuleService.class);
    ApplicationId appId = coreService.getAppId(OPENSTACK_TELEMETRY_APP_ID);
    List<FlowEntry> flows = Lists.newArrayList(flowService.getFlowEntriesById(appId));
    print(FORMAT, "SrcIp", "SrcPort", "DstIp", "DstPort", "Protocol");
    for (FlowEntry entry : flows) {
        TrafficSelector selector = entry.selector();
        IpPrefix srcIp = ((IPCriterion) selector.getCriterion(IPV4_SRC)).ip();
        IpPrefix dstIp = ((IPCriterion) selector.getCriterion(IPV4_DST)).ip();
        TpPort srcPort = TpPort.tpPort(0);
        TpPort dstPort = TpPort.tpPort(0);
        String protocolStr = "ANY";
        Criterion ipProtocolCriterion = selector.getCriterion(IP_PROTO);
        if (ipProtocolCriterion != null) {
            short protocol = ((IPProtocolCriterion) selector.getCriterion(IP_PROTO)).protocol();
            if (protocol == PROTOCOL_TCP) {
                srcPort = ((TcpPortCriterion) selector.getCriterion(TCP_SRC)).tcpPort();
                dstPort = ((TcpPortCriterion) selector.getCriterion(TCP_DST)).tcpPort();
                protocolStr = TCP;
            }
            if (protocol == PROTOCOL_UDP) {
                srcPort = ((UdpPortCriterion) selector.getCriterion(UDP_SRC)).udpPort();
                dstPort = ((UdpPortCriterion) selector.getCriterion(UDP_SRC)).udpPort();
                protocolStr = UDP;
            }
        }
        print(FORMAT, srcIp.toString(), srcPort.toString(), dstIp.toString(), dstPort.toString(), protocolStr);
    }
}
Also used : IPCriterion(org.onosproject.net.flow.criteria.IPCriterion) CoreService(org.onosproject.core.CoreService) IpPrefix(org.onlab.packet.IpPrefix) IPProtocolCriterion(org.onosproject.net.flow.criteria.IPProtocolCriterion) TcpPortCriterion(org.onosproject.net.flow.criteria.TcpPortCriterion) IPCriterion(org.onosproject.net.flow.criteria.IPCriterion) Criterion(org.onosproject.net.flow.criteria.Criterion) UdpPortCriterion(org.onosproject.net.flow.criteria.UdpPortCriterion) IPProtocolCriterion(org.onosproject.net.flow.criteria.IPProtocolCriterion) TrafficSelector(org.onosproject.net.flow.TrafficSelector) TpPort(org.onlab.packet.TpPort) FlowRuleService(org.onosproject.net.flow.FlowRuleService) ApplicationId(org.onosproject.core.ApplicationId) FlowEntry(org.onosproject.net.flow.FlowEntry)

Example 12 with FlowRuleService

use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.

the class FlowRuleProgrammableServerImpl method getFlowEntries.

@Override
public Collection<FlowEntry> getFlowEntries() {
    DeviceId deviceId = getDeviceId();
    checkNotNull(deviceId, MSG_DEVICE_ID_NULL);
    // Expected FlowEntries installed through ONOS
    FlowRuleService flowService = getHandler().get(FlowRuleService.class);
    Iterable<FlowEntry> flowEntries = flowService.getFlowEntries(deviceId);
    // Hit the path that provides the server's flow rules
    InputStream response = null;
    try {
        response = getController().get(deviceId, URL_RULE_MANAGEMENT, JSON);
    } catch (ProcessingException pEx) {
        log.error("Failed to get NIC flow entries from device: {}", deviceId);
        return Collections.EMPTY_LIST;
    }
    // Load the JSON into objects
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode objNode = null;
    try {
        Map<String, Object> jsonMap = mapper.readValue(response, Map.class);
        JsonNode jsonNode = mapper.convertValue(jsonMap, JsonNode.class);
        objNode = (ObjectNode) jsonNode;
    } catch (IOException ioEx) {
        log.error("Failed to get NIC flow entries from device: {}", deviceId);
        return Collections.EMPTY_LIST;
    }
    if (objNode == null) {
        log.error("Failed to get NIC flow entries from device: {}", deviceId);
        return Collections.EMPTY_LIST;
    }
    JsonNode scsNode = objNode.path(PARAM_RULES);
    // Here we store the trully installed rules
    Collection<FlowEntry> actualFlowEntries = Sets.<FlowEntry>newConcurrentHashSet();
    for (JsonNode scNode : scsNode) {
        String scId = get(scNode, PARAM_ID);
        String rxFilter = get(scNode.path(PARAM_NIC_RX_FILTER), PARAM_NIC_RX_METHOD);
        // Only Flow-based RxFilter is permitted
        if (RxFilter.getByName(rxFilter) != RxFilter.FLOW) {
            log.warn("Device with Rx filter {} is not managed by this driver", rxFilter.toString().toUpperCase());
            continue;
        }
        // Each device might have multiple NICs
        for (JsonNode nicNode : scNode.path(PARAM_NICS)) {
            JsonNode cpusNode = nicNode.path(PARAM_CPUS);
            // Each NIC can dispatch to multiple CPU cores
            for (JsonNode cpuNode : cpusNode) {
                String cpuId = get(cpuNode, PARAM_ID);
                JsonNode rulesNode = cpuNode.path(PARAM_RULES);
                // Multiple rules might correspond to each CPU core
                for (JsonNode ruleNode : rulesNode) {
                    long ruleId = ruleNode.path(PARAM_ID).asLong();
                    String ruleContent = get(ruleNode, PARAM_RULE_CONTENT);
                    // Search for this rule ID in ONOS's store
                    FlowRule r = findRuleInFlowEntries(flowEntries, ruleId);
                    // Local rule, not present in the controller => Ignore
                    if (r == null) {
                        continue;
                    // Rule trully present in the data plane => Add
                    } else {
                        actualFlowEntries.add(new DefaultFlowEntry(r, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
                    }
                }
            }
        }
    }
    return actualFlowEntries;
}
Also used : DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) NicFlowRule(org.onosproject.drivers.server.devices.nic.NicFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) FlowRuleService(org.onosproject.net.flow.FlowRuleService) FlowEntry(org.onosproject.net.flow.FlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessingException(javax.ws.rs.ProcessingException)

Example 13 with FlowRuleService

use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.

the class FlowsWebResource method getPendingFlows.

/**
 * Gets all pending flow entries. Returns array of all pending flow rules in the system.
 *
 * @return 200 OK with a collection of flows
 * @onos.rsModel FlowEntries
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("pending")
public Response getPendingFlows() {
    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) {
                if ((entry.state() == FlowEntry.FlowEntryState.PENDING_ADD) || (entry.state() == FlowEntry.FlowEntryState.PENDING_REMOVE)) {
                    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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 14 with FlowRuleService

use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.

the class FlowsWebResource method removeFlowByAppId.

/**
 * Removes flow rules by application ID.
 * Removes a collection of flow rules generated by the given application.
 *
 * @param appId application identifier
 * @return 204 NO CONTENT
 */
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("application/{appId}")
public Response removeFlowByAppId(@PathParam("appId") String appId) {
    FlowRuleService service = get(FlowRuleService.class);
    ApplicationService appService = get(ApplicationService.class);
    ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
    service.removeFlowRulesById(idInstant);
    return Response.noContent().build();
}
Also used : FlowRuleService(org.onosproject.net.flow.FlowRuleService) ApplicationId(org.onosproject.core.ApplicationId) ApplicationService(org.onosproject.app.ApplicationService) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 15 with FlowRuleService

use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.

the class FlowsWebResource method deleteFlowByDeviceIdAndFlowId.

/**
 * Removes flow rule. Removes the specified flow rule.
 *
 * @param deviceId device identifier
 * @param flowId   flow rule identifier
 * @return 204 NO CONTENT
 */
@DELETE
@Path("{deviceId}/{flowId}")
public Response deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, @PathParam("flowId") long flowId) {
    FlowRuleService service = get(FlowRuleService.class);
    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).forEach(service::removeFlowRules);
    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) FlowRuleService(org.onosproject.net.flow.FlowRuleService) FlowEntry(org.onosproject.net.flow.FlowEntry) ItemNotFoundException(org.onlab.util.ItemNotFoundException) Path(javax.ws.rs.Path) 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