Start bazel support for protobuf models and p4runtime build

Change-Id: Ib3beb70c1b401854444b106b4af3723cd40635e4
diff --git a/BUILD b/BUILD
index 31383aa..81cd843 100644
--- a/BUILD
+++ b/BUILD
@@ -15,6 +15,8 @@
         "//incubator/net:onos-incubator-net",
         "//protocols/openflow/api:onos-protocols-openflow-api-osgi",
         "//drivers/default:onos-drivers-default-osgi",
+        "//protocols/p4runtime/api:onos-protocols-p4runtime-api-osgi",
+        "//protocols/p4runtime/model:onos-protocols-p4runtime-model-osgi",
         #"//core/net:onos-core-net-osgi",
     ],
 )
diff --git a/WORKSPACE b/WORKSPACE
index e86cb0d..1a47662 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -1,4 +1,6 @@
 load("//tools/build/bazel:generate_workspace.bzl", "generated_maven_jars")
+load("//tools/build/bazel:p4lang_workspace.bzl", "generate_p4lang")
 generated_maven_jars()
+generate_p4lang()
 
 ONOS_VERSION = '1.14.0-SNAPSHOT'
diff --git a/protocols/p4runtime/api/BUILD b/protocols/p4runtime/api/BUILD
index 50a0624..4b86e7f 100644
--- a/protocols/p4runtime/api/BUILD
+++ b/protocols/p4runtime/api/BUILD
@@ -1,14 +1,14 @@
-GRPC_VER = "1.3.1"
+GRPC_VER = "1_3_1"
 
 COMPILE_DEPS = CORE_DEPS + [
-    #    '@grpc//jar',
+    "@grpc_core_" + GRPC_VER + "//jar",
 ]
 
 TEST_DEPS = TEST + [
     "//core/api:onos-api-tests",
 ]
 
-#osgi_jar_with_tests (
-#    deps = COMPILE_DEPS,
-#    test_deps = TEST_DEPS,
-#)
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
diff --git a/protocols/p4runtime/model/BUILD b/protocols/p4runtime/model/BUILD
index 39c314e..5fa75cd 100644
--- a/protocols/p4runtime/model/BUILD
+++ b/protocols/p4runtime/model/BUILD
@@ -1,10 +1,11 @@
 PROTOBUF_VER = "3_2_0"
 
 COMPILE_DEPS = CORE_DEPS + [
-    #'//protocols/p4runtime/proto:onos-protocols-p4runtime-proto',
-    #'@grpc_protobuf_' + PROTOBUF_VER + "//jar",
+    "//protocols/p4runtime/proto:p4_runtime_java_proto",
+    "//protocols/p4runtime/proto:p4_config_java_proto",
+    "@com_google_protobuf//:protobuf_java",
 ]
 
-#osgi_jar_with_tests (
-#    deps = COMPILE_DEPS,
-#)
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+)
diff --git a/protocols/p4runtime/proto/BUILD b/protocols/p4runtime/proto/BUILD
new file mode 100644
index 0000000..2a6d5f5
--- /dev/null
+++ b/protocols/p4runtime/proto/BUILD
@@ -0,0 +1,24 @@
+java_proto_library(
+    name = "p4_types_java_proto",
+    deps = [ "@p4lang_pi//:p4types_proto" ],
+    visibility = ["//visibility:public"],
+)
+
+java_proto_library(
+    name = "p4_config_java_proto",
+    deps = [ "@p4lang_pi//:p4config_proto" ],
+    visibility = ["//visibility:public"],
+)
+
+java_proto_library(
+    name = "p4_tmp_config_java_proto",
+    deps = [ "@p4lang_pi//:p4_tmp_config_proto" ],
+    visibility = ["//visibility:public"],
+)
+
+java_proto_library(
+    name = "p4_runtime_java_proto",
+    deps = [ "@p4lang_pi//:p4_runtime_proto" ],
+    visibility = ["//visibility:public"],
+)
+
diff --git a/tools/build/bazel/google_RPC_BUILD b/tools/build/bazel/google_RPC_BUILD
new file mode 100644
index 0000000..698693b
--- /dev/null
+++ b/tools/build/bazel/google_RPC_BUILD
@@ -0,0 +1,17 @@
+package(default_visibility = ["//visibility:public"])
+
+proto_library(
+    name = "status_proto",
+    srcs = [ "//:google/rpc/status.proto" ],
+    deps = [
+        "@com_google_protobuf//:any_proto",
+    ],
+)
+
+proto_library(
+    name = "code_proto",
+    srcs = [ "//:google/rpc/code.proto" ],
+    deps = [
+        "@com_google_protobuf//:any_proto",
+    ],
+)
diff --git a/tools/build/bazel/osgi-java-library.bzl b/tools/build/bazel/osgi-java-library.bzl
index e47c3d7..c3ed9b3 100644
--- a/tools/build/bazel/osgi-java-library.bzl
+++ b/tools/build/bazel/osgi-java-library.bzl
@@ -36,6 +36,9 @@
 
     # determine the dependencies and build the class path
     for dep in ctx.attr.deps:
