PEL: Const accessors for Private/UserHeader

Change the combined non-const accessor/setter functions for the
PrivateHeader and UserHeader fields to the standard const accessors, and
have separate setters for the fields that need to be modified.

In addition, make the 'get PrivateHeader' and 'get UserHeader' functions
on the PEL class return a const reference.

This allows const enforcement on the PEL class, for things like:

void func(const PEL& pel)
{
    auto id = pel.privateHeader().id();
}

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I840170e72b41e9e2465f716617500269244de6d9
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index af904ff..29d86d5 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -37,7 +37,7 @@
         _optionalSections.push_back(std::move(ud));
     }
 
-    _ph->sectionCount() = 2 + _optionalSections.size();
+    _ph->setSectionCount(2 + _optionalSections.size());
 }
 
 PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
@@ -55,7 +55,7 @@
     _ph = std::make_unique<PrivateHeader>(pelData);
     if (obmcLogID != 0)
     {
-        _ph->obmcLogID() = obmcLogID;
+        _ph->setOBMCLogID(obmcLogID);
     }
 
     _uh = std::make_unique<UserHeader>(pelData);
@@ -92,12 +92,12 @@
 void PEL::setCommitTime()
 {
     auto now = std::chrono::system_clock::now();
-    _ph->commitTimestamp() = getBCDTime(now);
+    _ph->setCommitTimestamp(getBCDTime(now));
 }
 
 void PEL::assignID()
 {
-    _ph->id() = generatePELID();
+    _ph->setID(generatePELID());
 }
 
 void PEL::flatten(std::vector<uint8_t>& pelBuffer)
diff --git a/extensions/openpower-pels/pel.hpp b/extensions/openpower-pels/pel.hpp
index f63cfdd..3d1318f 100644
--- a/extensions/openpower-pels/pel.hpp
+++ b/extensions/openpower-pels/pel.hpp
@@ -152,21 +152,21 @@
     /**
      * @brief Gives access to the Private Header section class
      *
-     * @return std::unique_ptr<PrivateHeader>& the private header
+     * @return const PrivateHeader& - the private header
      */
-    std::unique_ptr<PrivateHeader>& privateHeader()
+    const PrivateHeader& privateHeader() const
     {
-        return _ph;
+        return *_ph;
     }
 
     /**
      * @brief Gives access to the User Header section class
      *
-     * @return std::unique_ptr<UserHeader>& the user header
+     * @return const UserHeader& - the user header
      */
-    std::unique_ptr<UserHeader>& userHeader()
+    const UserHeader& userHeader() const
     {
-        return _uh;
+        return *_uh;
     }
 
     /**
diff --git a/extensions/openpower-pels/private_header.hpp b/extensions/openpower-pels/private_header.hpp
index 9c522a2..7d6a53d 100644
--- a/extensions/openpower-pels/private_header.hpp
+++ b/extensions/openpower-pels/private_header.hpp
@@ -75,9 +75,9 @@
     /**
      * @brief Returns the creation timestamp
      *
-     * @return BCDTime& - the timestamp
+     * @return const BCDTime& - the timestamp
      */
-    BCDTime& createTimestamp()
+    const BCDTime& createTimestamp() const
     {
         return _createTimestamp;
     }
@@ -85,19 +85,29 @@
     /**
      * @brief Returns the commit time timestamp
      *
-     * @return BCDTime& - the timestamp
+     * @return const BCDTime& - the timestamp
      */
-    BCDTime& commitTimestamp()
+    const BCDTime& commitTimestamp() const
     {
         return _commitTimestamp;
     }
 
     /**
+     * @brief Sets the commit timestamp
+     *
+     * @param[in] time - the new timestamp
+     */
+    void setCommitTimestamp(const BCDTime& time)
+    {
+        _commitTimestamp = time;
+    }
+
+    /**
      * @brief Returns the creator ID field
      *
-     * @return uint8_t& - the creator ID
+     * @return uint8_t - the creator ID
      */
