PEL: Flatten PEL from objects

Now that the Generic section object has been introduced so there are
objects for every section, a flatten can be done by flattening every
object inside the PEL and the previous workaround to save the original
raw data can be removed.

This also adds a test case that uses a real PEL from a previous
generation of systems to flatten to give some better coverage than just
using hand coded PEL sections.

A side affect of this is that the PEL constructors that take the raw
data cannot take a const vector of data, as the Stream class that will
be used to read from the vector cannot take a const.  Testcases have
been updated to ensure this data is not modified.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I64ae1d1d4a742c80e14666d6b2a6e1e0efd5fd62
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index c7df5e8..eb1a368 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -5,6 +5,8 @@
 #include "section_factory.hpp"
 #include "stream.hpp"
 
+#include <phosphor-logging/log.hpp>
+
 namespace openpower
 {
 namespace pels
@@ -23,19 +25,18 @@
     _ph->sectionCount() = 2;
 }
 
-PEL::PEL(const std::vector<uint8_t>& data) : PEL(data, 0)
+PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
 {
 }
 
-PEL::PEL(const std::vector<uint8_t>& data, uint32_t obmcLogID) : _rawPEL(data)
+PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
 {
-    _fromStream = true;
-    populateFromRawData(obmcLogID);
+    populateFromRawData(data, obmcLogID);
 }
 
-void PEL::populateFromRawData(uint32_t obmcLogID)
+void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
 {
-    Stream pelData{_rawPEL};
+    Stream pelData{data};
     _ph = std::make_unique<PrivateHeader>(pelData);
     if (obmcLogID != 0)
     {
@@ -88,28 +89,26 @@
 {
     Stream pelData{pelBuffer};
 
-    _ph->flatten(pelData);
-
-    // If constructed from a PEL stream originally, don't flatten the
-    // rest of the objects until we support every PEL section type.
-    // Still need the PrivateHeader, as we updated fields in it.
-    if (_fromStream)
+    if (!valid())
     {
-        return;
+        using namespace phosphor::logging;
+        log<level::WARNING>("Unflattening an invalid PEL");
     }
 
+    _ph->flatten(pelData);
     _uh->flatten(pelData);
+
+    for (auto& section : _optionalSections)
+    {
+        section->flatten(pelData);
+    }
 }
 
 std::vector<uint8_t> PEL::data()
 {
-    // Until we can recreate a complete PEL from objects, need to just flatten
-    // on top of the original PEL data which we need to keep around for this
-    // reason.  If creating a PEL from scratch, _rawPEL will get filled in with
-    // what we do have.
-
-    flatten(_rawPEL);
-    return _rawPEL;
+    std::vector<uint8_t> pelData;
+    flatten(pelData);
+    return pelData;
 }
 
 } // namespace pels