Search in sources :

Example 16 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class ControllerServiceInitializer method teardown.

@Override
public void teardown(ConfigurableComponent component) {
    try (NarCloseable narCloseable = NarCloseable.withComponentNarLoader(component.getClass(), component.getIdentifier())) {
        ControllerService controllerService = (ControllerService) component;
        final ComponentLog logger = new MockComponentLogger();
        final MockConfigurationContext context = new MockConfigurationContext();
        ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnShutdown.class, controllerService, logger, context);
    } finally {
        ExtensionManager.removeInstanceClassLoader(component.getIdentifier());
    }
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) MockConfigurationContext(org.apache.nifi.mock.MockConfigurationContext) MockComponentLogger(org.apache.nifi.mock.MockComponentLogger) ControllerService(org.apache.nifi.controller.ControllerService) ComponentLog(org.apache.nifi.logging.ComponentLog)

Example 17 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class HtmlDocumentationWriter method writeValidValues.

/**
 * Interrogates a PropertyDescriptor to get a list of AllowableValues, if
 * there are none, nothing is written to the stream.
 *
 * @param xmlStreamWriter the stream writer to use
 * @param property the property to describe
 * @throws XMLStreamException thrown if there was a problem writing to the
 * XML Stream
 */
protected void writeValidValues(XMLStreamWriter xmlStreamWriter, PropertyDescriptor property) throws XMLStreamException {
    if (property.getAllowableValues() != null && property.getAllowableValues().size() > 0) {
        xmlStreamWriter.writeStartElement("ul");
        for (AllowableValue value : property.getAllowableValues()) {
            xmlStreamWriter.writeStartElement("li");
            xmlStreamWriter.writeCharacters(value.getDisplayName());
            if (value.getDescription() != null) {
                writeValidValueDescription(xmlStreamWriter, value.getDescription());
            }
            xmlStreamWriter.writeEndElement();
        }
        xmlStreamWriter.writeEndElement();
    } else if (property.getControllerServiceDefinition() != null) {
        Class<? extends ControllerService> controllerServiceClass = property.getControllerServiceDefinition();
        writeSimpleElement(xmlStreamWriter, "strong", "Controller Service API: ");
        xmlStreamWriter.writeEmptyElement("br");
        xmlStreamWriter.writeCharacters(controllerServiceClass.getSimpleName());
        final List<Class<? extends ControllerService>> implementationList = lookupControllerServiceImpls(controllerServiceClass);
        // Convert it into an array before proceeding
        Class<? extends ControllerService>[] implementations = implementationList.stream().toArray(Class[]::new);
        xmlStreamWriter.writeEmptyElement("br");
        if (implementations.length > 0) {
            final String title = implementations.length > 1 ? "Implementations: " : "Implementation: ";
            writeSimpleElement(xmlStreamWriter, "strong", title);
            iterateAndLinkComponents(xmlStreamWriter, implementations, null, "<br>", controllerServiceClass.getSimpleName());
        } else {
            xmlStreamWriter.writeCharacters("No implementations found.");
        }
    }
}
Also used : AllowableValue(org.apache.nifi.components.AllowableValue) ArrayList(java.util.ArrayList) List(java.util.List) ControllerService(org.apache.nifi.controller.ControllerService)

Example 18 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class HtmlDocumentationWriterTest method testControllerServiceWithLogger.

