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