Search in sources :

Example 61 with ValidationResult

use of org.apache.nifi.components.ValidationResult 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 62 with ValidationResult

use of org.apache.nifi.components.ValidationResult 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 63 with ValidationResult

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

the class HiveConfigurator method validate.

public Collection<ValidationResult> validate(String configFiles, String principal, String keyTab, AtomicReference<ValidationResources> validationResourceHolder, ComponentLog log) {
    final List<ValidationResult> problems = new ArrayList<>();
    ValidationResources resources = validationResourceHolder.get();
    // then load the Configuration and set the new resources in the holder
    if (resources == null || !configFiles.equals(resources.getConfigResources())) {
        log.debug("Reloading validation resources");
        resources = new ValidationResources(configFiles, getConfigurationFromFiles(configFiles));
        validationResourceHolder.set(resources);
    }
    final Configuration hiveConfig = resources.getConfiguration();
    problems.addAll(KerberosProperties.validatePrincipalAndKeytab(this.getClass().getSimpleName(), hiveConfig, principal, keyTab, log));
    return problems;
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 64 with ValidationResult

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

the class HiveConnectionPool method customValidate.

@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    boolean confFileProvided = validationContext.getProperty(HIVE_CONFIGURATION_RESOURCES).isSet();
    final List<ValidationResult> problems = new ArrayList<>();
    if (confFileProvided) {
        final String explicitPrincipal = validationContext.getProperty(kerberosProperties.getKerberosPrincipal()).evaluateAttributeExpressions().getValue();
        final String explicitKeytab = validationContext.getProperty(kerberosProperties.getKerberosKeytab()).evaluateAttributeExpressions().getValue();
        final KerberosCredentialsService credentialsService = validationContext.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);
        final String resolvedPrincipal;
        final String resolvedKeytab;
        if (credentialsService == null) {
            resolvedPrincipal = explicitPrincipal;
            resolvedKeytab = explicitKeytab;
        } else {
            resolvedPrincipal = credentialsService.getPrincipal();
            resolvedKeytab = credentialsService.getKeytab();
        }
        final String configFiles = validationContext.getProperty(HIVE_CONFIGURATION_RESOURCES).evaluateAttributeExpressions().getValue();
        problems.addAll(hiveConfigurator.validate(configFiles, resolvedPrincipal, resolvedKeytab, validationResourceHolder, getLogger()));
        if (credentialsService != null && (explicitPrincipal != null || explicitKeytab != null)) {
            problems.add(new ValidationResult.Builder().subject("Kerberos Credentials").valid(false).explanation("Cannot specify both a Kerberos Credentials Service and a principal/keytab").build());
        }
        final String allowExplicitKeytabVariable = System.getenv(ALLOW_EXPLICIT_KEYTAB);
        if ("false".equalsIgnoreCase(allowExplicitKeytabVariable) && (explicitPrincipal != null || explicitKeytab != null)) {
            problems.add(new ValidationResult.Builder().subject("Kerberos Credentials").valid(false).explanation("The '" + ALLOW_EXPLICIT_KEYTAB + "' system environment variable is configured to forbid explicitly configuring principal/keytab in processors. " + "The Kerberos Credentials Service should be used instead of setting the Kerberos Keytab or Kerberos Principal property.").build());
        }
    }
    return problems;
}
Also used : KerberosCredentialsService(org.apache.nifi.kerberos.KerberosCredentialsService) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 65 with ValidationResult

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

the class AbstractCredentialsStrategy method validate.

@Override
public Collection<ValidationResult> validate(final ValidationContext validationContext, final CredentialsStrategy primaryStrategy) {
    boolean thisIsSelectedStrategy = this == primaryStrategy;
    String requiredMessageFormat = "property %1$s must be set with %2$s";
    String excludedMessageFormat = "property %1$s cannot be used with %2$s";
    String failureFormat = thisIsSelectedStrategy ? requiredMessageFormat : excludedMessageFormat;
    Collection<ValidationResult> validationFailureResults = null;
    for (PropertyDescriptor requiredProperty : requiredProperties) {
        boolean requiredPropertyIsSet = validationContext.getProperty(requiredProperty).isSet();
        if (requiredPropertyIsSet != thisIsSelectedStrategy) {
            String message = String.format(failureFormat, requiredProperty.getDisplayName(), primaryStrategy.getName());
            if (validationFailureResults == null) {
                validationFailureResults = new ArrayList<>();
            }
            validationFailureResults.add(new ValidationResult.Builder().subject(requiredProperty.getDisplayName()).valid(false).explanation(message).build());
        }
    }
    return validationFailureResults;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ValidationResult(org.apache.nifi.components.ValidationResult)

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)179 Test (org.junit.Test)80 ArrayList (java.util.ArrayList)64 TestRunner (org.apache.nifi.util.TestRunner)46 ValidationContext (org.apache.nifi.components.ValidationContext)37 MockProcessContext (org.apache.nifi.util.MockProcessContext)26 TdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.api.TdchConnectionService)23 Validator (org.apache.nifi.components.Validator)21 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)20 DevTdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.core.controllerservice.DevTdchConnectionService)18 DummyTdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.core.controllerservice.DummyTdchConnectionService)18 HashSet (java.util.HashSet)18 ProcessContext (org.apache.nifi.processor.ProcessContext)15 File (java.io.File)12 HashMap (java.util.HashMap)12 Collection (java.util.Collection)11 SSLContextService (org.apache.nifi.ssl.SSLContextService)11 Map (java.util.Map)10 List (java.util.List)9 ComponentLog (org.apache.nifi.logging.ComponentLog)9