Search in sources :

Example 51 with BundleCoordinate

use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.

the class FingerprintFactory method addControllerServiceFingerprint.

private void addControllerServiceFingerprint(final StringBuilder builder, final ControllerServiceDTO dto) {
    builder.append(dto.getId());
    builder.append(dto.getVersionedComponentId());
    builder.append(dto.getType());
    builder.append(dto.getName());
    addBundleFingerprint(builder, dto.getBundle());
    builder.append(dto.getComments());
    builder.append(dto.getAnnotationData());
    builder.append(dto.getState());
    // get the temp instance of the ControllerService so that we know the default property values
    final BundleCoordinate coordinate = getCoordinate(dto.getType(), dto.getBundle());
    final ConfigurableComponent configurableComponent = ExtensionManager.getTempComponent(dto.getType(), coordinate);
    if (configurableComponent == null) {
        logger.warn("Unable to get ControllerService of type {}; its default properties will be fingerprinted instead of being ignored.", dto.getType());
    }
    addPropertiesFingerprint(builder, configurableComponent, dto.getProperties());
}
Also used : ConfigurableComponent(org.apache.nifi.components.ConfigurableComponent) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate)

Example 52 with BundleCoordinate

use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.

the class FingerprintFactory method addReportingTaskFingerprint.

private void addReportingTaskFingerprint(final StringBuilder builder, final ReportingTaskDTO dto) {
    builder.append(dto.getId());
    builder.append(dto.getType());
    builder.append(dto.getName());
    addBundleFingerprint(builder, dto.getBundle());
    builder.append(dto.getComments());
    builder.append(dto.getSchedulingPeriod());
    builder.append(dto.getSchedulingStrategy());
    builder.append(dto.getAnnotationData());
    // get the temp instance of the ReportingTask so that we know the default property values
    final BundleCoordinate coordinate = getCoordinate(dto.getType(), dto.getBundle());
    final ConfigurableComponent configurableComponent = ExtensionManager.getTempComponent(dto.getType(), coordinate);
    if (configurableComponent == null) {
        logger.warn("Unable to get ReportingTask of type {}; its default properties will be fingerprinted instead of being ignored.", dto.getType());
    }
    addPropertiesFingerprint(builder, configurableComponent, dto.getProperties());
}
Also used : ConfigurableComponent(org.apache.nifi.components.ConfigurableComponent) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate)

Example 53 with BundleCoordinate

use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.

the class HtmlDocumentationWriter method iterateAndLinkComponents.

/**
 * Writes a link to another configurable component
 *
 * @param xmlStreamWriter the xml stream writer
 * @param linkedComponents the array of configurable component to link to
 * @param classNames the array of class names in string format to link to
 * @param separator a separator used to split the values (in case more than 1. If the separator is enclosed in
 *                  between "<" and ">" (.e.g "<br>" it is treated as a tag and written to the xmlStreamWriter as an
 *                  empty tag
 * @param sourceContextName the source context/name of the item being linked
 * @throws XMLStreamException thrown if there is a problem writing the XML
 */
