Merge branch 'master' into dev-karaf-4.2.1

Change-Id: I3c87139d15508e16a15df62fe73590b2a2ef7a04
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java
index e3664e1..c7142bf 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipeconf.java
@@ -100,6 +100,11 @@
         BMV2_JSON,
 
         /**
+         * Mellanox Spectrum configuration binary.
+         */
+        SPECTRUM_BIN,
+
+        /**
          * Barefoot's Tofino configuration binary.
          */
         TOFINO_BIN,
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
index e363f4a..fda0bb5 100644
--- a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/AbstractGnmiHandlerBehaviour.java
@@ -16,6 +16,7 @@
 
 package org.onosproject.drivers.gnmi;
 
+import io.grpc.StatusRuntimeException;
 import org.onosproject.gnmi.api.GnmiClient;
 import org.onosproject.gnmi.api.GnmiClientKey;
 import org.onosproject.gnmi.api.GnmiController;
@@ -26,11 +27,20 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
 /**
  * Abstract implementation of a behaviour handler for a gNMI device.
  */
 public class AbstractGnmiHandlerBehaviour extends AbstractHandlerBehaviour {
 
+    // Default timeout in seconds for device operations.
+    private static final String DEVICE_REQ_TIMEOUT = "deviceRequestTimeout";
+    private static final int DEFAULT_DEVICE_REQ_TIMEOUT = 60;
+
     public static final String GNMI_SERVER_ADDR_KEY = "gnmi_ip";
     public static final String GNMI_SERVER_PORT_KEY = "gnmi_port";
     private static final String GNMI_SERVICE_NAME = "gnmi";
@@ -43,19 +53,13 @@
     protected GnmiClient client;
 
     protected boolean setupBehaviour() {
-        // FIXME: Should create GnmiHandshaker which initialize the client
-        // instead of create client here.
         deviceId = handler().data().deviceId();
 
         controller = handler().get(GnmiController.class);
         client = controller.getClient(deviceId);
 
         if (client == null) {
-            client = createClient();
-        }
-
-        if (client == null) {
-            log.warn("Can not create client for {} (see log above)", deviceId);
+            log.warn("Unable to find client for {}, aborting operation", deviceId);
             return false;
         }
 
@@ -90,4 +94,58 @@
         }
         return controller.getClient(deviceId);
     }
+
+    /**
+     * Returns the device request timeout driver property, or a default value
+     * if the property is not present or cannot be parsed.
+     *
+     * @return timeout value
+     */
+    private int getDeviceRequestTimeout() {
+        final String timeout = handler().driver()
+                .getProperty(DEVICE_REQ_TIMEOUT);
+        if (timeout == null) {
+            return DEFAULT_DEVICE_REQ_TIMEOUT;
+        } else {
+            try {
+                return Integer.parseInt(timeout);
+            } catch (NumberFormatException e) {
+                log.error("{} driver property '{}' is not a number, using default value {}",
+                        DEVICE_REQ_TIMEOUT, timeout, DEFAULT_DEVICE_REQ_TIMEOUT);
+                return DEFAULT_DEVICE_REQ_TIMEOUT;
+            }
+        }
+    }
+
+    /**
+     * Convenience method to get the result of a completable future while
+     * setting a timeout and checking for exceptions.
+     *
+     * @param future        completable future
+     * @param opDescription operation description to use in log messages. Should
+     *                      be a sentence starting with a verb ending in -ing,
+     *                      e.g. "reading...", "writing...", etc.
+     * @param defaultValue  value to return if operation fails
+     * @param <U>           type of returned value
+     * @return future result or default value
+     */
+    <U> U getFutureWithDeadline(CompletableFuture<U> future, String opDescription,
+                                U defaultValue) {
+        try {
+            return future.get(getDeviceRequestTimeout(), TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            log.error("Exception while {} on {}", opDescription, deviceId);
+        } catch (ExecutionException e) {
+            final Throwable cause = e.getCause();
+            if (cause instanceof StatusRuntimeException) {
+                final StatusRuntimeException grpcError = (StatusRuntimeException) cause;
+                log.warn("Error while {} on {}: {}", opDescription, deviceId, grpcError.getMessage());
+            } else {
+                log.error("Exception while {} on {}", opDescription, deviceId, e.getCause());
+            }
+        } catch (TimeoutException e) {
+            log.error("Operation TIMEOUT while {} on {}", opDescription, deviceId);
+        }
+        return defaultValue;
+    }
 }
