ONOS-7251 - Initial implementation of fabric.p4 L2 broadcast feature.

Thrift client cherry-picked from the commit dd5792ac9ee38a702c3128a34224852b5c284687

Change-Id: I989f2b2074485a892195889a7c976b518510da88
diff --git a/protocols/bmv2/pom.xml b/protocols/bmv2/pom.xml
new file mode 100644
index 0000000..13c545e
--- /dev/null
+++ b/protocols/bmv2/pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2017-present Open Networking Foundation
+  ~
+  ~ 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.13.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-bmv2-protocol</artifactId>
+
+    <modules>
+        <module>thrift-api</module>
+    </modules>
+
+    <packaging>pom</packaging>
+
+    <description>BMv2 protocol subsystem</description>
+
+</project>
\ No newline at end of file
diff --git a/protocols/bmv2/thrift-api/BUCK b/protocols/bmv2/thrift-api/BUCK
new file mode 100644
index 0000000..b1d766a
--- /dev/null
+++ b/protocols/bmv2/thrift-api/BUCK
@@ -0,0 +1,102 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:libthrift',
+]
+
+# BMV2_COMMIT should be set to the same value as specified in install-p4-tools.sh
+BMV2_COMMIT = 'ed130d01be985d814c17de949839d484e76400b1'
+BMV2_BASEURL = 'https://cdn.rawgit.com/p4lang/behavioral-model/' + BMV2_COMMIT
+BMV2_NAMESPACE = 'org.onosproject.bmv2.thriftapi'
+
+THRIFT_EXE_BASEURL = 'https://cdn.rawgit.com/ccascone/mvn-thrift-compiler/1.1_0.9.3/exe/'
+THRIFT_EXE_SHA1S = {
+    'thrift-linux-x86_64.exe':'9b7b5d6eabc9552b8227e8f63981bc15c0985dd5',
+    'thrift-osx-x86_64.exe':'b9215c5141f56fd277b7cf41d9745af847afe498'
+}
+
+def prebuilt_thrift_compiler():
+    import platform
+    os_name = platform.system().lower()
+    if os_name == 'darwin':
+        os_name = 'osx'
+    arch = '%s-%s' % (os_name, platform.machine())
+    fname = 'thrift-%s.exe' % arch
+    if fname not in THRIFT_EXE_SHA1S:
+        raise Exception('Cannot download thrift compiler, architecture %s not supported' % arch)
+    remote_file(
+        name = 'thrift-binary',
+        out = 'thrift.binary',
+        url = THRIFT_EXE_BASEURL + fname,
+        sha1 = THRIFT_EXE_SHA1S[fname],
+    )
+    genrule (
+        name = 'thrift-exe',
+        srcs = [ ':thrift-binary' ],
+        bash = 'cp $(location :thrift-binary) $OUT && chmod +x $OUT',
+        executable = True,
+        out = 'thrift.exe'
+    )
+
+prebuilt_thrift_compiler()
+
+# TODO: or export local thrift executable
+# export_file(
+#       name = 'thrift-exe',
+#       src = '/usr/bin/thrift',
+#     )
+
+def remote_thrift_def(
+    name,
+    url,
+    sha1):
+    # Download *.thrift definition file.
+    remote_file(
+        name = name + '-rem',
+        out = name + '.thrift',
+        url = url,
+        sha1 = sha1,
+    )
+    # Add java namespace.
+    genrule (
+        name = name+'-ns',
+        srcs = [':' + name + '-rem'],
+        bash = 'cp $(location :' + name + '-rem) $OUT && '
+                + 'echo "namespace java ' + BMV2_NAMESPACE + '" | '
+                + 'cat - $OUT > temp && mv temp $OUT',
+        out = name + '.thrift',
+    )
+    # Generate Java sources.
+    genrule (
+        name = name+'-gen',
+        srcs = [':' + name + '-ns'],
+        # FIXME: is there a better way to get just the output dir of this rule?
+        # ...not the full file path in $OUT
+        cmd = '$(exe :thrift-exe) -o $SRCDIR/../' + name + '-gen '
+                + '--gen java $SRCDIR/' + name + '.thrift',
+        out = 'gen-java',
+    )
+    # Zip them.
+    zip_file(
+        name = name,
+        out = name + '.src.zip',
+        srcs = [':' + name + '-gen']
+    )
+
+
+remote_thrift_def(
+    name = 'simple_pre_lag',
+    url = BMV2_BASEURL + '/thrift_src/simple_pre_lag.thrift',
+    sha1 = 'f468ebebc7bb8577f11ca950939f34add5f5634c',
+)
+
+osgi_jar(
+    # If a source ending with *.src.zip is passed, Buck automatically looks for *.java files inside.
+    srcs = [':simple_pre_lag'],
+    deps = COMPILE_DEPS,
+    do_javadocs = False,
+    do_checkstyle = False
+)
+
+project_config(
+    src_target = ':onos-protocols-bmv2-thrift-api'
+)
\ No newline at end of file
diff --git a/protocols/bmv2/thrift-api/pom.xml b/protocols/bmv2/thrift-api/pom.xml
new file mode 100644
index 0000000..f595b68
--- /dev/null
+++ b/protocols/bmv2/thrift-api/pom.xml
@@ -0,0 +1,240 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2018-present Open Networking Foundation
+  ~
+  ~ 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-bmv2-protocol</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.13.0-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-bmv2-protocol-thrift-api</artifactId>
+
+    <packaging>bundle</packaging>
+
+    <properties>
+        <!-- BMv2 Commit ID and Thrift version -->
+        <bmv2.commit>8f675d0284e9e014f1b8ed502ba54e61d68108cf</bmv2.commit>
+        <bmv2.thrift.version>0.9.3</bmv2.thrift.version>
+        <bmv2.baseurl>https://cdn.rawgit.com/opennetworkinglab/onos-bmv2/${bmv2.commit}</bmv2.baseurl>
+        <bmv2.thrift.javanamespace>org.onosproject.bmv2.thriftapi</bmv2.thrift.javanamespace>
+        <bmv2.thrift.srcdir>${project.build.directory}/thrift-sources/${bmv2.commit}/</bmv2.thrift.srcdir>
+        <thrift.exedir>${project.build.directory}/thrift-compiler/</thrift.exedir>
+        <thrift.exefilename>thrift-${os.detected.classifier}.exe</thrift.exefilename>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.thrift</groupId>
+            <artifactId>libthrift</artifactId>
+            <version>0.9.3</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>com.googlecode.maven-download-plugin</groupId>
+                <artifactId>download-maven-plugin</artifactId>
+                <version>1.3.0</version>
+                <executions>
+                    <execution>
+                        <id>download-bmv2-thrift-standard</id>
+                        <phase>initialize</phase>
+                        <goals>
+                            <goal>wget</goal>
+                        </goals>
+                        <configuration>
+                            <url>${bmv2.baseurl}/thrift_src/standard.thrift</url>
+                            <outputDirectory>${bmv2.thrift.srcdir}</outputDirectory>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>download-bmv2-thrift-simple_pre</id>
+                        <phase>initialize</phase>
+                        <goals>
+                            <goal>wget</goal>
+                        </goals>
+                        <configuration>
+                            <url>${bmv2.baseurl}/thrift_src/simple_pre.thrift</url>
+                            <outputDirectory>${bmv2.thrift.srcdir}</outputDirectory>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>download-bmv2-thrift-simple_pre_lag</id>
+                        <phase>initialize</phase>
+                        <goals>
+                            <goal>wget</goal>
+                        </goals>
+                        <configuration>
+                            <url>${bmv2.baseurl}/thrift_src/simple_pre_lag.thrift</url>
+                            <outputDirectory>${bmv2.thrift.srcdir}</outputDirectory>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>download-bmv2-thrift-simple_switch</id>
+                        <phase>initialize</phase>
+                        <goals>
+                            <goal>wget</goal>
+                        </goals>
+                        <configuration>
+                            <url>${bmv2.baseurl}/targets/simple_switch/thrift/simple_switch.thrift</url>
+                            <outputDirectory>${bmv2.thrift.srcdir}</outputDirectory>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>download-bmv2-thrift-simple_switch-cpservice</id>
+                        <phase>initialize</phase>
+                        <goals>
+                            <goal>wget</goal>
+                        </goals>
+                        <configuration>
+                            <url>${bmv2.baseurl}/targets/simple_switch/thrift/control_plane.thrift</url>
+                            <outputDirectory>${bmv2.thrift.srcdir}</outputDirectory>
+                        </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.exefilename}</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>${project.basedir}/src/patch.sh</executable>
+                            <arguments>
+                                <argument>${bmv2.thrift.srcdir}</argument>
+                                <argument>${bmv2.thrift.javanamespace}</argument>
+                            </arguments>
+                        </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.exedir}/${thrift.exefilename}</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>
+                    <thriftSourceRoot>${bmv2.thrift.srcdir}</thriftSourceRoot>
+                    <thriftExecutable>${thrift.exedir}/${thrift.exefilename}</thriftExecutable>
+                    <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>thrift-sources</id>
+                        <phase>initialize</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}/generated-sources
+                                </source>
+                            </sources>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file