Skip to content

Commit 67a6dbe

Browse files
rahkrcopybara-github
authored andcommitted
Make CreateSubscription asynchronous
PiperOrigin-RevId: 615234587
1 parent 0d70804 commit 67a6dbe

8 files changed

Lines changed: 481 additions & 146 deletions

File tree

ecclesia/lib/redfish/event/server/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cc_library(
77
visibility = ["//visibility:public"],
88
deps = [
99
"@com_google_absl//absl/container:flat_hash_map",
10+
"@com_google_absl//absl/container:flat_hash_set",
1011
"@com_google_absl//absl/hash",
1112
"@com_google_absl//absl/status",
1213
"@com_google_absl//absl/status:statusor",
@@ -28,6 +29,7 @@ cc_library(
2829
"@com_google_absl//absl/algorithm:container",
2930
"@com_google_absl//absl/base:core_headers",
3031
"@com_google_absl//absl/container:flat_hash_map",
32+
"@com_google_absl//absl/container:flat_hash_set",
3133
"@com_google_absl//absl/hash",
3234
"@com_google_absl//absl/log",
3335
"@com_google_absl//absl/log:die_if_null",
@@ -106,6 +108,7 @@ cc_test(
106108
srcs = ["subscription_test.cc"],
107109
deps = [
108110
":subscription",
111+
"@com_google_absl//absl/container:flat_hash_set",
109112
"@com_google_absl//absl/status",
110113
"@com_google_absl//absl/status:statusor",
111114
"@com_google_absl//absl/strings",
@@ -122,7 +125,9 @@ cc_test(
122125
":subscription",
123126
":subscription_impl",
124127
":subscription_mock",
128+
"//ecclesia/lib/testing:status",
125129
"@com_google_absl//absl/container:flat_hash_map",
130+
"@com_google_absl//absl/container:flat_hash_set",
126131
"@com_google_absl//absl/status",
127132
"@com_google_absl//absl/status:statusor",
128133
"@com_google_absl//absl/strings",
@@ -159,7 +164,9 @@ cc_test(
159164
deps = [
160165
":subscription",
161166
":subscription_store",
167+
"//ecclesia/lib/testing:status",
162168
"@com_google_absl//absl/container:flat_hash_map",
169+
"@com_google_absl//absl/container:flat_hash_set",
163170
"@com_google_absl//absl/status",
164171
"@com_google_absl//absl/status:statusor",
165172
"@com_google_absl//absl/strings",

ecclesia/lib/redfish/event/server/subscription.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include <functional>
2020
#include <string>
2121
#include <utility>
22-
#include <vector>
2322

2423
#include "absl/container/flat_hash_map.h"
24+
#include "absl/container/flat_hash_set.h"
2525
#include "absl/hash/hash.h"
2626
#include "absl/status/status.h"
2727
#include "absl/status/statusor.h"
@@ -77,9 +77,12 @@ std::string EventId::ToString() const { return this->ToJSON().dump(); }
7777

7878
SubscriptionContext::SubscriptionContext(
7979
const SubscriptionId& subscription_id_in,
80+
const absl::flat_hash_map<EventSourceId, absl::flat_hash_set<std::string>>&
81+
event_source_to_uris_in,
8082
absl::flat_hash_map<std::string, Trigger> id_to_triggers_in,
8183
std::function<void(const nlohmann::json&)>&& on_event_callback_in)
8284
: subscription_id(subscription_id_in),
85+
event_source_to_uri(event_source_to_uris_in),
8386
id_to_triggers(std::move(id_to_triggers_in)),
8487
on_event_callback(std::move(on_event_callback_in)) {}
8588

@@ -104,9 +107,9 @@ absl::StatusOr<Trigger> Trigger::Create(const nlohmann::json& trigger_json) {
104107
return absl::InvalidArgumentError("Origin resources not populated");
105108
}
106109

107-
std::vector<std::string> origin_resources;
110+
absl::flat_hash_set<std::string> origin_resources;
108111
for (const auto& origin_resource : *find_origin_resources) {
109-
origin_resources.push_back(origin_resource["@odata.id"].get<std::string>());
112+
origin_resources.insert(origin_resource["@odata.id"].get<std::string>());
110113
}
111114

112115
std::string predicate;
@@ -121,14 +124,16 @@ absl::StatusOr<Trigger> Trigger::Create(const nlohmann::json& trigger_json) {
121124
}
122125

123126
// Create a Trigger object with the extracted information
124-
Trigger trigger(std::move(origin_resources), predicate, event_mask);
127+
Trigger trigger(id, std::move(origin_resources), predicate, event_mask);
125128

126129
return trigger;
127130
}
128131

129-
Trigger::Trigger(std::vector<std::string> origin_resources_in,
132+
Trigger::Trigger(absl::string_view id_in,
133+
absl::flat_hash_set<std::string> origin_resources_in,
130134
absl::string_view predicate_in, bool mask_in)
131-
: origin_resources(std::move(origin_resources_in)),
135+
: id(id_in),
136+
origin_resources(std::move(origin_resources_in)),
132137
predicate(predicate_in),
133138
mask(mask_in) {}
134139

ecclesia/lib/redfish/event/server/subscription.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <vector>
2727

2828
#include "absl/container/flat_hash_map.h"
29+
#include "absl/container/flat_hash_set.h"
2930
#include "absl/status/status.h"
3031
#include "absl/status/statusor.h"
3132
#include "absl/strings/string_view.h"
@@ -134,17 +135,21 @@ struct Trigger {
134135
static absl::StatusOr<Trigger> Create(const nlohmann::json &trigger_json);
135136

136137
// Constructor for Trigger
137-
explicit Trigger(std::vector<std::string> origin_resources_in,
138+
explicit Trigger(absl::string_view id_in,
139+
absl::flat_hash_set<std::string> origin_resources_in,
138140
absl::string_view predicate_in = "", bool mask_in = false);
139141

142+
// Trigger id
143+
std::string id;
144+
140145
// Converts Trigger to JSON format
141146
nlohmann::json ToJSON() const;
142147

143148
// Converts Trigger to string format
144149
std::string ToString() const;
145150

146151
// List of origin resources associated with the Trigger
147-
std::vector<std::string> origin_resources;
152+
absl::flat_hash_set<std::string> origin_resources;
148153

149154
// Map of event source to Redfish resource URI
150155
EventSourceToUri event_source_to_uri;
@@ -156,13 +161,25 @@ struct Trigger {
156161
bool mask;
157162
};
158163

164+
// Structure representing a subscription
159165
struct SubscriptionContext {
160166
SubscriptionContext(
161167
const SubscriptionId &subscription_id_in,
168+
const absl::flat_hash_map<EventSourceId, absl::flat_hash_set<std::string>>
169+
&event_source_to_uris_in,
162170
absl::flat_hash_map<std::string, Trigger> id_to_triggers_in,
163171
std::function<void(const nlohmann::json &)> &&on_event_callback_in);
164172

173+
// Unique identifier for the subscription
165174
SubscriptionId subscription_id;
175+
176+
// Map of event source IDs to corresponding URIs.
177+
// This map is used to process incoming events by looking up the URI an event
178+
// is associated with and then the URI is used to create an internal query
179+
// that builds `OriginOfCondtion`.
180+
absl::flat_hash_map<EventSourceId, absl::flat_hash_set<std::string>>
181+
event_source_to_uri;
182+
166183
// Map of trigger IDs to corresponding Trigger objects
167184
// This map associates each trigger ID with the corresponding Trigger
168185
// object, enabling efficient lookup and management of triggers for a given
@@ -277,6 +294,12 @@ class SubscriptionService {
277294
const nlohmann::json &request,
278295
std::function<void(const nlohmann::json &)> &&on_event_callback) = 0;
279296

297+
virtual void CreateSubscription(
298+
const nlohmann::json &request,
299+
std::function<void(const absl::StatusOr<SubscriptionId> &)>
300+
on_subscribe_callback,
301+
std::function<void(const nlohmann::json &)> on_event_callback) = 0;
302+
280303
// Deletes the subscription with the given subscription ID
281304
virtual void DeleteSubscription(const SubscriptionId &subscription_id) = 0;
282305

0 commit comments

Comments
 (0)