ONOS-3961 ONOS-4285 Implemented BMv2 drivers and protocol
Provides Thrift-based implementation for FlowRuleProgrammable and
PortDiscovery behaviours.
Change-Id: Ibbf8720d92301bcd23c5c583d156f464015ff1ef
diff --git a/core/api/src/main/java/org/onosproject/net/flow/criteria/ExtensionSelectorType.java b/core/api/src/main/java/org/onosproject/net/flow/criteria/ExtensionSelectorType.java
index 2d47842..f692bd2 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/criteria/ExtensionSelectorType.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/criteria/ExtensionSelectorType.java
@@ -38,7 +38,8 @@
NICIRA_MATCH_NSH_CH2(3),
NICIRA_MATCH_NSH_CH3(4),
NICIRA_MATCH_NSH_CH4(5),
- OFDPA_MATCH_VLAN_VID(16);
+ OFDPA_MATCH_VLAN_VID(16),
+ P4_BMV2_MATCH_KEY(128);
private ExtensionSelectorType type;
diff --git a/core/api/src/main/java/org/onosproject/net/flow/instructions/ExtensionTreatmentType.java b/core/api/src/main/java/org/onosproject/net/flow/instructions/ExtensionTreatmentType.java
index df9789e..648a397 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/instructions/ExtensionTreatmentType.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/instructions/ExtensionTreatmentType.java
@@ -45,7 +45,8 @@
NICIRA_SET_NSH_CH2(35),
NICIRA_SET_NSH_CH3(36),
NICIRA_SET_NSH_CH4(37),
- OFDPA_SET_VLAN_ID(64);
+ OFDPA_SET_VLAN_ID(64),
+ P4_BMV2_ACTION(128);
private ExtensionTreatmentType type;
diff --git a/drivers/bmv2/features.xml b/drivers/bmv2/features.xml
new file mode 100644
index 0000000..c78c28b
--- /dev/null
+++ b/drivers/bmv2/features.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+
+<!--
+ ~ Copyright 2014-2016 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.
+ -->
+
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+ <feature name="${project.artifactId}" version="${project.version}"
+ description="${project.description}">
+ <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+ <bundle>mvn:${project.groupId}/onos-bmv2-protocol/${project.version}</bundle>
+ </feature>
+</features>
diff --git a/drivers/bmv2/pom.xml b/drivers/bmv2/pom.xml
new file mode 100644
index 0000000..680f25d
--- /dev/null
+++ b/drivers/bmv2/pom.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2014-2016 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>onos-drivers-general</artifactId>
+ <groupId>org.onosproject</groupId>
+ <version>1.6.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>onos-drivers-bmv2</artifactId>
+ <version>1.6.0-SNAPSHOT</version>
+
+ <packaging>bundle</packaging>
+
+ <description>Device drivers for p4.org reference softswitch BMv2</description>
+
+ <properties>
+ <onos.app.name>org.onosproject.drivers.bmv2</onos.app.name>
+ <onos.app.origin>ON.Lab</onos.app.origin>
+ <onos.app.category>Drivers</onos.app.category>
+ <onos.app.title>BMv2 Device Drivers</onos.app.title>
+ <onos.app.url>http://onosproject.org</onos.app.url>
+ <onos.app.requires>
+ org.onosproject.bmv2
+ </onos.app.requires>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-bmv2-protocol</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java
new file mode 100644
index 0000000..89ed1bd
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DriversLoader.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2014-2016 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 org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for barefoot drivers from specific xml.
+ */
+@Component(immediate = true)
+public class Bmv2DriversLoader extends AbstractDriverLoader {
+
+ private static final String DRIVERS_XML = "/bmv2-drivers.xml";
+
+ public Bmv2DriversLoader() {
+ super(DRIVERS_XML);
+ }
+}
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleDriver.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleDriver.java
new file mode 100644
index 0000000..c000f52
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2FlowRuleDriver.java
@@ -0,0 +1,236 @@
+/*
+ * Copyright 2014-2016 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.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import org.onosproject.bmv2.api.Bmv2ExtensionSelector;
+import org.onosproject.bmv2.api.Bmv2ExtensionTreatment;
+import org.onosproject.bmv2.api.Bmv2TableEntry;
+import org.onosproject.bmv2.api.Bmv2Exception;
+import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
+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.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.ExtensionCriterion;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flow.criteria.ExtensionSelectorType;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class Bmv2FlowRuleDriver extends AbstractHandlerBehaviour
+ implements FlowRuleProgrammable {
+
+ private final Logger log =
+ LoggerFactory.getLogger(this.getClass());
+
+ // Bmv2 doesn't support proper table dump, use a local store
+ // FIXME: synchronize entries with device
+ private final Map<FlowRule, FlowEntry> deviceEntriesMap = Maps.newHashMap();
+ private final Map<Integer, Set<FlowRule>> tableRulesMap = Maps.newHashMap();
+ private final Map<FlowRule, Long> tableEntryIdsMap = Maps.newHashMap();
+
+ @Override
+ public Collection<FlowEntry> getFlowEntries() {
+ return Collections.unmodifiableCollection(
+ deviceEntriesMap.values());
+ }
+
+ @Override
+ public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) {
+ Bmv2ThriftClient deviceClient;
+ try {
+ deviceClient = getDeviceClient();
+ } catch (Bmv2Exception e) {
+ return Collections.emptyList();
+ }
+
+ List<FlowRule> appliedFlowRules = Lists.newArrayList();
+
+ for (FlowRule rule : rules) {
+
+ Bmv2TableEntry entry;
+
+ try {
+ entry = parseFlowRule(rule);
+ } catch (IllegalStateException e) {
+ log.error("Unable to parse flow rule", e);
+ continue;
+ }
+
+ // Instantiate flowrule set for table if it does not exist
+ if (!tableRulesMap.containsKey(rule.tableId())) {
+ tableRulesMap.put(rule.tableId(), Sets.newHashSet());
+ }
+
+ if (tableRulesMap.get(rule.tableId()).contains(rule)) {
+ /* Rule is already installed in the table */
+ long entryId = tableEntryIdsMap.get(rule);
+
+ try {
+ deviceClient.modifyTableEntry(
+ entry.tableName(), entryId, entry.action());
+
+ // Replace stored rule as treatment, etc. might have changed
+ // Java Set doesn't replace on add, remove first
+ tableRulesMap.get(rule.tableId()).remove(rule);
+ tableRulesMap.get(rule.tableId()).add(rule);
+ tableEntryIdsMap.put(rule, entryId);
+ deviceEntriesMap.put(rule, new DefaultFlowEntry(
+ rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
+ } catch (Bmv2Exception e) {
+ log.error("Unable to update flow rule", e);
+ continue;
+ }
+
+ } else {
+ /* Rule is new */
+ try {
+ long entryId = deviceClient.addTableEntry(entry);
+
+ tableRulesMap.get(rule.tableId()).add(rule);
+ tableEntryIdsMap.put(rule, entryId);
+ deviceEntriesMap.put(rule, new DefaultFlowEntry(
+ rule, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
+ } catch (Bmv2Exception e) {
+ log.error("Unable to add flow rule", e);
+ continue;
+ }
+ }
+
+ appliedFlowRules.add(rule);
+ }
+
+ return Collections.unmodifiableCollection(appliedFlowRules);
+ }
+
+ @Override
+ public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) {
+ Bmv2ThriftClient deviceClient;
+ try {
+ deviceClient = getDeviceClient();
+ } catch (Bmv2Exception e) {
+ return Collections.emptyList();
+ }
+
+ List<FlowRule> removedFlowRules = Lists.newArrayList();
+
+ for (FlowRule rule : rules) {
+
+ if (tableEntryIdsMap.containsKey(rule)) {
+ long entryId = tableEntryIdsMap.get(rule);
+ String tableName = parseTableName(rule.tableId());
+
+ try {
+ deviceClient.deleteTableEntry(tableName, entryId);
+ } catch (Bmv2Exception e) {
+ log.error("Unable to delete flow rule", e);
+ continue;
+ }
+
+ /* remove from local store */
+ tableEntryIdsMap.remove(rule);
+ tableRulesMap.get(rule.tableId()).remove(rule);
+ deviceEntriesMap.remove(rule);
+
+ removedFlowRules.add(rule);
+ }
+ }
+
+ return Collections.unmodifiableCollection(removedFlowRules);
+ }
+
+ private Bmv2TableEntry parseFlowRule(FlowRule flowRule) {
+
+ // TODO make it pipeline dependant, i.e. implement mapping
+
+ Bmv2TableEntry.Builder entryBuilder = Bmv2TableEntry.builder();
+
+ // Check selector
+ ExtensionCriterion ec =
+ (ExtensionCriterion) flowRule
+ .selector().getCriterion(Criterion.Type.EXTENSION);
+ Preconditions.checkState(
+ flowRule.selector().criteria().size() == 1
+ && ec != null,
+ "Selector must have only 1 criterion of type EXTENSION");
+ ExtensionSelector es = ec.extensionSelector();
+ Preconditions.checkState(
+ es.type() == ExtensionSelectorType.ExtensionSelectorTypes.P4_BMV2_MATCH_KEY.type(),
+ "ExtensionSelectorType must be P4_BMV2_MATCH_KEY");
+
+ // Selector OK, get Bmv2MatchKey
+ entryBuilder.withMatchKey(((Bmv2ExtensionSelector) es).matchKey());
+
+ // Check treatment
+ Instruction inst = flowRule.treatment().allInstructions().get(0);
+ Preconditions.checkState(
+ flowRule.treatment().allInstructions().size() == 1
+ && inst.type() == Instruction.Type.EXTENSION,
+ "Treatment must have only 1 instruction of type EXTENSION");
+ ExtensionTreatment et =
+ ((Instructions.ExtensionInstructionWrapper) inst)
+ .extensionInstruction();
+
+ Preconditions.checkState(
+ et.type() == ExtensionTreatmentType.ExtensionTreatmentTypes.P4_BMV2_ACTION.type(),
+ "ExtensionTreatmentType must be P4_BMV2_ACTION");
+
+ // Treatment OK, get Bmv2Action
+ entryBuilder.withAction(((Bmv2ExtensionTreatment) et).getAction());
+
+ // Table name
+ entryBuilder.withTableName(parseTableName(flowRule.tableId()));
+
+ if (!flowRule.isPermanent()) {
+ entryBuilder.withTimeout(flowRule.timeout());
+ }
+
+ entryBuilder.withPriority(flowRule.priority());
+
+ return entryBuilder.build();
+ }
+
+ private String parseTableName(int tableId) {
+ // TODO: map tableId with tableName according to P4 JSON
+ return "table" + String.valueOf(tableId);
+ }
+
+ private Bmv2ThriftClient getDeviceClient() throws Bmv2Exception {
+ try {
+ return Bmv2ThriftClient.of(handler().data().deviceId());
+ } catch (Bmv2Exception e) {
+ log.error("Failed to connect to Bmv2 device", e);
+ throw e;
+ }
+ }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PortGetterDriver.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PortGetterDriver.java
new file mode 100644
index 0000000..fa2dde0
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PortGetterDriver.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2014-2016 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.ImmutableList;
+import com.google.common.collect.Lists;
+import org.onosproject.bmv2.api.Bmv2Exception;
+import org.onosproject.bmv2.ctl.Bmv2ThriftClient;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.behaviour.PortDiscovery;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collections;
+import java.util.List;
+
+public class Bmv2PortGetterDriver extends AbstractHandlerBehaviour
+ implements PortDiscovery {
+
+ private final Logger log =
+ LoggerFactory.getLogger(this.getClass());
+
+ @Override
+ public List<PortDescription> getPorts() {
+ Bmv2ThriftClient deviceClient;
+ try {
+ deviceClient = Bmv2ThriftClient.of(handler().data().deviceId());
+ } catch (Bmv2Exception e) {
+ log.error("Failed to connect to Bmv2 device", e);
+ return Collections.emptyList();
+ }
+
+ List<PortDescription> portDescriptions = Lists.newArrayList();
+
+ try {
+
+ deviceClient.getPortsInfo().forEach(
+ p -> {
+ DefaultAnnotations.Builder builder =
+ DefaultAnnotations.builder();
+ p.getExtraProperties().forEach(builder::set);
+ SparseAnnotations annotations = builder.build();
+
+ portDescriptions.add(new DefaultPortDescription(
+ PortNumber.portNumber(
+ (long) p.portNumber(),
+ p.ifaceName()),
+ p.isUp(),
+ annotations
+ ));
+ });
+ } catch (Bmv2Exception e) {
+ log.error("Unable to get port description from Bmv2 device", e);
+ }
+
+ return ImmutableList.copyOf(portDescriptions);
+ }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java
new file mode 100644
index 0000000..95cb23d
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014-2016 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.
+ */
+
+/**
+ * BMv2 driver implementation.
+ */
+package org.onosproject.drivers.bmv2;
\ No newline at end of file
diff --git a/drivers/bmv2/src/main/resources/bmv2-drivers.xml b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
new file mode 100644
index 0000000..279d470
--- /dev/null
+++ b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ ~ Copyright 2014-2016 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.
+ -->
+<drivers>
+ <driver name="bmv2-thrift" manufacturer="p4.org" hwVersion="bmv2" swVersion="unknown">
+ <behaviour api="org.onosproject.net.behaviour.PortDiscovery"
+ impl="org.onosproject.drivers.bmv2.Bmv2PortGetterDriver"/>
+ <behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
+ impl="org.onosproject.drivers.bmv2.Bmv2FlowRuleDriver"/>
+ </driver>
+</drivers>
diff --git a/drivers/pom.xml b/drivers/pom.xml
index 981dadb..e9252ee 100644
--- a/drivers/pom.xml
+++ b/drivers/pom.xml
@@ -41,6 +41,7 @@
<module>utilities</module>
<module>lumentum</module>
<module>bti</module>
+ <module>bmv2</module>
</modules>
<!--<properties>
diff --git a/protocols/bmv2/pom.xml b/protocols/bmv2/pom.xml
new file mode 100644
index 0000000..3d82a36
--- /dev/null
+++ b/protocols/bmv2/pom.xml
@@ -0,0 +1,240 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2014-2016 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>onos-protocols</artifactId>
+ <groupId>org.onosproject</groupId>
+ <version>1.6.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>onos-bmv2-protocol</artifactId>
+ <version>1.6.0-SNAPSHOT</version>
+
+ <packaging>bundle</packaging>
+
+ <description>BMv2 protocol subsystem</description>
+
+ <properties>
+ <!-- BMv2 Commit ID and Thrift version -->
+ <bmv2.commit>a012ee4124c1892a91a359660824d311d5d7fe88</bmv2.commit>
+ <bmv2.thrift.version>0.9.3</bmv2.thrift.version>
+ <!-- Do not change below -->
+ <bmv2.baseurl>
+ https://raw.githubusercontent.com/p4lang/behavioral-model/${bmv2.commit}
+ </bmv2.baseurl>
+ <bmv2.thrift.srcdir>${project.basedir}/src/main/thrift</bmv2.thrift.srcdir>
+ <thrift.path>${project.build.directory}/thrift-compiler/</thrift.path>
+ <thrift.filename>thrift-${os.detected.classifier}.exe</thrift.filename>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.thrift</groupId>
+ <artifactId>libthrift</artifactId>
+ <version>${bmv2.thrift.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+
+ <repositories>
+ <!-- Needed for thrift-compiler, which is hosted on GitHub -->
+ <repository>
+ <id>jitpack.io</id>
+ <url>https://jitpack.io</url>
+ </repository>
+ </repositories>
+
+ <build>
+ <extensions>
+ <extension>
+ <groupId>kr.motd.maven</groupId>
+ <artifactId>os-maven-plugin</artifactId>
+ <version>1.4.0.Final</version>
+ </extension>
+ </extensions>
+
+ <plugins>
+ <!-- Download Thrift source files from BMv2 Github repo -->
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>wagon-maven-plugin</artifactId>
+ <version>1.0</version>
+ <executions>
+ <execution>
+ <id>download-bmv2-thrift-standard</id>
+ <phase>validate</phase>
+ <goals>
+ <goal>download-single</goal>
+ </goals>
+ <configuration>
+ <url>${bmv2.baseurl}</url>
+ <fromFile>thrift_src/standard.thrift</fromFile>
+ <toDir>${bmv2.thrift.srcdir}</toDir>
+ </configuration>
+ </execution>
+ <execution>
+ <id>download-bmv2-thrift-simple_pre</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>download-single</goal>
+ </goals>
+ <configuration>
+ <url>${bmv2.baseurl}</url>
+ <fromFile>thrift_src/simple_pre.thrift</fromFile>
+ <toDir>${bmv2.thrift.srcdir}</toDir>
+ </configuration>
+ </execution>
+ <execution>
+ <id>download-bmv2-thrift-simple_pre_lag</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>download-single</goal>
+ </goals>
+ <configuration>
+ <url>${bmv2.baseurl}</url>
+ <fromFile>thrift_src/simple_pre_lag.thrift
+ </fromFile>
+ <toDir>${bmv2.thrift.srcdir}</toDir>
+ </configuration>
+ </execution>
+ <execution>
+ <id>download-bmv2-thrift-simple_switch</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>download-single</goal>
+ </goals>
+ <configuration>
+ <url>${bmv2.baseurl}</url>
+ <fromFile>
+ targets/simple_switch/thrift/simple_switch.thrift
+ </fromFile>
+ <toDir>${bmv2.thrift.srcdir}</toDir>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- Extract Thrift compiler -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>unpack</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>unpack</goal>
+ </goals>
+ <configuration>
+ <artifactItems>
+ <artifactItem>
+ <groupId>com.github.ccascone</groupId>
+ <artifactId>mvn-thrift-compiler</artifactId>
+ <version>1.1_${bmv2.thrift.version}</version>
+ <type>jar</type>
+ <includes>${thrift.filename}</includes>
+ <outputDirectory>${project.build.directory}/thrift-compiler</outputDirectory>
+ </artifactItem>
+ </artifactItems>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- Add missing java namespace to Thrift files -->
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.4.0</version>
+ <executions>
+ <execution>
+ <id>add-bmv2-thrift-java-namespace</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ <configuration>
+ <executable>${bmv2.thrift.srcdir}/patch.sh
+ </executable>
+ </configuration>
+ </execution>
+ <execution>
+ <id>set-thrift-compiler-permissions</id>
+ <phase>initialize</phase>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ <configuration>
+ <executable>chmod</executable>
+ <arguments>
+ <argument>+x</argument>
+ <argument>${thrift.path}/${thrift.filename}</argument>
+ </arguments>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- Compile Thrift files -->
+ <plugin>
+ <groupId>org.apache.thrift.tools</groupId>
+ <artifactId>maven-thrift-plugin</artifactId>
+ <version>0.1.11</version>
+ <configuration>
+ <thriftExecutable>${thrift.path}/${thrift.filename}</thriftExecutable>
+ </configuration>
+ <executions>
+ <execution>
+ <id>thrift-sources</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <!-- Make generated sources visible -->
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>1.4</version>
+ <executions>
+ <execution>
+ <id>add-thrift-sources-to-path</id>
+ <phase>generate-sources</phase>
+ <goals>
+ <goal>add-source</goal>
+ </goals>
+ <configuration>
+ <sources>
+ <source>
+ ${project.build.directory}/target/generated-sources/thrift
+ </source>
+ </sources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Action.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Action.java
new file mode 100644
index 0000000..5bbc50b
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Action.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Bmv2 Action representation.
+ */
+public final class Bmv2Action {
+
+ private final String name;
+ private final List<ByteBuffer> parameters;
+
+ private Bmv2Action(String name, List<ByteBuffer> parameters) {
+ // hide constructor
+ this.name = name;
+ this.parameters = parameters;
+ }
+
+ /**
+ * Returns a new action builder.
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Get action name.
+ *
+ * @return action name
+ */
+ public final String name() {
+ return name;
+ }
+
+ /**
+ * Get list of action parameters ordered as per P4 action definition.
+ *
+ * @return List of action parameters
+ */
+ public final List<ByteBuffer> parameters() {
+ return Collections.unmodifiableList(parameters);
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(name, parameters);
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final Bmv2Action other = (Bmv2Action) obj;
+ return Objects.equals(this.name, other.name)
+ && Objects.equals(this.parameters, other.parameters);
+ }
+
+ @Override
+ public final String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("name", name)
+ .add("parameters", parameters)
+ .toString();
+ }
+
+ /**
+ * A Bmv2 action builder.
+ */
+ public static final class Builder {
+
+ private String name;
+ private List<ByteBuffer> parameters;
+
+ private Builder() {
+ this.parameters = Lists.newArrayList();
+ }
+
+ /**
+ * Set the action name.
+ *
+ * @param actionName a string value
+ * @return this
+ */
+ public Builder withName(String actionName) {
+ this.name = actionName;
+ return this;
+ }
+
+ /**
+ * Add a parameter at the end of the parameters list.
+ *
+ * @param parameter a ByteBuffer value
+ * @return this
+ */
+ public Builder addParameter(ByteBuffer parameter) {
+ parameters.add(parameter);
+ return this;
+ }
+
+ /**
+ * Builds a Bmv2 action object.
+ *
+ * @return a Bmv2 action
+ */
+ public Bmv2Action build() {
+ checkNotNull(name, "Action name not set");
+ return new Bmv2Action(name, parameters);
+ }
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Exception.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Exception.java
new file mode 100644
index 0000000..a428491
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2Exception.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+/**
+ * General Bmv2 exception.
+ */
+public class Bmv2Exception extends Exception {
+
+ public Bmv2Exception(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionSelector.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionSelector.java
new file mode 100644
index 0000000..1bd9f1c
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionSelector.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+import org.onlab.util.KryoNamespace;
+import org.onosproject.net.flow.AbstractExtension;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flow.criteria.ExtensionSelectorType;
+
+public class Bmv2ExtensionSelector extends AbstractExtension implements ExtensionSelector {
+
+ private final KryoNamespace appKryo = new KryoNamespace.Builder().build();
+ private Bmv2MatchKey matchKey;
+
+ public Bmv2ExtensionSelector(Bmv2MatchKey matchKey) {
+ this.matchKey = matchKey;
+ }
+
+ public Bmv2MatchKey matchKey() {
+ return matchKey;
+ }
+
+ @Override
+ public ExtensionSelectorType type() {
+ return ExtensionSelectorType.ExtensionSelectorTypes.P4_BMV2_MATCH_KEY.type();
+ }
+
+ @Override
+ public byte[] serialize() {
+ return appKryo.serialize(matchKey);
+ }
+
+ @Override
+ public void deserialize(byte[] data) {
+ matchKey = appKryo.deserialize(data);
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionTreatment.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionTreatment.java
new file mode 100644
index 0000000..cf48e38
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2ExtensionTreatment.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+import org.onlab.util.KryoNamespace;
+import org.onosproject.net.flow.AbstractExtension;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
+
+public class Bmv2ExtensionTreatment extends AbstractExtension implements ExtensionTreatment {
+
+ private final KryoNamespace appKryo = new KryoNamespace.Builder().build();
+ private Bmv2Action action;
+
+ public Bmv2ExtensionTreatment(Bmv2Action action) {
+ this.action = action;
+ }
+
+ public Bmv2Action getAction() {
+ return action;
+ }
+
+ @Override
+ public ExtensionTreatmentType type() {
+ return ExtensionTreatmentType.ExtensionTreatmentTypes.P4_BMV2_ACTION.type();
+ }
+
+ @Override
+ public byte[] serialize() {
+ return appKryo.serialize(action);
+ }
+
+ @Override
+ public void deserialize(byte[] data) {
+ action = appKryo.deserialize(data);
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2MatchKey.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2MatchKey.java
new file mode 100644
index 0000000..d068328
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2MatchKey.java
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.Lists;
+import org.p4.bmv2.thrift.BmMatchParam;
+import org.p4.bmv2.thrift.BmMatchParamExact;
+import org.p4.bmv2.thrift.BmMatchParamLPM;
+import org.p4.bmv2.thrift.BmMatchParamTernary;
+import org.p4.bmv2.thrift.BmMatchParamType;
+import org.p4.bmv2.thrift.BmMatchParamValid;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Bmv2 match key representation.
+ */
+public final class Bmv2MatchKey {
+
+ private final List<BmMatchParam> matchParams;
+
+ /**
+ * Creates a new match key.
+ *
+ * @param matchParams The ordered list of match parameters
+ */
+ private Bmv2MatchKey(List<BmMatchParam> matchParams) {
+ this.matchParams = matchParams;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Returns the match parameters defined for this match key (read-only).
+ *
+ * @return match parameters
+ */
+ public final List<BmMatchParam> bmMatchParams() {
+ return Collections.unmodifiableList(matchParams);
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hashCode(matchParams);
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final Bmv2MatchKey other = (Bmv2MatchKey) obj;
+
+ return Objects.equals(this.matchParams, other.matchParams);
+ }
+
+ @Override
+ public final String toString() {
+ return MoreObjects.toStringHelper(this)
+ .addValue(matchParams)
+ .toString();
+ }
+
+ /**
+ * Builder of a Bmv2 match key.
+ */
+ public static final class Builder {
+
+ private List<BmMatchParam> matchParams;
+
+ private Builder() {
+ this.matchParams = Lists.newArrayList();
+ }
+
+ /**
+ * Adds an exact match parameter.
+ *
+ * @param key a ByteBuffer value
+ * @return this
+ */
+ public Builder withExact(ByteBuffer key) {
+ this.matchParams.add(
+ new BmMatchParam(BmMatchParamType.EXACT)
+ .setExact(new BmMatchParamExact(key)));
+ return this;
+ }
+
+
+ /**
+ * Adds a longest prefix match parameter.
+ *
+ * @param key a ByteBuffer value
+ * @param prefixLength an integer value
+ * @return this
+ */
+ public Builder withLpm(ByteBuffer key, int prefixLength) {
+ this.matchParams.add(
+ new BmMatchParam(BmMatchParamType.LPM)
+ .setLpm(new BmMatchParamLPM(key, prefixLength)));
+ return this;
+ }
+
+ /**
+ * Adds a ternary match parameter.
+ *
+ * @param key a ByteBuffer value
+ * @param mask an ByteBuffer value
+ * @return this
+ */
+ public Builder withTernary(ByteBuffer key, ByteBuffer mask) {
+ this.matchParams.add(
+ new BmMatchParam(BmMatchParamType.TERNARY).
+ setTernary(new BmMatchParamTernary(key, mask)));
+ return this;
+ }
+
+ /**
+ * Adds a ternary match parameter where all bits are don't-care.
+ *
+ * @param byteLength an integer value representing the length in byte of the parameter
+ * @return this
+ */
+ public Builder withWildcard(int byteLength) {
+ byte[] zeros = new byte[byteLength];
+ Arrays.fill(zeros, (byte) 0);
+ return this.withTernary(ByteBuffer.wrap(zeros), ByteBuffer.wrap(zeros));
+ }
+
+ /**
+ * Adds a valid match parameter.
+ *
+ * @param key a boolean value
+ * @return this
+ */
+ public Builder withValid(boolean key) {
+ this.matchParams.add(
+ new BmMatchParam(BmMatchParamType.VALID)
+ .setValid(new BmMatchParamValid(key)));
+ return this;
+ }
+
+ /**
+ * Builds a new match key object.
+ *
+ * @return match key
+ */
+ public Bmv2MatchKey build() {
+ return new Bmv2MatchKey(this.matchParams);
+ }
+ }
+}
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2PortInfo.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2PortInfo.java
new file mode 100644
index 0000000..3f36ade
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2PortInfo.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+import com.google.common.base.MoreObjects;
+import org.p4.bmv2.thrift.DevMgrPortInfo;
+
+import java.util.Collections;
+import java.util.Map;
+
+/**
+ * Bmv2 representation of a switch port information.
+ */
+public final class Bmv2PortInfo {
+
+ private final DevMgrPortInfo portInfo;
+
+ public Bmv2PortInfo(DevMgrPortInfo portInfo) {
+ this.portInfo = portInfo;
+ }
+
+ public final String ifaceName() {
+ return portInfo.getIface_name();
+ }
+
+ public final int portNumber() {
+ return portInfo.getPort_num();
+ }
+
+ public final boolean isUp() {
+ return portInfo.isIs_up();
+ }
+
+ public final Map<String, String> getExtraProperties() {
+ return Collections.unmodifiableMap(portInfo.getExtra());
+ }
+
+ @Override
+ public final String toString() {
+ return MoreObjects.toStringHelper(this)
+ .addValue(portInfo)
+ .toString();
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2TableEntry.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2TableEntry.java
new file mode 100644
index 0000000..4bf0291
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/Bmv2TableEntry.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2014-2016 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.bmv2.api;
+
+import com.google.common.base.Preconditions;
+
+import java.util.Objects;
+
+/**
+ * Bmv2 representation of a table entry.
+ */
+public final class Bmv2TableEntry {
+
+ private static final int NO_PRIORITY_VALUE = -1;
+ private static final int NO_TIMEOUT_VALUE = -1;
+
+ private final String tableName;
+ private final Bmv2MatchKey matchKey;
+ private final Bmv2Action action;
+ private final int priority;
+ private final double timeout;
+
+ private Bmv2TableEntry(String tableName, Bmv2MatchKey matchKey,
+ Bmv2Action action, int priority, double timeout) {
+ this.tableName = tableName;
+ this.matchKey = matchKey;
+ this.action = action;
+ this.priority = priority;
+ this.timeout = timeout;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Returns the name of the table where this entry is installed.
+ *
+ * @return table name
+ */
+ public final String tableName() {
+ return this.tableName;
+ }
+
+ /**
+ * Returns the match key.
+ *
+ * @return match key
+ */
+ public final Bmv2MatchKey matchKey() {
+ return matchKey;
+ }
+
+ /**
+ * Returns the action.
+ *
+ * @return action
+ */
+ public final Bmv2Action action() {
+ return action;
+ }
+
+ /**
+ * Returns true is the entry has a valid priority value set.
+ *
+ * @return true if priority is set, false elsewhere
+ */
+ public final boolean hasPriority() {
+ return this.priority != NO_PRIORITY_VALUE;
+ }
+
+ /**
+ * Return the entry priority.
+ *
+ * @return priority
+ */
+ public final int priority() {
+ return priority;
+ }
+
+ /**
+ * Returns true is the entry has a valid timeout value set.
+ *
+ * @return true if timeout is set, false elsewhere
+ */
+ public final boolean hasTimeout() {
+ return this.timeout != NO_PRIORITY_VALUE;
+ }
+
+ /**
+ * Returns the entry timeout in fractional seconds.
+ *
+ * @return timeout
+ */
+ public final double timeout() {
+ return timeout;
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(matchKey, action, priority, timeout);
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final Bmv2TableEntry other = (Bmv2TableEntry) obj;
+ return Objects.equals(this.matchKey, other.matchKey)
+ && Objects.equals(this.action, other.action)
+ && Objects.equals(this.priority, other.priority)
+ && Objects.equals(this.timeout, other.timeout);
+ }
+
+ @Override
+ public final String toString() {
+ return com.google.common.base.MoreObjects.toStringHelper(this)
+ .addValue(matchKey)
+ .addValue(action)
+ .add("priority", priority)
+ .add("timeout", timeout)
+ .toString();
+ }
+
+ public static final class Builder {
+
+ private String tableName;
+ private Bmv2MatchKey matchKey;
+ private Bmv2Action action;
+ private int priority = NO_PRIORITY_VALUE;
+ private double timeout = NO_TIMEOUT_VALUE;
+
+ private Builder() {
+ // hide constructor
+ }
+
+ /**
+ * Sets the table name.
+ *
+ * @param tableName a string value
+ * @return this
+ */
+ public Builder withTableName(String tableName) {
+ this.tableName = tableName;
+ return this;
+ }
+
+ /**
+ * Sets the match key.
+ *
+ * @param matchKey a match key value
+ * @return this
+ */
+ public Builder withMatchKey(Bmv2MatchKey matchKey) {
+ this.matchKey = matchKey;
+ return this;
+ }
+
+ /**
+ * Sets the action.
+ *
+ * @param action an action value
+ * @return this
+ */
+ public Builder withAction(Bmv2Action action) {
+ this.action = action;
+ return this;
+ }
+
+ public Builder withPriority(int priority) {
+ this.priority = priority;
+ return this;
+ }
+
+ /**
+ * Sets the timeout.
+ *
+ * @param timeout a timeout value in fractional seconds
+ * @return this
+ */
+ public Builder withTimeout(double timeout) {
+ this.timeout = timeout;
+ return this;
+ }
+
+ /**
+ * Build the table entry.
+ *
+ * @return a new table entry object
+ */
+ public Bmv2TableEntry build() {
+ Preconditions.checkNotNull(tableName);
+ Preconditions.checkNotNull(matchKey);
+ Preconditions.checkNotNull(action);
+
+ return new Bmv2TableEntry(tableName, matchKey, action, priority,
+ timeout);
+
+ }
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/package-info.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/package-info.java
new file mode 100644
index 0000000..d8946e3
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014-2016 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.
+ */
+
+/**
+ * Bmv2 API abstractions.
+ */
+package org.onosproject.bmv2.api;
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/ctl/Bmv2ThriftClient.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/ctl/Bmv2ThriftClient.java
new file mode 100644
index 0000000..c3c86c1
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/ctl/Bmv2ThriftClient.java
@@ -0,0 +1,335 @@
+/*
+ * Copyright 2014-2016 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.bmv2.ctl;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.RemovalNotification;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TMultiplexedProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.onosproject.bmv2.api.Bmv2Action;
+import org.onosproject.bmv2.api.Bmv2Exception;
+import org.onosproject.bmv2.api.Bmv2PortInfo;
+import org.onosproject.bmv2.api.Bmv2TableEntry;
+import org.onosproject.net.DeviceId;
+import org.p4.bmv2.thrift.BmAddEntryOptions;
+import org.p4.bmv2.thrift.DevMgrPortInfo;
+import org.p4.bmv2.thrift.Standard;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Implementation of a Thrift client to control the Bmv2 switch.
+ */
+public final class Bmv2ThriftClient {
+ /*
+ FIXME: derive context_id from device id
+ Using different context id values should serve to control different
+ switches responding to the same IP address and port
+ */
+ private static final int CONTEXT_ID = 0;
+ /*
+ Static transport/client cache:
+ - avoids opening a new transport session when there's one already open
+ - close the connection after a predefined timeout of 5 seconds
+ */
+ private static LoadingCache<DeviceId, Pair<TTransport, Standard.Iface>>
+ clientCache = CacheBuilder.newBuilder()
+ .expireAfterAccess(5, TimeUnit.SECONDS)
+ .removalListener(new ClientRemovalListener())
+ .build(new ClientLoader());
+ private final Standard.Iface stdClient;
+
+ // ban constructor
+ private Bmv2ThriftClient(Standard.Iface stdClient) {
+ this.stdClient = stdClient;
+ }
+
+ /**
+ * Returns a client object to control the passed device.
+ *
+ * @param deviceId device id
+ * @return bmv2 client object
+ * @throws Bmv2Exception if a connection to the device cannot be established
+ */
+ public static Bmv2ThriftClient of(DeviceId deviceId) throws Bmv2Exception {
+ try {
+ checkNotNull(deviceId, "deviceId cannot be null");
+ return new Bmv2ThriftClient(clientCache.get(deviceId).getValue());
+ } catch (ExecutionException e) {
+ throw new Bmv2Exception(e.getMessage(), e.getCause());
+ }
+ }
+
+ /**
+ * Pings the device. Returns true if the device is reachable,
+ * false otherwise.
+ *
+ * @param deviceId device id
+ * @return true if reachable, false otherwise
+ */
+ public static boolean ping(DeviceId deviceId) {
+ // poll ports status as workaround to assess device reachability
+ try {
+ of(deviceId).stdClient.bm_dev_mgr_show_ports();
+ return true;
+ } catch (TException | Bmv2Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ * Parse device ID into host and port.
+ *
+ * @param did device ID
+ * @return a pair of host and port
+ */
+ private static Pair<String, Integer> parseDeviceId(DeviceId did) {
+ String[] info = did.toString().split(":");
+ if (info.length == 3) {
+ String host = info[1];
+ int port = Integer.parseInt(info[2]);
+ return ImmutablePair.of(host, port);
+ } else {
+ throw new IllegalArgumentException(
+ "Unable to parse BMv2 device ID "
+ + did.toString()
+ + ", expected format is scheme:host:port");
+ }
+ }
+
+ /**
+ * Adds a new table entry.
+ *
+ * @param entry a table entry value
+ * @return table-specific entry ID
+ * @throws Bmv2Exception if any error occurs
+ */
+ public final long addTableEntry(Bmv2TableEntry entry) throws Bmv2Exception {
+
+ long entryId = -1;
+
+ try {
+ BmAddEntryOptions options = new BmAddEntryOptions();
+
+ if (entry.hasPriority()) {
+ options.setPriority(entry.priority());
+ }
+
+ entryId = stdClient.bm_mt_add_entry(
+ CONTEXT_ID,
+ entry.tableName(),
+ entry.matchKey().bmMatchParams(),
+ entry.action().name(),
+ entry.action().parameters(),
+ options);
+
+ if (entry.hasTimeout()) {
+ /* bmv2 accepts timeouts in milliseconds */
+ int msTimeout = (int) Math.round(entry.timeout() * 1_000);
+ stdClient.bm_mt_set_entry_ttl(
+ CONTEXT_ID, entry.tableName(), entryId, msTimeout);
+ }
+
+ return entryId;
+
+ } catch (TException e) {
+ if (entryId != -1) {
+ try {
+ stdClient.bm_mt_delete_entry(
+ CONTEXT_ID, entry.tableName(), entryId);
+ } catch (TException e1) {
+ // this should never happen as we know the entry is there
+ throw new Bmv2Exception(e1.getMessage(), e1);
+ }
+ }
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Modifies a currently installed entry by updating its action.
+ *
+ * @param tableName string value of table name
+ * @param entryId long value of entry ID
+ * @param action an action value
+ * @throws Bmv2Exception if any error occurs
+ */
+ public final void modifyTableEntry(String tableName,
+ long entryId, Bmv2Action action)
+ throws Bmv2Exception {
+
+ try {
+ stdClient.bm_mt_modify_entry(
+ CONTEXT_ID,
+ tableName,
+ entryId,
+ action.name(),
+ action.parameters()
+ );
+ } catch (TException e) {
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Deletes currently installed entry.
+ *
+ * @param tableName string value of table name
+ * @param entryId long value of entry ID
+ * @throws Bmv2Exception if any error occurs
+ */
+ public final void deleteTableEntry(String tableName,
+ long entryId) throws Bmv2Exception {
+
+ try {
+ stdClient.bm_mt_delete_entry(CONTEXT_ID, tableName, entryId);
+ } catch (TException e) {
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Sets table default action.
+ *
+ * @param tableName string value of table name
+ * @param action an action value
+ * @throws Bmv2Exception if any error occurs
+ */
+ public final void setTableDefaultAction(String tableName, Bmv2Action action)
+ throws Bmv2Exception {
+
+ try {
+ stdClient.bm_mt_set_default_action(
+ CONTEXT_ID,
+ tableName,
+ action.name(),
+ action.parameters());
+ } catch (TException e) {
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Returns information of the ports currently configured in the switch.
+ *
+ * @return collection of port information
+ * @throws Bmv2Exception if any error occurs
+ */
+ public Collection<Bmv2PortInfo> getPortsInfo() throws Bmv2Exception {
+
+ try {
+ List<DevMgrPortInfo> portInfos = stdClient.bm_dev_mgr_show_ports();
+
+ Collection<Bmv2PortInfo> bmv2PortInfos = Lists.newArrayList();
+
+ bmv2PortInfos.addAll(
+ portInfos.stream()
+ .map(Bmv2PortInfo::new)
+ .collect(Collectors.toList()));
+
+ return bmv2PortInfos;
+
+ } catch (TException e) {
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Return a string representation of a table content.
+ *
+ * @param tableName string value of table name
+ * @return table string dump
+ * @throws Bmv2Exception if any error occurs
+ */
+ public String dumpTable(String tableName) throws Bmv2Exception {
+
+ try {
+ return stdClient.bm_dump_table(CONTEXT_ID, tableName);
+ } catch (TException e) {
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Reset the state of the switch (e.g. delete all entries, etc.).
+ *
+ * @throws Bmv2Exception if any error occurs
+ */
+ public void resetState() throws Bmv2Exception {
+
+ try {
+ stdClient.bm_reset_state();
+ } catch (TException e) {
+ throw new Bmv2Exception(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Transport/client cache loader.
+ */
+ private static class ClientLoader
+ extends CacheLoader<DeviceId, Pair<TTransport, Standard.Iface>> {
+
+ @Override
+ public Pair<TTransport, Standard.Iface> load(DeviceId deviceId)
+ throws TTransportException {
+ Pair<String, Integer> info = parseDeviceId(deviceId);
+ //make the expensive call
+ TTransport transport = new TSocket(
+ info.getLeft(), info.getRight());
+ TProtocol protocol = new TBinaryProtocol(transport);
+ Standard.Iface stdClient = new Standard.Client(
+ new TMultiplexedProtocol(protocol, "standard"));
+
+ transport.open();
+
+ return ImmutablePair.of(transport, stdClient);
+ }
+ }
+
+ /**
+ * Client cache removal listener. Close the connection on cache removal.
+ */
+ private static class ClientRemovalListener implements
+ RemovalListener<DeviceId, Pair<TTransport, Standard.Iface>> {
+
+ @Override
+ public void onRemoval(
+ RemovalNotification<DeviceId, Pair<TTransport, Standard.Iface>>
+ notification) {
+ // close the transport connection
+ notification.getValue().getKey().close();
+ }
+ }
+}
diff --git a/protocols/bmv2/src/main/java/org/onosproject/bmv2/ctl/package-info.java b/protocols/bmv2/src/main/java/org/onosproject/bmv2/ctl/package-info.java
new file mode 100644
index 0000000..9e79ae9
--- /dev/null
+++ b/protocols/bmv2/src/main/java/org/onosproject/bmv2/ctl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014-2016 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.
+ */
+
+/**
+ * BMv2 Thrift client implementation.
+ */
+package org.onosproject.bmv2.ctl;
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/thrift/.gitignore b/protocols/bmv2/src/main/thrift/.gitignore
new file mode 100644
index 0000000..bd21b8c
--- /dev/null
+++ b/protocols/bmv2/src/main/thrift/.gitignore
@@ -0,0 +1 @@
+*.thrift
\ No newline at end of file
diff --git a/protocols/bmv2/src/main/thrift/patch.sh b/protocols/bmv2/src/main/thrift/patch.sh
new file mode 100755
index 0000000..b3fc40f
--- /dev/null
+++ b/protocols/bmv2/src/main/thrift/patch.sh
@@ -0,0 +1,27 @@
+#! /bin/bash
+#
+# Copyright 2014-2016 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.
+
+# exit on errors
+set -e
+
+basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+ns="org.p4.bmv2.thrift"
+
+# add java namespace at beginning of file
+for f in ${basedir}/*.thrift
+do
+ echo "namespace java ${ns}" | cat - ${f} > temp && mv temp ${f}
+done
\ No newline at end of file
diff --git a/protocols/pom.xml b/protocols/pom.xml
index c48870e..c0e1d05 100644
--- a/protocols/pom.xml
+++ b/protocols/pom.xml
@@ -41,6 +41,7 @@
<module>ospf</module>
<module>isis</module>
<module>snmp</module>
+ <module>bmv2</module>
</modules>
<dependencies>