Search in sources :

Example 16 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class TestStandardValidators method testNonEmptyELValidator.

@Test
public void testNonEmptyELValidator() {
    Validator val = StandardValidators.NON_EMPTY_EL_VALIDATOR;
    ValidationContext vc = mock(ValidationContext.class);
    Mockito.when(vc.isExpressionLanguageSupported("foo")).thenReturn(true);
    ValidationResult vr = val.validate("foo", "", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "    h", vc);
    assertTrue(vr.isValid());
    Mockito.when(vc.isExpressionLanguagePresent("${test}")).thenReturn(true);
    vr = val.validate("foo", "${test}", vc);
    assertTrue(vr.isValid());
    vr = val.validate("foo", "${test", vc);
    assertTrue(vr.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 17 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class TestStandardValidators method testTimePeriodValidator.

@Test
public void testTimePeriodValidator() {
    Validator val = StandardValidators.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    ValidationResult vr;
    final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
    vr = val.validate("TimePeriodTest", "0 sense made", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", null, validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "0 secs", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "999 millis", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "999999999 nanos", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "1 sec", validationContext);
    assertTrue(vr.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 18 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class NotificationServiceManager method notify.

public void notify(final NotificationType type, final String subject, final String message) {
    final List<ConfiguredNotificationService> configs = servicesByNotificationType.get(type);
    if (configs == null || configs.isEmpty()) {
        return;
    }
    for (final ConfiguredNotificationService config : configs) {
        final NotificationService service = config.getService();
        final AtomicInteger attemptCount = new AtomicInteger(0);
        notificationExecutor.submit(new Runnable() {

            @Override
            public void run() {
                // Check if the service is valid; if not, warn now so that users know this before they fail to receive notifications
                final ValidationContext validationContext = new NotificationValidationContext(buildNotificationContext(config), variableRegistry);
                final Collection<ValidationResult> validationResults = service.validate(validationContext);
                final List<String> invalidReasons = new ArrayList<>();
                for (final ValidationResult result : validationResults) {
                    if (!result.isValid()) {
                        invalidReasons.add(result.toString());
                    }
                }
                // If the service is valid, attempt to send the notification
                boolean failure = false;
                if (invalidReasons.isEmpty()) {
                    final NotificationContext context = buildNotificationContext(config);
                    try {
                        service.notify(context, type, subject, message);
                        logger.info("Successfully sent notification of type {} to {}", type, service);
                    } catch (final Throwable t) {
                        // keep running even if a Throwable is caught because we need to ensure that we are able to restart NiFi
                        logger.error("Failed to send notification of type {} to {} with Subject {} due to {}. Will ", type, service == null ? "Unknown Notification Service" : service.toString(), subject, t.toString());
                        logger.error("", t);
                        failure = true;
                    }
                } else {
                    logger.warn("Notification Service {} is not valid for the following reasons: {}", service, invalidReasons);
                    failure = true;
                }
                final int attempts = attemptCount.incrementAndGet();
                if (failure) {
                    if (attempts < maxAttempts) {
                        logger.info("After failing to send notification to {} {} times, will attempt again in 1 minute", service, attempts);
                        notificationExecutor.schedule(this, 1, TimeUnit.MINUTES);
                    } else {
                        logger.info("After failing to send notification of type {} to {} {} times, will no longer attempt to send notification", type, service, attempts);
                    }
                }
            }
        });
        if (NotificationType.NIFI_STOPPED.equals(type)) {
            // we don't want to return before the notifier has had a chance to perform its task.
            while (attemptCount.get() == 0) {
                try {
                    Thread.sleep(1000L);
                } catch (final InterruptedException ie) {
                }
            }
        }
    }
}
Also used : NotificationValidationContext(org.apache.nifi.bootstrap.notification.NotificationValidationContext) NotificationService(org.apache.nifi.bootstrap.notification.NotificationService) ValidationResult(org.apache.nifi.components.ValidationResult) NotificationValidationContext(org.apache.nifi.bootstrap.notification.NotificationValidationContext) ValidationContext(org.apache.nifi.components.ValidationContext) NotificationContext(org.apache.nifi.bootstrap.notification.NotificationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 19 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class StandardProcessorNode method isValid.

@Override
public boolean isValid() {
    try {
        final ValidationContext validationContext = getValidationContext();
        final Collection<ValidationResult> validationResults = super.validate(validationContext);
        for (final ValidationResult result : validationResults) {
            if (!result.isValid()) {
                return false;
            }
        }
        for (final Relationship undef : getUndefinedRelationships()) {
            if (!isAutoTerminated(undef)) {
                return false;
            }
        }
        switch(getInputRequirement()) {
            case INPUT_ALLOWED:
                break;
            case INPUT_FORBIDDEN:
                {
                    if (!getIncomingNonLoopConnections().isEmpty()) {
                        return false;
                    }
                    break;
                }
            case INPUT_REQUIRED:
                {
                    if (getIncomingNonLoopConnections().isEmpty()) {
                        return false;
                    }
                    break;
                }
        }
    } catch (final Throwable t) {
        LOG.warn("Failed during validation", t);
        return false;
    }
    return true;
}
Also used : Relationship(org.apache.nifi.processor.Relationship) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 20 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class StandardProcessorNode method getValidationErrors.

@Override
public Collection<ValidationResult> getValidationErrors() {
    final List<ValidationResult> results = new ArrayList<>();
    try {
        // we are willing to make in order to save on validation costs that would be unnecessary most of the time.
        if (getScheduledState() == ScheduledState.STOPPED) {
            final ValidationContext validationContext = getValidationContext();
            final Collection<ValidationResult> validationResults = super.validate(validationContext);
            for (final ValidationResult result : validationResults) {
                if (!result.isValid()) {
                    results.add(result);
                }
            }
            for (final Relationship relationship : getUndefinedRelationships()) {
                if (!isAutoTerminated(relationship)) {
                    final ValidationResult error = new ValidationResult.Builder().explanation("Relationship '" + relationship.getName() + "' is not connected to any component and is not auto-terminated").subject("Relationship " + relationship.getName()).valid(false).build();
                    results.add(error);
                }
            }
            switch(getInputRequirement()) {
                case INPUT_ALLOWED:
                    break;
                case INPUT_FORBIDDEN:
                    {
                        final int incomingConnCount = getIncomingNonLoopConnections().size();
                        if (incomingConnCount != 0) {
                            results.add(new ValidationResult.Builder().explanation("Processor does not allow upstream connections but currently has " + incomingConnCount).subject("Upstream Connections").valid(false).build());
                        }
                        break;
                    }
                case INPUT_REQUIRED:
                    {
                        if (getIncomingNonLoopConnections().isEmpty()) {
                            results.add(new ValidationResult.Builder().explanation("Processor requires an upstream connection but currently has none").subject("Upstream Connections").valid(false).build());
                        }
                        break;
                    }
            }
        }
    } catch (final Throwable t) {
        results.add(new ValidationResult.Builder().explanation("Failed to run validation due to " + t.toString()).valid(false).build());
    }
    return results;
}
Also used : Relationship(org.apache.nifi.processor.Relationship) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Aggregations

ValidationContext (org.apache.nifi.components.ValidationContext)40 ValidationResult (org.apache.nifi.components.ValidationResult)37 Validator (org.apache.nifi.components.Validator)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)9 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)7 Collection (java.util.Collection)6 File (java.io.File)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 Collections (java.util.Collections)4 Set (java.util.Set)4 PropertyValue (org.apache.nifi.components.PropertyValue)4 MockStateManager (org.apache.nifi.state.MockStateManager)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)3 Relationship (org.apache.nifi.processor.Relationship)3 FileInputStream (java.io.FileInputStream)2