PEL: Remove a PEL based on an ID

When someone deletes an OpenBMC event log, the corresponding PEL should
also be deleted.  This commit adds support for that by keeping an
internal map of the IDs to the files that contain the PEL data in the
repository in order to find the file to remove.  This will then get
called in the phosphor-logging extension's delete hook.

The actual map key is a structure of both the PEL ID and OpenBMC log ID,
so either can be used to find a PEL in the repository, which will be
useful in other cases, for example for retrieving PEL data based on the
PEL ID.

As the map needs to match the actual repository file contents, it will
get built on startup, and modified when PELs are added and removed.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I33bb96da297c770e175c5c6b19705dda1c8766b6
diff --git a/test/openpower-pels/repository_test.cpp b/test/openpower-pels/repository_test.cpp
index 97019e8..6963184 100644
--- a/test/openpower-pels/repository_test.cpp
+++ b/test/openpower-pels/repository_test.cpp
@@ -71,3 +71,70 @@
     auto pelData = pel->data();
     EXPECT_EQ(*newData, pelData);
 }
+
+TEST_F(RepositoryTest, RestoreTest)
+{
+    using pelID = Repository::LogID::Pel;
+    using obmcID = Repository::LogID::Obmc;
+
+    std::vector<Repository::LogID> ids;
+
+    {
+        Repository repo{repoPath};
+
+        // Add some PELs to the repository
+        {
+            auto data = pelDataFactory(TestPelType::pelSimple);
+            auto pel = std::make_unique<PEL>(*data, 1);
+            pel->assignID();
+            repo.add(pel);
+            ids.emplace_back(pelID(pel->id()), obmcID(1));
+        }
+        {
+            auto data = pelDataFactory(TestPelType::pelSimple);
+            auto pel = std::make_unique<PEL>(*data, 2);
+            pel->assignID();
+            repo.add(pel);
+            ids.emplace_back(pelID(pel->id()), obmcID(2));
+        }
+
+        // Check they're there
+        EXPECT_TRUE(repo.hasPEL(ids[0]));
+        EXPECT_TRUE(repo.hasPEL(ids[1]));
+
+        // Do some other search tests while we're here.
+
+        // Search based on PEL ID
+        Repository::LogID id(pelID(ids[0].pelID));
+        EXPECT_TRUE(repo.hasPEL(id));
+
+        // Search based on OBMC log ID
+        id.pelID.id = 0;
+        id.obmcID = ids[0].obmcID;
+        EXPECT_TRUE(repo.hasPEL(id));
+
+        // ... based on the other PEL ID
+        id.pelID = ids[1].pelID;
+        id.obmcID.id = 0;
+        EXPECT_TRUE(repo.hasPEL(id));
+
+        // Not found
+        id.pelID.id = 99;
+        id.obmcID.id = 100;
+        EXPECT_FALSE(repo.hasPEL(id));
+    }
+
+    {
+        // Restore and check they're still there, then
+        // remove them.
+        Repository repo{repoPath};
+        EXPECT_TRUE(repo.hasPEL(ids[0]));
+        EXPECT_TRUE(repo.hasPEL(ids[1]));
+
+        repo.remove(ids[0]);
+        EXPECT_FALSE(repo.hasPEL(ids[0]));
+
+        repo.remove(ids[1]);
+        EXPECT_FALSE(repo.hasPEL(ids[1]));
+    }
+}