Ratan Gupta | aaf87de | 2018-04-16 15:25:13 +0530 | [diff] [blame] | 1 | /** |
| 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 | |
| 38 | namespace any_ns = std::experimental; |
| 39 | |
| 40 | namespace phosphor |
| 41 | { |
| 42 | namespace network |
| 43 | { |
| 44 | namespace snmp |
| 45 | { |
| 46 | |
| 47 | using OID = std::array<oid, MAX_OID_LEN>; |
| 48 | using OID_LEN = size_t; |
| 49 | using Type = u_char; |
| 50 | |
| 51 | using Value = |
| 52 | sdbusplus::message::variant<uint32_t, uint64_t, int32_t, std::string>; |
| 53 | // Generic snmp trap ID |
| 54 | oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0}; |
| 55 | |
| 56 | using 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 | */ |
| 63 | template <typename T> u_char getASNType() = delete; |
| 64 | |
| 65 | template <> u_char getASNType<uint32_t>() |
| 66 | { |
| 67 | return ASN_UNSIGNED; |
| 68 | } |
| 69 | |
| 70 | template <> u_char getASNType<uint64_t>() |
| 71 | { |
| 72 | return ASN_OPAQUE_U64; |
| 73 | } |
| 74 | |
| 75 | template <> u_char getASNType<int32_t>() |
| 76 | { |
| 77 | return ASN_INTEGER; |
| 78 | } |
| 79 | |
| 80 | template <> 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 | |
| 93 | class 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: |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 110 | /** @brief Add the variable in the snmp pdu object. |
| 111 | * @param[in] pdu - SNMP pdu object. |
| 112 | * @param[in] objID - SNMP object identifier. |
| 113 | * @param[in] objIDLen - Object identifier length. |
| 114 | * @param[in] type - ASN type of object. |
| 115 | * @param[in] val - Value of the object. |
| 116 | * @returns true on success otherwise false. |
| 117 | */ |
| 118 | bool addPDUVar(netsnmp_pdu &pdu, const OID &objID, size_t objIDLen, |
| 119 | u_char type, Value val); |
| 120 | |
Ratan Gupta | aaf87de | 2018-04-16 15:25:13 +0530 | [diff] [blame] | 121 | /** @brief get the SNMP notification type in the mib |
| 122 | * defined format. |
| 123 | * This is pure virtual function all the subclasses |
| 124 | * need to provide its own defined type. |
| 125 | * @returns the notification type string. |
| 126 | */ |
| 127 | virtual std::pair<OID, OID_LEN> getTrapOID() = 0; |
| 128 | |
| 129 | /** @brief get all the objects meta data defined under |
| 130 | * this notification. |
| 131 | */ |
| 132 | virtual std::vector<Object> getFieldOIDList() = 0; |
| 133 | }; |
| 134 | |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 135 | class TestErrorNotification; |
| 136 | |
Ratan Gupta | aaf87de | 2018-04-16 15:25:13 +0530 | [diff] [blame] | 137 | /** @class ErrorNotification |
| 138 | * @brief subclass of Notification |
| 139 | * |
| 140 | * A Error Notification represents the objects needed by the |
| 141 | * Error Object. |
| 142 | */ |
| 143 | class OBMCErrorNotification : public Notification |
| 144 | { |
| 145 | |
| 146 | private: |
| 147 | uint32_t OBMCErrorID; |
| 148 | uint64_t OBMCErrorTimestamp; |
| 149 | int32_t OBMCErrorSeverity; |
| 150 | std::string OBMCErrorMessage; |
| 151 | |
| 152 | public: |
| 153 | OBMCErrorNotification() = default; |
| 154 | OBMCErrorNotification(const OBMCErrorNotification &) = delete; |
| 155 | OBMCErrorNotification(OBMCErrorNotification &&) = default; |
| 156 | OBMCErrorNotification &operator=(const OBMCErrorNotification &) = delete; |
| 157 | OBMCErrorNotification &operator=(OBMCErrorNotification &&) = default; |
| 158 | ~OBMCErrorNotification() = default; |
| 159 | |
| 160 | /** @brief Constructor |
| 161 | * @param[in] id - The error entry id. |
| 162 | * @param[in] ts - The commit timestamp. |
| 163 | * @param[in] sev - The severity of the error. |
| 164 | * @param[in] msg - The message of the error. |
| 165 | */ |
| 166 | OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev, |
| 167 | std::string msg) : |
| 168 | OBMCErrorID(id), |
| 169 | OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg) |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | protected: |
| 174 | std::pair<OID, OID_LEN> getTrapOID() override |
| 175 | { |
| 176 | // notification sub types |
| 177 | OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1}; |
| 178 | OID_LEN idLen = 11; |
| 179 | return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen)); |
| 180 | } |
| 181 | |
| 182 | std::vector<Object> getFieldOIDList() override |
| 183 | { |
| 184 | std::vector<Object> objectList; |
| 185 | |
| 186 | OID_LEN idLen = 11; |
| 187 | OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1}; |
| 188 | auto type = getASNType<decltype(OBMCErrorID)>(); |
| 189 | auto tuple = |
| 190 | std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, OBMCErrorID); |
| 191 | |
| 192 | objectList.emplace_back(std::move(tuple)); |
| 193 | |
| 194 | id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2}; |
| 195 | type = getASNType<decltype(OBMCErrorTimestamp)>(); |
| 196 | tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, |
| 197 | OBMCErrorTimestamp); |
| 198 | |
| 199 | objectList.emplace_back(std::move(tuple)); |
| 200 | |
| 201 | id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3}; |
| 202 | type = getASNType<decltype(OBMCErrorSeverity)>(); |
| 203 | |
| 204 | tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, |
| 205 | OBMCErrorSeverity); |
| 206 | |
| 207 | objectList.emplace_back(std::move(tuple)); |
| 208 | |
| 209 | id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4}; |
| 210 | type = getASNType<decltype(OBMCErrorMessage)>(); |
| 211 | |
| 212 | tuple = std::tuple<OID, OID_LEN, Type, Value>(id, idLen, type, |
| 213 | OBMCErrorMessage); |
| 214 | |
| 215 | objectList.emplace_back(std::move(tuple)); |
| 216 | |
| 217 | return objectList; |
| 218 | } |
Ratan Gupta | ec26fa6 | 2018-04-16 15:28:36 +0530 | [diff] [blame] | 219 | |
| 220 | friend class TestErrorNotification; |
Ratan Gupta | aaf87de | 2018-04-16 15:25:13 +0530 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | } // namespace snmp |
| 224 | } // namespace network |
| 225 | } // namespace phosphor |