diff --git a/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiHandshaker.java b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiHandshaker.java
new file mode 100644
index 0000000..d1ee1b8
--- /dev/null
+++ b/drivers/gnmi/src/main/java/org/onosproject/drivers/gnmi/GnmiHandshaker.java
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+package org.onosproject.drivers.gnmi;
+
+import org.onosproject.gnmi.api.GnmiClient;
+import org.onosproject.gnmi.api.GnmiController;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.device.DeviceHandshaker;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Implementation of DeviceHandshaker for gNMI.
+ */
+public class GnmiHandshaker extends AbstractGnmiHandlerBehaviour implements DeviceHandshaker {
+
+    @Override
+    public CompletableFuture<Boolean> isReachable() {
+        return CompletableFuture
+                // gNMI requires a client to be created to
+                // check for reachability.
+                .supplyAsync(super::createClient)
+                .thenApplyAsync(client -> {
+                    if (client == null) {
+                        return false;
+                    }
+                    return handler()
+                            .get(GnmiController.class)
+                            .isReachable(handler().data().deviceId());
+                });
+    }
+
+    @Override
+    public void roleChanged(MastershipRole newRole) {
+        throw new UnsupportedOperationException("Mastership operation not supported");
+    }
+
+    @Override
+    public MastershipRole getRole() {
+        throw new UnsupportedOperationException("Mastership operation not supported");
+    }
+
+    @Override
+    public CompletableFuture<Boolean> connect() {
+        return CompletableFuture
+                .supplyAsync(super::createClient)
+                .thenComposeAsync(client -> {
+                    if (client == null) {
+                        return CompletableFuture.completedFuture(false);
+                    }
+                    return CompletableFuture.completedFuture(true);
+                });
+    }
+
+    @Override
+    public boolean isConnected() {
+        final GnmiController controller = handler().get(GnmiController.class);
+        final DeviceId deviceId = handler().data().deviceId();
+        final GnmiClient client = controller.getClient(deviceId);
+
+        if (client == null) {
+            return false;
+        }
+
+        return getFutureWithDeadline(client.isServiceAvailable(), "getting availability", false);
+    }
+
+    @Override
+    public CompletableFuture<Boolean> disconnect() {
+        final GnmiController controller = handler().get(GnmiController.class);
+        final DeviceId deviceId = handler().data().deviceId();
+        final GnmiClient client = controller.getClient(deviceId);
+        if (client == null) {
+            return CompletableFuture.completedFuture(true);
+        }
+        return client.shutdown()
+                .thenApplyAsync(v -> {
+                    controller.removeClient(deviceId);
+                    return true;
+                });
+    }
+}
diff --git a/drivers/gnmi/src/main/resources/gnmi-drivers.xml b/drivers/gnmi/src/main/resources/gnmi-drivers.xml
index 4ca539e..70c0b20 100644
--- a/drivers/gnmi/src/main/resources/gnmi-drivers.xml
+++ b/drivers/gnmi/src/main/resources/gnmi-drivers.xml
@@ -18,6 +18,8 @@
     <driver name="gnmi" manufacturer="gnmi" hwVersion="master" swVersion="master">
         <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
                    impl="org.onosproject.drivers.gnmi.OpenConfigGnmiDeviceDescriptionDiscovery"/>
+        <behaviour api="org.onosproject.net.device.DeviceHandshaker"
+                   impl="org.onosproject.drivers.gnmi.GnmiHandshaker"/>
     </driver>
 </drivers>
 
diff --git a/drivers/mellanox/src/main/java/org/onosproject/drivers/mellanox/SpectrumPipelineProgrammable.java b/drivers/mellanox/src/main/java/org/onosproject/drivers/mellanox/SpectrumPipelineProgrammable.java
index c13ab77..d421be4 100644
--- a/drivers/mellanox/src/main/java/org/onosproject/drivers/mellanox/SpectrumPipelineProgrammable.java
+++ b/drivers/mellanox/src/main/java/org/onosproject/drivers/mellanox/SpectrumPipelineProgrammable.java
@@ -16,17 +16,21 @@
 
 package org.onosproject.drivers.mellanox;
 
