Fixed a dual-level NPE involving flow-rule payload. We need more unit tests and automated system tests!!!

Change-Id: I821214cc274377dc22cb5ab88c48567eaab778cb
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java
index d04af51..2621f31 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/OpenFlowRuleProvider.java
@@ -37,6 +37,7 @@
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.FlowRuleBatchEntry;
 import org.onosproject.net.flow.FlowRuleBatchOperation;
+import org.onosproject.net.flow.FlowRuleExtPayLoad;
 import org.onosproject.net.flow.FlowRuleProvider;
 import org.onosproject.net.flow.FlowRuleProviderRegistry;
 import org.onosproject.net.flow.FlowRuleProviderService;
@@ -147,8 +148,9 @@
     private void applyRule(FlowRule flowRule) {
         OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId()
                 .uri()));
-        if (flowRule.payLoad().payLoad().length > 0) {
-            OFMessage msg = new ThirdPartyMessage(flowRule.payLoad().payLoad());
+        FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
+        if (hasPayload(flowRuleExtPayLoad)) {
+            OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
             sw.sendMsg(msg);
             return;
         }
@@ -161,14 +163,14 @@
         for (FlowRule flowRule : flowRules) {
             removeRule(flowRule);
         }
-
     }
 
     private void removeRule(FlowRule flowRule) {
         OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId()
                 .uri()));
-        if (flowRule.payLoad().payLoad().length > 0) {
-            OFMessage msg = new ThirdPartyMessage(flowRule.payLoad().payLoad());
+        FlowRuleExtPayLoad flowRuleExtPayLoad = flowRule.payLoad();
+        if (hasPayload(flowRuleExtPayLoad)) {
+            OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
             sw.sendMsg(msg);
             return;
         }
@@ -192,9 +194,10 @@
         OFFlowMod mod;
         for (FlowRuleBatchEntry fbe : batch.getOperations()) {
             // flow is the third party privacy flow
-            if (fbe.target().payLoad().payLoad().length > 0) {
-                OFMessage msg = new ThirdPartyMessage(fbe.target().payLoad()
-                        .payLoad());
+
+            FlowRuleExtPayLoad flowRuleExtPayLoad = fbe.target().payLoad();
+            if (hasPayload(flowRuleExtPayLoad)) {
+                OFMessage msg = new ThirdPartyMessage(flowRuleExtPayLoad.payLoad());
                 sw.sendMsg(msg);
                 continue;
             }
@@ -222,6 +225,12 @@
         sw.sendMsg(builder.build());
     }
 
+    private boolean hasPayload(FlowRuleExtPayLoad flowRuleExtPayLoad) {
+        return flowRuleExtPayLoad != null &&
+                flowRuleExtPayLoad.payLoad() != null &&
+                flowRuleExtPayLoad.payLoad().length > 0;
+    }
+
     private class InternalFlowProvider
             implements OpenFlowSwitchListener, OpenFlowEventListener {