Ignore entries from constant tables in P4Runtime

Change-Id: I6ba8591a9674287832a18e258c5dd3801c588a32
(cherry picked from commit ba73da5bd09a6b40ab5727ef4a03767d437687e8)
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java
index e2461a8..ba66c10 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiTableModel.java
@@ -106,6 +106,14 @@
     boolean hasDefaultMutableParams();
 
     /**
+     * Returns true if the table is populated with static entries that cannot be
+     * modified by the control plane at runtime.
+     *
+     * @return true if table is populated with static entries, false otherwise
+     */
+    boolean isConstantTable();
+
+    /**
      * Returns the action model associated with the given ID, if present. If not present, it means that this table does
      * not support such an action.
      *
diff --git a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java
index d655132..5e3e155 100644
--- a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java
+++ b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java
@@ -16,11 +16,9 @@
 
 package org.onosproject.drivers.p4runtime;
 
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Striped;
 import org.onlab.util.SharedExecutors;
 import org.onosproject.drivers.p4runtime.mirror.P4RuntimeTableMirror;
 import org.onosproject.drivers.p4runtime.mirror.TimedEntry;
@@ -46,9 +44,7 @@
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 import java.util.stream.Collectors;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -94,18 +90,9 @@
     // FIXME: set to true as soon as the feature is implemented in P4Runtime.
     private static final boolean DEFAULT_READ_ALL_DIRECT_COUNTERS = false;
 
-    private static final int TABLE_ENTRY_LOCK_EXPIRE_TIME_IN_MIN = 10;
-
     // Needed to synchronize operations over the same table entry.
-    private static final LoadingCache<PiTableEntryHandle, Lock>
-            ENTRY_LOCKS = CacheBuilder.newBuilder()
-            .expireAfterAccess(TABLE_ENTRY_LOCK_EXPIRE_TIME_IN_MIN, TimeUnit.MINUTES)
-            .build(new CacheLoader<PiTableEntryHandle, Lock>() {
-                @Override
-                public Lock load(PiTableEntryHandle handle) {
-                    return new ReentrantLock();
-                }
-            });
+    private static final Striped<Lock> ENTRY_LOCKS = Striped.lock(30);
+
     private PiPipelineModel pipelineModel;
     private P4RuntimeTableMirror tableMirror;
     private PiFlowRuleTranslator translator;
@@ -141,7 +128,11 @@
         // TODO: ONOS-7596 read counters with table entries
         final Collection<PiTableEntry> installedEntries = getFutureWithDeadline(
                 client.dumpAllTables(pipeconf), "dumping all tables",
-                Collections.emptyList());
+                Collections.emptyList())
+                // Filter out entries from constant table.
+                .stream()
+                .filter(e -> !tableIsConstant(e.table()))
+                .collect(Collectors.toList());
 
         if (installedEntries.isEmpty()) {
             return Collections.emptyList();
@@ -170,7 +161,7 @@
         }
 
         if (inconsistentEntries.size() > 0) {
-            // Async clean up inconsistent entries.
+            // Trigger clean up of inconsistent entries.
             SharedExecutors.getSingleThreadExecutor().execute(
                     () -> cleanUpInconsistentEntries(inconsistentEntries));
         }
@@ -261,14 +252,14 @@
                     .of(deviceId, piEntryToApply);
 
             // Serialize operations over the same match key/table/device ID.
-            ENTRY_LOCKS.getUnchecked(handle).lock();
+            ENTRY_LOCKS.get(handle).lock();
             try {
                 if (applyEntry(handle, piEntryToApply,
                                ruleToApply, driverOperation)) {
                     result.add(ruleToApply);
                 }
             } finally {
-                ENTRY_LOCKS.getUnchecked(handle).unlock();
+                ENTRY_LOCKS.get(handle).unlock();
             }
         }
 
@@ -398,6 +389,11 @@
                 !pipelineModel.table(tableId).get().counters().isEmpty();
     }
 
+    private boolean tableIsConstant(PiTableId tableId) {
+        return pipelineModel.table(tableId).isPresent() &&
+                pipelineModel.table(tableId).get().isConstantTable();
+    }
+
     enum Operation {
         APPLY, REMOVE
     }
diff --git a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java
index e60f25f..ff49928 100644
--- a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java
+++ b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java
@@ -215,7 +215,8 @@
                             tableFieldMapBuilder.build(),
                             tableActionMapBuilder.build(),
                             actionMap.get(tableMsg.getConstDefaultActionId()),
-                            tableMsg.getConstDefaultActionHasMutableParams()));
+                            tableMsg.getConstDefaultActionHasMutableParams(),
+                            tableMsg.getIsConstTable()));
 
         }
 
diff --git a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4TableModel.java b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4TableModel.java
index 89c4bed..77f55ec 100644
--- a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4TableModel.java
+++ b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4TableModel.java
@@ -50,14 +50,16 @@
     private final ImmutableMap<PiActionId, PiActionModel> actions;
     private final PiActionModel defaultAction;
     private final boolean hasDefaultMutableParams;
+    private final boolean isConstTable;
 
     P4TableModel(PiTableId id, PiTableType tableType,
-                        PiActionProfileModel actionProfile, long maxSize,
-                        ImmutableMap<PiCounterId, PiCounterModel> counters,
-                        ImmutableMap<PiMeterId, PiMeterModel> meters, boolean supportAging,
-                        ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields,
-                        ImmutableMap<PiActionId, PiActionModel> actions,
-                        PiActionModel defaultAction, boolean hasDefaultMutableParams) {
+                 PiActionProfileModel actionProfile, long maxSize,
+                 ImmutableMap<PiCounterId, PiCounterModel> counters,
+                 ImmutableMap<PiMeterId, PiMeterModel> meters, boolean supportAging,
+                 ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields,
+                 ImmutableMap<PiActionId, PiActionModel> actions,
+                 PiActionModel defaultAction, boolean hasDefaultMutableParams,
+                 boolean isConstTable) {
         this.id = id;
         this.tableType = tableType;
         this.actionProfile = actionProfile;
@@ -69,6 +71,7 @@
         this.actions = actions;
         this.defaultAction = defaultAction;
         this.hasDefaultMutableParams = hasDefaultMutableParams;
+        this.isConstTable = isConstTable;
     }
 
     @Override
@@ -127,6 +130,11 @@
     }
 
     @Override
+    public boolean isConstantTable() {
+        return isConstTable;
+    }
+
+    @Override
     public Optional<PiActionModel> action(PiActionId actionId) {
         return Optional.ofNullable(actions.get(actionId));
     }
diff --git a/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java b/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java
index 84d3af8..da10762 100644
--- a/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java
+++ b/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java
@@ -269,14 +269,17 @@
     private static final boolean HAS_DEFAULT_MUTABLE_PARAMS_1 = true;
     private static final boolean HAS_DEFAULT_MUTABLE_PARAMS_2 = false;
 
+    private static final boolean IS_CONST_TABLE_1 = true;
+    private static final boolean IS_CONST_TABLE_2 = false;
+
     private static final PiTableModel P4_TABLE_MODEL_1 =
             new P4TableModel(PI_TABLE_ID_1, PI_TABLE_TYPE_1, P4_ACTION_PROFILE_MODEL_1, MAX_SIZE_1, COUNTERS_1,
                              METERS_1, SUPPORT_AGING_1, MATCH_FIELDS_1, ACTIONS_1, P4_ACTION_MODEL_DEFAULT_1,
-                             HAS_DEFAULT_MUTABLE_PARAMS_1);
+                             HAS_DEFAULT_MUTABLE_PARAMS_1, IS_CONST_TABLE_1);
     private static final PiTableModel P4_TABLE_MODEL_2 =
             new P4TableModel(PI_TABLE_ID_2, PI_TABLE_TYPE_2, P4_ACTION_PROFILE_MODEL_2, MAX_SIZE_2, COUNTERS_2,
                              METERS_2, SUPPORT_AGING_2, MATCH_FIELDS_2, ACTIONS_2, P4_ACTION_MODEL_DEFAULT_2,
-                             HAS_DEFAULT_MUTABLE_PARAMS_2);
+                             HAS_DEFAULT_MUTABLE_PARAMS_2, IS_CONST_TABLE_2);
 
     /* Packet operations */
     private static final PiPacketOperationType PI_PACKET_OPERATION_TYPE_1 = PiPacketOperationType.PACKET_IN;
