[ONOS-6465] gRPC Protocol and controller
Change-Id: I0ae997f234ce95a78db2db1917f2cbbe3696ccfd
diff --git a/drivers/bmv2/BUCK b/drivers/bmv2/BUCK
index 95b2d44..33d4d66 100644
--- a/drivers/bmv2/BUCK
+++ b/drivers/bmv2/BUCK
@@ -1,5 +1,8 @@
COMPILE_DEPS = [
'//lib:CORE_DEPS',
+ '//protocols/grpc/api:onos-protocols-grpc-api',
+ '//lib:grpc-core-1.3.0',
+ '//lib:grpc-stub-1.3.0'
]
TEST_DEPS = [
@@ -9,9 +12,12 @@
BUNDLES = [
':onos-drivers-bmv2',
+ '//lib:grpc-core-1.3.0',
+ '//lib:grpc-stub-1.3.0',
+ '//protocols/grpc/api:onos-protocols-grpc-api',
]
-osgi_jar_with_tests (
+osgi_jar_with_tests(
deps = COMPILE_DEPS,
test_deps = TEST_DEPS,
resources_root = 'src/main/resources',
@@ -20,10 +26,12 @@
onos_app (
app_name = 'org.onosproject.drivers.bmv2',
- title = 'bmv2 Device Drivers',
+ title = 'BMv2 Device Drivers',
category = 'Drivers',
url = 'http://onosproject.org',
description = 'ONOS BMv2 Device Drivers application.',
included_bundles = BUNDLES,
- required_apps = [ 'org.onosproject.generaldeviceprovider' ],
+ required_apps = [
+ 'org.onosproject.generaldeviceprovider'
+ ],
)
diff --git a/drivers/bmv2/pom.xml b/drivers/bmv2/pom.xml
index f99f555..8227337 100644
--- a/drivers/bmv2/pom.xml
+++ b/drivers/bmv2/pom.xml
@@ -27,8 +27,15 @@
<artifactId>onos-drivers-bmv2</artifactId>
<packaging>bundle</packaging>
-
+
<description>BMv2 device drivers</description>
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-grpc-protocol-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
<properties>
<onos.app.name>org.onosproject.drivers.bmv2</onos.app.name>
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java
index 06f9c9e..8774c09 100644
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2Handshaker.java
@@ -16,6 +16,11 @@
package org.onosproject.drivers.bmv2;
+import org.onosproject.grpc.api.GrpcChannelId;
+import org.onosproject.grpc.api.GrpcController;
+import org.onosproject.grpc.api.GrpcServiceId;
+import org.onosproject.grpc.api.GrpcStreamObserverId;
+import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.device.DeviceHandshaker;
import org.onosproject.net.driver.AbstractHandlerBehaviour;
@@ -29,7 +34,7 @@
import static org.slf4j.LoggerFactory.getLogger;
/**
- * Implementation of the DeviceHandshaker for the bmv2 softswitch.
+ * Implementation of the DeviceHandshaker for the BMv2 softswitch.
*/
//TODO consider abstract class with empty connect method and
//the implementation into a protected one for reusability.
@@ -41,6 +46,9 @@
@Override
public CompletableFuture<Boolean> connect() {
+ GrpcController controller = handler().get(GrpcController.class);
+ DeviceId deviceId = handler().data().deviceId();
+
CompletableFuture<Boolean> result = new CompletableFuture<>();
DeviceKeyService deviceKeyService = handler().get(DeviceKeyService.class);
DriverData data = data();
@@ -54,6 +62,13 @@
deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(data.value("gnmi_key")))
.asUsernamePassword().username());
result.complete(true);
+
+ //we know we need packet in so we register the observer.
+ GrpcChannelId channelId = GrpcChannelId.of(deviceId, "bmv2");
+ GrpcServiceId serviceId = GrpcServiceId.of(channelId, "p4runtime");
+ GrpcStreamObserverId observerId = GrpcStreamObserverId.of(serviceId,
+ Bmv2PacketProgrammable.class.getSimpleName());
+ controller.addObserver(observerId, new Bmv2PacketInObserverHandler());
return result;
}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketInObserverHandler.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketInObserverHandler.java
new file mode 100644
index 0000000..3b9e145
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketInObserverHandler.java
@@ -0,0 +1,79 @@
+/*
+ * 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 io.grpc.ManagedChannel;
+import io.grpc.stub.StreamObserver;
+import org.onosproject.grpc.api.GrpcObserverHandler;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Sample Implementation of a PacketInObserverHandler.
+ * TODO refactor and actually use.
+ */
+public class Bmv2PacketInObserverHandler implements GrpcObserverHandler {
+
+ private final Logger log = getLogger(getClass());
+
+ //private final AbstractStub asyncStub;
+
+ //FIXME put at object due to p4Runtime compilation problems to be fixed.
+ private StreamObserver<Object> requestStreamObserver;
+
+ @Override
+ public void bindObserver(ManagedChannel channel) {
+
+ //asyncStub = ProtoGeneratedClass.newStub(channel);
+
+ //reqeustStreamObserver = asyncStub.MethodName(new PacketInObserver());
+
+ }
+
+ @Override
+ public Optional<StreamObserver> requestStreamObserver() {
+ return Optional.of(requestStreamObserver);
+ }
+
+ @Override
+ public void removeObserver() {
+ //this should complete the two way streaming
+ requestStreamObserver.onCompleted();
+ }
+
+ private class PacketInObserver implements StreamObserver<Object> {
+
+ @Override
+ public void onNext(Object o) {
+ log.info("onNext: {}", o.toString());
+
+ }
+
+ @Override
+ public void onError(Throwable throwable) {
+
+ }
+
+ @Override
+ public void onCompleted() {
+
+ }
+ }
+}
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
new file mode 100644
index 0000000..4b76067
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PacketProgrammable.java
@@ -0,0 +1,55 @@
+/*
+ * 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 io.grpc.stub.StreamObserver;
+import org.onosproject.grpc.api.GrpcChannelId;
+import org.onosproject.grpc.api.GrpcController;
+import org.onosproject.grpc.api.GrpcObserverHandler;
+import org.onosproject.grpc.api.GrpcServiceId;
+import org.onosproject.grpc.api.GrpcStreamObserverId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.packet.PacketProgrammable;
+
+import java.util.Optional;
+
+/**
+ * Packet Programmable behaviour for BMv2 devices.
+ */
+public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
+ @Override
+ public void emit(OutboundPacket packet) {
+ DriverHandler handler = handler();
+ GrpcController controller = handler.get(GrpcController.class);
+ DeviceId deviceId = handler.data().deviceId();
+ GrpcChannelId channelId = GrpcChannelId.of(deviceId, "bmv2");
+ GrpcServiceId serviceId = GrpcServiceId.of(channelId, "p4runtime");
+ GrpcStreamObserverId observerId = GrpcStreamObserverId.of(serviceId,
+ this.getClass().getSimpleName());
+ Optional<GrpcObserverHandler> manager = controller.getObserverManager(observerId);
+ if (!manager.isPresent()) {
+ //this is the first time the behaviour is called
+ controller.addObserver(observerId, new Bmv2PacketInObserverHandler());
+ }
+ //other already registered the observer for us.
+ Optional<StreamObserver> observer = manager.get().requestStreamObserver();
+ observer.ifPresent(objectStreamObserver -> objectStreamObserver.onNext(packet));
+ }
+}
diff --git a/drivers/bmv2/src/main/resources/bmv2-drivers.xml b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
index 7f0d3f2..bc343e0 100644
--- a/drivers/bmv2/src/main/resources/bmv2-drivers.xml
+++ b/drivers/bmv2/src/main/resources/bmv2-drivers.xml
@@ -15,9 +15,11 @@
~ limitations under the License.
-->
<drivers>
- <driver name="bmv2" manufacturer="barefoot" hwVersion="1.0.0" swVersion="1.0.0">
+ <driver name="bmv2" manufacturer="p4.org" hwVersion="master" swVersion="master">
<behaviour api="org.onosproject.net.device.DeviceHandshaker"
impl="org.onosproject.drivers.bmv2.Bmv2Handshaker"/>
+ <behaviour api="org.onosproject.net.packet.PacketProgrammable"
+ impl="org.onosproject.drivers.bmv2.Bmv2PacketProgrammable"/>
</driver>
</drivers>
diff --git a/drivers/p4runtime/proto/BUCK b/drivers/p4runtime/proto/BUCK
new file mode 100644
index 0000000..2c8ebdd
--- /dev/null
+++ b/drivers/p4runtime/proto/BUCK
@@ -0,0 +1,163 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+]
+
+
+PI_BASEURL = 'https://github.com/p4lang/PI.git'
+PROTOBUF_BASEURL = 'https://github.com/google/protobuf.git'
+
+PROTOC_VER = '3.3.0'
+GRPC_VER = '1.3.0'
+
+PROTOC_EXE_BASEURL = 'http://central.maven.org/maven2/com/google/protobuf/protoc/'
+GRPC_JAVA_BASEURL = 'http://central.maven.org/maven2/io/grpc/protoc-gen-grpc-java/'
+
+
+PROTOC_SHA1S = {
+ 'protoc-3.3.0-linux-x86_64.exe':'e6a95fc7477c602cc402ed976d3edbd82c841879',
+ 'protoc-3.3.0-osx-x86_64.exe':'3070e439f9557bb72fb04df631f29d7556c9029c'
+}
+
+GRPC_JAVA_SHA1S = {
+ 'protoc-gen-grpc-java-1.3.0-linux-x86_64.exe':'44a0fa3e6074852ea84f93d258233b3f4f6d9e53',
+ 'protoc-gen-grpc-java-1.3.0-osx-x86_64.exe':'61a1b81b9f0af7d0900c314a4201972b52fb5f12'
+}
+
+
+GRPC_DEPS = [
+ '//lib:grpc-core-' + GRPC_VER,
+ '//lib:grpc-protobuf-' + GRPC_VER,
+ '//lib:grpc-stub-' + GRPC_VER,
+ '//lib:grpc-netty-' + GRPC_VER,
+ '//lib:grpc-auth-' + GRPC_VER,
+ '//lib:protobuf-java-' + PROTOC_VER,
+]
+
+
+def get_arch():
+ import platform
+ os_name = platform.system().lower()
+ if os_name == 'darwin':
+ os_name = 'osx'
+ arch = '%s-%s' % (os_name, platform.machine())
+ return arch
+
+# TODO: defs to download prebuilt protoc and grpc java plugin should visible by other BUCK files.
+
+def prebuilt_protoc():
+ fname = 'protoc-%s-%s.exe' % (PROTOC_VER, get_arch())
+ if fname not in PROTOC_SHA1S:
+ raise Exception('Cannot download %s, architecture not supported' % fname)
+ remote_file(
+ name = 'protoc-binary',
+ out = 'protoc.binary',
+ url = PROTOC_EXE_BASEURL + PROTOC_VER + '/' + fname,
+ sha1 = PROTOC_SHA1S[fname],
+ )
+ genrule (
+ name = 'protoc-exe',
+ srcs = [ ':protoc-binary' ],
+ bash = 'cp $(location :protoc-binary) $OUT && chmod +x $OUT',
+ executable = True,
+ out = 'protoc.exe'
+ )
+
+
+def prebuilt_protoc_java_plugin():
+ arch = get_arch()
+ fname = 'protoc-gen-grpc-java-%s-%s.exe' % (GRPC_VER, get_arch())
+ if fname not in GRPC_JAVA_SHA1S:
+ raise Exception('Cannot download %s, architecture not supported' % fname)
+ remote_file(
+ name = 'grpc-java-binary',
+ out = 'grpc-java.binary',
+ url = GRPC_JAVA_BASEURL + GRPC_VER + '/' + fname,
+ sha1 = GRPC_JAVA_SHA1S[fname],
+ )
+ genrule (
+ name = 'grpc-java-exe',
+ srcs = [ ':grpc-java-binary' ],
+ bash = 'cp $(location :grpc-java-binary) $OUT && chmod +x $OUT',
+ executable = True,
+ out = 'grpc-java.exe'
+ )
+
+prebuilt_protoc()
+prebuilt_protoc_java_plugin()
+
+genrule (
+ name = 'p4lang-pi-repo',
+ # FIXME: should download a specific commit id/tag of p4runtime, right now we get the master.
+ bash = 'git clone --quiet ' + PI_BASEURL + ' $OUT > /dev/null && cd $OUT && '
+ + 'git submodule update --quiet --init --recursive > /dev/null',
+ out = 'repo',
+)
+
+genrule (
+ name = 'protoc-repo',
+ bash = 'git clone --quiet ' + PROTOBUF_BASEURL + ' $OUT > /dev/null && cd $OUT && '
+ + 'git checkout --quiet -b x tags/v' + PROTOC_VER + ' > /dev/null',
+ out = 'repo',
+)
+
+def protoc_gen(
+ name,
+ proto_file,
+ out_pkg,
+ ):
+ genrule(
+ name = name + '-gen',
+ cmd = '$(exe :protoc-exe) --plugin=protoc-gen-grpc-java=$(location :grpc-java-exe) '
+ + '--grpc-java_out=$SRCDIR/../' + name + '-gen '
+ + '--java_out=$SRCDIR/../' + name + '-gen '
+ + '-I$(location :p4lang-pi-repo)/proto '
+ + '-I$(location :protoc-repo)/src '
+ + proto_file,
+ out = out_pkg,
+ )
+ zip_file(
+ name = name,
+ out = name + '.src.zip',
+ srcs = [':'+name+'-gen']
+ )
+
+# Wondering which .proto files to build? Check p4runtime's Makefile:
+# https://github.com/p4lang/PI/blob/master/proto/Makefile.am
+protoc_gen(
+ name = 'p4runtime',
+ proto_file = '$(location :p4lang-pi-repo)/proto/p4/p4runtime.proto',
+ out_pkg = 'p4',
+)
+protoc_gen(
+ name = 'p4info',
+ proto_file = '$(location :p4lang-pi-repo)/proto/p4/config/p4info.proto',
+ out_pkg = 'p4',
+)
+protoc_gen(
+ name = 'google-rpc-status',
+ proto_file = '$(location :p4lang-pi-repo)/proto/google/rpc/status.proto',
+ out_pkg = 'com',
+)
+protoc_gen(
+ name = 'google-rpc-code',
+ proto_file = '$(location :p4lang-pi-repo)/proto/google/rpc/code.proto',
+ out_pkg = 'com',
+)
+protoc_gen(
+ name = 'p4config',
+ proto_file = '$(location :p4lang-pi-repo)/proto/p4/tmp/p4config.proto',
+ out_pkg = 'p4',
+)
+
+
+osgi_jar(
+ srcs = [':p4runtime', ':p4info', ':google-rpc-status', ':google-rpc-code', ':p4config'],
+ deps = COMPILE_DEPS + GRPC_DEPS,
+ do_javadocs = False,
+ do_checkstyle = False
+)
+
+
+project_config(
+ src_target = ':onos-drivers-p4runtime-proto'
+)
\ No newline at end of file