Search in sources :

Example 21 with DefaultFlowEntry

use of org.onosproject.net.flow.DefaultFlowEntry 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 22 with DefaultFlowEntry

use of org.onosproject.net.flow.DefaultFlowEntry in project fabric-tna by stratum.

the class MockFlowRuleService method apply.

@Override
public void apply(FlowRuleOperations ops) {
    AtomicBoolean thisSuccess = new AtomicBoolean(success);
    ops.stages().forEach(stage -> stage.forEach(flow -> {
        if (errorFlow == flow.rule().id().value()) {
            thisSuccess.set(false);
        } else {
            switch(flow.type()) {
                case ADD:
                case // TODO is this the right behavior for modify?
                MODIFY:
                    ((DefaultFlowEntry) flow.rule()).setState(FlowEntry.FlowEntryState.ADDED);
                    flows.add(flow.rule());
                    break;
                case REMOVE:
                    // Remove and add in REMOVED state
                    flows.remove(flow.rule());
                    ((DefaultFlowEntry) flow.rule()).setState(FlowEntry.FlowEntryState.REMOVED);
                    flows.add(flow.rule());
                    break;
                default:
                    break;
            }
        }
    }));
    if (thisSuccess.get()) {
        ops.callback().onSuccess(ops);
    } else {
        ops.callback().onError(ops);
    }
}
Also used : DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) FlowRuleOperations(org.onosproject.net.flow.FlowRuleOperations) FlowRule(org.onosproject.net.flow.FlowRule) ApplicationId(org.onosproject.core.ApplicationId) FlowEntry(org.onosproject.net.flow.FlowEntry) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FlowRuleServiceAdapter(org.onosproject.net.flow.FlowRuleServiceAdapter) DeviceId(org.onosproject.net.DeviceId) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 23 with DefaultFlowEntry

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

the class LumentumSdnRoadmFlowRuleProgrammable method getFlowEntries.

@Override
public Collection<FlowEntry> getFlowEntries() {
    try {
        snmp = new LumentumSnmpDevice(handler().data().deviceId());
    } catch (IOException e) {
        log.error("Failed to connect to device: ", e);
        return Collections.emptyList();
    }
    // Line in is last but one port, line out is last
    DeviceService deviceService = this.handler().get(DeviceService.class);
    List<Port> ports = deviceService.getPorts(data().deviceId());
    if (ports.size() < 2) {
        return Collections.emptyList();
    }
    PortNumber lineIn = ports.get(ports.size() - 2).number();
    PortNumber lineOut = ports.get(ports.size() - 1).number();
    Collection<FlowEntry> entries = Lists.newLinkedList();
    // Add rules
    OID addOid = new OID(CTRL_CHANNEL_STATE + "1");
    entries.addAll(fetchRules(addOid, true, lineOut).stream().map(fr -> new DefaultFlowEntry(fr, FlowEntry.FlowEntryState.ADDED, 0, 0, 0)).collect(Collectors.toList()));
    // Drop rules
    OID dropOid = new OID(CTRL_CHANNEL_STATE + "2");
    entries.addAll(fetchRules(dropOid, false, lineIn).stream().map(fr -> new DefaultFlowEntry(fr, FlowEntry.FlowEntryState.ADDED, 0, 0, 0)).collect(Collectors.toList()));
    return entries;
}
Also used : DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) Port(org.onosproject.net.Port) DeviceService(org.onosproject.net.device.DeviceService) IOException(java.io.IOException) OID(org.snmp4j.smi.OID) PortNumber(org.onosproject.net.PortNumber) FlowEntry(org.onosproject.net.flow.FlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry)

Example 24 with DefaultFlowEntry

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

the class GnmiTerminalDeviceFlowRuleProgrammable method getFlowEntries.