-    uint8_t& creatorID()
+    uint8_t creatorID() const
     {
         return _creatorID;
     }
@@ -105,9 +115,9 @@
     /**
      * @brief Returns the log type field
      *
-     * @return uint8_t& - the log type
+     * @return uint8_t - the log type
      */
-    uint8_t& logType()
+    uint8_t logType() const
     {
         return _logType;
     }
@@ -115,27 +125,47 @@
     /**
      * @brief Returns the section count field
      *
-     * @return uint8_t& - the section count
+     * @return uint8_t - the section count
      */
-    uint8_t& sectionCount()
+    uint8_t sectionCount() const
     {
         return _sectionCount;
     }
 
     /**
+     * @brief Sets the section count field
+     *
+     * @param[in] count - the new section count
+     */
+    void setSectionCount(uint8_t count)
+    {
+        _sectionCount = count;
+    }
+
+    /**
      * @brief Returns the OpenBMC log ID field
      *
      * This is the ID the OpenBMC event log that corresponds
      * to this PEL.
      *
-     * @return uint32_t& - the OpenBMC event log ID
+     * @return uint32_t - the OpenBMC event log ID
      */
-    uint32_t& obmcLogID()
+    uint32_t obmcLogID() const
     {
         return _obmcLogID;
     }
 
     /**
+     * @brief Sets the OpenBMC log ID field
+     *
+     * @param[in] id - the new ID
+     */
+    void setOBMCLogID(uint32_t id)
+    {
+        _obmcLogID = id;
+    }
+
+    /**
      * @brief Returns the Creator Version field
      *
      * @return CreatorVersion& - the creator version
@@ -148,19 +178,29 @@
     /**
      * @brief Returns the error log ID field
      *
-     * @return uint32_t& - the error log ID
+     * @return uint32_t - the error log ID
      */
-    uint32_t& id()
+    uint32_t id() const
     {
         return _id;
     }
 
     /**
+     * @brief Sets the ID field
+     *
+     * @param[in] id - the new ID
+     */
+    void setID(uint32_t id)
+    {
+        _id = id;
+    }
+
+    /**
      * @brief Returns the platform log ID field
      *
-     * @return uint32_t& - the platform log ID
+     * @return uint32_t - the platform log ID
      */
-    uint32_t& plid()
+    uint32_t plid() const
     {
         return _plid;
     }
diff --git a/extensions/openpower-pels/user_header.hpp b/extensions/openpower-pels/user_header.hpp
index c1b6636..459952b 100644
--- a/extensions/openpower-pels/user_header.hpp
+++ b/extensions/openpower-pels/user_header.hpp
@@ -64,9 +64,9 @@
     /**
      * @brief Returns the subsystem field.
      *
-     * @return uint8_t& - the subsystem
+     * @return uint8_t - the subsystem
      */
-    uint8_t& subsystem()
+    uint8_t subsystem() const
     {
         return _eventSubsystem;
     }
@@ -74,9 +74,9 @@
     /**
      * @brief Returns the event scope field.
      *
-     * @return uint8_t& - the event scope
+     * @return uint8_t - the event scope
      */
-    uint8_t& scope()
+    uint8_t scope() const
     {
         return _eventScope;
     }
@@ -84,9 +84,9 @@
     /**
      * @brief Returns the severity field.
      *
-     * @return uint8_t& - the severity
+     * @return uint8_t - the severity
      */
-    uint8_t& severity()
+    uint8_t severity() const
     {
         return _eventSeverity;
     }
@@ -94,9 +94,9 @@
     /**
      * @brief Returns the event type field.
      *
-     * @return uint8_t& - the event type
+     * @return uint8_t - the event type
      */
-    uint8_t& eventType()
+    uint8_t eventType() const
     {
         return _eventType;
     }
@@ -104,9 +104,9 @@
     /**
      * @brief Returns the problem domain field.
      *
-     * @return uint8_t& - the problem domain
+     * @return uint8_t - the problem domain
      */
