Skip to content

Commit 2072f78

Browse files
config: add export final participant configuration as string (#218)
When a participant configuration was processed, it may include fragments included by `includes:` statements. This adds a new API to retrieve the finalized participant configuration as a JSON string. --------- Co-authored-by: Daniel Edwards <daniel.edwards@vector.com>
1 parent 4f05f22 commit 2072f78

File tree

11 files changed

+218
-140
lines changed

11 files changed

+218
-140
lines changed

SilKit/IntegrationTests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ add_silkit_test_to_executable(SilKitIntegrationTests
255255
SOURCES ITest_MultiSim.cpp
256256
)
257257

258+
add_silkit_test_to_executable(SilKitIntegrationTests
259+
SOURCES ITest_Config.cpp
260+
)
261+
258262
if(SILKIT_BUILD_DASHBOARD)
259263
add_silkit_test_to_executable(SilKitInternalIntegrationTests
260264
SOURCES ITest_Dashboard.cpp

SilKit/IntegrationTests/Hourglass/MockCapi.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,12 @@ extern "C"
808808
return globalCapi->SilKit_ParticipantConfiguration_Destroy(participantConfiguration);
809809
}
810810

811+
SilKit_ReturnCode SilKitCALL SilKit_ParticipantConfiguration_ToJson(const SilKit_ParticipantConfiguration* inParticipantConfiguration,
812+
char** outString, size_t* requiredSize)
813+
{
814+
return globalCapi->SilKit_ParticipantConfiguration_ToJson(inParticipantConfiguration, outString, requiredSize);
815+
}
816+
811817
// Logger
812818

813819
SilKit_ReturnCode SilKitCALL SilKit_Logger_Log(SilKit_Logger* logger, SilKit_LoggingLevel level,

SilKit/IntegrationTests/Hourglass/MockCapi.hpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
/* Copyright (c) 2022 Vector Informatik GmbH
2-
3-
Permission is hereby granted, free of charge, to any person obtaining
4-
a copy of this software and associated documentation files (the
5-
"Software"), to deal in the Software without restriction, including
6-
without limitation the rights to use, copy, modify, merge, publish,
7-
distribute, sublicense, and/or sell copies of the Software, and to
8-
permit persons to whom the Software is furnished to do so, subject to
9-
the following conditions:
10-
11-
The above copyright notice and this permission notice shall be
12-
included in all copies or substantial portions of the Software.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
1+
// SPDX-FileCopyrightText: 2022 Vector Informatik GmbH
2+
//
3+
// SPDX-License-Identifier: MIT
214

225
#include "gtest/gtest.h"
236
#include "gmock/gmock.h"
@@ -437,6 +420,10 @@ class MockCapi
437420
MOCK_METHOD(SilKit_ReturnCode, SilKit_ParticipantConfiguration_Destroy,
438421
(SilKit_ParticipantConfiguration * participantConfiguration));
439422

423+
MOCK_METHOD(SilKit_ReturnCode, SilKit_ParticipantConfiguration_ToJson,
424+
(const SilKit_ParticipantConfiguration* inParticipantConfiguration,
425+
char** outString, size_t* requiredSize));
426+
440427
// Logger
441428

442429
MOCK_METHOD(SilKit_ReturnCode, SilKit_Logger_Log,

SilKit/IntegrationTests/Hourglass/Test_HourglassParticipantLogger.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
/* Copyright (c) 2022 Vector Informatik GmbH
2-
3-
Permission is hereby granted, free of charge, to any person obtaining
4-
a copy of this software and associated documentation files (the
5-
"Software"), to deal in the Software without restriction, including
6-
without limitation the rights to use, copy, modify, merge, publish,
7-
distribute, sublicense, and/or sell copies of the Software, and to
8-
permit persons to whom the Software is furnished to do so, subject to
9-
the following conditions:
10-
11-
The above copyright notice and this permission notice shall be
12-
included in all copies or substantial portions of the Software.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
1+
// SPDX-FileCopyrightText: 2022 Vector Informatik GmbH
2+
//
3+
// SPDX-License-Identifier: MIT
214

225
#include "gtest/gtest.h"
236
#include "gmock/gmock.h"
@@ -87,6 +70,33 @@ TEST_F(Test_HourglassParticipantLogger, SilKit_ParticipantConfiguration_Destroy)
8770
}
8871
}
8972

73+
TEST_F(Test_HourglassParticipantLogger, SilKit_ParticipantConfiguration_ToJson)
74+
{
75+
auto&& configString = R"(schemaVersion: 1
76+
Description: test
77+
78+
Middleware:
79+
RegistryUri: silkit://localhost:8501
80+
ConnectAttempts: 1
81+
TcpNoDelay: true
82+
AcceptorUris: [tcp://0.0.0.0:8502]
83+
RegistryAsFallbackProxy: true
84+
85+
Logging:
86+
Sinks:
87+
- Type: Stdout
88+
Level: Debug
89+
)";
90+
91+
auto&& config = SilKit::Config::ParticipantConfigurationFromString(configString);
92+
EXPECT_CALL(capi, SilKit_ParticipantConfiguration_ToJson(testing::_, testing::_, testing::_))
93+
.Times(1);
94+
{
95+
auto jsonString = SilKit::Config::ParticipantConfigurationToJson(config);
96+
}
97+
98+
}
99+
90100
TEST_F(Test_HourglassParticipantLogger, SilKit_Participant_Create1)
91101
{
92102
std::string name = "Participant1";
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// SPDX-FileCopyrightText: 2025 Vector Informatik GmbH
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
6+
#include <iostream>
7+
#include <cstring>
8+
9+
#include "silkit/services/all.hpp"
10+
#include "SimTestHarness.hpp"
11+
12+
#include "gmock/gmock.h"
13+
#include "gtest/gtest.h"
14+
15+
16+
namespace {
17+
18+
using namespace std::chrono_literals;
19+
using namespace SilKit;
20+
21+
22+
TEST(ITest_Config, test_to_json)
23+
{
24+
auto&& configString = R"(schemaVersion: 1
25+
Description: test
26+
27+
Middleware:
28+
RegistryUri: silkit://localhost:8501
29+
ConnectAttempts: 1
30+
TcpNoDelay: true
31+
AcceptorUris: [tcp://0.0.0.0:8502]
32+
RegistryAsFallbackProxy: true
33+
34+
Logging:
35+
Sinks:
36+
- Type: Stdout
37+
Level: Debug
38+
)";
39+
40+
auto&& config = SilKit::Config::ParticipantConfigurationFromString(configString);
41+
auto jsonString = SilKit::Config::ParticipantConfigurationToJson(config);
42+
ASSERT_GT(jsonString.size(), 0u);
43+
auto&& config2 = SilKit::Config::ParticipantConfigurationFromString(jsonString);
44+
auto jsonString2 = SilKit::Config::ParticipantConfigurationToJson(config2);
45+
ASSERT_GT(jsonString2.size(), 0u);
46+
EXPECT_EQ(jsonString, jsonString2) << "The parsed config should be the same as the original yaml";
47+
}
48+
49+
TEST(ITest_Config, test_to_json_in_capi)
50+
{
51+
auto&& configString = R"(schemaVersion: 1
52+
Description: test
53+
54+
Middleware:
55+
RegistryUri: silkit://localhost:8501
56+
ConnectAttempts: 1
57+
TcpNoDelay: true
58+
AcceptorUris: [tcp://0.0.0.0:8502]
59+
RegistryAsFallbackProxy: true
60+
61+
Logging:
62+
Sinks:
63+
- Type: Stdout
64+
Level: Debug
65+
)";
66+
67+
SilKit_ParticipantConfiguration* config{nullptr};
68+
auto result = SilKit_ParticipantConfiguration_FromString(&config, configString);
69+
ASSERT_EQ(result, SilKit_ReturnCode_SUCCESS);
70+
71+
std::array<char, 1024> buf{};
72+
std::memset(buf.data(), 'A', buf.size());
73+
size_t size{};
74+
75+
// get size
76+
result = SilKit_ParticipantConfiguration_ToJson(config, nullptr, &size);
77+
ASSERT_EQ(result, SilKit_ReturnCode_SUCCESS);
78+
ASSERT_GT(size, 0) << "the string should have length > 0";
79+
ASSERT_LT(size, buf.size()) << "the string should have length <= buf.size()";
80+
81+
//get contents, n
82+
auto&& ptr = buf.data();
83+
result = SilKit_ParticipantConfiguration_ToJson(config, &ptr, &size);
84+
ASSERT_EQ(result, SilKit_ReturnCode_SUCCESS);
85+
ASSERT_GT(size, 0) << "the string should have length > 0";
86+
ASSERT_EQ(ptr[size], 'A') << "there should be no trailing \0";
87+
88+
result = SilKit_ParticipantConfiguration_Destroy(config);
89+
ASSERT_EQ(result, SilKit_ReturnCode_SUCCESS);
90+
}
91+
92+
93+
} // anonymous namespace

SilKit/include/silkit/capi/SilKit.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
/* Copyright (c) 2022 Vector Informatik GmbH
2-
3-
Permission is hereby granted, free of charge, to any person obtaining
4-
a copy of this software and associated documentation files (the
5-
"Software"), to deal in the Software without restriction, including
6-
without limitation the rights to use, copy, modify, merge, publish,
7-
distribute, sublicense, and/or sell copies of the Software, and to
8-
permit persons to whom the Software is furnished to do so, subject to
9-
the following conditions:
10-
11-
The above copyright notice and this permission notice shall be
12-
included in all copies or substantial portions of the Software.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
1+
// SPDX-FileCopyrightText: 2022 Vector Informatik GmbH
2+
//
3+
// SPDX-License-Identifier: MIT
214

225
#pragma once
236

@@ -104,4 +87,14 @@ typedef SilKit_ReturnCode(SilKitFPTR* SilKit_ParticipantConfiguration_Destroy_t)
10487
SilKit_ParticipantConfiguration* participantConfiguration);
10588

10689

90+
/*! \brief Return a JSON-string of the complete parsed participant configuration.
91+
* \param outputJsonString the JSON string of the configuration. When this is NULL outputSize will contain the required size for the JSON string.
92+
* \param outputSize the size of the outputJsonString is stored here. Can be called with outputJsonString set to NULL to get the required size.
93+
*/
94+
SilKitAPI SilKit_ReturnCode SilKitCALL SilKit_ParticipantConfiguration_ToJson(
95+
const SilKit_ParticipantConfiguration* inParticipantConfiguration, char** outputJsonString, size_t* outputSize);
96+
97+
typedef SilKit_ReturnCode(SilKitFPTR* SilKit_ParticipantConfiguration_ToJson_t)(
98+
const SilKit_ParticipantConfiguration* intParticipantConfiguration, char** outputJsonString, size_t* outputSize);
99+
107100
SILKIT_END_DECLS

SilKit/include/silkit/config/IParticipantConfiguration.hpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
/* Copyright (c) 2022 Vector Informatik GmbH
2-
3-
Permission is hereby granted, free of charge, to any person obtaining
4-
a copy of this software and associated documentation files (the
5-
"Software"), to deal in the Software without restriction, including
6-
without limitation the rights to use, copy, modify, merge, publish,
7-
distribute, sublicense, and/or sell copies of the Software, and to
8-
permit persons to whom the Software is furnished to do so, subject to
9-
the following conditions:
10-
11-
The above copyright notice and this permission notice shall be
12-
included in all copies or substantial portions of the Software.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
1+
// SPDX-FileCopyrightText: 2022 Vector Informatik GmbH
2+
//
3+
// SPDX-License-Identifier: MIT
214

225
#pragma once
236

@@ -73,6 +56,10 @@ DETAIL_SILKIT_CPP_API auto ParticipantConfigurationFromString(const std::string&
7356
DETAIL_SILKIT_CPP_API auto ParticipantConfigurationFromFile(const std::string& filename)
7457
-> std::shared_ptr<SilKit::Config::IParticipantConfiguration>;
7558

59+
//! \brief Returns the parsed configuration with all includes processed.
60+
DETAIL_SILKIT_CPP_API auto ParticipantConfigurationToJson(
61+
std::shared_ptr<SilKit::Config::IParticipantConfiguration> config) -> std::string;
62+
7663
} // namespace Config
7764
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE
7865
} // namespace SilKit

SilKit/include/silkit/detail/impl/config/IParticipantConfiguration.ipp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
1-
// Copyright (c) 2023 Vector Informatik GmbH
1+
// SPDX-FileCopyrightText: 2023 Vector Informatik GmbH
22
//
3-
// Permission is hereby granted, free of charge, to any person obtaining
4-
// a copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to
8-
// permit persons to whom the Software is furnished to do so, subject to
9-
// the following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be
12-
// included in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15-
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17-
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18-
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19-
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20-
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3+
// SPDX-License-Identifier: MIT
214

225
#pragma once
236

@@ -26,6 +9,9 @@
269
#include "silkit/detail/impl/ThrowOnError.hpp"
2710
#include "silkit/detail/impl/config/ParticipantConfiguration.hpp"
2811

12+
#include <string>
13+
#include <vector>
14+
2915
namespace SilKit {
3016
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_BEGIN
3117
namespace Config {
@@ -52,6 +38,25 @@ auto ParticipantConfigurationFromFile(const std::string& path)
5238
return std::make_shared<Impl::Config::ParticipantConfiguration>(participantConfiguration);
5339
}
5440

41+
auto ParticipantConfigurationToJson(std::shared_ptr<SilKit::Config::IParticipantConfiguration> config) -> std::string
42+
{
43+
size_t size{};
44+
auto&& concreteConfig = std::dynamic_pointer_cast<Impl::Config::ParticipantConfiguration>(config);
45+
auto returnCode = SilKit_ParticipantConfiguration_ToJson(concreteConfig->Get(), nullptr, &size);
46+
Impl::ThrowOnError(returnCode);
47+
48+
std::vector<char> buffer;
49+
if( size > 0)
50+
{
51+
buffer.resize(size);
52+
auto&& data = buffer.data();
53+
returnCode = SilKit_ParticipantConfiguration_ToJson(concreteConfig->Get(), &data, &size);
54+
Impl::ThrowOnError(returnCode);
55+
}
56+
return {buffer.data(), buffer.size()};
57+
}
58+
59+
5560
} // namespace Config
5661
DETAIL_SILKIT_DETAIL_VN_NAMESPACE_CLOSE
5762
} // namespace SilKit
@@ -61,5 +66,6 @@ namespace SilKit {
6166
namespace Config {
6267
using SilKit::DETAIL_SILKIT_DETAIL_NAMESPACE_NAME::Config::ParticipantConfigurationFromString;
6368
using SilKit::DETAIL_SILKIT_DETAIL_NAMESPACE_NAME::Config::ParticipantConfigurationFromFile;
69+
using SilKit::DETAIL_SILKIT_DETAIL_NAMESPACE_NAME::Config::ParticipantConfigurationToJson;
6470
} // namespace Config
6571
} // namespace SilKit

0 commit comments

Comments
 (0)