blob: 090582893c69df14c637f9b3715b52a8870a5aa0 [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
36#include <experimental/any>
37#include <sstream>
38#include <string>
39#include <tuple>
40#include <vector>
41
42namespace any_ns = std::experimental;
43
44namespace phosphor
45{
46namespace network
47{
48namespace snmp
49{
50
51using OID = std::array<oid, MAX_OID_LEN>;
52using OID_LEN = size_t;
53using Type = u_char;
54
Patrick Williamse5d90c32020-05-13 18:00:44 -050055using Value = std::variant<uint32_t, uint64_t, int32_t, std::string>;
Ratan Guptaaaf87de2018-04-16 15:25:13 +053056// Generic snmp trap ID
57oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
58
59using Object = std::tuple<OID, OID_LEN, Type, Value>;
60
61/** @brief Get the ASN object type from the given templatized type.
62 * Specialize this template for handling a specific type.
63 * @tparam T - type of object from ASN type would be decided.
64 * @returns the ASN object type.
65 */
Patrick Williams1334b7b2021-02-22 17:15:12 -060066template <typename T>
67u_char getASNType() = delete;
Ratan Guptaaaf87de2018-04-16 15:25:13 +053068
Patrick Williams1334b7b2021-02-22 17:15:12 -060069template <>
70u_char getASNType<uint32_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053071{
72 return ASN_UNSIGNED;
73}
74
Patrick Williams1334b7b2021-02-22 17:15:12 -060075template <>
76u_char getASNType<uint64_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053077{
78 return ASN_OPAQUE_U64;
79}
80
Patrick Williams1334b7b2021-02-22 17:15:12 -060081template <>
82u_char getASNType<int32_t>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053083{
84 return ASN_INTEGER;
85}
86
Patrick Williams1334b7b2021-02-22 17:15:12 -060087template <>
88u_char getASNType<std::string>()
Ratan Guptaaaf87de2018-04-16 15:25:13 +053089{
90 return ASN_OCTET_STR;
91}
92
93/** @class Notification
94 * @brief Notification interface.
95 *
96 * This class implements the sendTrap function which
97 * send the list of objects defined by the specific notification
98 * to the configured SNMP manager.
99 */
100
101class Notification
102{
103
104 public:
105 Notification() = default;
Patrick Williams1334b7b2021-02-22 17:15:12 -0600106 Notification(const Notification&) = delete;
107 Notification(Notification&&) = default;
108 Notification& operator=(const Notification&) = delete;
109 Notification& operator=(Notification&&) = default;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530110 virtual ~Notification() = default;
111
112 /** @brief Send the snmp trap to the configured
113 * manager.
114 */
115 void sendTrap();
116
117 protected:
Ratan Guptaec26fa62018-04-16 15:28:36 +0530118 /** @brief Add the variable in the snmp pdu object.
119 * @param[in] pdu - SNMP pdu object.
120 * @param[in] objID - SNMP object identifier.
121 * @param[in] objIDLen - Object identifier length.
122 * @param[in] type - ASN type of object.
123 * @param[in] val - Value of the object.
124 * @returns true on success otherwise false.
125 */
Patrick Williams1334b7b2021-02-22 17:15:12 -0600126 bool addPDUVar(netsnmp_pdu& pdu, const OID& objID, size_t objIDLen,
Ratan Guptaec26fa62018-04-16 15:28:36 +0530127 u_char type, Value val);
128
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530129 /** @brief get the SNMP notification type in the mib
130 * defined format.
131 * This is pure virtual function all the subclasses
132 * need to provide its own defined type.
133 * @returns the notification type string.
134 */
135 virtual std::pair<OID, OID_LEN> getTrapOID() = 0;
136
137 /** @brief get all the objects meta data defined under
138 * this notification.
139 */
140 virtual std::vector<Object> getFieldOIDList() = 0;
141};
142
Ratan Guptaec26fa62018-04-16 15:28:36 +0530143class TestErrorNotification;
144
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530145/** @class ErrorNotification
146 * @brief subclass of Notification
147 *
148 * A Error Notification represents the objects needed by the
149 * Error Object.
150 */
151class OBMCErrorNotification : public Notification
152{
153
154 private:
155 uint32_t OBMCErrorID;
156 uint64_t OBMCErrorTimestamp;
157 int32_t OBMCErrorSeverity;
158 std::string OBMCErrorMessage;
159
160 public:
161 OBMCErrorNotification() = default;
Patrick Williams1334b7b2021-02-22 17:15:12 -0600162 OBMCErrorNotification(const OBMCErrorNotification&) = delete;
163 OBMCErrorNotification(OBMCErrorNotification&&) = default;
164 OBMCErrorNotification& operator=(const OBMCErrorNotification&) = delete;
165 OBMCErrorNotification& operator=(OBMCErrorNotification&&) = default;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530166 ~OBMCErrorNotification() = default;
167
168 /** @brief Constructor
169 * @param[in] id - The error entry id.
170 * @param[in] ts - The commit timestamp.
171 * @param[in] sev - The severity of the error.
172 * @param[in] msg - The message of the error.
173 */
174 OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev,
175 std::string msg) :
176 OBMCErrorID(id),
177 OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg)
Patrick Williams1334b7b2021-02-22 17:15:12 -0600178 {}
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530179
180 protected:
181 std::pair<OID, OID_LEN> getTrapOID() override
182 {
183 // notification sub types
184 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1};
185 OID_LEN idLen = 11;
186 return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen));
187 }
188
189 std::vector<Object> getFieldOIDList() override
190 {
191 std::vector<Object> objectList;
192
193 OID_LEN idLen = 11;
194 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1};
195 auto type = getASNType<decltype(OBMCErrorID)>();
196 auto tuple =
197 std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, OBMCErrorID);
198
199 objectList.emplace_back(std::move(tuple));
200
201 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2};
202 type = getASNType<decltype(OBMCErrorTimestamp)>();
203 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
204 OBMCErrorTimestamp);
205
206 objectList.emplace_back(std::move(tuple));
207
208 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3};
209 type = getASNType<decltype(OBMCErrorSeverity)>();
210
211 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
212 OBMCErrorSeverity);
213
214 objectList.emplace_back(std::move(tuple));
215
216 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4};
217 type = getASNType<decltype(OBMCErrorMessage)>();
218
219 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
220 OBMCErrorMessage);
221
222 objectList.emplace_back(std::move(tuple));
223
224 return objectList;
225 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530226
227 friend class TestErrorNotification;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530228};
229
230} // namespace snmp
231} // namespace network
232} // namespace phosphor