+import org.apache.commons.io.IOUtils;
 import org.onosproject.drivers.p4runtime.AbstractP4RuntimePipelineProgrammable;
 import org.onosproject.net.behaviour.PiPipelineProgrammable;
 import org.onosproject.net.pi.model.PiPipeconf;
 import org.onosproject.net.pi.model.PiPipeconfId;
+import org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
 import org.onosproject.net.pi.service.PiPipeconfService;
 
+import java.io.IOException;
+
 import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
 import java.util.Optional;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.SPECTRUM_BIN;
 
 /**
  * Implementation of the PiPipelineProgrammable behaviour for a Spectrum-based
@@ -36,23 +40,48 @@
         extends AbstractP4RuntimePipelineProgrammable
         implements PiPipelineProgrammable {
 
-    private static final PiPipeconfId FABRIC_PIPECONF_ID =
-            new PiPipeconfId("org.onosproject.pipelines.fabric");
+    private static final PiPipeconfId MLNX_FABRIC_PIPECONF_ID =
+            new PiPipeconfId("org.onosproject.pipelines.fabric-mlnx");
 
     @Override
     public ByteBuffer createDeviceDataBuffer(PiPipeconf pipeconf) {
-        checkArgument(pipeconf.id().equals(FABRIC_PIPECONF_ID),
-                      format("Cannot program Spectrum device with a pipeconf " +
-                                     "other than '%s' (found '%s')",
-                             FABRIC_PIPECONF_ID, pipeconf.id()));
-        // Dummy value.
-        // We assume switch to be already configured with fabric.p4 profile.
-        return ByteBuffer.allocate(1).put((byte) 1);
+        log.debug("Creating device data buffer for {} in pipeconf {}", SPECTRUM_BIN, pipeconf.id());
+        ByteBuffer deviceData;
+        try {
+            deviceData = extensionBuffer(pipeconf, SPECTRUM_BIN);
+        } catch (ExtensionException e) {
+            log.error("Failed to create device data buffer for {} in pipeconf {}", SPECTRUM_BIN, pipeconf.id());
+            return null;
+        }
+        // flip buffer data so they can be read
+        deviceData.flip();
+        return deviceData.asReadOnlyBuffer();
     }
 
     @Override
     public Optional<PiPipeconf> getDefaultPipeconf() {
         return handler().get(PiPipeconfService.class)
-                .getPipeconf(FABRIC_PIPECONF_ID);
+                .getPipeconf(MLNX_FABRIC_PIPECONF_ID);
+    }
+
+    private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) {
+        if (!pipeconf.extension(extType).isPresent()) {
+            log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id());
+            throw new ExtensionException();
+        }
+        try {
+            byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get());
+            // Length of the extension + bytes.
+            return ByteBuffer.allocate(bytes.length)
+                    .order(ByteOrder.LITTLE_ENDIAN)
+                    .put(bytes);
+        } catch (IOException ex) {
+            log.warn("Unable to read extension {} from pipeconf {}: {}",
+                     extType, pipeconf.id(), ex.getMessage());
+            throw new ExtensionException();
+        }
+    }
+
+    private static class ExtensionException extends IllegalArgumentException {
     }
 }
diff --git a/pipelines/fabric/src/main/java/org/onosproject/pipelines/fabric/PipeconfLoader.java b/pipelines/fabric/src/main/java/org/onosproject/pipelines/fabric/PipeconfLoader.java
index 48ec2ab..89653d8 100644
--- a/pipelines/fabric/src/main/java/org/onosproject/pipelines/fabric/PipeconfLoader.java
+++ b/pipelines/fabric/src/main/java/org/onosproject/pipelines/fabric/PipeconfLoader.java
@@ -71,12 +71,14 @@
     private static final String P4C_RES_BASE_PATH = P4C_OUT_PATH + "/%s/%s/%s/";
 
     private static final String SEP = File.separator;
+    private static final String SPECTRUM = "spectrum";
     private static final String TOFINO = "tofino";
     private static final String BMV2 = "bmv2";
     private static final String DEFAULT_PLATFORM = "default";
     private static final String BMV2_JSON = "bmv2.json";
     private static final String P4INFO_TXT = "p4info.txt";
     private static final String CPU_PORT_TXT = "cpu_port.txt";
+    private static final String SPECTRUM_BIN = "spectrum.bin";
     private static final String TOFINO_BIN = "tofino.bin";
     private static final String TOFINO_CTX_JSON = "context.json";
     private static final String INT_PROFILE_SUFFIX = "-int";
@@ -133,6 +135,9 @@
                 case BMV2:
                     pipeconfBuilder = bmv2Pipeconf(profile, platform);
                     break;
+                case SPECTRUM:
+                    pipeconfBuilder = spectrumPipeconf(profile, platform);
+                    break;
                 case TOFINO:
                     pipeconfBuilder = tofinoPipeconf(profile, platform);
                     break;
@@ -177,6 +182,28 @@
                 .addExtension(ExtensionType.BMV2_JSON, bmv2JsonUrl);
     }
 
+    private static DefaultPiPipeconf.Builder  spectrumPipeconf(String profile, String platform)
+            throws FileNotFoundException {
+        final URL spectrumBinUrl = PipeconfLoader.class.getResource(format(
+                P4C_RES_BASE_PATH + SPECTRUM_BIN, profile, SPECTRUM, platform));
+        final URL p4InfoUrl = PipeconfLoader.class.getResource(format(
+                P4C_RES_BASE_PATH + P4INFO_TXT, profile, SPECTRUM, platform));
+        final URL cpuPortUrl = PipeconfLoader.class.getResource(format(
+                P4C_RES_BASE_PATH + CPU_PORT_TXT, profile, SPECTRUM, platform));
+        if (spectrumBinUrl == null) {
+            throw new FileNotFoundException(SPECTRUM_BIN);
+        }
+        if (p4InfoUrl == null) {
+            throw new FileNotFoundException(P4INFO_TXT);
+        }
+        if (cpuPortUrl == null) {
+            throw new FileNotFoundException(CPU_PORT_TXT);
+        }
+        return basePipeconfBuilder(
+                profile, platform, p4InfoUrl, cpuPortUrl)
+                .addExtension(ExtensionType.SPECTRUM_BIN, spectrumBinUrl);
+    }
+
     private static DefaultPiPipeconf.Builder tofinoPipeconf(
             String profile, String platform)
             throws FileNotFoundException {
diff --git a/pipelines/fabric/src/main/resources/p4c-out/fabric-mlnx/spectrum/default/README.md b/pipelines/fabric/src/main/resources/p4c-out/fabric-mlnx/spectrum/default/README.md
new file mode 100644
index 0000000..b3611d3
--- /dev/null
+++ b/pipelines/fabric/src/main/resources/p4c-out/fabric-mlnx/spectrum/default/README.md
@@ -0,0 +1,18 @@
+# Running ONOS with Mellanox Spectrum/Spectrum2 Switches
+
+## Spectrum and Fabric.p4
+The Spectrum architecture supports the fabric.p4 pipeline, but using the spectrum_model.p4 instead of the v1model.p4.
+The folder location p4c-out/fabric-mlnx/spectrum/default is where the P4 compiler artifacts should be placed for 
+ONOS to properly load and configure the pipeline for the Spectrum switch. 
+These files include:
+
+* cpu_port.txt:   defines the SDN port number to be used when sending a packet to the controller
+* p4info.txt:     the P4Runtime output file, in protobuf text format, when compiling fabric.p4
+* spectrum.bin:   The "binary blob" P4 compiler output, which contains all the data necessary to reconfigure the 
+  switch pipeline (P4 device config, as described in the P4Runtime specification)
+
+Since at this time the Mellanox P4 compiler backend is under active development and is not currently open sourced,
+please contact your Mellanox representative for access to the compiler and/or the compiler artifacts described above.
+
+For the latest details, please take a look at the wiki page instructions:
+https://wiki.onosproject.org/display/ONOS/Controlling+P4Runtime-enabled+Mellanox+Spectrum+switch+with+ONOS
diff --git a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
index 7f43d1f..242bc94 100644
--- a/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
+++ b/protocols/gnmi/api/src/main/java/org/onosproject/gnmi/api/GnmiClient.java
@@ -57,5 +57,13 @@
      */
     CompletableFuture<SetResponse> set(SetRequest request);
 