protected void iterateAndLinkComponents(final XMLStreamWriter xmlStreamWriter, final Class<? extends ConfigurableComponent>[] linkedComponents, final String[] classNames, final String separator, final String sourceContextName) throws XMLStreamException {
    String effectiveSeparator = separator;
    // Treat the the possible separators
    boolean separatorIsElement;
    if (effectiveSeparator.startsWith("<") && effectiveSeparator.endsWith(">")) {
        separatorIsElement = true;
    } else {
        separatorIsElement = false;
    }
    // Whatever the result, strip the possible < and > characters
    effectiveSeparator = effectiveSeparator.replaceAll("\\<([^>]*)>", "$1");
    int index = 0;
    for (final Class<? extends ConfigurableComponent> linkedComponent : linkedComponents) {
        final String linkedComponentName = linkedComponent.getName();
        final List<Bundle> linkedComponentBundles = ExtensionManager.getBundles(linkedComponentName);
        if (linkedComponentBundles != null && linkedComponentBundles.size() > 0) {
            final Bundle firstLinkedComponentBundle = linkedComponentBundles.get(0);
            final BundleCoordinate coordinate = firstLinkedComponentBundle.getBundleDetails().getCoordinate();
            final String group = coordinate.getGroup();
            final String id = coordinate.getId();
            final String version = coordinate.getVersion();
            if (index != 0) {
                if (separatorIsElement) {
                    xmlStreamWriter.writeEmptyElement(effectiveSeparator);
                } else {
                    xmlStreamWriter.writeCharacters(effectiveSeparator);
                }
            }
            writeLink(xmlStreamWriter, linkedComponent.getSimpleName(), "../../../../../components/" + group + "/" + id + "/" + version + "/" + linkedComponent.getCanonicalName() + "/index.html");
            ++index;
        } else {
            LOGGER.warn("Could not link to {} because no bundles were found for {}", new Object[] { linkedComponentName, sourceContextName });
        }
    }
    if (classNames != null) {
        for (final String className : classNames) {
            if (index != 0) {
                if (separatorIsElement) {
                    xmlStreamWriter.writeEmptyElement(effectiveSeparator);
                } else {
                    xmlStreamWriter.writeCharacters(effectiveSeparator);
                }
            }
            final List<Bundle> linkedComponentBundles = ExtensionManager.getBundles(className);
            if (linkedComponentBundles != null && linkedComponentBundles.size() > 0) {
                final Bundle firstBundle = linkedComponentBundles.get(0);
                final BundleCoordinate firstCoordinate = firstBundle.getBundleDetails().getCoordinate();
                final String group = firstCoordinate.getGroup();
                final String id = firstCoordinate.getId();
                final String version = firstCoordinate.getVersion();
                final String link = "../../../../../components/" + group + "/" + id + "/" + version + "/" + className + "/index.html";
                final int indexOfLastPeriod = className.lastIndexOf(".") + 1;
                writeLink(xmlStreamWriter, className.substring(indexOfLastPeriod), link);
                ++index;
            } else {
                LOGGER.warn("Could not link to {} because no bundles were found for {}", new Object[] { className, sourceContextName });
            }
        }
    }
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate)

Example 54 with BundleCoordinate

use of org.apache.nifi.bundle.BundleCoordinate in project nifi by apache.

the class AbstractConfiguredComponent method matchesApi.

/**
 * Determines if the given controller service node has the required API as an ancestor.
 *
 * @param controllerServiceImplBundle the bundle of a controller service being referenced by a processor
 * @param requiredApiCoordinate the controller service API required by the processor
 * @return true if the controller service node has the require API as an ancestor, false otherwise
 */
private boolean matchesApi(final Bundle controllerServiceImplBundle, final BundleCoordinate requiredApiCoordinate) {
    // start with the coordinate of the controller service for cases where the API and service are in the same bundle
    BundleCoordinate controllerServiceDependencyCoordinate = controllerServiceImplBundle.getBundleDetails().getCoordinate();
    boolean foundApiDependency = false;
    while (controllerServiceDependencyCoordinate != null) {
        // determine if the dependency coordinate matches the required API
        if (requiredApiCoordinate.equals(controllerServiceDependencyCoordinate)) {
            foundApiDependency = true;
            break;
        }
        // move to the next dependency in the chain, or stop if null
        final Bundle controllerServiceDependencyBundle = ExtensionManager.getBundle(controllerServiceDependencyCoordinate);
        if (controllerServiceDependencyBundle == null) {
            controllerServiceDependencyCoordinate = null;
        } else {
            controllerServiceDependencyCoordinate = controllerServiceDependencyBundle.getBundleDetails().getDependencyCoordinate();
        }
    }
    return foundApiDependency;
}
Also used : Bundle(org.apache.nifi.bundle.Bundle) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate)

Example 55 with BundleCoordinate

use of org.apache.nifi.bundle.BundleCoordinate 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

BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)65 Bundle (org.apache.nifi.bundle.Bundle)23 LinkedHashSet (java.util.LinkedHashSet)18 ArrayList (java.util.ArrayList)16 BundleDTO (org.apache.nifi.web.api.dto.BundleDTO)16 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 URL (java.net.URL)13 HashSet (java.util.HashSet)13 File (java.io.File)12 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)12 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)12 ConfigurableComponent (org.apache.nifi.components.ConfigurableComponent)10 List (java.util.List)8 Map (java.util.Map)8 Set (java.util.Set)8 Position (org.apache.nifi.connectable.Position)7 IOException (java.io.IOException)6 Collectors (java.util.stream.Collectors)6 Collections (java.util.Collections)5