Merge remote-tracking branch 'origin/master' into dev/auklet

Change-Id: I4b2ac52b779116ba7a00e6ceb07352626fd55eed
diff --git a/cli/src/main/java/org/onosproject/cli/net/DpisListCommand.java b/cli/src/main/java/org/onosproject/cli/net/DpisListCommand.java
index 6bdcedb..d34c9ed 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DpisListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DpisListCommand.java
@@ -213,7 +213,7 @@
     }
 
     private void printDpiStatisticsJson(int number, DpiStatistics ds) {
-        String index = number < 0 ? String.format("  -  ") : String.format("%5d", number);
+        String index = number < 0 ? "  -  " : String.format("%5d", number);
         if ("".equals(ds.receivedTime())) {
             print("ReceivedTime is null, No valid DPI Statistics!");
             return;
@@ -226,7 +226,7 @@
 
     private void printDpiStatisticsClass(int number, DpiStatistics ds) {
         String printLine = "";
-        String index = number < 0 ? String.format("  -  ") : String.format("%5d", number);
+        String index = number < 0 ? "  -  " : String.format("%5d", number);
 
         DpiStatInfo dsi = ds.dpiStatInfo();
         if (dsi == null) {
diff --git a/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java
index 8fb3714..595599d 100644
--- a/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/IntentsListCommand.java
@@ -119,7 +119,7 @@
      */
     private static final String SUMMARY_TITLES =
             BOLD + format(
-            "\n%1s%21s%14s%14s%14s%14s%14s%14s%14s%14s%14s%14s",
+            "%n%1s%21s%14s%14s%14s%14s%14s%14s%14s%14s%14s%14s",
             "Intent type",
             "Total",
             "Installed",
@@ -355,7 +355,7 @@
             StringBuilder builder = new StringBuilder();
 
             builder.append(format(
-                    "\n%1s%s%14d%14d%14d%14d%14d%14d%14d%14d%14d%14d",
+                    "%n%1s%s%14d%14d%14d%14d%14d%14d%14d%14d%14d%14d",
                     BOLD + intentType + RESET,
                     Strings.padStart(String.valueOf(total),
                                      (32 - intentType.length()),
diff --git a/core/security/src/main/java/org/onosproject/security/impl/SecurityModeManager.java b/core/security/src/main/java/org/onosproject/security/impl/SecurityModeManager.java
index 8590256..c3278e7 100644
--- a/core/security/src/main/java/org/onosproject/security/impl/SecurityModeManager.java
+++ b/core/security/src/main/java/org/onosproject/security/impl/SecurityModeManager.java
@@ -186,7 +186,7 @@
                         appId -> store.isSecured(appId) &&
                                 appAdminService.getState(appId) == ApplicationState.ACTIVE).forEach(appId -> {
                     store.requestPermission(appId, permission);
-                    print("[POLICY VIOLATION] APP: %s / Bundle: %s / Permission: %s ",
+                    log.debug("[POLICY VIOLATION] APP: %s / Bundle: %s / Permission: %s ",
                             appId.name(), location, permission.toString());
                 });
             }
@@ -264,7 +264,7 @@
     private List<Permission> getMaximumPermissions(ApplicationId appId) {
         Application app = appAdminService.getApplication(appId);
         if (app == null) {
-            print("Unknown application.");
+            log.debug("Unknown application.");
             return null;
         }
         List<Permission> appPerms;
@@ -284,12 +284,6 @@
         return appPerms;
     }
 
-
-    private void print(String format, Object... args) {
-        System.out.println(String.format("SM-ONOS: " + format, args));
-        log.warn(String.format(format, args));
-    }
-
     private PermissionAdmin getPermissionAdmin() {
         BundleContext context = getBundleContext();
         return (PermissionAdmin) context.getService(context.getServiceReference(PermissionAdmin.class.getName()));
diff --git a/pipelines/fabric/src/main/resources/fabric.p4 b/pipelines/fabric/src/main/resources/fabric.p4
index 56ab6e9..8205196 100644
--- a/pipelines/fabric/src/main/resources/fabric.p4
+++ b/pipelines/fabric/src/main/resources/fabric.p4
@@ -66,7 +66,7 @@
     apply {
         pkt_io_egress.apply(hdr, fabric_metadata, standard_metadata);
 #ifdef WITH_SPGW
-        spgw_egress.apply(hdr.gtpu_ipv4, hdr.gtpu_udp, hdr.gtpu,
+        spgw_egress.apply(hdr.ipv4, hdr.gtpu_ipv4, hdr.gtpu_udp, hdr.gtpu,
                           fabric_metadata.spgw, standard_metadata);
 #endif // WITH_SPGW
     }
diff --git a/pipelines/fabric/src/main/resources/include/spgw.p4 b/pipelines/fabric/src/main/resources/include/spgw.p4
index 3edca18..5637adf 100644
--- a/pipelines/fabric/src/main/resources/include/spgw.p4
+++ b/pipelines/fabric/src/main/resources/include/spgw.p4
@@ -199,6 +199,7 @@
 
 
 control spgw_egress(
+        in ipv4_t               ipv4,
         out ipv4_t              gtpu_ipv4,
         out udp_t               gtpu_udp,
         out gtpu_t              gtpu,
@@ -211,8 +212,8 @@
         gtpu_ipv4.version = IP_VERSION_4;
         gtpu_ipv4.ihl = IPV4_MIN_IHL;
         gtpu_ipv4.diffserv = 0;
-        gtpu_ipv4.total_len = ((bit<16>)std_meta.packet_length
-            - ETH_HDR_SIZE + IPV4_HDR_SIZE + UDP_HDR_SIZE + GTP_HDR_SIZE);
+        gtpu_ipv4.total_len = ipv4.total_len
+                + (IPV4_HDR_SIZE + UDP_HDR_SIZE + GTP_HDR_SIZE);
         gtpu_ipv4.identification = 0x1513; /* From NGIC */
         gtpu_ipv4.flags = 0;
         gtpu_ipv4.frag_offset = 0;
@@ -225,8 +226,8 @@
         gtpu_udp.setValid();
         gtpu_udp.src_port = UDP_PORT_GTPU;
         gtpu_udp.dst_port = UDP_PORT_GTPU;
-        gtpu_udp.len = ((bit<16>)std_meta.packet_length
-            - ETH_HDR_SIZE + UDP_HDR_SIZE + GTP_HDR_SIZE);
+        gtpu_udp.len = ipv4.total_len
+                + (UDP_HDR_SIZE + GTP_HDR_SIZE);
         gtpu_udp.checksum = 0; // Updated later
 
         gtpu.setValid();
@@ -237,7 +238,7 @@
         gtpu.seq_flag = 0;
         gtpu.npdu_flag = 0;
         gtpu.msgtype = GTP_GPDU;
-        gtpu.msglen = ((bit<16>)std_meta.packet_length - ETH_HDR_SIZE);
+        gtpu.msglen = ipv4.total_len;
         gtpu.teid = spgw_meta.teid;
     }
 
@@ -307,6 +308,7 @@
             HashAlgorithm.csum16
         );
 
+#ifdef WITH_SPGW_UDP_CSUM_UPDATE
         // Compute outer UDP checksum.
         update_checksum_with_payload(gtpu_udp.isValid(),
             {
@@ -327,6 +329,7 @@
             gtpu_udp.checksum,
             HashAlgorithm.csum16
         );
+#endif // WITH_SPGW_UDP_CSUM_UPDATE
     }
 }
 
diff --git a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/HexDump.java b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/HexDump.java
index 0ee620b..777dc61 100644
--- a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/HexDump.java
+++ b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/HexDump.java
@@ -40,7 +40,7 @@
                 StringBuilder sb = new StringBuilder();
                 for (int k = 0; (k < 16) && (buff.readableBytes() != 0); ++k) {
                     if (0 == k % 4) {
-                        sb.append(String.format(" ")); // blank after 4 bytes
+                        sb.append(" "); // blank after 4 bytes
                     }
                     sb.append(String.format("%02X ", buff.readByte()));
                 }
diff --git a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
index bac0bb9..534ef06 100644
--- a/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
+++ b/protocols/p4runtime/ctl/src/main/java/org/onosproject/p4runtime/ctl/P4RuntimeClientImpl.java
@@ -23,6 +23,7 @@
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
 import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
 import io.grpc.Context;
 import io.grpc.ManagedChannel;
 import io.grpc.Status;
@@ -42,8 +43,8 @@
 import org.onosproject.net.pi.runtime.PiCounterCellData;
 import org.onosproject.net.pi.runtime.PiCounterCellId;
 import org.onosproject.net.pi.runtime.PiPacketOperation;
-import org.onosproject.net.pi.service.PiPipeconfService;
 import org.onosproject.net.pi.runtime.PiTableEntry;
+import org.onosproject.net.pi.service.PiPipeconfService;
 import org.onosproject.p4runtime.api.P4RuntimeClient;
 import org.onosproject.p4runtime.api.P4RuntimeEvent;
 import org.slf4j.Logger;
@@ -159,12 +160,11 @@
             writeLock.lock();
             try {
                 return supplier.get();
+            } catch (StatusRuntimeException ex) {
+                logP4RuntimeErrorStatus(ex, opDescription);
+                throw ex;
             } catch (Throwable ex) {
-                if (ex instanceof StatusRuntimeException) {
-                    log.warn("Unable to execute {} on {}: {}", opDescription, deviceId, ex.toString());
-                } else {
-                    log.error("Exception in client of {}, executing {}", deviceId, opDescription, ex);
-                }
+                log.error("Exception in client of {}, executing {}", deviceId, opDescription, ex);
                 throw ex;
             } finally {
                 writeLock.unlock();
@@ -172,6 +172,18 @@
         }, contextExecutor);
     }
 
+    private void logP4RuntimeErrorStatus(StatusRuntimeException ex, String description) {
+        String statusString = ex.getStatus().getDescription();
+        try {
+            com.google.rpc.Status status = com.google.rpc.Status.parseFrom(statusString.getBytes());
+            log.warn("{} failed on {} due to {}", description, deviceId, status.toString());
+        } catch (InvalidProtocolBufferException e) {
+            log.warn("{} failed on {} due to {}", description, deviceId, statusString);
+        } catch (NullPointerException e) {
+            log.warn("{} failed on {}", description, deviceId);
+        }
+    }
+
     @Override
     public CompletableFuture<Boolean> initStreamChannel() {
         return supplyInContext(this::doInitStreamChannel, "initStreamChannel");
@@ -295,7 +307,11 @@
         try {
             streamRequestObserver.onNext(requestMsg);
             return result.get();
-        } catch (InterruptedException | ExecutionException | StatusRuntimeException e) {
+        } catch (StatusRuntimeException e) {
+            logP4RuntimeErrorStatus(e, "Arbitration update");
+            arbitrationUpdateMap.remove(newElectionId);
+            return false;
+        } catch (InterruptedException | ExecutionException e) {
             log.warn("Arbitration update failed for {} due to {}", deviceId, e);
             arbitrationUpdateMap.remove(newElectionId);
             return false;
@@ -344,7 +360,7 @@
             this.blockingStub.setForwardingPipelineConfig(request);
             return true;
         } catch (StatusRuntimeException ex) {
-            log.warn("Unable to set pipeline config for {}: {}", deviceId, ex.getMessage());
+            logP4RuntimeErrorStatus(ex, "Set pipeline config");
             return false;
         }
     }
@@ -378,7 +394,7 @@
             blockingStub.write(writeRequestBuilder.build());
             return true;
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to write table entries ({}): {}", opType, e.getMessage());
+            logP4RuntimeErrorStatus(e, "Write table entries");
             return false;
         }
     }
@@ -409,7 +425,7 @@
         try {
             responses = blockingStub.read(requestMsg);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to dump table: {}", e.getMessage());
+            logP4RuntimeErrorStatus(e, "Dump table");
             return Collections.emptyList();
         }
 
@@ -518,7 +534,7 @@
         try {
             responses = () -> blockingStub.read(request);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to read counters: {}", e.getMessage());
+            logP4RuntimeErrorStatus(e, "Read counter");
             return Collections.emptyList();
         }
 
@@ -565,7 +581,7 @@
             blockingStub.write(writeRequestMsg);
             return true;
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to write ({}) group members: {}", opType, e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("%s group members", opType));
             return false;
         }
     }
@@ -608,7 +624,8 @@
         try {
             groupResponses = blockingStub.read(groupRequestMsg);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable dump groups from action profile '{}': {}", piActionProfileId.id(), e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("Dump group from action profile %s",
+                                                     piActionProfileId.id()));
             return Collections.emptySet();
         }
 
