Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 : #include <regex>
38 :
39 : #include <nlohmann/json.hpp>
40 :
41 : #include <ert/tracing/Logger.hpp>
42 :
43 : #include <AdminClientEndpointData.hpp>
44 : #include <Configuration.hpp>
45 :
46 :
47 : namespace h2agent
48 : {
49 : namespace model
50 : {
51 :
52 173 : AdminClientEndpointData::AdminClientEndpointData() {
53 173 : client_endpoint_schema_.setJson(h2agent::adminSchemas::client_endpoint); // won't fail
54 173 : }
55 :
56 3 : std::string AdminClientEndpointData::asJsonString() const {
57 :
58 3 : nlohmann::json result = nlohmann::json::array();
59 :
60 3 : this->forEach([&](const KeyType& k, const ValueType& value) {
61 3 : result.push_back(value->asJson()); // json_ is altered with connection status
62 3 : });
63 :
64 : // Client endpoint configuration is shown as an array regardless if there is 1 item, N items or none ([]):
65 6 : return (result.dump());
66 3 : }
67 :
68 26 : AdminClientEndpointData::LoadResult AdminClientEndpointData::loadSingle(const nlohmann::json &j, const common_resources_t &cr) {
69 :
70 26 : std::string error;
71 26 : if (!client_endpoint_schema_.validate(j, error)) {
72 4 : return BadSchema;
73 : }
74 :
75 : // Client endpoint object to fill:
76 22 : auto clientEndpoint = std::make_shared<AdminClientEndpoint>();
77 :
78 22 : if (clientEndpoint->load(j)) {
79 :
80 : // Metrics data:
81 17 : clientEndpoint->setMetricsData(cr.MetricsPtr, cr.ResponseDelaySecondsHistogramBucketBoundaries, cr.MessageSizeBytesHistogramBucketBoundaries, cr.ApplicationName);
82 :
83 : // Push the key in the map:
84 17 : admin_client_endpoint_key_t key = clientEndpoint->getKey();
85 :
86 : // First find (avoid deadlock):
87 17 : auto registeredClientEndpoint = find(key);
88 :
89 : // host, port or secure changes implies re-creation for the client connection id:
90 17 : if (registeredClientEndpoint) {
91 14 : if (registeredClientEndpoint->getHost() != clientEndpoint->getHost() ||
92 14 : registeredClientEndpoint->getPort() != clientEndpoint->getPort() ||
93 5 : registeredClientEndpoint->getSecure() != clientEndpoint->getSecure()) {
94 :
95 3 : registeredClientEndpoint->load(j);
96 3 : if (!cr.ConfigurationPtr->getLazyClientConnection()) registeredClientEndpoint->connect(true /* from scratch */);
97 3 : return Accepted;
98 : }
99 :
100 4 : LOGINFORMATIONAL(
101 : bool updated = (registeredClientEndpoint->getPermit() != clientEndpoint->getPermit());
102 : ert::tracing::Logger::informational(ert::tracing::Logger::asString("Client endpoint '%s' has been updated%s", key.c_str(), updated ? "":" but no changes detected"), ERT_FILE_LOCATION);
103 : );
104 : }
105 : else {
106 10 : add(key, clientEndpoint);
107 10 : if (!cr.ConfigurationPtr->getLazyClientConnection()) clientEndpoint->connect();
108 : }
109 :
110 14 : return Success;
111 :
112 17 : }
113 :
114 5 : return BadContent;
115 26 : }
116 :
117 20 : AdminClientEndpointData::LoadResult AdminClientEndpointData::load(const nlohmann::json &j, const common_resources_t &cr) {
118 :
119 20 : if (j.is_array()) {
120 8 : bool oneAccepted = false;
121 30 : for (auto it : j) // "it" is of type json::reference and has no key() member
122 : {
123 14 : LoadResult result = loadSingle(it, cr);
124 14 : if (result != Success && result != Accepted)
125 3 : return result;
126 11 : if (result == Accepted) oneAccepted = true;
127 14 : }
128 :
129 5 : return oneAccepted ? Accepted:Success;
130 : }
131 :
132 12 : return loadSingle(j, cr);
133 : }
134 :
135 19 : std::shared_ptr<AdminClientEndpoint> AdminClientEndpointData::find(const admin_client_endpoint_key_t &id) const {
136 :
137 19 : bool exists{};
138 19 : auto result = get(id, exists);
139 :
140 38 : return (exists ? result:nullptr);
141 19 : }
142 :
143 : }
144 : }
145 :
|