-    uint8_t& problemDomain()
+    uint8_t problemDomain() const
     {
         return _problemDomain;
     }
@@ -114,9 +114,9 @@
     /**
      * @brief Returns the problem vector field.
      *
-     * @return uint8_t& - the problem vector
+     * @return uint8_t - the problem vector
      */
-    uint8_t& problemVector()
+    uint8_t problemVector() const
     {
         return _problemVector;
     }
@@ -124,9 +124,9 @@
     /**
      * @brief Returns the action flags field.
      *
-     * @return uint16_t& - the action flags
+     * @return uint16_t - the action flags
      */
-    uint16_t& actionFlags()
+    uint16_t actionFlags() const
     {
         return _actionFlags;
     }
diff --git a/test/openpower-pels/pel_test.cpp b/test/openpower-pels/pel_test.cpp
index 6494c52..c83b54f 100644
--- a/test/openpower-pels/pel_test.cpp
+++ b/test/openpower-pels/pel_test.cpp
@@ -26,8 +26,8 @@
     EXPECT_TRUE(pel->valid());
     EXPECT_EQ(pel->id(), 0x80818283);
     EXPECT_EQ(pel->plid(), 0x50515253);
-    EXPECT_EQ(pel->userHeader()->subsystem(), 0x10);
-    EXPECT_EQ(pel->userHeader()->actionFlags(), 0x80C0);
+    EXPECT_EQ(pel->userHeader().subsystem(), 0x10);
+    EXPECT_EQ(pel->userHeader().actionFlags(), 0x80C0);
 
     // Test that data in == data out
     auto flattenedData = pel->data();
@@ -86,8 +86,8 @@
 
     auto pel = std::make_unique<PEL>(data);
 
-    EXPECT_TRUE(pel->privateHeader()->valid());
-    EXPECT_FALSE(pel->userHeader()->valid());
+    EXPECT_TRUE(pel->privateHeader().valid());
+    EXPECT_FALSE(pel->userHeader().valid());
     EXPECT_FALSE(pel->valid());
 
     // Now corrupt the private header
@@ -95,8 +95,8 @@
     data.at(0) = 0;
     pel = std::make_unique<PEL>(data);
 
-    EXPECT_FALSE(pel->privateHeader()->valid());
-    EXPECT_TRUE(pel->userHeader()->valid());
+    EXPECT_FALSE(pel->privateHeader().valid());
+    EXPECT_TRUE(pel->userHeader().valid());
     EXPECT_FALSE(pel->valid());
 }
 
@@ -105,8 +105,8 @@
     std::vector<uint8_t> data;
     auto pel = std::make_unique<PEL>(data);
 
-    EXPECT_FALSE(pel->privateHeader()->valid());
-    EXPECT_FALSE(pel->userHeader()->valid());
+    EXPECT_FALSE(pel->privateHeader().valid());
+    EXPECT_FALSE(pel->userHeader().valid());
     EXPECT_FALSE(pel->valid());
 }
 
@@ -128,8 +128,8 @@
             dataIface};
 
     EXPECT_TRUE(pel.valid());
-    EXPECT_EQ(pel.privateHeader()->obmcLogID(), 42);
-    EXPECT_EQ(pel.userHeader()->severity(), 0x40);
+    EXPECT_EQ(pel.privateHeader().obmcLogID(), 42);
+    EXPECT_EQ(pel.userHeader().severity(), 0x40);
 
     EXPECT_EQ(pel.primarySRC().value()->asciiString(),
               "BD051234                        ");
@@ -259,4 +259,4 @@
     EXPECT_EQ(newJSON["KEY1"], "VALUE1");
     EXPECT_EQ(newJSON["KEY2"], "VALUE2");
     EXPECT_EQ(newJSON["KEY3"], "VALUE3");
