Search in sources :

Example 56 with FlowEntry

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

the class FlowsResourceTest method testFlowsSingleDevice.

/**
 * Tests the result of a rest api GET for a device.
 */
@Test
public void testFlowsSingleDevice() {
    setupMockFlows();
    final Set<FlowEntry> flows = new HashSet<>();
    flows.add(flow5);
    flows.add(flow6);
    expect(mockFlowService.getFlowEntries(anyObject())).andReturn(flows).anyTimes();
    replay(mockFlowService);
    replay(mockDeviceService);
    final WebTarget wt = target();
    final String response = wt.path("flows/" + deviceId3).request().get(String.class);
    final JsonObject result = Json.parse(response).asObject();
    assertThat(result, notNullValue());
    assertThat(result.names(), hasSize(1));
    assertThat(result.names().get(0), is("flows"));
    final JsonArray jsonFlows = result.get("flows").asArray();
    assertThat(jsonFlows, notNullValue());
    assertThat(jsonFlows, hasFlow(flow5));
    assertThat(jsonFlows, hasFlow(flow6));
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) WebTarget(javax.ws.rs.client.WebTarget) Matchers.containsString(org.hamcrest.Matchers.containsString) EasyMock.anyString(org.easymock.EasyMock.anyString) FlowEntry(org.onosproject.net.flow.FlowEntry) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 57 with FlowEntry

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

the class IntentsResourceTest method testRelatedFlowsForIntents.

/**
 * Tests the result of a rest api GET for related flows for single intent.
 */
@Test
public void testRelatedFlowsForIntents() {
    List<FlowEntry> flowEntries = new ArrayList<>();
    flowEntries.add(flow1);
    flowEntries.add(flow2);
    List<List<FlowEntry>> paths = new ArrayList<>();
    paths.add(flowEntries);
    List<FlowRule> flowRules = new ArrayList<>();
    flowRules.add(flowRule1);
    flowRules.add(flowRule2);
    FlowRuleIntent flowRuleIntent = new FlowRuleIntent(APP_ID, null, flowRules, new HashSet<NetworkResource>(), PathIntent.ProtectionType.PRIMARY, null);
    Intent intent = new MockIntent(3L);
    installableIntents.add(flowRuleIntent);
    intents.add(intent);
    expect(mockIntentService.getIntent(Key.of(0, APP_ID))).andReturn(intent).anyTimes();
    expect(mockIntentService.getIntent(Key.of("0", APP_ID))).andReturn(intent).anyTimes();
    expect(mockIntentService.getIntent(Key.of(0, APP_ID))).andReturn(intent).anyTimes();
    expect(mockIntentService.getIntent(Key.of("0x0", APP_ID))).andReturn(null).anyTimes();
    expect(mockIntentService.getInstallableIntents(intent.key())).andReturn(installableIntents).anyTimes();
    replay(mockIntentService);
    expect(mockFlowService.getFlowEntries(deviceId1)).andReturn(flowEntries).anyTimes();
    replay(mockFlowService);
    expect(mockCoreService.getAppId(APP_ID.name())).andReturn(APP_ID).anyTimes();
    expect(mockCoreService.getAppId(APP_ID.id())).andReturn(APP_ID).anyTimes();
    replay(mockCoreService);
    final WebTarget wt = target();
    // Test get using key string
    final String response = wt.path("intents/relatedflows/" + APP_ID.name() + "/0").request().get(String.class);
    final JsonObject result = Json.parse(response).asObject();
    assertThat(result, matchesRelatedFlowEntries(paths, APP_ID.name()));
    // Test get using numeric value
    final String responseNumeric = wt.path("intents/relatedflows/" + APP_ID.name() + "/0x0").request().get(String.class);
    final JsonObject resultNumeric = Json.parse(responseNumeric).asObject();
    assertThat(resultNumeric, matchesRelatedFlowEntries(paths, APP_ID.name()));
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject) FlowRuleIntent(org.onosproject.net.intent.FlowRuleIntent) HostToHostIntent(org.onosproject.net.intent.HostToHostIntent) ConnectivityIntent(org.onosproject.net.intent.ConnectivityIntent) Intent(org.onosproject.net.intent.Intent) PathIntent(org.onosproject.net.intent.PathIntent) PointToPointIntent(org.onosproject.net.intent.PointToPointIntent) MockIntent(org.onosproject.net.intent.IntentTestsMocks.MockIntent) MockIntent(org.onosproject.net.intent.IntentTestsMocks.MockIntent) NetworkResource(org.onosproject.net.NetworkResource) List(java.util.List) ArrayList(java.util.ArrayList) FlowRule(org.onosproject.net.flow.FlowRule) WebTarget(javax.ws.rs.client.WebTarget) FlowEntry(org.onosproject.net.flow.FlowEntry) FlowRuleIntent(org.onosproject.net.intent.FlowRuleIntent) Test(org.junit.Test)