@@ -360,4 +363,4 @@
                 .addEqualityGroup(P4_PIPELINE_MODEL_2)
                 .testEquals();
     }
-}
\ No newline at end of file
+}
diff --git a/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4TableModelTest.java b/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4TableModelTest.java
index 21c7900..89b10d0 100644
--- a/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4TableModelTest.java
+++ b/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4TableModelTest.java
@@ -38,6 +38,7 @@
 import org.onosproject.net.pi.model.PiTableId;
 import org.onosproject.net.pi.model.PiTableModel;
 import org.onosproject.net.pi.model.PiTableType;
+
 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
 
 /**
@@ -241,18 +242,21 @@
     private static final boolean HAS_DEFAULT_MUTABLE_PARAMS_1 = true;
     private static final boolean HAS_DEFAULT_MUTABLE_PARAMS_2 = false;
 
+    private static final boolean IS_CONST_TABLE_1 = true;
+    private static final boolean IS_CONST_TABLE_2 = false;
+
     private static final PiTableModel P4_TABLE_MODEL_1 =
             new P4TableModel(PI_TABLE_ID_1, PI_TABLE_TYPE_1, P4_ACTION_PROFILE_MODEL_1, MAX_SIZE_1, COUNTERS_1,
                              METERS_1, SUPPORT_AGING_1, MATCH_FIELDS_1, ACTIONS_1, P4_ACTION_MODEL_DEFAULT_1,
-                             HAS_DEFAULT_MUTABLE_PARAMS_1);
+                             HAS_DEFAULT_MUTABLE_PARAMS_1, IS_CONST_TABLE_1);
     private static final PiTableModel SAME_AS_P4_TABLE_MODEL_1 =
             new P4TableModel(PI_TABLE_ID_1, PI_TABLE_TYPE_1, P4_ACTION_PROFILE_MODEL_1, MAX_SIZE_1, COUNTERS_1,
                              METERS_1, SUPPORT_AGING_1, MATCH_FIELDS_1, ACTIONS_1, P4_ACTION_MODEL_DEFAULT_1,
-                             HAS_DEFAULT_MUTABLE_PARAMS_1);
+                             HAS_DEFAULT_MUTABLE_PARAMS_1, IS_CONST_TABLE_1);
     private static final PiTableModel P4_TABLE_MODEL_2 =
             new P4TableModel(PI_TABLE_ID_2, PI_TABLE_TYPE_2, P4_ACTION_PROFILE_MODEL_2, MAX_SIZE_2, COUNTERS_2,
                              METERS_2, SUPPORT_AGING_2, MATCH_FIELDS_2, ACTIONS_2, P4_ACTION_MODEL_DEFAULT_2,
-                             HAS_DEFAULT_MUTABLE_PARAMS_2);
+                             HAS_DEFAULT_MUTABLE_PARAMS_2, IS_CONST_TABLE_2);
 
     /**
      * Checks that the P4TableModel class is immutable.