@@ -656,7 +673,8 @@
         try {
             memberResponses = blockingStub.read(memberRequestMsg);
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to read members from action profile {}: {}", piActionProfileId, e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("Read members from action profile %s",
+                                                     piActionProfileId.id()));
             return Collections.emptyList();
         }
 
@@ -715,7 +733,7 @@
             blockingStub.write(writeRequestMsg);
             return true;
         } catch (StatusRuntimeException e) {
-            log.warn("Unable to write groups ({}): {}", opType, e.getMessage());
+            logP4RuntimeErrorStatus(e, String.format("%s group", opType));
             return false;
         }
     }
diff --git a/protocols/pcep/pcepio/src/main/java/org/onosproject/pcepio/util/HexDump.java b/protocols/pcep/pcepio/src/main/java/org/onosproject/pcepio/util/HexDump.java
index b5897d5..79f3815 100644
--- a/protocols/pcep/pcepio/src/main/java/org/onosproject/pcepio/util/HexDump.java
+++ b/protocols/pcep/pcepio/src/main/java/org/onosproject/pcepio/util/HexDump.java
@@ -41,7 +41,7 @@
                 StringBuilder sb = new StringBuilder();
                 for (int k = 0; (k < 16) && (iStartIndex < iEndIndex); ++k) {
                     if (0 == k % 4) {
-                        sb.append(String.format(" ")); //blank after 4 bytes
+                        sb.append(" "); //blank after 4 bytes
                     }
                     sb.append(String.format("%02X ", yTemp[iStartIndex++]));
                 }