Example 58 with FlowEntry

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

the class PolatisInternalConnectivity method filterUsedPorts.

private Set<PortNumber> filterUsedPorts(Set<PortNumber> ports, Collection<FlowEntry> flowEntries) {
    if (ports.isEmpty() || flowEntries.isEmpty()) {
        return ports;
    }
    for (FlowEntry flowEntry : flowEntries) {
        PortNumber inputPort = ((PortCriterion) flowEntry.selector().getCriterion(Criterion.Type.IN_PORT)).port();
        ports.remove(inputPort);
        PortNumber outputPort = ((OutputInstruction) flowEntry.treatment().allInstructions().get(0)).port();
        ports.remove(outputPort);
        log.debug("Cross-connection {}-{} removed from output port list", inputPort, outputPort);
    }
    log.debug("Ports after filtering out used ones: " + ports);
    return ports;
}
Also used : OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) PortNumber(org.onosproject.net.PortNumber) FlowEntry(org.onosproject.net.flow.FlowEntry)

Example 59 with FlowEntry

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

the class P4RuntimeFlowRuleProgrammable method getFlowEntries.

@Override
public Collection<FlowEntry> getFlowEntries() {
    if (!setupBehaviour("getFlowEntries()")) {
        return Collections.emptyList();
    }
    if (driverBoolProperty(READ_FROM_MIRROR, DEFAULT_READ_FROM_MIRROR)) {
        return getFlowEntriesFromMirror();
    }
    final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder();
    final List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
    // Read table entries from device.
    final Collection<PiTableEntry> deviceEntries = getAllTableEntriesFromDevice();
    if (deviceEntries == null) {
        // Potential error at the client level.
        return Collections.emptyList();
    }
    // Synchronize mirror with the device state.
    tableMirror.sync(deviceId, deviceEntries);
    if (deviceEntries.isEmpty()) {
        // Nothing to do.
        return Collections.emptyList();
    }
    final Map<PiTableEntryHandle, PiCounterCellData> counterCellMap = readEntryCounters(deviceEntries);
    // Forge flow entries with counter values.
    for (PiTableEntry entry : deviceEntries) {
        final PiTableEntryHandle handle = entry.handle(deviceId);
        final FlowEntry flowEntry = forgeFlowEntry(entry, handle, counterCellMap.get(handle));
        if (flowEntry == null) {
            // program via default_action, which cannot be removed.)
            if (!isOriginalDefaultEntry(entry)) {
                inconsistentEntries.add(entry);
            }
        } else {
            result.add(flowEntry);
        }
    }
    // Default entries need to be treated in a different way according to the spec:
    // the client can modify (reset or update) them but cannot remove the entries
    List<PiTableEntry> inconsistentDefaultEntries = Lists.newArrayList();
    List<PiTableEntry> tempDefaultEntries = inconsistentEntries.stream().filter(PiTableEntry::isDefaultAction).collect(Collectors.toList());
    inconsistentEntries.removeAll(tempDefaultEntries);
    // Once we have removed the default entry from inconsistentEntries we need to
    // craft for each default entry a copy without the action field. According to
    // the spec leaving the action field unset will reset the original default entry.
    tempDefaultEntries.forEach(piTableEntry -> {
        PiTableEntry resetEntry = PiTableEntry.builder().forTable(piTableEntry.table()).build();
        inconsistentDefaultEntries.add(resetEntry);
    });
    // Clean up of inconsistent entries.
    if (!inconsistentEntries.isEmpty() || !inconsistentDefaultEntries.isEmpty()) {
        WriteRequest writeRequest = client.write(p4DeviceId, pipeconf);
        // Trigger remove of inconsistent entries.
        if (!inconsistentEntries.isEmpty()) {
            log.warn("Found {} inconsistent table entries on {}, removing them...", inconsistentEntries.size(), deviceId);
            writeRequest = writeRequest.entities(inconsistentEntries, DELETE);
        }
        // Trigger reset of inconsistent default entries.
        if (!inconsistentDefaultEntries.isEmpty()) {
            log.warn("Found {} inconsistent default table entries on {}, resetting them...", inconsistentDefaultEntries.size(), deviceId);
            writeRequest = writeRequest.entities(inconsistentDefaultEntries, MODIFY);
        }
        // Submit delete request for non-default entries and modify request
        // for default entries. Updates mirror when done.
        writeRequest.submit().whenComplete((response, ex) -> {
            if (ex != null) {
                log.error("Exception removing inconsistent table entries", ex);
            } else {
                log.debug("Successfully removed {} out of {} inconsistent entries", response.success().size(), response.all().size());
            }
            // We can use the entity as the handle does not contain the action field
            // so the key will be removed even if the table entry is different
            response.success().forEach(entity -> tableMirror.remove((PiTableEntryHandle) entity.handle()));
        });
    }
    return result.build();
}
Also used : PiCounterCellData(org.onosproject.net.pi.runtime.PiCounterCellData) ImmutableList(com.google.common.collect.ImmutableList) WriteRequest(org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) FlowEntry(org.onosproject.net.flow.FlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) PiTableEntryHandle(org.onosproject.net.pi.runtime.PiTableEntryHandle)