+    /**
+     * Check weather the gNMI service is available or not by sending a
+     * dummy get request message.
+     *
+     * @return true if gNMI service available; false otherwise
+     */
+    CompletableFuture<Boolean> isServiceAvailable();
+
     // TODO: Support gNMI subscription
 }
diff --git a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
index 74a9ec0..b8179a9 100644
--- a/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
+++ b/protocols/gnmi/ctl/src/main/java/org/onosproject/gnmi/ctl/GnmiClientImpl.java
@@ -19,10 +19,13 @@
 import gnmi.Gnmi.CapabilityResponse;
 import gnmi.Gnmi.GetRequest;
 import gnmi.Gnmi.GetResponse;
+import gnmi.Gnmi.Path;
+import gnmi.Gnmi.PathElem;
 import gnmi.Gnmi.SetRequest;
 import gnmi.Gnmi.SetResponse;
 import gnmi.gNMIGrpc;
 import io.grpc.ManagedChannel;
+import io.grpc.Status;
 import io.grpc.StatusRuntimeException;
 import org.onosproject.gnmi.api.GnmiClientKey;
 import org.onosproject.grpc.ctl.AbstractGrpcClient;
@@ -37,6 +40,9 @@
  * Implementation of gNMI client.
  */
 public class GnmiClientImpl extends AbstractGrpcClient implements GnmiClient {
+    private static final PathElem DUMMY_PATH_ELEM = PathElem.newBuilder().setName("onos-gnmi-test").build();
+    private static final Path DUMMY_PATH = Path.newBuilder().addElem(DUMMY_PATH_ELEM).build();
+    private static final GetRequest DUMMY_REQUEST = GetRequest.newBuilder().addPath(DUMMY_PATH).build();
     private final Logger log = getLogger(getClass());
     private final gNMIGrpc.gNMIBlockingStub blockingStub;
 
@@ -60,6 +66,11 @@
         return supplyInContext(() -> doSet(request), "set");
     }
 
