PEL: Create object for every section

When unflattening a PEL, create objects for every PEL section in the
log.  It will use a factory method to choose which object type to create
based on the section ID in the section header.  All of these object will
go into a vector of Section objects, which is the base class for every
PEL section class.

The factory will default to creating a Generic object when it doesn't
have any other type to create.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Ief0e4df5c586a46cea66ca47b4479e3444815309
diff --git a/extensions/openpower-pels/openpower-pels.mk b/extensions/openpower-pels/openpower-pels.mk
index f1603b6..cca1228 100644
--- a/extensions/openpower-pels/openpower-pels.mk
+++ b/extensions/openpower-pels/openpower-pels.mk
@@ -13,5 +13,6 @@
 	extensions/openpower-pels/private_header.cpp \
 	extensions/openpower-pels/registry.cpp \
 	extensions/openpower-pels/repository.cpp \
+	extensions/openpower-pels/section_factory.cpp \
 	extensions/openpower-pels/severity.cpp \
 	extensions/openpower-pels/user_header.cpp
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index 6a56e15..c7df5e8 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -2,6 +2,7 @@
 
 #include "bcd_time.hpp"
 #include "log_id.hpp"
+#include "section_factory.hpp"
 #include "stream.hpp"
 
 namespace openpower
@@ -42,6 +43,13 @@
     }
 
     _uh = std::make_unique<UserHeader>(pelData);
+
+    // Use the section factory to create the rest of the objects
+    for (size_t i = 2; i < _ph->sectionCount(); i++)
+    {
+        auto section = section_factory::create(pelData);
+        _optionalSections.push_back(std::move(section));
+    }
 }
 
 bool PEL::valid() const
@@ -53,6 +61,15 @@
         valid = _uh->valid();
     }
 
+    if (valid)
+    {
+        if (!std::all_of(_optionalSections.begin(), _optionalSections.end(),
+                         [](const auto& section) { return section->valid(); }))
+        {
+            valid = false;
+        }
+    }
+
     return valid;
 }
 
diff --git a/extensions/openpower-pels/pel.hpp b/extensions/openpower-pels/pel.hpp
index ff45acb..29a7978 100644
--- a/extensions/openpower-pels/pel.hpp
+++ b/extensions/openpower-pels/pel.hpp
@@ -163,6 +163,17 @@
     }
 
     /**
+     * @brief Returns the optional sections, which is everything but
+     *        the Private and User Headers.
+     *
+     * @return const std::vector<std::unique_ptr<Section>>&
+     */
+    const std::vector<std::unique_ptr<Section>>& optionalSections() const
+    {
+        return _optionalSections;
+    }
+
+    /**
      * @brief Returns the PEL data.
      *
      * @return std::vector<uint8_t> - the raw PEL data
@@ -225,6 +236,11 @@
      * @brief If the PEL came from a flattened data stream.
      */
     bool _fromStream = false;
+
+    /**
+     * @brief Holds all sections by the PH and UH.
+     */
+    std::vector<std::unique_ptr<Section>> _optionalSections;
 };
 
 } // namespace pels
diff --git a/extensions/openpower-pels/section_factory.cpp b/extensions/openpower-pels/section_factory.cpp
new file mode 100644
index 0000000..69274c0
--- /dev/null
+++ b/extensions/openpower-pels/section_factory.cpp
@@ -0,0 +1,52 @@
+#include "section_factory.hpp"
+
+#include "failing_mtms.hpp"
+#include "generic.hpp"
+#include "pel_types.hpp"
+#include "private_header.hpp"
+#include "user_header.hpp"
+
+namespace openpower
+{
+namespace pels
+{
+namespace section_factory
+{
+std::unique_ptr<Section> create(Stream& pelData)
+{
+    std::unique_ptr<Section> section;
+
+    // Peek the section ID to create the appriopriate object.
+    // If not enough data remains to do so, an invalid
+    // Generic object will be created in the default case.
+    uint16_t sectionID = 0;
+
+    if (pelData.remaining() >= 2)
+    {
+        pelData >> sectionID;
+        pelData.offset(pelData.offset() - 2);
+    }
+
+    switch (sectionID)
+    {
+        case static_cast<uint16_t>(SectionID::privateHeader):
+            section = std::make_unique<PrivateHeader>(pelData);
+            break;
+        case static_cast<uint16_t>(SectionID::userHeader):
+            section = std::make_unique<UserHeader>(pelData);
+            break;
+        case static_cast<uint16_t>(SectionID::failingMTMS):
+            section = std::make_unique<FailingMTMS>(pelData);
+            break;
+        default:
+            // A generic object, but at least an object.
+            section = std::make_unique<Generic>(pelData);
+            break;
+    }
+
+    return section;
+}
+
+} // namespace section_factory
+} // namespace pels
+} // namespace openpower
diff --git a/extensions/openpower-pels/section_factory.hpp b/extensions/openpower-pels/section_factory.hpp
new file mode 100644
index 0000000..8807eba
--- /dev/null
+++ b/extensions/openpower-pels/section_factory.hpp
@@ -0,0 +1,30 @@
+#pragma once
+
+#include "section.hpp"
+#include "stream.hpp"
+
+namespace openpower
+{
+namespace pels
+{
+namespace section_factory
+{
+
+/**
+ * @brief Create a PEL section based on its data
+ *
+ * This creates the appropriate PEL section object based on the section ID in
+ * the first 2 bytes of the stream, but returns the base class Section pointer.
+ *
+ * If there isn't a class specifically for that section, it defaults to
+ * creating an instance of the 'Generic' class.
+ *
+ * @param[in] pelData - The PEL data stream
+ *
+ * @return std::unique_ptr<Section> - class of the appropriate type
+ */
+std::unique_ptr<Section> create(Stream& pelData);
+
+} // namespace section_factory
+} // namespace pels
+} // namespace openpower
diff --git a/test/openpower-pels/Makefile.include b/test/openpower-pels/Makefile.include
index 611a2fa..eacc6ae 100644
--- a/test/openpower-pels/Makefile.include
+++ b/test/openpower-pels/Makefile.include
@@ -20,11 +20,15 @@
 
 pel_objects = \
 	$(top_builddir)/extensions/openpower-pels/bcd_time.o \