Example 60 with FlowEntry

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

the class P4RuntimeTableStatisticsDiscovery method getTableStatistics.

@Override
public List<TableStatisticsEntry> getTableStatistics() {
    if (!setupBehaviour("getTableStatistics()")) {
        return Collections.emptyList();
    }
    FlowRuleService flowService = handler().get(FlowRuleService.class);
    PiPipelineInterpreter interpreter = getInterpreter(handler());
    PiPipelineModel model = pipeconf.pipelineModel();
    List<TableStatisticsEntry> tableStatsList;
    List<FlowEntry> rules = newArrayList(flowService.getFlowEntries(deviceId));
    Map<PiTableId, Integer> piTableFlowCount = piFlowRuleCounting(model, interpreter, rules);
    Map<PiTableId, Long> piTableMatchCount = piMatchedCounting(model, interpreter, rules);
    tableStatsList = generatePiFlowTableStatistics(piTableFlowCount, piTableMatchCount, model, deviceId);
    return tableStatsList;
}
Also used : PiTableId(org.onosproject.net.pi.model.PiTableId) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter) FlowRuleService(org.onosproject.net.flow.FlowRuleService) FlowEntry(org.onosproject.net.flow.FlowEntry) PiPipelineModel(org.onosproject.net.pi.model.PiPipelineModel) DefaultTableStatisticsEntry(org.onosproject.net.flow.DefaultTableStatisticsEntry) TableStatisticsEntry(org.onosproject.net.flow.TableStatisticsEntry)

Aggregations

FlowEntry (org.onosproject.net.flow.FlowEntry)123 DefaultFlowEntry (org.onosproject.net.flow.DefaultFlowEntry)61 FlowRule (org.onosproject.net.flow.FlowRule)51 Test (org.junit.Test)31 DefaultFlowRule (org.onosproject.net.flow.DefaultFlowRule)31 FlowRuleService (org.onosproject.net.flow.FlowRuleService)27 ArrayList (java.util.ArrayList)24 StoredFlowEntry (org.onosproject.net.flow.StoredFlowEntry)23 DeviceId (org.onosproject.net.DeviceId)20 Device (org.onosproject.net.Device)19 PortNumber (org.onosproject.net.PortNumber)17 TrafficSelector (org.onosproject.net.flow.TrafficSelector)16 Instruction (org.onosproject.net.flow.instructions.Instruction)16 DeviceService (org.onosproject.net.device.DeviceService)15 List (java.util.List)14 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)13 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)13 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)12 HashSet (java.util.HashSet)12 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)11