Search in sources :

Example 91 with FlowEntry

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

the class FlowBucket method remove.

/**
 * Removes the given flow rule from the bucket.
 *
 * @param rule  the rule to remove
 * @param term  the term in which the change occurred
 * @param clock the logical clock
 * @return the removed flow entry
 */
public FlowEntry remove(FlowEntry rule, long term, LogicalClock clock) {
    final AtomicReference<FlowEntry> removedRule = new AtomicReference<>();
    flowBucket.computeIfPresent(rule.id(), (flowId, flowEntries) -> {
        flowEntries.computeIfPresent((StoredFlowEntry) rule, (k, stored) -> {
            if (rule instanceof DefaultFlowEntry) {
                DefaultFlowEntry toRemove = (DefaultFlowEntry) rule;
                if (stored instanceof DefaultFlowEntry) {
                    DefaultFlowEntry storedEntry = (DefaultFlowEntry) stored;
                    if (toRemove.created() < storedEntry.created()) {
                        LOGGER.debug("Trying to remove more recent flow entry {} (stored: {})", toRemove, stored);
                        // the key is not updated, removedRule remains null
                        return stored;
                    }
                }
            }
            removedRule.set(stored);
            return null;
        });
        return flowEntries.isEmpty() ? null : flowEntries;
    });
    if (removedRule.get() != null) {
        recordUpdate(term, clock.getTimestamp());
        return removedRule.get();
    } else {
        return null;
    }
}
Also used : DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) FlowEntry(org.onosproject.net.flow.FlowEntry) StoredFlowEntry(org.onosproject.net.flow.StoredFlowEntry)

Example 92 with FlowEntry

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

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

the class PolatisInternalConnectivity method getUsedPorts.

private Set<PortNumber> getUsedPorts() {
    Set<PortNumber> usedPorts = new HashSet<PortNumber>();
    Collection<FlowEntry> flowEntries = parseConnections(this);
    for (FlowEntry flowEntry : flowEntries) {
        usedPorts.add(((PortCriterion) flowEntry.selector().getCriterion(Criterion.Type.IN_PORT)).port());
        usedPorts.add(((OutputInstruction) flowEntry.treatment().allInstructions().get(0)).port());
    }
    return usedPorts;
}
Also used : PortNumber(org.onosproject.net.PortNumber) FlowEntry(org.onosproject.net.flow.FlowEntry) HashSet(java.util.HashSet)

Example 94 with FlowEntry

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

the class PolatisInternalConnectivity method getOutputPorts.

/**
 * Returns a set of possible output PortNumbers to which a given input port can be internally connected
 * on a given device.
 * <p>
 * This is a callback method required by the InternalConnectivity behaviour.
 * @param inputPortNum Input port number on device
 * @return             List of possible output ports
 */
@Override
public Set<PortNumber> getOutputPorts(PortNumber inputPortNum) {
    // NOTE: in this implementation, inputPortNum is the supplied port number and
    // can be an "input" port OR an "output" port
    // This reflects the fact that reverse propagation is possible through a Polatis switch
    // (output port -> input port)
    Set<PortNumber> ports = new HashSet<PortNumber>();
    Set<PortNumber> enabledPorts = new HashSet<PortNumber>();
    Collection<FlowEntry> flowEntries = parseConnections(this);
    // e.g. one selector (IN_PORT) and one action (set output port)
    if (flowEntries.stream().map(flow -> ((PortCriterion) flow.selector().getCriterion(Criterion.Type.IN_PORT)).port()).collect(Collectors.toSet()).contains(inputPortNum) || flowEntries.stream().map(flow -> ((OutputInstruction) flow.treatment().allInstructions().get(0)).port()).collect(Collectors.toSet()).contains(inputPortNum)) {
        log.warn("Queried port {} is already used in a cross-connect", inputPortNum);
        return ports;
    } else {
        DeviceId deviceID = handler().data().deviceId();
        DeviceService deviceService = checkNotNull(this.handler().get(DeviceService.class));
        Port inputPort = deviceService.getPort(new ConnectPoint(deviceID, inputPortNum));
        if (inputPort.annotations().value(KEY_PORTDIR).equals(VALUE_CC)) {
            ports = deviceService.getPorts(deviceID).stream().filter(p -> !p.equals(inputPortNum)).map(p -> p.number()).collect(Collectors.toSet());
        } else {
            ports = deviceService.getPorts(deviceID).stream().filter((p) -> {
                Port port = deviceService.getPort(new ConnectPoint(deviceID, p.number()));
                return !port.annotations().value(KEY_PORTDIR).equals(inputPort.annotations().value(KEY_PORTDIR));
            }).map(p -> p.number()).collect(Collectors.toSet());
        }
        // Remove disabled ports
        enabledPorts = ports.stream().filter(p -> deviceService.getPort(new ConnectPoint(deviceID, p)).isEnabled()).collect(Collectors.toSet());
    }
    log.debug("Ports before filtering out used and disabled ones: " + ports);
    return filterUsedPorts(enabledPorts, flowEntries);
}
Also used : OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) Logger(org.slf4j.Logger) KEY_PORTDIR(org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_PORTDIR) InternalConnectivity(org.onosproject.net.behaviour.InternalConnectivity) Collection(java.util.Collection) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) FlowEntry(org.onosproject.net.flow.FlowEntry) Set(java.util.Set) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) Collectors(java.util.stream.Collectors) ConnectPoint(org.onosproject.net.ConnectPoint) AbstractHandlerBehaviour(org.onosproject.net.driver.AbstractHandlerBehaviour) HashSet(java.util.HashSet) Port(org.onosproject.net.Port) PolatisUtility(org.onosproject.drivers.polatis.netconf.PolatisUtility) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DeviceId(org.onosproject.net.DeviceId) Criterion(org.onosproject.net.flow.criteria.Criterion) OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) DeviceId(org.onosproject.net.DeviceId) Port(org.onosproject.net.Port) DeviceService(org.onosproject.net.device.DeviceService) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) PortNumber(org.onosproject.net.PortNumber) FlowEntry(org.onosproject.net.flow.FlowEntry) ConnectPoint(org.onosproject.net.ConnectPoint) HashSet(java.util.HashSet)

Example 95 with FlowEntry

use of org.onosproject.net.flow.FlowEntry 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) PortFormat(org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.PortFormat) 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) Pair(org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.crossconnects.Pair) Set(java.util.Set) PortCriterion(org.onosproject.net.flow.criteria.PortCriterion) HandlerBehaviour(org.onosproject.net.driver.HandlerBehaviour) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) List(java.util.List) FlowRuleService(org.onosproject.net.flow.FlowRuleService) TrafficSelector(org.onosproject.net.flow.TrafficSelector) CrossConnects(org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.CrossConnects) DefaultPair(org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.crossconnects.DefaultPair) FlowRule(org.onosproject.net.flow.FlowRule) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) Criterion(org.onosproject.net.flow.criteria.Criterion) DefaultCrossConnects(org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.DefaultCrossConnects) 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)

Aggregations

FlowEntry (org.onosproject.net.flow.FlowEntry)122 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)26 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 Instruction (org.onosproject.net.flow.instructions.Instruction)16 TrafficSelector (org.onosproject.net.flow.TrafficSelector)15 List (java.util.List)14 DeviceService (org.onosproject.net.device.DeviceService)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