blob: 7477895e46749c91c88a01b7750876650d5a6140 [file] [log] [blame]
Ratan Guptaaaf87de2018-04-16 15:25:13 +05301/**
2 * @brief SNMP Error Notification class.
3 *
4 * This file is part of phosphor-snmp project.
5 *
6 * Copyright (c) 2018 IBM Corporation
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * Note: In near future this file will be autogenerated by the custom parser.
21 *
22 */
23
24#pragma once
25
Patrick Williams1334b7b2021-02-22 17:15:12 -060026// net-snmp requires a very specific header include order.
27// disable clang-format around this block
28// clang-format off
Ratan Guptaaaf87de2018-04-16 15:25:13 +053029#include <net-snmp/net-snmp-config.h>
30#include <net-snmp/net-snmp-includes.h>
31#include <net-snmp/agent/net-snmp-agent-includes.h>
Patrick Williams1334b7b2021-02-22 17:15:12 -060032// clang-format on
Ratan Guptaaaf87de2018-04-16 15:25:13 +053033
34#include <sdbusplus/server.hpp>
35
Ratan Guptaaaf87de2018-04-16 15:25:13 +053036#include <sstream>
37#include <string>
Ratan Guptaaaf87de2018-04-16 15:25:13 +053038#include <vector>
39
Ratan Guptaaaf87de2018-04-16 15:25:13 +053040namespace phosphor
41{
42namespace network
43{
44namespace snmp
45{
46
47using OID = std::array<oid, MAX_OID_LEN>;
48using OID_LEN = size_t;
49using Type = u_char;
50
Patrick Williamse5d90c32020-05-13 18:00:44 -050051using Value = std::variant<uint32_t, uint64_t, int32_t, std::string>;
Ratan Guptaaaf87de2018-04-16 15:25:13 +053052// Generic snmp trap ID
53oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
Ratan Gupta0d5094b2021-03-05 19:25:26 +053054oid sysuptimeOID[] = {1, 3, 6, 1, 2, 1, 1, 3, 0};
Ratan Guptaaaf87de2018-04-16 15:25:13 +053055
Ed Tanous46d7ea42023-07-12 14:59:29 -070056struct Object
57{
58 OID oid;
59 OID_LEN oid_len;
60 Type type;
61 Value value;
62};
Ratan Guptaaaf87de2018-04-16 15:25:13 +053063
64/** @brief Get the ASN object type from the given templatized type.
65 * Specialize this template for handling a specific type.
66 * @tparam T - type of object from ASN type would be decided.
67 * @returns the ASN object type.
68 */
Patrick Williams1334b7b2021-02-22 17:15:12 -060069template <typename T>
70u_char getASNType() = delete;
Ratan Guptaaaf87de2018-04-16 15:25:13 +053071
Patrick Williams1334b7b2021-02-22 17:15:12 -060072template <>
73u_char getASNType<uint32_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053074{
75 return ASN_UNSIGNED;
76}
77
Patrick Williams1334b7b2021-02-22 17:15:12 -060078template <>
79u_char getASNType<uint64_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053080{
81 return ASN_OPAQUE_U64;
82}
83
Patrick Williams1334b7b2021-02-22 17:15:12 -060084template <>
85u_char getASNType<int32_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053086{
87 return ASN_INTEGER;
88}
89
Patrick Williams1334b7b2021-02-22 17:15:12 -060090template <>
91u_char getASNType<std::string>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053092{
93 return ASN_OCTET_STR;
94}
95
96/** @class Notification
97 * @brief Notification interface.
98 *
99 * This class implements the sendTrap function which
100 * send the list of objects defined by the specific notification
101 * to the configured SNMP manager.
102 */
103
104class Notification
105{
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530106 public:
107 Notification() = default;
Patrick Williams1334b7b2021-02-22 17:15:12 -0600108 Notification(const Notification&) = delete;
109 Notification(Notification&&) = default;
110 Notification& operator=(const Notification&) = delete;
111 Notification& operator=(Notification&&) = default;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530112 virtual ~Notification() = default;
113
114 /** @brief Send the snmp trap to the configured
115 * manager.
116 */
117 void sendTrap();
118
119 protected:
Ratan Guptaec26fa62018-04-16 15:28:36 +0530120 /** @brief Add the variable in the snmp pdu object.
121 * @param[in] pdu - SNMP pdu object.
122 * @param[in] objID - SNMP object identifier.
123 * @param[in] objIDLen - Object identifier length.
124 * @param[in] type - ASN type of object.
125 * @param[in] val - Value of the object.
126 * @returns true on success otherwise false.
127 */
Patrick Williams1334b7b2021-02-22 17:15:12 -0600128 bool addPDUVar(netsnmp_pdu& pdu, const OID& objID, size_t objIDLen,
Ratan Guptaec26fa62018-04-16 15:28:36 +0530129 u_char type, Value val);
130
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530131 /** @brief get the SNMP notification type in the mib
132 * defined format.
133 * This is pure virtual function all the subclasses
134 * need to provide its own defined type.
135 * @returns the notification type string.
136 */
137 virtual std::pair<OID, OID_LEN> getTrapOID() = 0;
138
139 /** @brief get all the objects meta data defined under
140 * this notification.
141 */
142 virtual std::vector<Object> getFieldOIDList() = 0;
143};
144
Ratan Guptaec26fa62018-04-16 15:28:36 +0530145class TestErrorNotification;
146
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530147/** @class ErrorNotification
148 * @brief subclass of Notification
149 *
150 * A Error Notification represents the objects needed by the
151 * Error Object.
152 */
153class OBMCErrorNotification : public Notification
154{
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530155 private:
Ed Tanous6d421732023-07-12 14:00:51 -0700156 uint32_t OBMCErrorID = 0;
157 uint64_t OBMCErrorTimestamp = 0;
158 int32_t OBMCErrorSeverity = 0;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530159 std::string OBMCErrorMessage;
160
161 public:
Ed Tanous69cea062023-07-12 14:23:37 -0700162 OBMCErrorNotification() = delete;
Patrick Williams1334b7b2021-02-22 17:15:12 -0600163 OBMCErrorNotification(const OBMCErrorNotification&) = delete;
164 OBMCErrorNotification(OBMCErrorNotification&&) = default;
165 OBMCErrorNotification& operator=(const OBMCErrorNotification&) = delete;
166 OBMCErrorNotification& operator=(OBMCErrorNotification&&) = default;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530167 ~OBMCErrorNotification() = default;
168
169 /** @brief Constructor
170 * @param[in] id - The error entry id.
171 * @param[in] ts - The commit timestamp.
172 * @param[in] sev - The severity of the error.
173 * @param[in] msg - The message of the error.
174 */
175 OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev,
176 std::string msg) :
177 OBMCErrorID(id),
178 OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg)
Patrick Williams1334b7b2021-02-22 17:15:12 -0600179 {}
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530180
181 protected:
182 std::pair<OID, OID_LEN> getTrapOID() override
183 {
184 // notification sub types
185 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1};
186 OID_LEN idLen = 11;
187 return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen));
188 }
189
190 std::vector<Object> getFieldOIDList() override
191 {
192 std::vector<Object> objectList;
Ed Tanous69cea062023-07-12 14:23:37 -0700193 objectList.reserve(4);
194 {
195 OID_LEN idLen = 11;
196 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1};
197 u_char type = getASNType<decltype(OBMCErrorID)>();
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530198
Ed Tanous69cea062023-07-12 14:23:37 -0700199 objectList.emplace_back(id, idLen, type, OBMCErrorID);
200 }
201 {
202 OID_LEN idLen = 11;
203 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2};
204 u_char type = getASNType<decltype(OBMCErrorTimestamp)>();
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530205
Ed Tanous69cea062023-07-12 14:23:37 -0700206 objectList.emplace_back(id, idLen, type, OBMCErrorTimestamp);
207 }
208 {
209 OID_LEN idLen = 11;
210 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3};
211 u_char type = getASNType<decltype(OBMCErrorSeverity)>();
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530212
Ed Tanous69cea062023-07-12 14:23:37 -0700213 objectList.emplace_back(id, idLen, type, OBMCErrorSeverity);
214 }
215 {
216 OID_LEN idLen = 11;
217 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4};
218 u_char type = getASNType<decltype(OBMCErrorMessage)>();
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530219
Ed Tanous69cea062023-07-12 14:23:37 -0700220 objectList.emplace_back(id, idLen, type, OBMCErrorMessage);
221 }
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530222 return objectList;
223 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530224
225 friend class TestErrorNotification;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530226};
227
228} // namespace snmp
229} // namespace network
230} // namespace phosphor