+	$(top_builddir)/extensions/openpower-pels/failing_mtms.o \
+	$(top_builddir)/extensions/openpower-pels/generic.o \
 	$(top_builddir)/extensions/openpower-pels/log_id.o \
+	$(top_builddir)/extensions/openpower-pels/mtms.o \
 	$(top_builddir)/extensions/openpower-pels/pel.o \
 	$(top_builddir)/extensions/openpower-pels/pel_values.o \
 	$(top_builddir)/extensions/openpower-pels/private_header.o \
 	$(top_builddir)/extensions/openpower-pels/registry.o \
+	$(top_builddir)/extensions/openpower-pels/section_factory.o \
 	$(top_builddir)/extensions/openpower-pels/severity.o \
 	$(top_builddir)/extensions/openpower-pels/user_header.o
 
diff --git a/test/openpower-pels/pel_test.cpp b/test/openpower-pels/pel_test.cpp
index 6d888b7..8c9faae 100644
--- a/test/openpower-pels/pel_test.cpp
+++ b/test/openpower-pels/pel_test.cpp
@@ -1,4 +1,5 @@
 #include "elog_entry.hpp"
+#include "extensions/openpower-pels/generic.hpp"
 #include "extensions/openpower-pels/pel.hpp"
 #include "pel_utils.hpp"
 
@@ -124,3 +125,94 @@
 
     // Add more checks as more sections are added
 }
+
+// Test that we'll create Generic optional sections for sections that
+// there aren't explicit classes for.
+TEST_F(PELTest, GenericSectionTest)
+{
+    auto data = pelDataFactory(TestPelType::pelSimple);
+
+    std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX'
+                                  0x00, 0x18, // Size
+                                  0x01, 0x02, // version, subtype
+                                  0x03, 0x04, // comp ID
+
+                                  // some data
+                                  0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63,
+                                  0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A,
+                                  0x00};
+
+    std::vector<uint8_t> section2{
+        0x59, 0x59, // ID 'YY'
+        0x00, 0x20, // Size
+        0x01, 0x02, // version, subtype
+        0x03, 0x04, // comp ID
+
+        // some data
+        0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F,
+        0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
+
+    // Add the new sections at the end
+    data->insert(data->end(), section1.begin(), section1.end());
+    data->insert(data->end(), section2.begin(), section2.end());
+
+    // Increment the section count
+    data->at(27) += 2;
+
+    PEL pel{*data};
+
+    const auto& sections = pel.optionalSections();
+
+    bool foundXX = false;
+    bool foundYY = false;
+
+    // Check that we can find these 2 Generic sections
+    for (const auto& section : sections)
+    {
+        if (section->header().id == 0x5858)
+        {
+            foundXX = true;
+            EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
+        }
+        else if (section->header().id == 0x5959)
+        {
+            foundYY = true;
+            EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
+        }
+    }
+
+    EXPECT_TRUE(foundXX);
+    EXPECT_TRUE(foundYY);
+}
+
+// Test that an invalid section will still get a Generic object
+TEST_F(PELTest, InvalidGenericTest)
+{
+    auto data = pelDataFactory(TestPelType::pelSimple);
+
+    // Not a valid section
+    std::vector<uint8_t> section1{0x01, 0x02, 0x03};
+
+    data->insert(data->end(), section1.begin(), section1.end());
+
+    // Increment the section count
+    data->at(27) += 1;
+
+    PEL pel{*data};
+    EXPECT_FALSE(pel.valid());
+
+    const auto& sections = pel.optionalSections();
+
+    bool foundGeneric = false;
+    for (const auto& section : sections)
+    {
+        if (dynamic_cast<Generic*>(section.get()) != nullptr)
+        {
+            foundGeneric = true;
+            EXPECT_EQ(section->valid(), false);
+            break;
+        }
+    }
+
+    EXPECT_TRUE(foundGeneric);
+}