blob: fc554acc72e15d80b11a7ff522063997594242c8 [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>
38#include <tuple>
39#include <vector>
40
Ratan Guptaaaf87de2018-04-16 15:25:13 +053041namespace phosphor
42{
43namespace network
44{
45namespace snmp
46{
47
48using OID = std::array<oid, MAX_OID_LEN>;
49using OID_LEN = size_t;
50using Type = u_char;
51
Patrick Williamse5d90c32020-05-13 18:00:44 -050052using Value = std::variant<uint32_t, uint64_t, int32_t, std::string>;
Ratan Guptaaaf87de2018-04-16 15:25:13 +053053// Generic snmp trap ID
54oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
Ratan Gupta0d5094b2021-03-05 19:25:26 +053055oid sysuptimeOID[] = {1, 3, 6, 1, 2, 1, 1, 3, 0};
Ratan Guptaaaf87de2018-04-16 15:25:13 +053056
57using Object = std::tuple<OID, OID_LEN, Type, Value>;
58
59/** @brief Get the ASN object type from the given templatized type.
60 * Specialize this template for handling a specific type.
61 * @tparam T - type of object from ASN type would be decided.
62 * @returns the ASN object type.
63 */
Patrick Williams1334b7b2021-02-22 17:15:12 -060064template <typename T>
65u_char getASNType() = delete;
Ratan Guptaaaf87de2018-04-16 15:25:13 +053066
Patrick Williams1334b7b2021-02-22 17:15:12 -060067template <>
68u_char getASNType<uint32_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053069{
70 return ASN_UNSIGNED;
71}
72
Patrick Williams1334b7b2021-02-22 17:15:12 -060073template <>
74u_char getASNType<uint64_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053075{
76 return ASN_OPAQUE_U64;
77}
78
Patrick Williams1334b7b2021-02-22 17:15:12 -060079template <>
80u_char getASNType<int32_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053081{
82 return ASN_INTEGER;
83}
84
Patrick Williams1334b7b2021-02-22 17:15:12 -060085template <>
86u_char getASNType<std::string>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053087{
88 return ASN_OCTET_STR;
89}
90
91/** @class Notification
92 * @brief Notification interface.
93 *
94 * This class implements the sendTrap function which
95 * send the list of objects defined by the specific notification
96 * to the configured SNMP manager.
97 */
98
99class Notification
100{
101
102 public:
103 Notification() = default;
Patrick Williams1334b7b2021-02-22 17:15:12 -0600104 Notification(const Notification&) = delete;
105 Notification(Notification&&) = default;
106 Notification& operator=(const Notification&) = delete;
107 Notification& operator=(Notification&&) = default;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530108 virtual ~Notification() = default;
109
110 /** @brief Send the snmp trap to the configured
111 * manager.
112 */
113 void sendTrap();
114
115 protected:
Ratan Guptaec26fa62018-04-16 15:28:36 +0530116 /** @brief Add the variable in the snmp pdu object.
117 * @param[in] pdu - SNMP pdu object.
118 * @param[in] objID - SNMP object identifier.
119 * @param[in] objIDLen - Object identifier length.
120 * @param[in] type - ASN type of object.
121 * @param[in] val - Value of the object.
122 * @returns true on success otherwise false.
123 */
Patrick Williams1334b7b2021-02-22 17:15:12 -0600124 bool addPDUVar(netsnmp_pdu& pdu, const OID& objID, size_t objIDLen,
Ratan Guptaec26fa62018-04-16 15:28:36 +0530125 u_char type, Value val);
126
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530127 /** @brief get the SNMP notification type in the mib
128 * defined format.
129 * This is pure virtual function all the subclasses
130 * need to provide its own defined type.
131 * @returns the notification type string.
132 */
133 virtual std::pair<OID, OID_LEN> getTrapOID() = 0;
134
135 /** @brief get all the objects meta data defined under
136 * this notification.
137 */
138 virtual std::vector<Object> getFieldOIDList() = 0;
139};
140
Ratan Guptaec26fa62018-04-16 15:28:36 +0530141class TestErrorNotification;
142
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530143/** @class ErrorNotification
144 * @brief subclass of Notification
145 *
146 * A Error Notification represents the objects needed by the
147 * Error Object.
148 */
149class OBMCErrorNotification : public Notification
150{
151
152 private:
153 uint32_t OBMCErrorID;
154 uint64_t OBMCErrorTimestamp;
155 int32_t OBMCErrorSeverity;
156 std::string OBMCErrorMessage;
157
158 public:
159 OBMCErrorNotification() = default;
Patrick Williams1334b7b2021-02-22 17:15:12 -0600160 OBMCErrorNotification(const OBMCErrorNotification&) = delete;
161 OBMCErrorNotification(OBMCErrorNotification&&) = default;
162 OBMCErrorNotification& operator=(const OBMCErrorNotification&) = delete;
163 OBMCErrorNotification& operator=(OBMCErrorNotification&&) = default;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530164 ~OBMCErrorNotification() = default;
165
166 /** @brief Constructor
167 * @param[in] id - The error entry id.
168 * @param[in] ts - The commit timestamp.
169 * @param[in] sev - The severity of the error.
170 * @param[in] msg - The message of the error.
171 */
172 OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev,
173 std::string msg) :
174 OBMCErrorID(id),
175 OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg)
Patrick Williams1334b7b2021-02-22 17:15:12 -0600176 {}
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530177
178 protected:
179 std::pair<OID, OID_LEN> getTrapOID() override
180 {
181 // notification sub types
182 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1};
183 OID_LEN idLen = 11;
184 return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen));
185 }
186
187 std::vector<Object> getFieldOIDList() override
188 {
189 std::vector<Object> objectList;
190
191 OID_LEN idLen = 11;
192 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1};
193 auto type = getASNType<decltype(OBMCErrorID)>();
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530194
Ed Tanous65113c22022-05-27 13:38:07 -0700195 objectList.emplace_back(id, idLen, type, OBMCErrorID);
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530196
197 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2};
198 type = getASNType<decltype(OBMCErrorTimestamp)>();
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530199
Ed Tanous65113c22022-05-27 13:38:07 -0700200 objectList.emplace_back(id, idLen, type, OBMCErrorTimestamp);
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530201
202 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3};
203 type = getASNType<decltype(OBMCErrorSeverity)>();
204
Ed Tanous65113c22022-05-27 13:38:07 -0700205 objectList.emplace_back(id, idLen, type, OBMCErrorSeverity);
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530206
207 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4};
208 type = getASNType<decltype(OBMCErrorMessage)>();
209
Ed Tanous65113c22022-05-27 13:38:07 -0700210 objectList.emplace_back(id, idLen, type, OBMCErrorMessage);
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530211
212 return objectList;
213 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530214
215 friend class TestErrorNotification;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530216};
217
218} // namespace snmp
219} // namespace network
220} // namespace phosphor