Search in sources :

Example 21 with ValidationContext

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

the class StandardStateManagerProvider method createStateProvider.

private static StateProvider createStateProvider(final File configFile, final Scope scope, final NiFiProperties properties, final VariableRegistry variableRegistry) throws ConfigParseException, IOException {
    final String providerId;
    final String providerIdPropertyName;
    final String providerDescription;
    final String providerXmlElementName;
    final String oppositeScopeXmlElementName;
    switch(scope) {
        case CLUSTER:
            providerId = properties.getClusterStateProviderId();
            providerIdPropertyName = NiFiProperties.STATE_MANAGEMENT_CLUSTER_PROVIDER_ID;
            providerDescription = "Cluster State Provider";
            providerXmlElementName = "cluster-provider";
            oppositeScopeXmlElementName = "local-provider";
            break;
        case LOCAL:
            providerId = properties.getLocalStateProviderId();
            providerIdPropertyName = NiFiProperties.STATE_MANAGEMENT_LOCAL_PROVIDER_ID;
            providerDescription = "Local State Provider";
            providerXmlElementName = "local-provider";
            oppositeScopeXmlElementName = "cluster-provider";
            break;
        default:
            throw new AssertionError("Attempted to create State Provider for unknown Scope: " + scope);
    }
    if (!configFile.exists()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the State Management Configuration File " + configFile + " does not exist");
    }
    if (!configFile.canRead()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the State Management Configuration File " + configFile + " cannot be read");
    }
    if (providerId == null) {
        if (scope == Scope.CLUSTER) {
            throw new IllegalStateException("Cannot create Cluster State Provider because the '" + providerIdPropertyName + "' property is missing from the NiFi Properties file. In order to run NiFi in a cluster, the " + providerIdPropertyName + " property must be configured in nifi.properties");
        }
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property is missing from the NiFi Properties file");
    }
    if (providerId.trim().isEmpty()) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file has no value set. This is a required property and must reference the identifier of one of the " + providerXmlElementName + " elements in the State Management Configuration File (" + configFile + ")");
    }
    final StateManagerConfiguration config = StateManagerConfiguration.parse(configFile);
    final StateProviderConfiguration providerConfig = config.getStateProviderConfiguration(providerId);
    if (providerConfig == null) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file is set to '" + providerId + "', but there is no " + providerXmlElementName + " entry in the State Management Configuration File (" + configFile + ") with this id");
    }
    if (providerConfig.getScope() != scope) {
        throw new IllegalStateException("Cannot create " + providerDescription + " because the '" + providerIdPropertyName + "' property in the NiFi Properties file is set to '" + providerId + "', but this id is assigned to a " + oppositeScopeXmlElementName + " entry in the State Management Configuration File (" + configFile + "), rather than a " + providerXmlElementName + " entry");
    }
    final String providerClassName = providerConfig.getClassName();
    final StateProvider provider;
    try {
        provider = instantiateStateProvider(providerClassName);
    } catch (final Exception e) {
        throw new RuntimeException("Cannot create " + providerDescription + " of type " + providerClassName, e);
    }
    if (!ArrayUtils.contains(provider.getSupportedScopes(), scope)) {
        throw new RuntimeException("Cannot use " + providerDescription + " (" + providerClassName + ") as it only supports scope(s) " + ArrayUtils.toString(provider.getSupportedScopes()) + " but " + "instance" + " is configured to use scope " + scope);
    }
    // create variable registry
    final Map<PropertyDescriptor, PropertyValue> propertyMap = new HashMap<>();
    final Map<PropertyDescriptor, String> propertyStringMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : provider.getPropertyDescriptors()) {
        propertyMap.put(descriptor, new StandardPropertyValue(descriptor.getDefaultValue(), null, variableRegistry));
        propertyStringMap.put(descriptor, descriptor.getDefaultValue());
    }
    for (final Map.Entry<String, String> entry : providerConfig.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = provider.getPropertyDescriptor(entry.getKey());
        propertyStringMap.put(descriptor, entry.getValue());
        propertyMap.put(descriptor, new StandardPropertyValue(entry.getValue(), null, variableRegistry));
    }
    final SSLContext sslContext = SslContextFactory.createSslContext(properties, false);
    final ComponentLog logger = new SimpleProcessLogger(providerId, provider);
    final StateProviderInitializationContext initContext = new StandardStateProviderInitializationContext(providerId, propertyMap, sslContext, logger);
    synchronized (provider) {
        provider.initialize(initContext);
    }
    final ValidationContext validationContext = new StandardValidationContext(null, propertyStringMap, null, null, null, variableRegistry);
    final Collection<ValidationResult> results = provider.validate(validationContext);
    final StringBuilder validationFailures = new StringBuilder();
    int invalidCount = 0;
    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            validationFailures.append(result.toString()).append("\n");
            invalidCount++;
        }
    }
    if (invalidCount > 0) {
        throw new IllegalStateException("Could not initialize State Providers because the " + providerDescription + " is not valid. The following " + invalidCount + " Validation Errors occurred:\n" + validationFailures.toString() + "\nPlease check the configuration of the " + providerDescription + " with ID [" + providerId.trim() + "] in the file " + configFile.getAbsolutePath());
    }
    return provider;
}
Also used : StandardStateProviderInitializationContext(org.apache.nifi.controller.state.StandardStateProviderInitializationContext) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult) SimpleProcessLogger(org.apache.nifi.processor.SimpleProcessLogger) StateProviderInitializationContext(org.apache.nifi.components.state.StateProviderInitializationContext) StandardStateProviderInitializationContext(org.apache.nifi.controller.state.StandardStateProviderInitializationContext) StandardValidationContext(org.apache.nifi.processor.StandardValidationContext) StateManagerConfiguration(org.apache.nifi.controller.state.config.StateManagerConfiguration) StateProviderConfiguration(org.apache.nifi.controller.state.config.StateProviderConfiguration) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) StateProvider(org.apache.nifi.components.state.StateProvider) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) SSLContext(javax.net.ssl.SSLContext) ComponentLog(org.apache.nifi.logging.ComponentLog) IOException(java.io.IOException) ConfigParseException(org.apache.nifi.controller.state.ConfigParseException) ValidationContext(org.apache.nifi.components.ValidationContext) StandardValidationContext(org.apache.nifi.processor.StandardValidationContext) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StateMap(org.apache.nifi.components.state.StateMap)

