JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | #include "logging.hpp" |
| 3 | |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 4 | #include <boost/beast/http/fields.hpp> |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 5 | #include <boost/container/flat_map.hpp> |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 6 | #include <boost/url/parse.hpp> |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 7 | #include <boost/url/url.hpp> |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 8 | #include <nlohmann/json.hpp> |
| 9 | |
| 10 | namespace persistent_data |
| 11 | { |
| 12 | |
| 13 | struct UserSubscription |
| 14 | { |
| 15 | std::string id; |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 16 | boost::urls::url destinationUrl; |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 17 | std::string protocol; |
Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 18 | bool verifyCertificate = true; |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 19 | std::string retryPolicy; |
| 20 | std::string customText; |
| 21 | std::string eventFormatType; |
| 22 | std::string subscriptionType; |
| 23 | std::vector<std::string> registryMsgIds; |
| 24 | std::vector<std::string> registryPrefixes; |
| 25 | std::vector<std::string> resourceTypes; |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 26 | boost::beast::http::fields httpHeaders; |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 27 | std::vector<std::string> metricReportDefinitions; |
| 28 | |
| 29 | static std::shared_ptr<UserSubscription> |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 30 | fromJson(const nlohmann::json::object_t& j, |
| 31 | const bool loadFromOldConfig = false) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 32 | { |
| 33 | std::shared_ptr<UserSubscription> subvalue = |
| 34 | std::make_shared<UserSubscription>(); |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 35 | for (const auto& element : j) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 36 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 37 | if (element.first == "Id") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 38 | { |
| 39 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 40 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 41 | if (value == nullptr) |
| 42 | { |
| 43 | continue; |
| 44 | } |
| 45 | subvalue->id = *value; |
| 46 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 47 | else if (element.first == "Destination") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 48 | { |
| 49 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 50 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 51 | if (value == nullptr) |
| 52 | { |
| 53 | continue; |
| 54 | } |
Ed Tanous | 6fd2955 | 2023-10-04 09:40:14 -0700 | [diff] [blame] | 55 | boost::system::result<boost::urls::url> url = |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 56 | boost::urls::parse_absolute_uri(*value); |
| 57 | if (!url) |
| 58 | { |
| 59 | continue; |
| 60 | } |
| 61 | subvalue->destinationUrl = std::move(*url); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 62 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 63 | else if (element.first == "Protocol") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 64 | { |
| 65 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 66 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 67 | if (value == nullptr) |
| 68 | { |
| 69 | continue; |
| 70 | } |
| 71 | subvalue->protocol = *value; |
| 72 | } |
Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 73 | else if (element.first == "VerifyCertificate") |
| 74 | { |
| 75 | const bool* value = element.second.get_ptr<const bool*>(); |
| 76 | if (value == nullptr) |
| 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | subvalue->verifyCertificate = *value; |
| 81 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 82 | else if (element.first == "DeliveryRetryPolicy") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 83 | { |
| 84 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 85 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 86 | if (value == nullptr) |
| 87 | { |
| 88 | continue; |
| 89 | } |
| 90 | subvalue->retryPolicy = *value; |
| 91 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 92 | else if (element.first == "Context") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 93 | { |
| 94 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 95 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 96 | if (value == nullptr) |
| 97 | { |
| 98 | continue; |
| 99 | } |
| 100 | subvalue->customText = *value; |
| 101 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 102 | else if (element.first == "EventFormatType") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 103 | { |
| 104 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 105 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 106 | if (value == nullptr) |
| 107 | { |
| 108 | continue; |
| 109 | } |
| 110 | subvalue->eventFormatType = *value; |
| 111 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 112 | else if (element.first == "SubscriptionType") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 113 | { |
| 114 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 115 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 116 | if (value == nullptr) |
| 117 | { |
| 118 | continue; |
| 119 | } |
| 120 | subvalue->subscriptionType = *value; |
| 121 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 122 | else if (element.first == "MessageIds") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 123 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 124 | const nlohmann::json::array_t* obj = |
| 125 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 126 | if (obj == nullptr) |
| 127 | { |
| 128 | continue; |
| 129 | } |
| 130 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 131 | { |
| 132 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 133 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 134 | if (value == nullptr) |
| 135 | { |
| 136 | continue; |
| 137 | } |
| 138 | subvalue->registryMsgIds.emplace_back(*value); |
| 139 | } |
| 140 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 141 | else if (element.first == "RegistryPrefixes") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 142 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 143 | const nlohmann::json::array_t* obj = |
| 144 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 145 | if (obj == nullptr) |
| 146 | { |
| 147 | continue; |
| 148 | } |
| 149 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 150 | { |
| 151 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 152 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 153 | if (value == nullptr) |
| 154 | { |
| 155 | continue; |
| 156 | } |
| 157 | subvalue->registryPrefixes.emplace_back(*value); |
| 158 | } |
| 159 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 160 | else if (element.first == "ResourceTypes") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 161 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 162 | const nlohmann::json::array_t* obj = |
| 163 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 164 | if (obj == nullptr) |
| 165 | { |
| 166 | continue; |
| 167 | } |
| 168 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 169 | { |
| 170 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 171 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 172 | if (value == nullptr) |
| 173 | { |
| 174 | continue; |
| 175 | } |
| 176 | subvalue->resourceTypes.emplace_back(*value); |
| 177 | } |
| 178 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 179 | else if (element.first == "HttpHeaders") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 180 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 181 | const nlohmann::json::object_t* obj = |
| 182 | element.second.get_ptr<const nlohmann::json::object_t*>(); |
| 183 | if (obj == nullptr) |
| 184 | { |
| 185 | continue; |
| 186 | } |
| 187 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 188 | { |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 189 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 190 | val.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 191 | if (value == nullptr) |
| 192 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 193 | BMCWEB_LOG_ERROR("Failed to parse value for key{}", |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 194 | val.first); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 195 | continue; |
| 196 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 197 | subvalue->httpHeaders.set(val.first, *value); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 198 | } |
| 199 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 200 | else if (element.first == "MetricReportDefinitions") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 201 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 202 | const nlohmann::json::array_t* obj = |
| 203 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 204 | if (obj == nullptr) |
| 205 | { |
| 206 | continue; |
| 207 | } |
| 208 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 209 | { |
| 210 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 211 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 212 | if (value == nullptr) |
| 213 | { |
| 214 | continue; |
| 215 | } |
| 216 | subvalue->metricReportDefinitions.emplace_back(*value); |
| 217 | } |
| 218 | } |
| 219 | else |
| 220 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 221 | BMCWEB_LOG_ERROR( |
| 222 | "Got unexpected property reading persistent file: {}", |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 223 | element.first); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 224 | continue; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if ((subvalue->id.empty() && !loadFromOldConfig) || |
| 229 | subvalue->destinationUrl.empty() || subvalue->protocol.empty() || |
Sunitha Harish | e6a7165 | 2021-08-06 03:15:59 -0500 | [diff] [blame] | 230 | subvalue->retryPolicy.empty() || |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 231 | subvalue->eventFormatType.empty() || |
Sunitha Harish | e6a7165 | 2021-08-06 03:15:59 -0500 | [diff] [blame] | 232 | subvalue->subscriptionType.empty()) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 233 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 234 | BMCWEB_LOG_ERROR("Subscription missing required field " |
| 235 | "information, refusing to restore"); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 236 | return nullptr; |
| 237 | } |
| 238 | |
| 239 | return subvalue; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | struct EventServiceConfig |
| 244 | { |
| 245 | bool enabled = true; |
| 246 | uint32_t retryAttempts = 3; |
| 247 | uint32_t retryTimeoutInterval = 30; |
| 248 | |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 249 | void fromJson(const nlohmann::json::object_t& j) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 250 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 251 | for (const auto& element : j) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 252 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 253 | if (element.first == "ServiceEnabled") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 254 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 255 | const bool* value = element.second.get_ptr<const bool*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 256 | if (value == nullptr) |
| 257 | { |
| 258 | continue; |
| 259 | } |
| 260 | enabled = *value; |
| 261 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 262 | else if (element.first == "DeliveryRetryAttempts") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 263 | { |
| 264 | const uint64_t* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 265 | element.second.get_ptr<const uint64_t*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 266 | if ((value == nullptr) || |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 267 | (*value > std::numeric_limits<uint32_t>::max())) |
| 268 | { |
| 269 | continue; |
| 270 | } |
| 271 | retryAttempts = static_cast<uint32_t>(*value); |
| 272 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 273 | else if (element.first == "DeliveryRetryIntervalSeconds") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 274 | { |
| 275 | const uint64_t* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 276 | element.second.get_ptr<const uint64_t*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 277 | if ((value == nullptr) || |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 278 | (*value > std::numeric_limits<uint32_t>::max())) |
| 279 | { |
| 280 | continue; |
| 281 | } |
| 282 | retryTimeoutInterval = static_cast<uint32_t>(*value); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | class EventServiceStore |
| 289 | { |
| 290 | public: |
| 291 | boost::container::flat_map<std::string, std::shared_ptr<UserSubscription>> |
| 292 | subscriptionsConfigMap; |
| 293 | EventServiceConfig eventServiceConfig; |
| 294 | |
| 295 | static EventServiceStore& getInstance() |
| 296 | { |
| 297 | static EventServiceStore eventServiceStore; |
| 298 | return eventServiceStore; |
| 299 | } |
| 300 | |
| 301 | EventServiceConfig& getEventServiceConfig() |
| 302 | { |
| 303 | return eventServiceConfig; |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | } // namespace persistent_data |