diff --git a/tools/test/bin/onos b/tools/test/bin/onos
index c74ebc6..393982a 100755
--- a/tools/test/bin/onos
+++ b/tools/test/bin/onos
@@ -5,32 +5,28 @@
 
 function _usage () {
 cat << _EOF_
-usage:
- $(basename $0) [-w] [-f] [node]
+usage: $(basename $0) [node [command]]
 
-flags:
-- -w : Waits for ONOS instance to reach run-level 100, i.e. to be fully started.
-- -f : Deprecated - use bash's IFS expansion of positional parameters
+ONOS remote command-line client.
 
-options:
-- [node] : the node to attach to
+Parameters:
+  - node          ip or hostname of cluster node to attach to
+  - command       command to be executed
 
-summary:
- ONOS remote command-line client.
-
- The -w flag depends on 'onos-wait-for-start'. If [node] is unspecified, \$OCI
- is used.
-
+If node is unspecified, the value of \$OCI environment variable is used.
 _EOF_
 }
-[ "$1" = "-h" ] && _usage && exit 0
 
-[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
-. $ONOS_ROOT/tools/build/envDefaults
-. $ONOS_ROOT/tools/test/bin/find-node.sh
+[ "$1" = "-h" -o "$1" = '-?' ] && _usage && exit 0
+[ "$1" = "-w" ] && echo "-w option is deprecated" && shift   # Deprecated
+[ "$1" = "-f" ] && echo "-f option is deprecated" && shift   # Deprecated
 
-[ "$1" = "-w" ] && shift && onos-wait-for-start $1
-[ "$1" = "-f" ] && shift   # Deprecated
+# If ONOS_HOME is set, respect its value.
+# If ONOS_HOME is not set (e.g. in the init or service environment),
+# set it based on this script's path.
+ONOS_HOME=${ONOS_HOME:-$(cd $(dirname $0)/.. >/dev/null 2>&1 && pwd)}
+[ $(dirname $0)/find-node.sh ] && . $(dirname $0)/find-node.sh
+[ -x ${ONOS_HOME}/bin/_find-node ] && . ${ONOS_HOME}/bin/_find-node
 
 [ -n "$1" ] && OCI=$(find_node $1) && shift
 
diff --git a/utils/misc/src/main/java/org/onlab/util/HexDump.java b/utils/misc/src/main/java/org/onlab/util/HexDump.java
index 692d688..853a597 100644
--- a/utils/misc/src/main/java/org/onlab/util/HexDump.java
+++ b/utils/misc/src/main/java/org/onlab/util/HexDump.java
@@ -43,7 +43,7 @@
                 StringBuilder sb = new StringBuilder();
                 for (int k = 0; (k < 16) && (buff.readableBytes() != 0); ++k) {
                     if (0 == k % 4) {
-                        sb.append(String.format(" ")); // blank after 4 bytes
+                        sb.append(" "); // blank after 4 bytes
                     }
                     sb.append(String.format("%02X ", buff.readByte()));
                 }