Example 22 with ValidationContext

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

the class AbstractConfiguredComponent method getValidationContext.

protected ValidationContext getValidationContext() {
    while (true) {
        ValidationContext context = this.validationContext.get();
        if (context != null) {
            return context;
        }
        context = getValidationContextFactory().newValidationContext(getProperties(), getAnnotationData(), getProcessGroupIdentifier(), getIdentifier());
        final boolean updated = validationContext.compareAndSet(null, context);
        if (updated) {
            return context;
        }
    }
}
Also used : ValidationContext(org.apache.nifi.components.ValidationContext)

Example 23 with ValidationContext

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

the class AbstractConfiguredComponent method getValidationErrors.

public Collection<ValidationResult> getValidationErrors(final Set<String> serviceIdentifiersNotToValidate) {
    final List<ValidationResult> results = new ArrayList<>();
    lock.lock();
    try {
        final ValidationContext validationContext;
        if (serviceIdentifiersNotToValidate == null || serviceIdentifiersNotToValidate.isEmpty()) {
            validationContext = getValidationContext();
        } else {
            validationContext = getValidationContextFactory().newValidationContext(serviceIdentifiersNotToValidate, getProperties(), getAnnotationData(), getProcessGroupIdentifier(), getIdentifier());
        }
        final Collection<ValidationResult> validationResults;
        try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getComponent().getClass(), getComponent().getIdentifier())) {
            validationResults = getComponent().validate(validationContext);
        }
        for (final ValidationResult result : validationResults) {
            if (!result.isValid()) {
                results.add(result);
            }
        }
    } catch (final Throwable t) {
        logger.error("Failed to perform validation of " + this, t);
        results.add(new ValidationResult.Builder().explanation("Failed to run validation due to " + t.toString()).valid(false).build());
    } finally {
        lock.unlock();
    }
    return results;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 24 with ValidationContext

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

the class ThriftConnectionPool method createMultipleFilesExistValidator.

/**
 * Validates that one or more files exist, as specified in a single property.
 *
 * @return a validator which describes the result of the validation
 */
