1 /**
2  * Copyright © 2019 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "severity.hpp"
17 
18 namespace openpower
19 {
20 namespace pels
21 {
22 
23 using LogSeverity = phosphor::logging::Entry::Level;
24 
25 namespace
26 {
27 
28 LogSeverity convertPELSeverityToOBMC(SeverityType pelSeverity)
29 {
30     LogSeverity logSeverity = LogSeverity::Error;
31 
32     const std::map<SeverityType, LogSeverity> severities{
33         {SeverityType::nonError, LogSeverity::Informational},
34         {SeverityType::recovered, LogSeverity::Informational},
35         {SeverityType::predictive, LogSeverity::Warning},
36         {SeverityType::unrecoverable, LogSeverity::Error},
37         {SeverityType::critical, LogSeverity::Critical},
38         {SeverityType::diagnostic, LogSeverity::Error},
39         {SeverityType::symptom, LogSeverity::Warning}};
40 
41     auto s = severities.find(pelSeverity);
42     if (s != severities.end())
43     {
44         logSeverity = s->second;
45     }
46 
47     return logSeverity;
48 }
49 
50 } // namespace
51 
52 uint8_t convertOBMCSeverityToPEL(LogSeverity severity)
53 {
54     uint8_t pelSeverity = static_cast<uint8_t>(SeverityType::unrecoverable);
55     switch (severity)
56     {
57         case (LogSeverity::Notice):
58         case (LogSeverity::Informational):
59         case (LogSeverity::Debug):
60             pelSeverity = static_cast<uint8_t>(SeverityType::nonError);
61             break;
62 
63         case (LogSeverity::Warning):
64             pelSeverity = static_cast<uint8_t>(SeverityType::predictive);
65             break;
66 
67         case (LogSeverity::Critical):
68             pelSeverity = static_cast<uint8_t>(SeverityType::critical);
69             break;
70 
71         case (LogSeverity::Emergency):
72         case (LogSeverity::Alert):
73         case (LogSeverity::Error):
74             pelSeverity = static_cast<uint8_t>(SeverityType::unrecoverable);
75             break;
76     }
77 
78     return pelSeverity;
79 }
80 
81 std::optional<LogSeverity> fixupLogSeverity(LogSeverity obmcSeverity,
82                                             SeverityType pelSeverity)
83 {
84     bool isNonErrPelSev = (pelSeverity == SeverityType::nonError) ||
85                           (pelSeverity == SeverityType::recovered);
86 
87     bool isNonErrObmcSev = (obmcSeverity == LogSeverity::Notice) ||
88                            (obmcSeverity == LogSeverity::Informational) ||
89                            (obmcSeverity == LogSeverity::Debug);
90 
91     // If a nonError/recovered PEL, then the LogSeverity must be
92     // Notice/Informational/Debug, otherwise set it to Informational.
93     if (isNonErrPelSev && !isNonErrObmcSev)
94     {
95         return LogSeverity::Informational;
96     }
97 
98     // If a Notice/Informational/Debug LogSeverity, then the PEL
99     // severity must be nonError/recovered, otherwise set it
100     // to an appropriate value based on the actual PEL severity.
101     if (isNonErrObmcSev && !isNonErrPelSev)
102     {
103         return convertPELSeverityToOBMC(pelSeverity);
104     }
105 
106     // If PEL is critical, the LogSeverity should be as well.
107     if ((obmcSeverity != LogSeverity::Critical) &&
108         (pelSeverity == SeverityType::critical))
109     {
110         return LogSeverity::Critical;
111     }
112 
113     return std::nullopt;
114 }
115 
116 } // namespace pels
117 } // namespace openpower
118