@Override
public Collection<FlowEntry> getFlowEntries() {
    // TODO: currently, we store flow rules in a cluster store. Should check if rule/config exists via gNMI.
    if (!setupBehaviour("getFlowEntries")) {
        return Collections.emptyList();
    }
    DeviceConnectionCache cache = getConnectionCache();
    Set<FlowRule> cachedRules = cache.get(deviceId);
    if (cachedRules == null) {
        return ImmutableList.of();
    }
    return cachedRules.stream().filter(Objects::nonNull).map(r -> new DefaultFlowEntry(r, FlowEntry.FlowEntryState.ADDED, 0, 0, 0)).collect(Collectors.toList());
}
Also used : FlowRuleProgrammable(org.onosproject.net.flow.FlowRuleProgrammable) Gnmi(gnmi.Gnmi) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) FlowEntry(org.onosproject.net.flow.FlowEntry) OC_NAME(org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME) ArrayList(java.util.ArrayList) Frequency(org.onlab.util.Frequency) ImmutableList(com.google.common.collect.ImmutableList) Port(org.onosproject.net.Port) GnmiClient(org.onosproject.gnmi.api.GnmiClient) AbstractGrpcHandlerBehaviour(org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour) GnmiPathBuilder(org.onosproject.gnmi.api.GnmiUtils.GnmiPathBuilder) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) DeviceConnectionCache(org.onosproject.drivers.odtn.impl.DeviceConnectionCache) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) FlowRuleParser(org.onosproject.drivers.odtn.impl.FlowRuleParser) FlowRule(org.onosproject.net.flow.FlowRule) GnmiController(org.onosproject.gnmi.api.GnmiController) Collections(java.util.Collections) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) Objects(java.util.Objects) DeviceConnectionCache(org.onosproject.drivers.odtn.impl.DeviceConnectionCache) FlowRule(org.onosproject.net.flow.FlowRule)

Example 25 with DefaultFlowEntry

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

the class FlowRuleManagerTest method flowRemoved.

@Test
public void flowRemoved() {
    FlowRule f1 = addFlowRule(1);
    FlowRule f2 = addFlowRule(2);
    StoredFlowEntry fe1 = new DefaultFlowEntry(f1);
    FlowEntry fe2 = new DefaultFlowEntry(f2);
    providerService.pushFlowMetrics(DID, ImmutableList.of(fe1, fe2));
    service.removeFlowRules(f1);
    // FIXME modification of "stored" flow entry outside of store
    fe1.setState(FlowEntryState.REMOVED);
    providerService.flowRemoved(fe1);
    validateEvents(RULE_ADD_REQUESTED, RULE_ADD_REQUESTED, RULE_ADDED, RULE_ADDED, RULE_REMOVE_REQUESTED, RULE_REMOVED);
    providerService.flowRemoved(fe1);
    validateEvents();
    FlowRule f3 = flowRule(3, 3);
    FlowEntry fe3 = new DefaultFlowEntry(f3);
    service.applyFlowRules(f3);
    providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
    validateEvents(RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED);
    providerService.flowRemoved(fe3);
    validateEvents();
}
Also used : StoredFlowEntry(org.onosproject.net.flow.StoredFlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) FlowEntry(org.onosproject.net.flow.FlowEntry) StoredFlowEntry(org.onosproject.net.flow.StoredFlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) Test(org.junit.Test)

Aggregations

DefaultFlowEntry (org.onosproject.net.flow.DefaultFlowEntry)44 FlowEntry (org.onosproject.net.flow.FlowEntry)38 FlowRule (org.onosproject.net.flow.FlowRule)31 StoredFlowEntry (org.onosproject.net.flow.StoredFlowEntry)22 DefaultFlowRule (org.onosproject.net.flow.DefaultFlowRule)21 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)8 DeviceId (org.onosproject.net.DeviceId)7 Collectors (java.util.stream.Collectors)6 Map (java.util.Map)4 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)4 DeviceConnectionCache (org.onosproject.drivers.odtn.impl.DeviceConnectionCache)4 PortNumber (org.onosproject.net.PortNumber)4 DeviceService (org.onosproject.net.device.DeviceService)4 FlowId (org.onosproject.net.flow.FlowId)4 Logger (org.slf4j.Logger)4 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 Objects (java.util.Objects)3 Set (java.util.Set)3