blob: 5102fc678b13e6375e7c375734a6946e8a33cb77 [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
26#include <net-snmp/net-snmp-config.h>
27#include <net-snmp/net-snmp-includes.h>
28#include <net-snmp/agent/net-snmp-agent-includes.h>
29
30#include <sdbusplus/server.hpp>
31
32#include <experimental/any>
33#include <sstream>
34#include <string>
35#include <tuple>
36#include <vector>
37
38namespace any_ns = std::experimental;
39
40namespace 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};
54
55using Object = std::tuple<OID, OID_LEN, Type, Value>;
56
57/** @brief Get the ASN object type from the given templatized type.
58 * Specialize this template for handling a specific type.
59 * @tparam T - type of object from ASN type would be decided.
60 * @returns the ASN object type.
61 */
62template <typename T> u_char getASNType() = delete;
63
64template <> u_char getASNType<uint32_t>()
65{
66 return ASN_UNSIGNED;
67}
68
69template <> u_char getASNType<uint64_t>()
70{
71 return ASN_OPAQUE_U64;
72}
73
74template <> u_char getASNType<int32_t>()
75{
76 return ASN_INTEGER;
77}
78
79template <> u_char getASNType<std::string>()
80{
81 return ASN_OCTET_STR;
82}
83
84/** @class Notification
85 * @brief Notification interface.
86 *
87 * This class implements the sendTrap function which
88 * send the list of objects defined by the specific notification
89 * to the configured SNMP manager.
90 */
91
92class Notification
93{
94
95 public:
96 Notification() = default;
97 Notification(const Notification &) = delete;
98 Notification(Notification &&) = default;
99 Notification &operator=(const Notification &) = delete;
100 Notification &operator=(Notification &&) = default;
101 virtual ~Notification() = default;
102
103 /** @brief Send the snmp trap to the configured
104 * manager.
105 */
106 void sendTrap();
107
108 protected:
Ratan Guptaec26fa62018-04-16 15:28:36 +0530109 /** @brief Add the variable in the snmp pdu object.
110 * @param[in] pdu - SNMP pdu object.
111 * @param[in] objID - SNMP object identifier.
112 * @param[in] objIDLen - Object identifier length.
113 * @param[in] type - ASN type of object.
114 * @param[in] val - Value of the object.
115 * @returns true on success otherwise false.
116 */
117 bool addPDUVar(netsnmp_pdu &pdu, const OID &objID, size_t objIDLen,
118 u_char type, Value val);
119
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530120 /** @brief get the SNMP notification type in the mib
121 * defined format.
122 * This is pure virtual function all the subclasses
123 * need to provide its own defined type.
124 * @returns the notification type string.
125 */
126 virtual std::pair<OID, OID_LEN> getTrapOID() = 0;
127
128 /** @brief get all the objects meta data defined under
129 * this notification.
130 */
131 virtual std::vector<Object> getFieldOIDList() = 0;
132};
133
Ratan Guptaec26fa62018-04-16 15:28:36 +0530134class TestErrorNotification;
135
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530136/** @class ErrorNotification
137 * @brief subclass of Notification
138 *
139 * A Error Notification represents the objects needed by the
140 * Error Object.
141 */
142class OBMCErrorNotification : public Notification
143{
144
145 private:
146 uint32_t OBMCErrorID;
147 uint64_t OBMCErrorTimestamp;
148 int32_t OBMCErrorSeverity;
149 std::string OBMCErrorMessage;
150
151 public:
152 OBMCErrorNotification() = default;
153 OBMCErrorNotification(const OBMCErrorNotification &) = delete;
154 OBMCErrorNotification(OBMCErrorNotification &&) = default;
155 OBMCErrorNotification &operator=(const OBMCErrorNotification &) = delete;
156 OBMCErrorNotification &operator=(OBMCErrorNotification &&) = default;
157 ~OBMCErrorNotification() = default;
158
159 /** @brief Constructor
160 * @param[in] id - The error entry id.
161 * @param[in] ts - The commit timestamp.
162 * @param[in] sev - The severity of the error.
163 * @param[in] msg - The message of the error.
164 */
165 OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev,
166 std::string msg) :
167 OBMCErrorID(id),
168 OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg)
169 {
170 }
171
172 protected:
173 std::pair<OID, OID_LEN> getTrapOID() override
174 {
175 // notification sub types
176 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1};
177 OID_LEN idLen = 11;
178 return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen));
179 }
180
181 std::vector<Object> getFieldOIDList() override
182 {
183 std::vector<Object> objectList;
184
185 OID_LEN idLen = 11;
186 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1};
187 auto type = getASNType<decltype(OBMCErrorID)>();
188 auto tuple =
189 std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, OBMCErrorID);
190
191 objectList.emplace_back(std::move(tuple));
192
193 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2};
194 type = getASNType<decltype(OBMCErrorTimestamp)>();
195 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
196 OBMCErrorTimestamp);
197
198 objectList.emplace_back(std::move(tuple));
199
200 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3};
201 type = getASNType<decltype(OBMCErrorSeverity)>();
202
203 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
204 OBMCErrorSeverity);
205
206 objectList.emplace_back(std::move(tuple));
207
208 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4};
209 type = getASNType<decltype(OBMCErrorMessage)>();
210
211 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
212 OBMCErrorMessage);
213
214 objectList.emplace_back(std::move(tuple));
215
216 return objectList;
217 }
Ratan Guptaec26fa62018-04-16 15:28:36 +0530218
219 friend class TestErrorNotification;
Ratan Guptaaaf87de2018-04-16 15:25:13 +0530220};
221
222} // namespace snmp
223} // namespace network
224} // namespace phosphor