[ONOS-5853] FlowRuleBuilder: automatically set temp/perm field

Add a new method to the interface to allow setting the idle timeout
directly and it will automatically decide if the rule is permanent
or temporary.

Currently the rules created from OF switches would always be made
temporary, even if the idle-timeout was 0. This would cause ONOS
FlowRuleManager to believe they had expired and would remove them.

To reproduce set up a cluster and make a device change mastership, the
new master would have all rules as temporary, even the ones that were
permanent previously on the first master.

Change-Id: I6bca5ba4d0e2194efad21a98cbcd0ab040977f03
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRule.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRule.java
index 4da33aa..6d6b318 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRule.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRule.java
@@ -249,6 +249,21 @@
         Builder makeTemporary(int timeout);
 
         /**
+         * Sets the idle timeout parameter in flow table.
+         *
+         * Will automatically make it permanent or temporary if the timeout is 0 or not, respectively.
+         * @param timeout an integer
+         * @return this
+         */
+        default Builder withIdleTimeout(int timeout) {
+            if (timeout == 0) {
+                return makePermanent();
+            } else {
+                return makeTemporary(timeout);
+            }
+        }
+
+        /**
          * Sets hard timeout parameter in flow table.
          * @param timeout an integer
          * @return this
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/util/FlowEntryBuilder.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/util/FlowEntryBuilder.java
index c9f5dd3..c68c173 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/util/FlowEntryBuilder.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/util/FlowEntryBuilder.java
@@ -180,7 +180,7 @@
                             .withSelector(buildSelector())
                             .withTreatment(buildTreatment())
                             .withPriority(stat.getPriority())
-                            .makeTemporary(stat.getIdleTimeout())
+                            .withIdleTimeout(stat.getIdleTimeout())
                             .withCookie(stat.getCookie().getValue());
                     if (stat.getVersion() != OFVersion.OF_10) {
                         builder.forTable(stat.getTableId().getValue());
@@ -206,7 +206,7 @@
                             .forDevice(deviceId)
                             .withSelector(buildSelector())
                             .withPriority(removed.getPriority())
-                            .makeTemporary(removed.getIdleTimeout())
+                            .withIdleTimeout(removed.getIdleTimeout())
                             .withCookie(removed.getCookie().getValue())
                             .withReason(FlowRule.FlowRemoveReason.parseShort(removed.getReason()));
 
@@ -236,7 +236,7 @@
                             .withSelector(buildSelector())
                             .withTreatment(buildTreatment())
                             .withPriority(flowMod.getPriority())
-                            .makeTemporary(flowMod.getIdleTimeout())
+                            .withIdleTimeout(flowMod.getIdleTimeout())
                             .withCookie(flowMod.getCookie().getValue());
                     if (flowMod.getVersion() != OFVersion.OF_10) {
                         builder.forTable(flowMod.getTableId().getValue());