+        if len(dep.java.outputs.jars) == 0:
+            continue
+
         file = dep.java.outputs.jars[0].class_jar
 
         if cp:
diff --git a/tools/build/bazel/p4lang_BUILD b/tools/build/bazel/p4lang_BUILD
new file mode 100644
index 0000000..52d7082
--- /dev/null
+++ b/tools/build/bazel/p4lang_BUILD
@@ -0,0 +1,40 @@
+package(default_visibility = ["//visibility:public"])
+
+proto_library(
+    name = "p4types_proto",
+    srcs = [ "//:p4/p4types.proto" ],
+    deps = [
+        "@com_google_protobuf//:any_proto",
+    ],
+)
+
+proto_library(
+    name = "p4config_proto",
+    srcs = [ "//:p4/config/p4info.proto" ],
+    deps = [
+        "@com_google_protobuf//:any_proto",
+        ":p4types_proto",
+    ],
+)
+
+proto_library(
+    name = "p4_tmp_config_proto",
+    srcs = [ "//:p4/tmp/p4config.proto" ],
+    deps = [
+        "@com_google_protobuf//:any_proto",
+        ":p4types_proto",
+    ],
+)
+
+proto_library(
+    name = "p4_runtime_proto",
+    srcs = [ "//:p4/p4runtime.proto" ],
+    deps = [
+        "@com_google_protobuf//:any_proto",
+        "@google_rpc//:status_proto",
+        "@google_rpc//:code_proto",
+        ":p4types_proto",
+        ":p4config_proto",
+        ":p4_tmp_config_proto",
+    ],
+)
\ No newline at end of file
diff --git a/tools/build/bazel/p4lang_workspace.bzl b/tools/build/bazel/p4lang_workspace.bzl
new file mode 100644
index 0000000..03b4e40
--- /dev/null
+++ b/tools/build/bazel/p4lang_workspace.bzl
@@ -0,0 +1,36 @@
+GITHUB_BASE_URL = "https://github.com"
+
+PI_COMMIT_SHORT = "219b3d6"
+PI_COMMIT = "219b3d67299ec09b49f433d7341049256ab5f512"
+PI_REPO = "p4lang/PI"
+
+GOOGLE_RPC_COMMIT_SHORT = "932f273"
+GOOGLE_RPC_COMMIT = "master"
+GOOGLE_RPC_REPO = "googleapis/googleapis"
+
+def _format_github_url(repo, commit):
+    return GITHUB_BASE_URL + "/{0}/tarball/{1}".format(repo, commit)
+
+def generate_p4lang():
+    native.http_archive(
+        name = "com_google_protobuf",
+        sha256 = "cef7f1b5a7c5fba672bec2a319246e8feba471f04dcebfe362d55930ee7c1c30",
+        strip_prefix = "protobuf-3.5.0",
+        urls = ["https://github.com/google/protobuf/archive/v3.5.0.zip"],
+    )
+
+    native.new_http_archive(
+        name = "p4lang_pi",
+        urls = [_format_github_url(PI_REPO, PI_COMMIT)],
+        build_file = "//tools/build/bazel:p4lang_BUILD",
+        strip_prefix = "p4lang-PI-" + PI_COMMIT_SHORT + "/proto",
+        type = "tar.gz",
+    )
+
+    native.new_http_archive(
+        name = "google_rpc",
+        urls = [_format_github_url(GOOGLE_RPC_REPO, GOOGLE_RPC_COMMIT)],
+        build_file = "//tools/build/bazel:google_RPC_BUILD",
+        strip_prefix = "googleapis-googleapis-" + GOOGLE_RPC_COMMIT_SHORT,
+        type = "tar.gz",
+    )
diff --git a/tools/build_rules/prelude_bazel b/tools/build_rules/prelude_bazel
index 138126c..2662c07 100644
--- a/tools/build_rules/prelude_bazel
+++ b/tools/build_rules/prelude_bazel
@@ -1,7 +1,7 @@
 load("//tools/build/bazel:generate_workspace.bzl",
         "generated_java_libraries", "COMPILE", "CORE_DEPS", "JACKSON",
-        "TEST_ADAPTERS", "TEST", "TEST_REST", "METRICS", "KRYO", "NETTY")
+        "TEST_ADAPTERS", "TEST", "TEST_REST", "METRICS", "KRYO", "NETTY", "GRPC_1_3")
 
-load("//tools/build/bazel:osgi-java-library.bzl", "osgi_jar_with_tests", "osgi_jar")
+load("//tools/build/bazel:osgi-java-library.bzl", "osgi_jar_with_tests", "osgi_jar", "wrapped_osgi_library")
 
 generated_java_libraries()