Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions extension-framework/cpp-extension-lib/include/api/core/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,22 @@ void useControllerServiceClassDefinition(Fn&& fn) {
const auto full_name = minifi::core::className<Class>();

std::vector<MinifiPropertyDefinition> class_properties = utils::toProperties(Class::Properties, string_vector_cache);
std::vector<MinifiStringView> provided_interfaces;
if constexpr (requires { Class::ProvidedInterfaces; }) {
provided_interfaces.reserve(Class::ProvidedInterfaces.size());
for (const auto& iface : Class::ProvidedInterfaces) {
provided_interfaces.push_back(utils::minifiStringView(iface.name));
}
}

MinifiControllerServiceClassDefinition definition{.full_name = utils::minifiStringView(full_name),
.description = utils::minifiStringView(Class::Description),
.class_properties_count = gsl::narrow<uint32_t>(class_properties.size()),
.class_properties_ptr = class_properties.data(),

.provided_interfaces_count = gsl::narrow<uint32_t>(provided_interfaces.size()),
.provided_interfaces_ptr = provided_interfaces.data(),

.callbacks = MinifiControllerServiceCallbacks{
.create = [](MinifiControllerServiceMetadata metadata) -> MINIFI_OWNED void* {
try {
Expand All @@ -181,6 +191,20 @@ void useControllerServiceClassDefinition(Fn&& fn) {
static_cast<Class*>(self)->disable();
} catch (...) {}
},
.get_interface = [](void* self, MinifiStringView interface_name) -> void* {
try {
const std::string_view name_view{interface_name.data, interface_name.length};

if constexpr (requires { Class::ProvidedInterfaces; }) {
for (const auto& iface : Class::ProvidedInterfaces) {
if (iface.name == name_view) {
return iface.cast(self);
}
}
}
return nullptr;
} catch (...) { return nullptr; }
}
}};

fn(definition);
Expand Down
35 changes: 35 additions & 0 deletions extensions/stable-api-sandbox/AnimalControllerServiceApis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <cinttypes>

namespace org::apache::nifi::minifi::api_sandbox {
class NumberOfLegsControllerApi {
public:
virtual ~NumberOfLegsControllerApi() = default;
virtual uint8_t numberOfLegs() const = 0;
};

class CanFlyControllerApi {
public:
Comment thread
martinzink marked this conversation as resolved.
virtual ~CanFlyControllerApi() = default;
virtual bool canFly() const = 0;
};

} // namespace org::apache::nifi::minifi::api_sandbox
30 changes: 30 additions & 0 deletions extensions/stable-api-sandbox/AnimalControllerServices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "AnimalControllerServices.h"

#include "api/utils/ProcessorConfigUtils.h"
namespace org::apache::nifi::minifi::api_sandbox {

MinifiStatus DogController::enableImpl(api::core::ControllerServiceContext& ctx) {
this->has_jetpack_ = ctx.getProperty(HasJetpack) | minifi::utils::andThen(parsing::parseBool) |
minifi::utils::orThrow(fmt::format("Expected parsable bool from \"{}\"", HasJetpack.name));

return MINIFI_STATUS_SUCCESS;
}

} // namespace org::apache::nifi::minifi::api_sandbox
67 changes: 67 additions & 0 deletions extensions/stable-api-sandbox/AnimalControllerServices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "AnimalControllerServiceApis.h"
#include "api/core/ControllerServiceImpl.h"
#include "api/utils/Export.h"
#include "core/PropertyDefinitionBuilder.h"
#include "minifi-cpp/core/ProvidedControllerServiceInterface.h"

namespace org::apache::nifi::minifi::api_sandbox {
class DogController : public api::core::ControllerServiceImpl, public CanFlyControllerApi, public NumberOfLegsControllerApi {
public:
EXTENSIONAPI static constexpr const char* Description = "Test DogController";
EXTENSIONAPI static constexpr auto HasJetpack = core::PropertyDefinitionBuilder<>::createProperty("Has Jetpack")
.withDescription("Whether or not the dog has a jetpack")
.withDefaultValue("false")
.withValidator(core::StandardPropertyValidators::BOOLEAN_VALIDATOR)
.build();

EXTENSIONAPI static constexpr auto Properties = std::to_array<core::PropertyReference>({
HasJetpack,
});
EXTENSIONAPI static constexpr auto ProvidedInterfaces =
std::to_array<core::ProvidedInterface>({core::createProvidedInterface<CanFlyControllerApi, DogController>(),
core::createProvidedInterface<NumberOfLegsControllerApi, DogController>()});

using ControllerServiceImpl::ControllerServiceImpl;
MinifiStatus enableImpl(api::core::ControllerServiceContext& ctx) override;

uint8_t numberOfLegs() const override { return 4; }
bool canFly() const override { return has_jetpack_; }

private:
bool has_jetpack_ = false;
};

class DuckController : public api::core::ControllerServiceImpl, public CanFlyControllerApi, public NumberOfLegsControllerApi {
public:
EXTENSIONAPI static constexpr const char* Description = "Test DuckController";

EXTENSIONAPI static constexpr std::array<core::PropertyReference, 0> Properties = {};
EXTENSIONAPI static constexpr auto ProvidedInterfaces =
std::to_array<core::ProvidedInterface>({core::createProvidedInterface<CanFlyControllerApi, DuckController>(),
core::createProvidedInterface<NumberOfLegsControllerApi, DuckController>()});
using ControllerServiceImpl::ControllerServiceImpl;
MinifiStatus enableImpl(api::core::ControllerServiceContext&) override { return MINIFI_STATUS_SUCCESS; }

uint8_t numberOfLegs() const override { return 2; }
bool canFly() const override { return true; }
};
} // namespace org::apache::nifi::minifi::api_sandbox
31 changes: 31 additions & 0 deletions extensions/stable-api-sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

if (NOT ENABLE_TEST_PROCESSORS)
return()
endif ()

include(${CMAKE_SOURCE_DIR}/extensions/ExtensionHeader.txt)
file(GLOB SOURCES "*.cpp")

add_minifi_library(minifi-stable-api-sandbox SHARED ${SOURCES})

target_link_libraries(minifi-stable-api-sandbox minifi-cpp-extension-lib)

register_c_api_extension(minifi-stable-api-sandbox "Stable API Sandbox" SANDBOX-EXTENSION "Stable API Sandbox" "extensions/stable-api-sandbox/tests")
38 changes: 38 additions & 0 deletions extensions/stable-api-sandbox/ExtensionInitializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "AnimalControllerServices.h"
#include "ZooProcessor.h"
#include "api/core/Resource.h"
#include "api/utils/minifi-c-utils.h"

#define MKSOC(x) #x
#define MAKESTRING(x) MKSOC(x) // NOLINT(cppcoreguidelines-macro-usage)

namespace minifi = org::apache::nifi::minifi;

CEXTENSIONAPI const uint32_t MinifiApiVersion = MINIFI_API_VERSION;

CEXTENSIONAPI void MinifiInitExtension(MinifiExtensionContext* extension_context) {
const MinifiExtensionDefinition extension_definition{.name = minifi::api::utils::minifiStringView(MAKESTRING(EXTENSION_NAME)),
.version = minifi::api::utils::minifiStringView(MAKESTRING(EXTENSION_VERSION)),
.deinit = nullptr,
.user_data = nullptr};
auto* extension = MinifiRegisterExtension(extension_context, &extension_definition);
minifi::api::core::registerProcessors<minifi::api_sandbox::ZooProcessor>(extension);
minifi::api::core::registerControllerServices<minifi::api_sandbox::DogController, minifi::api_sandbox::DuckController>(extension);
}
46 changes: 46 additions & 0 deletions extensions/stable-api-sandbox/ZooProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ZooProcessor.h"

#include "AnimalControllerServices.h"
#include "api/core/ProcessContext.h"

namespace org::apache::nifi::minifi::api_sandbox {

MinifiStatus ZooProcessor::onTriggerImpl(api::core::ProcessContext& process_context, api::core::ProcessSession&) {
if (const auto can_fly_opaque = process_context.getControllerService(CanFlyService)) {
if (*can_fly_opaque) {
const auto can_fly_controller_name = process_context.getProperty(CanFlyService, nullptr) | utils::orThrow("Should be here");
const CanFlyControllerApi* can_fly = reinterpret_cast<CanFlyControllerApi*>(*can_fly_opaque);
logger_->log_critical("Can {} fly? {}", can_fly_controller_name, can_fly->canFly());
}
}
if (const auto num_of_legs_opaque = process_context.getControllerService(NumberOfLegsService)) {
if (*num_of_legs_opaque) {
const auto num_of_legs_name = process_context.getProperty(NumberOfLegsService, nullptr) | utils::orThrow("Should be here");
const NumberOfLegsControllerApi* num_legs = reinterpret_cast<NumberOfLegsControllerApi*>(*num_of_legs_opaque);
logger_->log_critical("{} has {} legs", num_of_legs_name, num_legs->numberOfLegs());
}
}
return MINIFI_STATUS_SUCCESS;
}

MinifiStatus ZooProcessor::onScheduleImpl(api::core::ProcessContext& process_context) {
return ProcessorImpl::onScheduleImpl(process_context);
}
} // namespace org::apache::nifi::minifi::api_sandbox
63 changes: 63 additions & 0 deletions extensions/stable-api-sandbox/ZooProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "AnimalControllerServiceApis.h"
#include "api/core/ProcessorImpl.h"
#include "api/utils/Export.h"
#include "core/PropertyDefinitionBuilder.h"
#include "minifi-cpp/core/Annotation.h"

namespace org::apache::nifi::minifi::api_sandbox {

class ZooProcessor : public api::core::ProcessorImpl {
public:
EXTENSIONAPI static constexpr const char* Description = "Test ZooProcessor";

EXTENSIONAPI static constexpr auto CanFlyService = core::PropertyDefinitionBuilder<>::createProperty("Can fly service")
.withDescription("Test CanFlyService")
.isRequired(true)
.withAllowedTypes<CanFlyControllerApi>()
.build();

EXTENSIONAPI static constexpr auto NumberOfLegsService = core::PropertyDefinitionBuilder<>::createProperty("Number of legs service")
.withDescription("Test NumberOfLegsService")
.isRequired(true)
.withAllowedTypes<NumberOfLegsControllerApi>()
.build();

EXTENSIONAPI static constexpr auto Properties = std::to_array<core::PropertyReference>({
CanFlyService,
NumberOfLegsService,
});
EXTENSIONAPI static constexpr auto Success = core::RelationshipDefinition{"success", "success"};
EXTENSIONAPI static constexpr auto Failure = core::RelationshipDefinition{"failure", "failure"};
EXTENSIONAPI static constexpr auto Relationships = std::array{Success, Failure};
EXTENSIONAPI static constexpr bool SupportsDynamicProperties = false;
EXTENSIONAPI static constexpr bool SupportsDynamicRelationships = false;
EXTENSIONAPI static constexpr core::annotation::Input InputRequirement = core::annotation::Input::INPUT_FORBIDDEN;
EXTENSIONAPI static constexpr bool IsSingleThreaded = true;

using ProcessorImpl::ProcessorImpl;

protected:
MinifiStatus onTriggerImpl(api::core::ProcessContext&, api::core::ProcessSession&) override;
MinifiStatus onScheduleImpl(api::core::ProcessContext&) override;
};

} // namespace org::apache::nifi::minifi::api_sandbox
35 changes: 35 additions & 0 deletions extensions/stable-api-sandbox/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

file(GLOB SOURCES "*.cpp")

SET(EXTENSIONS_TEST_COUNT 0)
FOREACH (testfile ${SOURCES})
get_filename_component(testfilename "${testfile}" NAME_WE)
add_minifi_executable(${testfilename} "${testfile}")
target_include_directories(${testfilename} BEFORE PRIVATE "${CMAKE_SOURCE_DIR}/libminifi/include")
target_include_directories(${testfilename} BEFORE PRIVATE "${CMAKE_SOURCE_DIR}/extensions/stable-api-sandbox")
createTests(${testfilename})
target_link_libraries(${testfilename} Catch2WithMain)
target_link_libraries(${testfilename} minifi-stable-api-sandbox)
target_link_libraries(${testfilename} libminifi-c-unittest)

MATH(EXPR EXTENSIONS_TEST_COUNT "${EXTENSIONS_TEST_COUNT}+1")
add_test(NAME ${testfilename} COMMAND ${testfilename} WORKING_DIRECTORY ${TEST_DIR})
ENDFOREACH ()
Loading
Loading