Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/config/context_provider.h" 4 : 5 : #include "source/common/common/callback_impl.h" 6 : #include "source/common/common/stl_helpers.h" 7 : #include "source/common/common/thread.h" 8 : #include "source/common/config/xds_context_params.h" 9 : 10 : namespace Envoy { 11 : namespace Config { 12 : 13 : class ContextProviderImpl : public ContextProvider { 14 : public: 15 : ContextProviderImpl(const envoy::config::core::v3::Node& node, 16 : const Protobuf::RepeatedPtrField<std::string>& node_context_params) 17 248 : : node_context_(XdsContextParams::encodeNodeContext(node, node_context_params)) {} 18 : 19 : // Config::ContextProvider 20 0 : const xds::core::v3::ContextParams& nodeContext() const override { return node_context_; } 21 : const xds::core::v3::ContextParams& 22 0 : dynamicContext(absl::string_view resource_type_url) const override { 23 0 : ASSERT_IS_MAIN_OR_TEST_THREAD(); 24 0 : auto it = dynamic_context_.find(resource_type_url); 25 0 : if (it != dynamic_context_.end()) { 26 0 : return it->second; 27 0 : } 28 0 : return xds::core::v3::ContextParams::default_instance(); 29 0 : }; 30 : void setDynamicContextParam(absl::string_view resource_type_url, absl::string_view key, 31 0 : absl::string_view value) override { 32 0 : ASSERT_IS_MAIN_OR_TEST_THREAD(); 33 0 : (*dynamic_context_[resource_type_url] 34 0 : .mutable_params())[toStdStringView(key)] = // NOLINT(std::string_view) 35 0 : toStdStringView(value); // NOLINT(std::string_view) 36 0 : update_cb_helper_.runCallbacks(resource_type_url); 37 0 : } 38 : void unsetDynamicContextParam(absl::string_view resource_type_url, 39 0 : absl::string_view key) override { 40 0 : ASSERT_IS_MAIN_OR_TEST_THREAD(); 41 0 : dynamic_context_[resource_type_url].mutable_params()->erase( 42 0 : toStdStringView(key)); // NOLINT(std::string_view) 43 0 : update_cb_helper_.runCallbacks(resource_type_url); 44 0 : } 45 : ABSL_MUST_USE_RESULT Common::CallbackHandlePtr 46 277 : addDynamicContextUpdateCallback(UpdateNotificationCb callback) const override { 47 277 : ASSERT_IS_MAIN_OR_TEST_THREAD(); 48 277 : return update_cb_helper_.add(callback); 49 277 : }; 50 : 51 : private: 52 : const xds::core::v3::ContextParams node_context_; 53 : // Map from resource type URL to dynamic context parameters. 54 : absl::flat_hash_map<std::string, xds::core::v3::ContextParams> dynamic_context_; 55 : mutable Common::CallbackManager<absl::string_view> update_cb_helper_; 56 : }; 57 : 58 : } // namespace Config 59 : } // namespace Envoy