@Test
public void testControllerServiceWithLogger() throws InitializationException, IOException {
    ControllerService controllerService = new ControllerServiceWithLogger();
    controllerService.initialize(new MockControllerServiceInitializationContext());
    DocumentationWriter writer = new HtmlDocumentationWriter();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.write(controllerService, baos, false);
    String results = new String(baos.toByteArray());
    XmlValidator.assertXmlValid(results);
}
Also used : MockControllerServiceInitializationContext(org.apache.nifi.mock.MockControllerServiceInitializationContext) ControllerServiceWithLogger(org.apache.nifi.documentation.example.ControllerServiceWithLogger) DocumentationWriter(org.apache.nifi.documentation.DocumentationWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ControllerService(org.apache.nifi.controller.ControllerService) FullyDocumentedControllerService(org.apache.nifi.documentation.example.FullyDocumentedControllerService) Test(org.junit.Test)

Example 19 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class PropertyDescriptor method validate.

/**
 * Validates the given input against this property descriptor's validator.
 * If this descriptor has a set of allowable values then the given value is
 * only checked against the allowable values.
 *
 * @param input the value to validate
 * @param context the context of validation
 * @return the result of validating the input
 */
public ValidationResult validate(final String input, final ValidationContext context) {
    ValidationResult lastResult = Validator.INVALID.validate(this.name, input, context);
    if (allowableValues != null && !allowableValues.isEmpty()) {
        final ConstrainedSetValidator csValidator = new ConstrainedSetValidator(allowableValues);
        final ValidationResult csResult = csValidator.validate(this.name, input, context);
        if (csResult.isValid()) {
            lastResult = csResult;
        } else {
            return csResult;
        }
    }
    // if the property descriptor identifies a Controller Service, validate that the ControllerService exists, is of the correct type, and is valid
    if (controllerServiceDefinition != null) {
        final Set<String> validIdentifiers = context.getControllerServiceLookup().getControllerServiceIdentifiers(controllerServiceDefinition);
        if (validIdentifiers != null && validIdentifiers.contains(input)) {
            final ControllerService controllerService = context.getControllerServiceLookup().getControllerService(input);
            if (!context.isValidationRequired(controllerService)) {
                return new ValidationResult.Builder().input(input).subject(getName()).valid(true).build();
            }
            final String serviceId = controllerService.getIdentifier();
            if (!isDependentServiceEnableable(context, serviceId)) {
                return new ValidationResult.Builder().input(context.getControllerServiceLookup().getControllerServiceName(serviceId)).subject(getName()).valid(false).explanation("Controller Service " + controllerService + " is disabled").build();
            }
            final Collection<ValidationResult> validationResults = controllerService.validate(context.getControllerServiceValidationContext(controllerService));
            final List<ValidationResult> invalidResults = new ArrayList<>();
            for (final ValidationResult result : validationResults) {
                if (!result.isValid()) {
                    invalidResults.add(result);
                }
            }
            if (!invalidResults.isEmpty()) {
                return new ValidationResult.Builder().input(input).subject(getName()).valid(false).explanation("Controller Service is not valid: " + (invalidResults.size() > 1 ? invalidResults : invalidResults.get(0))).build();
            }
            return new ValidationResult.Builder().input(input).subject(getName()).valid(true).build();
        } else {
            return new ValidationResult.Builder().input(input).subject(getName()).valid(false).explanation("Invalid Controller Service: " + input + " is not a valid Controller Service Identifier or does not reference the correct type of Controller Service").build();
        }
    }
    for (final Validator validator : validators) {
        lastResult = validator.validate(this.name, input, context);
        if (!lastResult.isValid()) {
            break;
        }
    }
    return lastResult;
}
Also used : ArrayList(java.util.ArrayList) ControllerService(org.apache.nifi.controller.ControllerService)

Example 20 with ControllerService

use of org.apache.nifi.controller.ControllerService in project nifi by apache.

the class MetricsReportingTaskTest method setUp.

/**
 * Set up the test environment and mock behaviour. This includes registering {@link #reporterServiceStub} in the
 * different contexts, overriding {@link MetricsReportingTask#currentStatusReference} and instantiating the test
 * subject.
 */
@Before
public void setUp() throws Exception {
    Map<String, ControllerService> services = new HashMap<>();
    services.put(REPORTER_SERVICE_IDENTIFIER, reporterServiceStub);
    testedReportingTask = new MetricsReportingTask();
    reportingContextStub = new MockReportingContext(services, new MockStateManager(testedReportingTask), new MockVariableRegistry());
    rootGroupStatus = new ProcessGroupStatus();
    innerGroupStatus = new ProcessGroupStatus();
    when(reporterServiceStub.createReporter(any())).thenReturn(reporterMock);
    when(reporterServiceStub.getIdentifier()).thenReturn(REPORTER_SERVICE_IDENTIFIER);
    reportingContextStub.setProperty(MetricsReportingTask.REPORTER_SERVICE.getName(), REPORTER_SERVICE_IDENTIFIER);
    reportingContextStub.addControllerService(reporterServiceStub, REPORTER_SERVICE_IDENTIFIER);
    configurationContextStub = new MockConfigurationContext(reportingContextStub.getProperties(), reportingContextStub.getControllerServiceLookup());
    reportingInitContextStub = new MockReportingInitializationContext(TEST_INIT_CONTEXT_ID, TEST_INIT_CONTEXT_NAME, new MockComponentLog(TEST_TASK_ID, testedReportingTask));
}
Also used : ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) HashMap(java.util.HashMap) MockStateManager(org.apache.nifi.state.MockStateManager) MockComponentLog(org.apache.nifi.util.MockComponentLog) MockVariableRegistry(org.apache.nifi.util.MockVariableRegistry) MockReportingInitializationContext(org.apache.nifi.util.MockReportingInitializationContext) ControllerService(org.apache.nifi.controller.ControllerService) MockReportingContext(org.apache.nifi.util.MockReportingContext) Before(org.junit.Before)

Aggregations

ControllerService (org.apache.nifi.controller.ControllerService)25 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)6 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)6 NarCloseable (org.apache.nifi.nar.NarCloseable)6 List (java.util.List)5 Map (java.util.Map)5 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 LinkedHashSet (java.util.LinkedHashSet)4 Set (java.util.Set)4 TimeUnit (java.util.concurrent.TimeUnit)4 Collectors (java.util.stream.Collectors)4 Bundle (org.apache.nifi.bundle.Bundle)4 ValidationResult (org.apache.nifi.components.ValidationResult)4 Connectable (org.apache.nifi.connectable.Connectable)4 Connection (org.apache.nifi.connectable.Connection)4 Port (org.apache.nifi.connectable.Port)4 ProcessorNode (org.apache.nifi.controller.ProcessorNode)4 Label (org.apache.nifi.controller.label.Label)4