Draft implementation BMv2 FlowRuleProgrammable
Change-Id: I927564010204a2c59604a30e593fd1a8568f04db
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleProgrammable.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleProgrammable.java
new file mode 100644
index 0000000..c08c5a7
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleProgrammable.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.bmv2;
+
+import com.google.common.collect.Maps;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.flow.DefaultFlowEntry;
+import org.onosproject.net.flow.FlowEntry;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleProgrammable;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.runtime.PiFlowRuleTranslationService;
+import org.onosproject.net.pi.runtime.PiPipeconfService;
+import org.onosproject.net.pi.runtime.PiTableEntry;
+import org.onosproject.p4runtime.api.P4RuntimeClient;
+import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
+import org.onosproject.p4runtime.api.P4RuntimeController;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Implementation of the flow rule programmable behaviour for BMv2.
+ */
+public class Bmv2FlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private static final Map<DeviceId, Collection<Bmv2FlowRuleWrapper>> INSTALLED_FLOWENTRIES = Maps.newHashMap();
+
+ P4RuntimeClient client;
+ PiPipeconf pipeconf;
+ PiPipelineInterpreter interpreter;
+ DeviceId deviceId;
+ PiFlowRuleTranslationService piFlowRuleTranslationService;
+
+ private boolean init() {
+ deviceId = handler().data().deviceId();
+
+ P4RuntimeController controller = handler().get(P4RuntimeController.class);
+ if (!controller.hasClient(deviceId)) {
+ log.warn("Unable to find client for {}, aborting flow rule operation", deviceId);
+ return false;
+ }
+
+ client = controller.getClient(deviceId);
+ piFlowRuleTranslationService = handler().get(PiFlowRuleTranslationService.class);
+ PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
+
+ if (piPipeconfService.ofDevice(deviceId).isPresent() &&
+ piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
+ pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
+ } else {
+ log.warn("Unable to get the pipeconf of {}", deviceId);
+ return false;
+ }
+
+ DeviceService deviceService = handler().get(DeviceService.class);
+ Device device = deviceService.getDevice(deviceId);
+ interpreter = device.is(PiPipelineInterpreter.class) ? device.as(PiPipelineInterpreter.class) : null;
+ if (device.is(PiPipelineInterpreter.class)) {
+ log.warn("Device {} unable to instantiate interpreter of pipeconf {}", deviceId, pipeconf.id());
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public Collection<FlowEntry> getFlowEntries() {
+
+ if (!init()) {
+ return Collections.emptyList();
+ }
+
+ //TD DO: retrieve statistics.
+ long packets = 0;
+ long bytes = 0;
+ Collection<FlowEntry> flowEntries = INSTALLED_FLOWENTRIES
+ .get(deviceId).stream().map(wrapper -> new DefaultFlowEntry(wrapper.rule(),
+ FlowEntry.FlowEntryState.ADDED,
+ wrapper.lifeInSeconds(),
+ packets,
+ bytes))
+ .collect(Collectors.toList());
+
+ return Collections.unmodifiableCollection(flowEntries);
+ }
+
+ @Override
+ public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
+
+ Collection<FlowRule> insertFlowRules = rules.stream()
+ .filter(r -> INSTALLED_FLOWENTRIES.get(deviceId) == null ||
+ INSTALLED_FLOWENTRIES.get(deviceId).stream().noneMatch(x -> x.rule().id() == r.id()))
+ .collect(Collectors.toList());
+
+ Collection<FlowRule> updateFlowRules = rules.stream()
+ .filter(r -> INSTALLED_FLOWENTRIES.get(deviceId) != null &&
+ INSTALLED_FLOWENTRIES.get(deviceId).stream().anyMatch(x -> x.rule().id() == r.id()))
+ .collect(Collectors.toList());
+
+ Collection<FlowRule> flowRules = processFlowRules(insertFlowRules, WriteOperationType.INSERT);
+ flowRules.addAll(processFlowRules(updateFlowRules, WriteOperationType.MODIFY));
+ return flowRules;
+ }
+
+ @Override
+ public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
+
+ return processFlowRules(rules, WriteOperationType.DELETE);
+ }
+
+ private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, WriteOperationType opType) {
+
+ if (!init()) {
+ return Collections.emptyList();
+ }
+
+ Collection<PiTableEntry> piTableEntries = rules.stream().map(r -> {
+ PiTableEntry piTableEntry = null;
+ try {
+ piTableEntry = piFlowRuleTranslationService.translate(r, pipeconf);
+ } catch (PiFlowRuleTranslationService.PiFlowRuleTranslationException e) {
+ log.error("Flow rule {} can not translte to PiTableEntry {}", r.toString(), e.getMessage());
+ }
+ return piTableEntry;
+ }).collect(Collectors.toList());
+
+ Collection<FlowRule> installedEntries = Collections.emptyList();
+ client.writeTableEntries(piTableEntries, opType, pipeconf).whenComplete((r, e) -> {
+ if (r) {
+
+ Collection<Bmv2FlowRuleWrapper> bmv2FlowRuleWrappers = rules.stream()
+ .map(rule -> new Bmv2FlowRuleWrapper(rule, System.currentTimeMillis()))
+ .collect(Collectors.toList());
+
+ if (opType == WriteOperationType.INSERT) {
+ INSTALLED_FLOWENTRIES.put(deviceId, bmv2FlowRuleWrappers);
+ } else if (opType == WriteOperationType.MODIFY) {
+ rules.stream().forEach(rule -> INSTALLED_FLOWENTRIES
+ .get(deviceId).removeIf(x -> x.rule().id() == rule.id()));
+ INSTALLED_FLOWENTRIES.put(deviceId, bmv2FlowRuleWrappers);
+ } else if (opType == WriteOperationType.DELETE) {
+ INSTALLED_FLOWENTRIES.get(deviceId).removeAll(bmv2FlowRuleWrappers);
+ }
+ installedEntries.addAll(rules);
+ }
+ });
+
+ return installedEntries;
+ }
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleWrapper.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleWrapper.java
new file mode 100644
index 0000000..3fe72c1
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleWrapper.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.bmv2;
+
+import com.google.common.base.Objects;
+import org.onosproject.net.flow.FlowRule;
+
+/**
+ * A wrapper for a ONOS flow rule installed on a BMv2 device.
+ */
+public final class Bmv2FlowRuleWrapper {
+
+ private final FlowRule rule;
+ private final long installedOnMillis;
+
+ /**
+ * Creates a new flow rule wrapper.
+ *
+ * @param rule a flow rule
+ * @param installedOnMillis the time (in milliseconds, since January 1, 1970 UTC) when the flow rule was installed
+ * on the device
+ */
+ public Bmv2FlowRuleWrapper(FlowRule rule, long installedOnMillis) {
+ this.rule = rule;
+ this.installedOnMillis = installedOnMillis;
+ }
+
+ /**
+ * Returns the flow rule contained by this wrapper.
+ *
+ * @return a flow rule
+ */
+ public FlowRule rule() {
+ return rule;
+ }
+
+ /**
+ * Return the number of seconds since when this flow rule was installed on the device.
+ *
+ * @return an integer value
+ */
+ public long lifeInSeconds() {
+ return (System.currentTimeMillis() - installedOnMillis) / 1000;
+ }
+
+ /**
+ * Returns the the time (in milliseconds, since January 1, 1970 UTC) when the flow rule was installed on
+ * the device.
+ *
+ * @return a long value
+ */
+ public long installedOnMillis() {
+ return installedOnMillis;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(rule, installedOnMillis);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final Bmv2FlowRuleWrapper other = (Bmv2FlowRuleWrapper) obj;
+ return Objects.equal(this.rule, other.rule)
+ && Objects.equal(this.installedOnMillis, other.installedOnMillis);
+ }
+
+ @Override
+ public String toString() {
+ return installedOnMillis + "-" + rule.hashCode();
+ }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketProgrammable.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketProgrammable.java
index 49f0f4b..c48eef3 100644
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketProgrammable.java
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketProgrammable.java
@@ -57,7 +57,7 @@
piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
} else {
- log.warn("Unable to get the pipeconf of the {}", deviceId);
+ log.warn("Unable to get the pipeconf of {}", deviceId);
return;
}
@@ -65,7 +65,7 @@
Device device = deviceService.getDevice(deviceId);
final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class)
? device.as(PiPipelineInterpreter.class) : null;
- if (interpreter == null) {
+ if (device.is(PiPipelineInterpreter.class)) {
log.warn("Device {} unable to instantiate interpreter of pipeconf {}", deviceId, pipeconf.id());
return;
}
diff --git a/drivers/bmv2/src/main/resources/bmv2-drivers.xml b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
index 0120974..3a7227e 100644
--- a/drivers/bmv2/src/main/resources/bmv2-drivers.xml
+++ b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
@@ -20,6 +20,8 @@
impl="org.onosproject.drivers.bmv2.Bmv2Handshaker"/>
<behaviour api="org.onosproject.net.packet.PacketProgrammable"
impl="org.onosproject.drivers.bmv2.Bmv2PacketProgrammable"/>
+ <behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
+ impl="org.onosproject.drivers.bmv2.Bmv2FlowRuleProgrammable"/>
<behaviour api="org.onosproject.net.pi.model.PiPipelineProgrammable"
impl="org.onosproject.drivers.bmv2.Bmv2PipelineProgrammable"/>
</driver>