public static final Validator createMultipleFilesExistValidator() {
    return new Validator() {

        @Override
        public ValidationResult validate(String subject, String input, ValidationContext context) {
            final String[] files = input.split(",");
            for (String filename : files) {
                try {
                    final File file = new File(filename.trim());
                    final boolean valid = file.exists() && file.isFile();
                    if (!valid) {
                        final String message = "File " + file + " does not exist or is not a file";
                        return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(message).build();
                    }
                } catch (SecurityException e) {
                    final String message = "Unable to access " + filename + " due to " + e.getMessage();
                    return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(message).build();
                }
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 25 with ValidationContext

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

the class AbstractConfiguredComponent method validate.

@Override
public Collection<ValidationResult> validate(final ValidationContext context) {
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(getComponent().getClass(), getComponent().getIdentifier())) {
        final Collection<ValidationResult> validationResults = getComponent().validate(context);
        // validate selected controller services implement the API required by the processor
        final List<PropertyDescriptor> supportedDescriptors = getComponent().getPropertyDescriptors();
        if (null != supportedDescriptors) {
            for (final PropertyDescriptor descriptor : supportedDescriptors) {
                if (descriptor.getControllerServiceDefinition() == null) {
                    // skip properties that aren't for a controller service
                    continue;
                }
                final String controllerServiceId = context.getProperty(descriptor).getValue();
                if (controllerServiceId == null) {
                    // if the property value is null we should already have a validation error
                    continue;
                }
                final ControllerServiceNode controllerServiceNode = getControllerServiceProvider().getControllerServiceNode(controllerServiceId);
                if (controllerServiceNode == null) {
                    // if the node was null we should already have a validation error
                    continue;
                }
                final Class<? extends ControllerService> controllerServiceApiClass = descriptor.getControllerServiceDefinition();
                final ClassLoader controllerServiceApiClassLoader = controllerServiceApiClass.getClassLoader();
                final Consumer<String> addValidationError = explanation -> validationResults.add(new ValidationResult.Builder().input(controllerServiceId).subject(descriptor.getDisplayName()).valid(false).explanation(explanation).build());
                final Bundle controllerServiceApiBundle = ExtensionManager.getBundle(controllerServiceApiClassLoader);
                if (controllerServiceApiBundle == null) {
                    addValidationError.accept(String.format("Unable to find bundle for ControllerService API class %s.", controllerServiceApiClass.getCanonicalName()));
                    continue;
                }
                final BundleCoordinate controllerServiceApiCoordinate = controllerServiceApiBundle.getBundleDetails().getCoordinate();
                final Bundle controllerServiceBundle = ExtensionManager.getBundle(controllerServiceNode.getBundleCoordinate());
                if (controllerServiceBundle == null) {
                    addValidationError.accept(String.format("Unable to find bundle for coordinate %s.", controllerServiceNode.getBundleCoordinate()));
                    continue;
                }
                final BundleCoordinate controllerServiceCoordinate = controllerServiceBundle.getBundleDetails().getCoordinate();
                final boolean matchesApi = matchesApi(controllerServiceBundle, controllerServiceApiCoordinate);
                if (!matchesApi) {
                    final String controllerServiceType = controllerServiceNode.getComponentType();
                    final String controllerServiceApiType = controllerServiceApiClass.getSimpleName();
                    final String explanation = new StringBuilder().append(controllerServiceType).append(" - ").append(controllerServiceCoordinate.getVersion()).append(" from ").append(controllerServiceCoordinate.getGroup()).append(" - ").append(controllerServiceCoordinate.getId()).append(" is not compatible with ").append(controllerServiceApiType).append(" - ").append(controllerServiceApiCoordinate.getVersion()).append(" from ").append(controllerServiceApiCoordinate.getGroup()).append(" - ").append(controllerServiceApiCoordinate.getId()).toString();
                    addValidationError.accept(explanation);
                }
            }
        }
        return validationResults;
    }
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) URL(java.net.URL) ValidationContext(org.apache.nifi.components.ValidationContext) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigurableComponent(org.apache.nifi.components.ConfigurableComponent) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ClassLoaderUtils(org.apache.nifi.util.file.classloader.ClassLoaderUtils) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ControllerServiceProvider(org.apache.nifi.controller.service.ControllerServiceProvider) NarCloseable(org.apache.nifi.nar.NarCloseable) LinkedHashSet(java.util.LinkedHashSet) ValidationResult(org.apache.nifi.components.ValidationResult) CharacterFilterUtils(org.apache.nifi.util.CharacterFilterUtils) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) Lock(java.util.concurrent.locks.Lock) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry) ExtensionManager(org.apache.nifi.nar.ExtensionManager) Collections(java.util.Collections) NarCloseable(org.apache.nifi.nar.NarCloseable) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Bundle(org.apache.nifi.bundle.Bundle) ValidationResult(org.apache.nifi.components.ValidationResult) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode)

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