Skip to content

Commit 6566375

Browse files
committed
MINIFICPP-2798 Mock library for C extensions
1 parent f2d1f88 commit 6566375

28 files changed

Lines changed: 907 additions & 107 deletions
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

extension-framework/cpp-extension-lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ target_include_directories(minifi-cpp-extension-lib PUBLIC include)
2424
target_link_libraries(minifi-cpp-extension-lib PUBLIC minifi-core-framework-common minifi-c-api)
2525

2626
add_subdirectory(libtest)
27-
27+
add_subdirectory(mocklib)

extension-framework/cpp-extension-lib/include/api/core/ProcessContext.h

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Licensed to the Apache Software Foundation (ASF) under one or more
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
33
* contributor license agreements. See the NOTICE file distributed with
44
* this work for additional information regarding copyright ownership.
55
* The ASF licenses this file to You under the Apache License, Version 2.0
@@ -20,23 +20,48 @@
2020
#include <string>
2121
#include <expected>
2222

23+
#include "api/core/FlowFile.h"
24+
#include "api/utils/Ssl.h"
2325
#include "minifi-c.h"
2426
#include "minifi-cpp/core/PropertyDefinition.h"
25-
#include "api/core/FlowFile.h"
2627

2728
namespace org::apache::nifi::minifi::api::core {
2829

2930
class ProcessContext {
3031
public:
31-
explicit ProcessContext(MinifiProcessContext* impl): impl_(impl) {}
32+
virtual ~ProcessContext() noexcept = default;
33+
34+
ProcessContext() = default;
35+
ProcessContext(const ProcessContext&) = delete;
36+
ProcessContext(ProcessContext&&) = delete;
37+
ProcessContext& operator=(const ProcessContext&) = delete;
38+
ProcessContext& operator=(ProcessContext&&) = delete;
39+
40+
[[nodiscard]] virtual std::expected<std::string, std::error_code> getProperty(const minifi::core::PropertyReference& prop,
41+
const FlowFile* ff) const = 0;
42+
[[nodiscard]] virtual std::expected<MinifiControllerService*, std::error_code> getControllerService(std::string_view name,
43+
std::string_view type) const = 0;
44+
[[nodiscard]] virtual bool hasNonEmptyProperty(std::string_view name) const = 0;
45+
[[nodiscard]] virtual std::map<std::string, std::string> getDynamicProperties(const FlowFile* flow_file) const = 0;
46+
47+
[[nodiscard]] virtual std::expected<utils::net::SslData, std::error_code> getSslData(std::string_view name) const = 0;
48+
};
49+
50+
class CffiProcessContext : public ProcessContext {
51+
public:
52+
explicit CffiProcessContext(MinifiProcessContext* impl) : impl_(impl) {}
3253

33-
std::expected<std::string, std::error_code> getProperty(std::string_view name, const FlowFile* flow_file = nullptr) const;
34-
std::expected<std::string, std::error_code> getProperty(const minifi::core::PropertyReference& property_reference, const FlowFile* flow_file = nullptr) const {
35-
return getProperty(property_reference.name, flow_file);
36-
}
37-
[[nodiscard]] std::expected<MinifiControllerService*, std::error_code> getControllerService(std::string_view controller_service_name, std::string_view controller_service_class) const;
54+
[[nodiscard]] std::expected<std::string, std::error_code> getProperty(const minifi::core::PropertyReference& property_reference,
55+
const FlowFile* flow_file) const override;
56+
[[nodiscard]] std::expected<MinifiControllerService*, std::error_code> getControllerService(std::string_view name,
57+
std::string_view type) const override;
58+
[[nodiscard]] std::map<std::string, std::string> getDynamicProperties(const FlowFile* flow_file) const override;
59+
[[nodiscard]] bool hasNonEmptyProperty(std::string_view name) const override;
3860

39-
[[nodiscard]] bool hasNonEmptyProperty(std::string_view name) const;
61+
[[nodiscard]] std::expected<utils::net::SslData, std::error_code> getSslData(std::string_view name) const override;
62+
63+
private:
64+
[[nodiscard]] std::expected<std::string, std::error_code> getProperty(std::string_view name, const FlowFile* flow_file) const;
4065

4166
private:
4267
MinifiProcessContext* impl_;

extension-framework/cpp-extension-lib/include/api/core/ProcessSession.h

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,54 @@ namespace org::apache::nifi::minifi::api::core {
2929

3030
class ProcessSession {
3131
public:
32-
explicit ProcessSession(MinifiProcessSession* impl): impl_(impl) {}
32+
virtual ~ProcessSession() = default;
3333

34-
FlowFile create(const FlowFile* parent = nullptr);
35-
FlowFile get();
36-
void transfer(FlowFile ff, const minifi::core::Relationship& relationship);
37-
void remove(FlowFile ff);
38-
void write(FlowFile& flow, const io::OutputStreamCallback& callback);
39-
void read(FlowFile& flow, const io::InputStreamCallback& callback);
34+
ProcessSession() = default;
4035

41-
void setAttribute(FlowFile& ff, std::string_view key, std::string value);
42-
void removeAttribute(FlowFile& ff, std::string_view key);
43-
std::optional<std::string> getAttribute(FlowFile& ff, std::string_view key);
44-
std::map<std::string, std::string> getAttributes(FlowFile& ff);
36+
ProcessSession(const ProcessSession&) = delete;
37+
ProcessSession(ProcessSession&&) = delete;
38+
ProcessSession& operator=(const ProcessSession&) = delete;
39+
ProcessSession& operator=(ProcessSession&&) = delete;
40+
41+
virtual FlowFile create(const FlowFile* parent = nullptr) = 0;
42+
virtual FlowFile get() = 0;
43+
44+
virtual void penalize(FlowFile& ff) = 0;
45+
virtual void transfer(FlowFile ff, const minifi::core::Relationship& relationship) = 0;
46+
virtual void remove(FlowFile ff) = 0;
47+
virtual void write(FlowFile& flow, const io::OutputStreamCallback& callback) = 0;
48+
virtual void read(FlowFile& flow, const io::InputStreamCallback& callback) = 0;
49+
50+
virtual void setAttribute(FlowFile& ff, std::string_view key, std::string value) = 0;
51+
virtual void removeAttribute(FlowFile& ff, std::string_view key) = 0;
52+
[[nodiscard]] virtual std::optional<std::string> getAttribute(FlowFile& ff, std::string_view key) = 0;
53+
[[nodiscard]] virtual std::map<std::string, std::string> getAttributes(const FlowFile& ff) const = 0;
54+
[[nodiscard]] virtual std::string getFlowFileId(const FlowFile& ff) const = 0;
55+
[[nodiscard]] virtual uint64_t getFlowFileSize(const FlowFile& ff) const = 0;
4556

4657
void writeBuffer(FlowFile& flow_file, std::span<const char> buffer);
4758
void writeBuffer(FlowFile& flow_file, std::span<const std::byte> buffer);
48-
std::vector<std::byte> readBuffer(FlowFile& flow_file);
59+
[[nodiscard]] std::vector<std::byte> readBuffer(FlowFile& flow_file);
60+
};
61+
62+
class CffiProcessSession : public ProcessSession {
63+
public:
64+
explicit CffiProcessSession(MinifiProcessSession* impl): impl_(impl) {}
65+
66+
FlowFile create(const FlowFile* parent = nullptr) override;
67+
FlowFile get() override;
68+
void penalize(FlowFile& ff) override;
69+
void transfer(FlowFile ff, const minifi::core::Relationship& relationship) override;
70+
void remove(FlowFile ff) override;
71+
void write(FlowFile& flow, const io::OutputStreamCallback& callback) override;
72+
void read(FlowFile& flow, const io::InputStreamCallback& callback) override;
73+
74+
void setAttribute(FlowFile& ff, std::string_view key, std::string value) override;
75+
void removeAttribute(FlowFile& ff, std::string_view key) override;
76+
[[nodiscard]] std::optional<std::string> getAttribute(FlowFile& ff, std::string_view key) override;
77+
[[nodiscard]] std::map<std::string, std::string> getAttributes(const FlowFile& ff) const override;
78+
[[nodiscard]] std::string getFlowFileId(const FlowFile& ff) const override;
79+
[[nodiscard]] uint64_t getFlowFileSize(const FlowFile& ff) const override;
4980

5081
private:
5182
MinifiProcessSession* impl_;

extension-framework/cpp-extension-lib/include/api/core/Resource.h

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,38 @@ void useProcessorClassDefinition(Fn&& fn) {
4949
std::vector<MinifiDynamicPropertyDefinition> dynamic_properties;
5050
for (auto& prop : Class::DynamicProperties) {
5151
dynamic_properties.push_back(MinifiDynamicPropertyDefinition {
52-
.name = utils::toStringView(prop.name),
53-
.value = utils::toStringView(prop.value),
54-
.description = utils::toStringView(prop.description),
52+
.name = utils::minifiStringView(prop.name),
53+
.value = utils::minifiStringView(prop.value),
54+
.description = utils::minifiStringView(prop.description),
5555
.supports_expression_language = prop.supports_expression_language
5656
});
5757
}
5858
std::vector<MinifiRelationshipDefinition> relationships;
5959
for (auto& rel : Class::Relationships) {
6060
relationships.push_back(MinifiRelationshipDefinition{
61-
.name = utils::toStringView(rel.name),
62-
.description = utils::toStringView(rel.description)
61+
.name = utils::minifiStringView(rel.name),
62+
.description = utils::minifiStringView(rel.description)
6363
});
6464
}
6565
std::vector<std::vector<MinifiStringView>> attribute_relationships_cache;
6666
std::vector<MinifiOutputAttributeDefinition> output_attributes;
6767
for (auto& attr : Class::OutputAttributes) {
6868
std::vector<MinifiStringView> rel_cache;
6969
for (auto& rel : attr.relationships) {
70-
rel_cache.push_back(utils::toStringView(rel.name));
70+
rel_cache.push_back(utils::minifiStringView(rel.name));
7171
}
7272
output_attributes.push_back(MinifiOutputAttributeDefinition {
73-
.name = utils::toStringView(attr.name),
73+
.name = utils::minifiStringView(attr.name),
7474
.relationships_count = gsl::narrow<uint32_t>(attr.relationships.size()),
7575
.relationships_ptr = rel_cache.data(),
76-
.description = utils::toStringView(attr.description)
76+
.description = utils::minifiStringView(attr.description)
7777
});
7878
attribute_relationships_cache.push_back(std::move(rel_cache));
7979
}
8080

8181
MinifiProcessorClassDefinition definition{
82-
.full_name = utils::toStringView(full_name),
83-
.description = utils::toStringView(Class::Description),
82+
.full_name = utils::minifiStringView(full_name),
83+
.description = utils::minifiStringView(Class::Description),
8484
.class_properties_count = gsl::narrow<uint32_t>(class_properties.size()),
8585
.class_properties_ptr = class_properties.data(),
8686
.dynamic_properties_count = gsl::narrow<uint32_t>(dynamic_properties.size()),
@@ -100,7 +100,7 @@ void useProcessorClassDefinition(Fn&& fn) {
100100
return new Class{minifi::core::ProcessorMetadata{
101101
.uuid = minifi::utils::Identifier::parse(std::string{metadata.uuid.data, metadata.uuid.length}).value(),
102102
.name = std::string{metadata.name.data, metadata.name.length},
103-
.logger = std::make_shared<logging::Logger>(metadata.logger)}};
103+
.logger = std::make_shared<logging::CffiLogger>(metadata.logger)}};
104104
} catch (...) { return nullptr; }
105105
},
106106
.destroy = [] (MINIFI_OWNED void* self) -> void {
@@ -110,16 +110,16 @@ void useProcessorClassDefinition(Fn&& fn) {
110110
return static_cast<Class*>(self)->getTriggerWhenEmpty();
111111
},
112112
.onTrigger = [] (void* self, MinifiProcessContext* context, MinifiProcessSession* session) -> MinifiStatus {
113-
ProcessContext context_wrapper(context);
114-
ProcessSession session_wrapper(session);
113+
CffiProcessContext context_wrapper(context);
114+
CffiProcessSession session_wrapper(session);
115115
try {
116116
return static_cast<Class*>(self)->onTrigger(context_wrapper, session_wrapper);
117117
} catch (...) {
118118
return MINIFI_STATUS_UNKNOWN_ERROR;
119119
}
120120
},
121121
.onSchedule = [] (void* self, MinifiProcessContext* context) -> MinifiStatus {
122-
ProcessContext context_wrapper(context);
122+
CffiProcessContext context_wrapper(context);
123123
try {
124124
return static_cast<Class*>(self)->onSchedule(context_wrapper);
125125
} catch (...) {
@@ -136,7 +136,7 @@ void useProcessorClassDefinition(Fn&& fn) {
136136
std::vector<MinifiStringView> names;
137137
std::vector<double> values;
138138
for (auto& [name, val] : metrics) {
139-
names.push_back(utils::toStringView(name));
139+
names.push_back(utils::minifiStringView(name));
140140
values.push_back(val);
141141
}
142142
return MinifiPublishedMetricsCreate(gsl::narrow<uint32_t>(metrics.size()), names.data(), values.data());
@@ -155,8 +155,8 @@ void useControllerServiceClassDefinition(Fn&& fn) {
155155

156156
std::vector<MinifiPropertyDefinition> class_properties = utils::toProperties(Class::Properties, string_vector_cache);
157157

158-
MinifiControllerServiceClassDefinition definition{.full_name = utils::toStringView(full_name),
159-
.description = utils::toStringView(Class::Description),
158+
MinifiControllerServiceClassDefinition definition{.full_name = utils::minifiStringView(full_name),
159+
.description = utils::minifiStringView(Class::Description),
160160
.class_properties_count = gsl::narrow<uint32_t>(class_properties.size()),
161161
.class_properties_ptr = class_properties.data(),
162162

@@ -166,7 +166,7 @@ void useControllerServiceClassDefinition(Fn&& fn) {
166166
return new Class{minifi::core::ControllerServiceMetadata{
167167
.uuid = minifi::utils::Identifier::parse(std::string{metadata.uuid.data, metadata.uuid.length}).value(),
168168
.name = std::string{metadata.name.data, metadata.name.length},
169-
.logger = std::make_shared<logging::Logger>(metadata.logger)}};
169+
.logger = std::make_shared<logging::CffiLogger>(metadata.logger)}};
170170
} catch (...) { return nullptr; }
171171
},
172172
.destroy = [](MINIFI_OWNED void* self) -> void { delete static_cast<Class*>(self); },
@@ -186,4 +186,18 @@ void useControllerServiceClassDefinition(Fn&& fn) {
186186
fn(definition);
187187
}
188188

189+
template <typename... Processors>
190+
void registerProcessors(MinifiExtension* extension) {
191+
(core::useProcessorClassDefinition<Processors>([&](const MinifiProcessorClassDefinition& definition) {
192+
MinifiRegisterProcessor(extension, &definition);
193+
}), ...);
194+
}
195+
196+
template <typename... ControllerServices>
197+
void registerControllerServices(MinifiExtension* extension) {
198+
(core::useControllerServiceClassDefinition<ControllerServices>([&](const MinifiControllerServiceClassDefinition& definition) {
199+
MinifiRegisterControllerService(extension, &definition);
200+
}), ...);
201+
}
202+
189203
} // namespace org::apache::nifi::minifi::api::core

extension-framework/cpp-extension-lib/include/api/core/logging/Logger.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
#include <iostream>
2121
#include <string>
2222

23-
#include "fmt/chrono.h"
2423
#include "minifi-c.h"
2524
#include "minifi-cpp/core/logging/Logger.h"
2625

2726
namespace org::apache::nifi::minifi::api::core::logging {
2827

29-
class Logger : public minifi::core::logging::Logger {
28+
class CffiLogger : public minifi::core::logging::Logger {
3029
public:
31-
explicit Logger(MinifiLogger* impl): impl_(impl) {}
30+
explicit CffiLogger(MinifiLogger* impl): impl_(impl) {}
3231

3332
void set_max_log_size(int size) override;
3433
void log_string(minifi::core::logging::LOG_LEVEL level, std::string str) override;

0 commit comments

Comments
 (0)