blob: d21e68cbe0c8aedbac3b1ee083e27eac29557091 [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
51using Value =
52 sdbusplus::message::variant<uint32_t, uint64_t, int32_t, std::string>;
53// Generic snmp trap ID
54oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
55
56using Object = std::tuple<OID, OID_LEN, Type, Value>;
57
58/** @brief Get the ASN object type from the given templatized type.
59 * Specialize this template for handling a specific type.
60 * @tparam T - type of object from ASN type would be decided.
61 * @returns the ASN object type.
62 */
63template <typename T> u_char getASNType() = delete;
64
65template <> u_char getASNType<uint32_t>()
66{
67 return ASN_UNSIGNED;
68}
69
70template <> u_char getASNType<uint64_t>()
71{
72 return ASN_OPAQUE_U64;
73}
74
75template <> u_char getASNType<int32_t>()
76{
77 return ASN_INTEGER;
78}
79
80template <> u_char getASNType<std::string>()
81{
82 return ASN_OCTET_STR;
83}
84
85/** @class Notification
86 * @brief Notification interface.
87 *
88 * This class implements the sendTrap function which
89 * send the list of objects defined by the specific notification
90 * to the configured SNMP manager.
91 */
92
93class Notification
94{
95
96 public:
97 Notification() = default;
98 Notification(const Notification &) = delete;
99 Notification(Notification &&) = default;
100 Notification &operator=(const Notification &) = delete;
101 Notification &operator=(Notification &&) = default;
102 virtual ~Notification() = default;
103
104 /** @brief Send the snmp trap to the configured
105 * manager.
106 */
107 void sendTrap();
108
109 protected:
110 /** @brief get the SNMP notification type in the mib
111 * defined format.
112 * This is pure virtual function all the subclasses
113 * need to provide its own defined type.
114 * @returns the notification type string.
115 */
116 virtual std::pair<OID, OID_LEN> getTrapOID() = 0;
117
118 /** @brief get all the objects meta data defined under
119 * this notification.
120 */
121 virtual std::vector<Object> getFieldOIDList() = 0;
122};
123
124/** @class ErrorNotification
125 * @brief subclass of Notification
126 *
127 * A Error Notification represents the objects needed by the
128 * Error Object.
129 */
130class OBMCErrorNotification : public Notification
131{
132
133 private:
134 uint32_t OBMCErrorID;
135 uint64_t OBMCErrorTimestamp;
136 int32_t OBMCErrorSeverity;
137 std::string OBMCErrorMessage;
138
139 public:
140 OBMCErrorNotification() = default;
141 OBMCErrorNotification(const OBMCErrorNotification &) = delete;
142 OBMCErrorNotification(OBMCErrorNotification &&) = default;
143 OBMCErrorNotification &operator=(const OBMCErrorNotification &) = delete;
144 OBMCErrorNotification &operator=(OBMCErrorNotification &&) = default;
145 ~OBMCErrorNotification() = default;
146
147 /** @brief Constructor
148 * @param[in] id - The error entry id.
149 * @param[in] ts - The commit timestamp.
150 * @param[in] sev - The severity of the error.
151 * @param[in] msg - The message of the error.
152 */
153 OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev,
154 std::string msg) :
155 OBMCErrorID(id),
156 OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg)
157 {
158 }
159
160 protected:
161 std::pair<OID, OID_LEN> getTrapOID() override
162 {
163 // notification sub types
164 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1};
165 OID_LEN idLen = 11;
166 return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen));
167 }
168
169 std::vector<Object> getFieldOIDList() override
170 {
171 std::vector<Object> objectList;
172
173 OID_LEN idLen = 11;
174 OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1};
175 auto type = getASNType<decltype(OBMCErrorID)>();
176 auto tuple =
177 std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, OBMCErrorID);
178
179 objectList.emplace_back(std::move(tuple));
180
181 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2};
182 type = getASNType<decltype(OBMCErrorTimestamp)>();
183 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
184 OBMCErrorTimestamp);
185
186 objectList.emplace_back(std::move(tuple));
187
188 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3};
189 type = getASNType<decltype(OBMCErrorSeverity)>();
190
191 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
192 OBMCErrorSeverity);
193
194 objectList.emplace_back(std::move(tuple));
195
196 id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4};
197 type = getASNType<decltype(OBMCErrorMessage)>();
198
199 tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type,
200 OBMCErrorMessage);
201
202 objectList.emplace_back(std::move(tuple));
203
204 return objectList;
205 }
206};
207
208} // namespace snmp
209} // namespace network
210} // namespace phosphor