1 #pragma once
2 
3 #include "registry.hpp"
4 #include "section_header.hpp"
5 
6 #include <optional>
7 
8 namespace openpower
9 {
10 namespace pels
11 {
12 /**
13  * @class Section
14  *
15  * The base class for a PEL section.  It contains the SectionHeader
16  * as all sections start with it.
17  *
18  */
19 class Section
20 {
21   public:
22     Section() = default;
23     virtual ~Section() = default;
24     Section(const Section&) = default;
25     Section& operator=(const Section&) = default;
26     Section(Section&&) = default;
27     Section& operator=(Section&&) = default;
28 
29     /**
30      * @brief Returns a reference to the SectionHeader
31      */
32     const SectionHeader& header() const
33     {
34         return _header;
35     }
36 
37     /**
38      * @brief Says if the section is valid.
39      */
40     bool valid() const
41     {
42         return _valid;
43     }
44 
45     /**
46      * @brief Flatten the section into the stream
47      *
48      * @param[in] stream - The stream to write to
49      */
50     virtual void flatten(Stream& stream) const = 0;
51 
52     /**
53      * @brief Get section in JSON. Derived classes to override when required to.
54      * @return std::optional<std::string> - If a section comes with a JSON
55      * representation, this would return the string for it.
56      */
57     virtual std::optional<std::string> getJSON() const
58     {
59         return std::nullopt;
60     }
61 
62     /**
63      * @brief Get section in JSON. Derived classes to override when required to.
64      * @param[in] registry - Registry object reference
65      * @param[in] plugins - Vector of strings of plugins found in filesystem
66      * @param[in] creatorID - Creator Subsystem ID from Private Header
67      * @return std::optional<std::string> - If a section comes with a JSON
68      * representation, this would return the string for it.
69      */
70     virtual std::optional<std::string>
71         getJSON(message::Registry& /*registry*/,
72                 const std::vector<std::string>& /*plugins*/,
73                 uint8_t /*creatorID*/) const
74     {
75         return std::nullopt;
76     }
77 
78     /**
79      * @brief Get section in JSON. Derived classes to override when required to.
80      * @param[in] creatorID - Creator Subsystem ID from Private Header
81      * @param[in] plugins - Vector of strings of plugins found in filesystem
82      * @return std::optional<std::string> - If a section comes with a JSON
83      * representation, this would return the string for it.
84      */
85     virtual std::optional<std::string>
86         getJSON(uint8_t /*creatorID*/,
87                 const std::vector<std::string>& /*plugins*/) const
88     {
89         return std::nullopt;
90     }
91 
92     /**
93      * @brief Shrinks a PEL section
94      *
95      * If this is even possible for a section depends on which section
96      * it is.  If a section cannot be shrunk, it doesn't need to implement
97      * shrink so it will just return false, meaning no shrinking was done.
98      *
99      * If a section can be shrunk, this function will be overridden in that
100      * class.
101      *
102      * @param[in] newSize - The new size, in bytes, to shrink to
103      *
104      * @return bool - true if successful, false else
105      */
106     virtual bool shrink(size_t /*newSize*/)
107     {
108         return false;
109     }
110 
111     /**
112      * @brief Returns any debug data stored in the object
113      *
114      * @return std::vector<std::string>& - The debug data
115      */
116     const std::vector<std::string>& getDebugData() const
117     {
118         return _debugData;
119     }
120 
121   protected:
122     /**
123      * @brief Returns the flattened size of the section header
124      */
125     static constexpr size_t flattenedSize()
126     {
127         return SectionHeader::flattenedSize();
128     }
129 
130     /**
131      * @brief Adds debug data to the object that may be displayed
132      *        in a UserData section in the PEL.
133      *
134      * @param[in] data - The new entry to add to the vector of data.
135      */
136     void addDebugData(const std::string& data)
137     {
138         _debugData.push_back(data);
139     }
140 
141     /**
142      * @brief Used to validate the section.
143      *
144      * Implemented by derived classes.
145      */
146     virtual void validate() = 0;
147 
148     /**
149      * @brief The section header structure.
150      *
151      * Filled in by derived classes.
152      */
153     SectionHeader _header;
154 
155     /**
156      * @brief The section valid flag.
157      *
158      * This is determined by the derived class.
159      */
160     bool _valid = false;
161 
162     /**
163      * @brief Messages that derived classes can add during construction
164      *        of a PEL when something happens that would be useful to
165      *        store in the PEL.  This may get added into a UserData section
166      *        in the PEL.
167      */
168     std::vector<std::string> _debugData;
169 };
170 } // namespace pels
171 } // namespace openpower
172