1aaf87deeSRatan Gupta /**
2aaf87deeSRatan Gupta  * @brief SNMP Error Notification class.
3aaf87deeSRatan Gupta  *
4aaf87deeSRatan Gupta  * This file is part of phosphor-snmp project.
5aaf87deeSRatan Gupta  *
6aaf87deeSRatan Gupta  * Copyright (c) 2018 IBM Corporation
7aaf87deeSRatan Gupta  *
8aaf87deeSRatan Gupta  * Licensed under the Apache License, Version 2.0 (the "License");
9aaf87deeSRatan Gupta  * you may not use this file except in compliance with the License.
10aaf87deeSRatan Gupta  * You may obtain a copy of the License at
11aaf87deeSRatan Gupta  *
12aaf87deeSRatan Gupta  *     http://www.apache.org/licenses/LICENSE-2.0
13aaf87deeSRatan Gupta  *
14aaf87deeSRatan Gupta  * Unless required by applicable law or agreed to in writing, software
15aaf87deeSRatan Gupta  * distributed under the License is distributed on an "AS IS" BASIS,
16aaf87deeSRatan Gupta  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17aaf87deeSRatan Gupta  * See the License for the specific language governing permissions and
18aaf87deeSRatan Gupta  * limitations under the License.
19aaf87deeSRatan Gupta  *
20aaf87deeSRatan Gupta  * Note: In near future this file will be autogenerated by the custom parser.
21aaf87deeSRatan Gupta  *
22aaf87deeSRatan Gupta  */
23aaf87deeSRatan Gupta 
24aaf87deeSRatan Gupta #pragma once
25aaf87deeSRatan Gupta 
261334b7b3SPatrick Williams // net-snmp requires a very specific header include order.
271334b7b3SPatrick Williams // disable clang-format around this block
281334b7b3SPatrick Williams // clang-format off
29aaf87deeSRatan Gupta #include <net-snmp/net-snmp-config.h>
30aaf87deeSRatan Gupta #include <net-snmp/net-snmp-includes.h>
31aaf87deeSRatan Gupta #include <net-snmp/agent/net-snmp-agent-includes.h>
321334b7b3SPatrick Williams // clang-format on
33aaf87deeSRatan Gupta 
34aaf87deeSRatan Gupta #include <sdbusplus/server.hpp>
35aaf87deeSRatan Gupta 
36aaf87deeSRatan Gupta #include <sstream>
37aaf87deeSRatan Gupta #include <string>
38aaf87deeSRatan Gupta #include <vector>
39aaf87deeSRatan Gupta 
40aaf87deeSRatan Gupta namespace phosphor
41aaf87deeSRatan Gupta {
42aaf87deeSRatan Gupta namespace network
43aaf87deeSRatan Gupta {
44aaf87deeSRatan Gupta namespace snmp
45aaf87deeSRatan Gupta {
46aaf87deeSRatan Gupta 
47aaf87deeSRatan Gupta using OID = std::array<oid, MAX_OID_LEN>;
48aaf87deeSRatan Gupta using OID_LEN = size_t;
49aaf87deeSRatan Gupta using Type = u_char;
50aaf87deeSRatan Gupta 
51e5d90c35SPatrick Williams using Value = std::variant<uint32_t, uint64_t, int32_t, std::string>;
52aaf87deeSRatan Gupta // Generic snmp trap ID
53aaf87deeSRatan Gupta oid SNMPTrapOID[] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
540d5094bfSRatan Gupta oid sysuptimeOID[] = {1, 3, 6, 1, 2, 1, 1, 3, 0};
55aaf87deeSRatan Gupta 
56*46d7ea4cSEd Tanous struct Object
57*46d7ea4cSEd Tanous {
58*46d7ea4cSEd Tanous     OID oid;
59*46d7ea4cSEd Tanous     OID_LEN oid_len;
60*46d7ea4cSEd Tanous     Type type;
61*46d7ea4cSEd Tanous     Value value;
62*46d7ea4cSEd Tanous };
63aaf87deeSRatan Gupta 
64aaf87deeSRatan Gupta /** @brief Get the ASN object type from the given templatized type.
65aaf87deeSRatan Gupta  *         Specialize this template for handling a specific type.
66aaf87deeSRatan Gupta  *  @tparam T - type of object from ASN type would be decided.
67aaf87deeSRatan Gupta  *  @returns the ASN object type.
68aaf87deeSRatan Gupta  */
691334b7b3SPatrick Williams template <typename T>
701334b7b3SPatrick Williams u_char getASNType() = delete;
71aaf87deeSRatan Gupta 
721334b7b3SPatrick Williams template <>
getASNType()731334b7b3SPatrick Williams u_char getASNType<uint32_t>()
74aaf87deeSRatan Gupta {
75aaf87deeSRatan Gupta     return ASN_UNSIGNED;
76aaf87deeSRatan Gupta }
77aaf87deeSRatan Gupta 
781334b7b3SPatrick Williams template <>
getASNType()791334b7b3SPatrick Williams u_char getASNType<uint64_t>()
80aaf87deeSRatan Gupta {
81aaf87deeSRatan Gupta     return ASN_OPAQUE_U64;
82aaf87deeSRatan Gupta }
83aaf87deeSRatan Gupta 
841334b7b3SPatrick Williams template <>
getASNType()851334b7b3SPatrick Williams u_char getASNType<int32_t>()
86aaf87deeSRatan Gupta {
87aaf87deeSRatan Gupta     return ASN_INTEGER;
88aaf87deeSRatan Gupta }
89aaf87deeSRatan Gupta 
901334b7b3SPatrick Williams template <>
getASNType()911334b7b3SPatrick Williams u_char getASNType<std::string>()
92aaf87deeSRatan Gupta {
93aaf87deeSRatan Gupta     return ASN_OCTET_STR;
94aaf87deeSRatan Gupta }
95aaf87deeSRatan Gupta 
96aaf87deeSRatan Gupta /** @class Notification
97aaf87deeSRatan Gupta  *  @brief Notification interface.
98aaf87deeSRatan Gupta  *
99aaf87deeSRatan Gupta  *  This class implements the sendTrap function which
100aaf87deeSRatan Gupta  *  send the list of objects defined by the specific notification
101aaf87deeSRatan Gupta  *  to the configured SNMP manager.
102aaf87deeSRatan Gupta  */
103aaf87deeSRatan Gupta 
104aaf87deeSRatan Gupta class Notification
105aaf87deeSRatan Gupta {
106aaf87deeSRatan Gupta   public:
107aaf87deeSRatan Gupta     Notification() = default;
108aaf87deeSRatan Gupta     Notification(const Notification&) = delete;
109aaf87deeSRatan Gupta     Notification(Notification&&) = default;
110aaf87deeSRatan Gupta     Notification& operator=(const Notification&) = delete;
111aaf87deeSRatan Gupta     Notification& operator=(Notification&&) = default;
112aaf87deeSRatan Gupta     virtual ~Notification() = default;
113aaf87deeSRatan Gupta 
114aaf87deeSRatan Gupta     /** @brief Send the snmp trap to the configured
115aaf87deeSRatan Gupta      *          manager.
116aaf87deeSRatan Gupta      */
117aaf87deeSRatan Gupta     void sendTrap();
118aaf87deeSRatan Gupta 
119aaf87deeSRatan Gupta   protected:
120ec26fa6bSRatan Gupta     /** @brief Add the variable in the snmp pdu object.
121ec26fa6bSRatan Gupta      *  @param[in] pdu - SNMP pdu object.
122ec26fa6bSRatan Gupta      *  @param[in] objID -  SNMP object identifier.
123ec26fa6bSRatan Gupta      *  @param[in] objIDLen - Object identifier length.
124ec26fa6bSRatan Gupta      *  @param[in] type - ASN type of object.
125ec26fa6bSRatan Gupta      *  @param[in] val - Value of the object.
126ec26fa6bSRatan Gupta      *  @returns true on success otherwise false.
127ec26fa6bSRatan Gupta      */
128ec26fa6bSRatan Gupta     bool addPDUVar(netsnmp_pdu& pdu, const OID& objID, size_t objIDLen,
129ec26fa6bSRatan Gupta                    u_char type, Value val);
130ec26fa6bSRatan Gupta 
131aaf87deeSRatan Gupta     /** @brief get the SNMP notification type in the mib
132aaf87deeSRatan Gupta      *         defined format.
133aaf87deeSRatan Gupta      *         This is pure virtual function all the subclasses
134aaf87deeSRatan Gupta      *         need to provide its own defined type.
135aaf87deeSRatan Gupta      *  @returns the notification type string.
136aaf87deeSRatan Gupta      */
137aaf87deeSRatan Gupta     virtual std::pair<OID, OID_LEN> getTrapOID() = 0;
138aaf87deeSRatan Gupta 
139aaf87deeSRatan Gupta     /** @brief get all the objects meta data defined under
140aaf87deeSRatan Gupta      *         this notification.
141aaf87deeSRatan Gupta      */
142aaf87deeSRatan Gupta     virtual std::vector<Object> getFieldOIDList() = 0;
143aaf87deeSRatan Gupta };
144aaf87deeSRatan Gupta 
145ec26fa6bSRatan Gupta class TestErrorNotification;
146ec26fa6bSRatan Gupta 
147aaf87deeSRatan Gupta /** @class ErrorNotification
148aaf87deeSRatan Gupta  *  @brief subclass of Notification
149aaf87deeSRatan Gupta  *
150aaf87deeSRatan Gupta  *  A Error Notification represents the objects needed by the
151aaf87deeSRatan Gupta  *  Error Object.
152aaf87deeSRatan Gupta  */
153aaf87deeSRatan Gupta class OBMCErrorNotification : public Notification
154aaf87deeSRatan Gupta {
155aaf87deeSRatan Gupta   private:
1566d421739SEd Tanous     uint32_t OBMCErrorID = 0;
1576d421739SEd Tanous     uint64_t OBMCErrorTimestamp = 0;
1586d421739SEd Tanous     int32_t OBMCErrorSeverity = 0;
159aaf87deeSRatan Gupta     std::string OBMCErrorMessage;
160aaf87deeSRatan Gupta 
161aaf87deeSRatan Gupta   public:
16269cea063SEd Tanous     OBMCErrorNotification() = delete;
163aaf87deeSRatan Gupta     OBMCErrorNotification(const OBMCErrorNotification&) = delete;
164aaf87deeSRatan Gupta     OBMCErrorNotification(OBMCErrorNotification&&) = default;
165aaf87deeSRatan Gupta     OBMCErrorNotification& operator=(const OBMCErrorNotification&) = delete;
166aaf87deeSRatan Gupta     OBMCErrorNotification& operator=(OBMCErrorNotification&&) = default;
167aaf87deeSRatan Gupta     ~OBMCErrorNotification() = default;
168aaf87deeSRatan Gupta 
169aaf87deeSRatan Gupta     /** @brief Constructor
170aaf87deeSRatan Gupta      *  @param[in] id - The error entry id.
171aaf87deeSRatan Gupta      *  @param[in] ts - The commit timestamp.
172aaf87deeSRatan Gupta      *  @param[in] sev - The severity of the error.
173aaf87deeSRatan Gupta      *  @param[in] msg - The message of the error.
174aaf87deeSRatan Gupta      */
OBMCErrorNotification(uint32_t id,uint64_t ts,int32_t sev,std::string msg)175aaf87deeSRatan Gupta     OBMCErrorNotification(uint32_t id, uint64_t ts, int32_t sev,
176aaf87deeSRatan Gupta                           std::string msg) :
177aaf87deeSRatan Gupta         OBMCErrorID(id),
178aaf87deeSRatan Gupta         OBMCErrorTimestamp(ts), OBMCErrorSeverity(sev), OBMCErrorMessage(msg)
1791334b7b3SPatrick Williams     {}
180aaf87deeSRatan Gupta 
181aaf87deeSRatan Gupta   protected:
getTrapOID()182aaf87deeSRatan Gupta     std::pair<OID, OID_LEN> getTrapOID() override
183aaf87deeSRatan Gupta     {
184aaf87deeSRatan Gupta         // notification sub types
185aaf87deeSRatan Gupta         OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 0, 1};
186aaf87deeSRatan Gupta         OID_LEN idLen = 11;
187aaf87deeSRatan Gupta         return std::make_pair<OID, OID_LEN>(std::move(id), std::move(idLen));
188aaf87deeSRatan Gupta     }
189aaf87deeSRatan Gupta 
getFieldOIDList()190aaf87deeSRatan Gupta     std::vector<Object> getFieldOIDList() override
191aaf87deeSRatan Gupta     {
192aaf87deeSRatan Gupta         std::vector<Object> objectList;
19369cea063SEd Tanous         objectList.reserve(4);
19469cea063SEd Tanous         {
195aaf87deeSRatan Gupta             OID_LEN idLen = 11;
196aaf87deeSRatan Gupta             OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 1};
19769cea063SEd Tanous             u_char type = getASNType<decltype(OBMCErrorID)>();
198aaf87deeSRatan Gupta 
19965113c2bSEd Tanous             objectList.emplace_back(id, idLen, type, OBMCErrorID);
20069cea063SEd Tanous         }
20169cea063SEd Tanous         {
20269cea063SEd Tanous             OID_LEN idLen = 11;
20369cea063SEd Tanous             OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 2};
20469cea063SEd Tanous             u_char type = getASNType<decltype(OBMCErrorTimestamp)>();
205aaf87deeSRatan Gupta 
20665113c2bSEd Tanous             objectList.emplace_back(id, idLen, type, OBMCErrorTimestamp);
20769cea063SEd Tanous         }
20869cea063SEd Tanous         {
20969cea063SEd Tanous             OID_LEN idLen = 11;
21069cea063SEd Tanous             OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 3};
21169cea063SEd Tanous             u_char type = getASNType<decltype(OBMCErrorSeverity)>();
212aaf87deeSRatan Gupta 
21365113c2bSEd Tanous             objectList.emplace_back(id, idLen, type, OBMCErrorSeverity);
21469cea063SEd Tanous         }
21569cea063SEd Tanous         {
21669cea063SEd Tanous             OID_LEN idLen = 11;
21769cea063SEd Tanous             OID id = {1, 3, 6, 1, 4, 1, 49871, 1, 0, 1, 4};
21869cea063SEd Tanous             u_char type = getASNType<decltype(OBMCErrorMessage)>();
219aaf87deeSRatan Gupta 
22065113c2bSEd Tanous             objectList.emplace_back(id, idLen, type, OBMCErrorMessage);
22169cea063SEd Tanous         }
222aaf87deeSRatan Gupta         return objectList;
223aaf87deeSRatan Gupta     }
224ec26fa6bSRatan Gupta 
225ec26fa6bSRatan Gupta     friend class TestErrorNotification;
226aaf87deeSRatan Gupta };
227aaf87deeSRatan Gupta 
228aaf87deeSRatan Gupta } // namespace snmp
229aaf87deeSRatan Gupta } // namespace network
230aaf87deeSRatan Gupta } // namespace phosphor
231