-}
\ No newline at end of file
+}
diff --git a/test/openpower-pels/private_header_test.cpp b/test/openpower-pels/private_header_test.cpp
index 07746b3..4eb052b 100644
--- a/test/openpower-pels/private_header_test.cpp
+++ b/test/openpower-pels/private_header_test.cpp
@@ -28,7 +28,7 @@
     EXPECT_EQ(ph.header().subType, 0x02);
     EXPECT_EQ(ph.header().componentID, 0x0304);
 
-    auto& ct = ph.createTimestamp();
+    auto ct = ph.createTimestamp();
     EXPECT_EQ(ct.yearMSB, 0x20);
     EXPECT_EQ(ct.yearLSB, 0x30);
     EXPECT_EQ(ct.month, 0x05);
@@ -38,7 +38,7 @@
     EXPECT_EQ(ct.seconds, 0x01);
     EXPECT_EQ(ct.hundredths, 0x63);
 
-    auto& mt = ph.commitTimestamp();
+    auto mt = ph.commitTimestamp();
     EXPECT_EQ(mt.yearMSB, 0x20);
     EXPECT_EQ(mt.yearLSB, 0x31);
     EXPECT_EQ(mt.month, 0x06);
@@ -68,7 +68,7 @@
     EXPECT_EQ(data, newData);
 
     // Change a field, then flatten and unflatten again
-    ph.creatorID() = 0x55;
+    ph.setID(0x55);
 
     newStream.offset(0);
     newData.clear();
@@ -79,7 +79,7 @@
     PrivateHeader newPH(newStream);
 
     EXPECT_TRUE(newPH.valid());
-    EXPECT_EQ(newPH.creatorID(), 0x55);
+    EXPECT_EQ(newPH.id(), 0x55);
 }
 
 TEST_F(PrivateHeaderTest, ShortDataTest)
diff --git a/test/openpower-pels/real_pel_test.cpp b/test/openpower-pels/real_pel_test.cpp
index ec5282d..59cc84e 100644
--- a/test/openpower-pels/real_pel_test.cpp
+++ b/test/openpower-pels/real_pel_test.cpp
@@ -537,7 +537,7 @@
     // Check that the code can extract an object for every section.
     //(The PrivateHeader and UserHeader account for the + 2 below.)
     const auto& sections = pel.optionalSections();
-    EXPECT_EQ(pel.privateHeader()->sectionCount(), sections.size() + 2);
+    EXPECT_EQ(pel.privateHeader().sectionCount(), sections.size() + 2);
 
     auto src = pel.primarySRC();
     EXPECT_EQ(src.value()->asciiString(), "B181A80E                        ");
diff --git a/test/openpower-pels/repository_test.cpp b/test/openpower-pels/repository_test.cpp
index 85ceab6..bd1953f 100644
--- a/test/openpower-pels/repository_test.cpp
+++ b/test/openpower-pels/repository_test.cpp
@@ -61,7 +61,7 @@
 
     // Check that the PEL was stored where it was supposed to be,
     // and that it wrote the PEL data.
-    const auto& ts = pel->privateHeader()->commitTimestamp();
+    const auto ts = pel->privateHeader().commitTimestamp();
     auto name = Repository::getPELFilename(pel->id(), ts);
 
     fs::path file = repoPath / "logs" / name;
diff --git a/test/openpower-pels/user_header_test.cpp b/test/openpower-pels/user_header_test.cpp
index 88ac9a5..ab8b7d6 100644
--- a/test/openpower-pels/user_header_test.cpp
+++ b/test/openpower-pels/user_header_test.cpp
@@ -42,20 +42,6 @@
 
     uh.flatten(newStream);
     EXPECT_EQ(data, newData);
-
-    // Change a field, then flatten and unflatten again
-    uh.subsystem() = 0x44;
-
-    newStream.offset(0);
-    newData.clear();
-    uh.flatten(newStream);
-    EXPECT_NE(data, newData);
-
-    newStream.offset(0);
-    UserHeader newUH(newStream);
-
-    EXPECT_TRUE(newUH.valid());
-    EXPECT_EQ(newUH.subsystem(), 0x44);
 }
 
 TEST(UserHeaderTest, ShortDataTest)