Search in sources :

Example 71 with ValidationResult

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

Example 72 with ValidationResult

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

the class StandardProcessorNode method verifyCanStart.

@Override
public void verifyCanStart(final Set<ControllerServiceNode> ignoredReferences) {
    final ScheduledState currentState = getPhysicalScheduledState();
    if (currentState != ScheduledState.STOPPED && currentState != ScheduledState.DISABLED) {
        throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is not stopped. Current state is " + currentState.name());
    }
    verifyNoActiveThreads();
    if (ignoredReferences != null) {
        final Set<String> ids = new HashSet<>();
        for (final ControllerServiceNode node : ignoredReferences) {
            ids.add(node.getIdentifier());
        }
        final Collection<ValidationResult> validationResults = getValidationErrors(ids);
        for (final ValidationResult result : validationResults) {
            if (!result.isValid()) {
                throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is not valid: " + result);
            }
        }
    } else {
        if (!isValid()) {
            throw new IllegalStateException(this.getIdentifier() + " is not in a valid state");
        }
    }
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ValidationResult(org.apache.nifi.components.ValidationResult) HashSet(java.util.HashSet)

Example 73 with ValidationResult

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

the class AbstractReportingTaskNode method verifyCanStart.

@Override
public void verifyCanStart(final Set<ControllerServiceNode> ignoredReferences) {
    switch(getScheduledState()) {
        case DISABLED:
            throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is disabled");
        case RUNNING:
            throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is already running");
        case STOPPED:
            break;
    }
    final int activeThreadCount = getActiveThreadCount();
    if (activeThreadCount > 0) {
        throw new IllegalStateException(this.getIdentifier() + " cannot be started because it has " + activeThreadCount + " active threads already");
    }
    final Set<String> ids = new HashSet<>();
    for (final ControllerServiceNode node : ignoredReferences) {
        ids.add(node.getIdentifier());
    }
    final Collection<ValidationResult> validationResults = getValidationErrors(ids);
    for (final ValidationResult result : validationResults) {
        if (!result.isValid()) {
            throw new IllegalStateException(this.getIdentifier() + " cannot be started because it is not valid: " + result);
        }
    }
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ValidationResult(org.apache.nifi.components.ValidationResult) HashSet(java.util.HashSet)

Example 74 with ValidationResult

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

the class StandardControllerServiceProvider method createGhostControllerService.

private ControllerServiceNode createGhostControllerService(final String type, final String id, final BundleCoordinate bundleCoordinate) {
    final ControllerServiceInvocationHandler invocationHandler = new ControllerServiceInvocationHandler() {

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            final String methodName = method.getName();
            if ("validate".equals(methodName)) {
                final ValidationResult result = new ValidationResult.Builder().input("Any Property").subject("Missing Controller Service").valid(false).explanation("Controller Service could not be created because the Controller Service Type (" + type + ") could not be found").build();
                return Collections.singleton(result);
            } else if ("getPropertyDescriptor".equals(methodName)) {
                final String propertyName = (String) args[0];
                return new PropertyDescriptor.Builder().name(propertyName).description(propertyName).sensitive(true).required(true).build();
            } else if ("getPropertyDescriptors".equals(methodName)) {
                return Collections.emptyList();
            } else if ("onPropertyModified".equals(methodName)) {
                return null;
            } else if ("getIdentifier".equals(methodName)) {
                return id;
            } else if ("toString".equals(methodName)) {
                return "GhostControllerService[id=" + id + ", type=" + type + "]";
            } else if ("hashCode".equals(methodName)) {
                return 91 * type.hashCode() + 41 * id.hashCode();
            } else if ("equals".equals(methodName)) {
                return proxy == args[0];
            } else {
                throw new IllegalStateException("Controller Service could not be created because the Controller Service Type (" + type + ") could not be found");
            }
        }

        @Override
        public void setServiceNode(ControllerServiceNode serviceNode) {
        // nothing to do
        }
    };
    final ControllerService proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { ControllerService.class }, invocationHandler);
    final String simpleClassName = type.contains(".") ? StringUtils.substringAfterLast(type, ".") : type;
    final String componentType = "(Missing) " + simpleClassName;
    final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>(proxiedService, bundleCoordinate, null);
    final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry(this.variableRegistry);
    final ControllerServiceNode serviceNode = new StandardControllerServiceNode(proxiedLoggableComponent, proxiedLoggableComponent, invocationHandler, id, new StandardValidationContextFactory(this, variableRegistry), this, componentType, type, componentVarRegistry, flowController, true);
    serviceCache.putIfAbsent(id, serviceNode);
    return serviceNode;
}
Also used : StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) Method(java.lang.reflect.Method) ValidationResult(org.apache.nifi.components.ValidationResult) ControllerService(org.apache.nifi.controller.ControllerService) StandardValidationContextFactory(org.apache.nifi.processor.StandardValidationContextFactory) LoggableComponent(org.apache.nifi.controller.LoggableComponent) StandardComponentVariableRegistry(org.apache.nifi.registry.variable.StandardComponentVariableRegistry) ComponentVariableRegistry(org.apache.nifi.registry.ComponentVariableRegistry)

Example 75 with ValidationResult

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

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