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; |
| 18 | std::string retryPolicy; |
| 19 | std::string customText; |
| 20 | std::string eventFormatType; |
| 21 | std::string subscriptionType; |
| 22 | std::vector<std::string> registryMsgIds; |
| 23 | std::vector<std::string> registryPrefixes; |
| 24 | std::vector<std::string> resourceTypes; |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 25 | boost::beast::http::fields httpHeaders; |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 26 | std::vector<std::string> metricReportDefinitions; |
| 27 | |
| 28 | static std::shared_ptr<UserSubscription> |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 29 | fromJson(const nlohmann::json::object_t& j, |
| 30 | 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 | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 72 | else if (element.first == "DeliveryRetryPolicy") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 73 | { |
| 74 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 75 | element.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 76 | if (value == nullptr) |
| 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | subvalue->retryPolicy = *value; |
| 81 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 82 | else if (element.first == "Context") |
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->customText = *value; |
| 91 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 92 | else if (element.first == "EventFormatType") |
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->eventFormatType = *value; |
| 101 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 102 | else if (element.first == "SubscriptionType") |
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->subscriptionType = *value; |
| 111 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 112 | else if (element.first == "MessageIds") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 113 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 114 | const nlohmann::json::array_t* obj = |
| 115 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 116 | if (obj == nullptr) |
| 117 | { |
| 118 | continue; |
| 119 | } |
| 120 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 121 | { |
| 122 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 123 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 124 | if (value == nullptr) |
| 125 | { |
| 126 | continue; |
| 127 | } |
| 128 | subvalue->registryMsgIds.emplace_back(*value); |
| 129 | } |
| 130 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 131 | else if (element.first == "RegistryPrefixes") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 132 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 133 | const nlohmann::json::array_t* obj = |
| 134 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 135 | if (obj == nullptr) |
| 136 | { |
| 137 | continue; |
| 138 | } |
| 139 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 140 | { |
| 141 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 142 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 143 | if (value == nullptr) |
| 144 | { |
| 145 | continue; |
| 146 | } |
| 147 | subvalue->registryPrefixes.emplace_back(*value); |
| 148 | } |
| 149 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 150 | else if (element.first == "ResourceTypes") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 151 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 152 | const nlohmann::json::array_t* obj = |
| 153 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 154 | if (obj == nullptr) |
| 155 | { |
| 156 | continue; |
| 157 | } |
| 158 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 159 | { |
| 160 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 161 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 162 | if (value == nullptr) |
| 163 | { |
| 164 | continue; |
| 165 | } |
| 166 | subvalue->resourceTypes.emplace_back(*value); |
| 167 | } |
| 168 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 169 | else if (element.first == "HttpHeaders") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 170 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 171 | const nlohmann::json::object_t* obj = |
| 172 | element.second.get_ptr<const nlohmann::json::object_t*>(); |
| 173 | if (obj == nullptr) |
| 174 | { |
| 175 | continue; |
| 176 | } |
| 177 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 178 | { |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 179 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 180 | val.second.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 181 | if (value == nullptr) |
| 182 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 183 | BMCWEB_LOG_ERROR("Failed to parse value for key{}", |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 184 | val.first); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 185 | continue; |
| 186 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 187 | subvalue->httpHeaders.set(val.first, *value); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 188 | } |
| 189 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 190 | else if (element.first == "MetricReportDefinitions") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 191 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 192 | const nlohmann::json::array_t* obj = |
| 193 | element.second.get_ptr<const nlohmann::json::array_t*>(); |
| 194 | if (obj == nullptr) |
| 195 | { |
| 196 | continue; |
| 197 | } |
| 198 | for (const auto& val : *obj) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 199 | { |
| 200 | const std::string* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 201 | val.get_ptr<const std::string*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 202 | if (value == nullptr) |
| 203 | { |
| 204 | continue; |
| 205 | } |
| 206 | subvalue->metricReportDefinitions.emplace_back(*value); |
| 207 | } |
| 208 | } |
| 209 | else |
| 210 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 211 | BMCWEB_LOG_ERROR( |
| 212 | "Got unexpected property reading persistent file: {}", |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 213 | element.first); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 214 | continue; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if ((subvalue->id.empty() && !loadFromOldConfig) || |
| 219 | subvalue->destinationUrl.empty() || subvalue->protocol.empty() || |
Sunitha Harish | e6a7165 | 2021-08-06 03:15:59 -0500 | [diff] [blame] | 220 | subvalue->retryPolicy.empty() || |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 221 | subvalue->eventFormatType.empty() || |
Sunitha Harish | e6a7165 | 2021-08-06 03:15:59 -0500 | [diff] [blame] | 222 | subvalue->subscriptionType.empty()) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 223 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 224 | BMCWEB_LOG_ERROR("Subscription missing required field " |
| 225 | "information, refusing to restore"); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | return subvalue; |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | struct EventServiceConfig |
| 234 | { |
| 235 | bool enabled = true; |
| 236 | uint32_t retryAttempts = 3; |
| 237 | uint32_t retryTimeoutInterval = 30; |
| 238 | |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 239 | void fromJson(const nlohmann::json::object_t& j) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 240 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 241 | for (const auto& element : j) |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 242 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 243 | if (element.first == "ServiceEnabled") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 244 | { |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 245 | const bool* value = element.second.get_ptr<const bool*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 246 | if (value == nullptr) |
| 247 | { |
| 248 | continue; |
| 249 | } |
| 250 | enabled = *value; |
| 251 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 252 | else if (element.first == "DeliveryRetryAttempts") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 253 | { |
| 254 | const uint64_t* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 255 | element.second.get_ptr<const uint64_t*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 256 | if ((value == nullptr) || |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 257 | (*value > std::numeric_limits<uint32_t>::max())) |
| 258 | { |
| 259 | continue; |
| 260 | } |
| 261 | retryAttempts = static_cast<uint32_t>(*value); |
| 262 | } |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 263 | else if (element.first == "DeliveryRetryIntervalSeconds") |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 264 | { |
| 265 | const uint64_t* value = |
Ed Tanous | 0bdda66 | 2023-08-03 17:27:34 -0700 | [diff] [blame] | 266 | element.second.get_ptr<const uint64_t*>(); |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 267 | if ((value == nullptr) || |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 268 | (*value > std::numeric_limits<uint32_t>::max())) |
| 269 | { |
| 270 | continue; |
| 271 | } |
| 272 | retryTimeoutInterval = static_cast<uint32_t>(*value); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | class EventServiceStore |
| 279 | { |
| 280 | public: |
| 281 | boost::container::flat_map<std::string, std::shared_ptr<UserSubscription>> |
| 282 | subscriptionsConfigMap; |
| 283 | EventServiceConfig eventServiceConfig; |
| 284 | |
| 285 | static EventServiceStore& getInstance() |
| 286 | { |
| 287 | static EventServiceStore eventServiceStore; |
| 288 | return eventServiceStore; |
| 289 | } |
| 290 | |
| 291 | EventServiceConfig& getEventServiceConfig() |
| 292 | { |
| 293 | return eventServiceConfig; |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | } // namespace persistent_data |