+    @Override
+    public CompletableFuture<Boolean> isServiceAvailable() {
+        return supplyInContext(this::doServiceAvailable, "isServiceAvailable");
+    }
+
     private CapabilityResponse doCapability() {
         CapabilityRequest request = CapabilityRequest.newBuilder().build();
         try {
@@ -87,4 +98,17 @@
             return null;
         }
     }
+
+    private boolean doServiceAvailable() {
+        try {
+            blockingStub.get(DUMMY_REQUEST);
+            return true;
+        } catch (StatusRuntimeException e) {
+            // This gRPC call should throw INVALID_ARGUMENT status exception
+            // since "/onos-gnmi-test" path does not exists in any config model
+            // For other status code such as UNIMPLEMENT, means the gNMI
+            // service is not available on the device.
+            return e.getStatus().getCode().equals(Status.Code.INVALID_ARGUMENT);
+        }
+    }
 }
diff --git a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java
index 087898b..2e3ea5d 100644
--- a/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java
+++ b/protocols/grpc/api/src/main/java/org/onosproject/grpc/api/GrpcClientController.java
@@ -66,11 +66,12 @@
      * Check reachability of the gRPC server running on the given device.
      * Reachability can be tested only if a client is previously created
      * using {@link #createClient(GrpcClientKey)}.
-     * Different gRPC service may have different ways to test if it is
-     * reachable or not.
+     * Note that this only checks the reachability instead of checking service
+     * availability, different gRPC client checks service availability with
+     * different way.
      *
      * @param deviceId the device identifier
-     * @return true of client was created and is able to contact the gNMI server;
+     * @return true if client was created and is able to contact the gNMI server;
      *         false otherwise
      */
     boolean isReachable(DeviceId deviceId);