Search in sources :

Example 6 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class ControllerSearchService method search.

private ComponentSearchResultDTO search(final String searchStr, final Connection connection) {
    final List<String> matches = new ArrayList<>();
    // search id and name
    addIfAppropriate(searchStr, connection.getIdentifier(), "Id", matches);
    addIfAppropriate(searchStr, connection.getVersionedComponentId().orElse(null), "Version Control ID", matches);
    addIfAppropriate(searchStr, connection.getName(), "Name", matches);
    // search relationships
    for (final Relationship relationship : connection.getRelationships()) {
        addIfAppropriate(searchStr, relationship.getName(), "Relationship", matches);
    }
    // search prioritizers
    final FlowFileQueue queue = connection.getFlowFileQueue();
    for (final FlowFilePrioritizer comparator : queue.getPriorities()) {
        addIfAppropriate(searchStr, comparator.getClass().getName(), "Prioritizer", matches);
    }
    // search expiration
    if (StringUtils.containsIgnoreCase("expires", searchStr) || StringUtils.containsIgnoreCase("expiration", searchStr)) {
        final int expirationMillis = connection.getFlowFileQueue().getFlowFileExpiration(TimeUnit.MILLISECONDS);
        if (expirationMillis > 0) {
            matches.add("FlowFile expiration: " + connection.getFlowFileQueue().getFlowFileExpiration());
        }
    }
    // search back pressure
    if (StringUtils.containsIgnoreCase("back pressure", searchStr) || StringUtils.containsIgnoreCase("pressure", searchStr)) {
        final String backPressureDataSize = connection.getFlowFileQueue().getBackPressureDataSizeThreshold();
        final Double backPressureBytes = DataUnit.parseDataSize(backPressureDataSize, DataUnit.B);
        if (backPressureBytes > 0) {
            matches.add("Back pressure data size: " + backPressureDataSize);
        }
        final long backPressureCount = connection.getFlowFileQueue().getBackPressureObjectThreshold();
        if (backPressureCount > 0) {
            matches.add("Back pressure count: " + backPressureCount);
        }
    }
    // search the source
    final Connectable source = connection.getSource();
    addIfAppropriate(searchStr, source.getIdentifier(), "Source id", matches);
    addIfAppropriate(searchStr, source.getName(), "Source name", matches);
    addIfAppropriate(searchStr, source.getComments(), "Source comments", matches);
    // search the destination
    final Connectable destination = connection.getDestination();
    addIfAppropriate(searchStr, destination.getIdentifier(), "Destination id", matches);
    addIfAppropriate(searchStr, destination.getName(), "Destination name", matches);
    addIfAppropriate(searchStr, destination.getComments(), "Destination comments", matches);
    if (matches.isEmpty()) {
        return null;
    }
    final ComponentSearchResultDTO result = new ComponentSearchResultDTO();
    result.setId(connection.getIdentifier());
    // determine the name of the search match
    if (StringUtils.isNotBlank(connection.getName())) {
        result.setName(connection.getName());
    } else if (!connection.getRelationships().isEmpty()) {
        final List<String> relationships = new ArrayList<>(connection.getRelationships().size());
        for (final Relationship relationship : connection.getRelationships()) {
            if (StringUtils.isNotBlank(relationship.getName())) {
                relationships.add(relationship.getName());
            }
        }
        if (!relationships.isEmpty()) {
            result.setName(StringUtils.join(relationships, ", "));
        }
    }
    // ensure a name is added
    if (result.getName() == null) {
        result.setName("From source " + connection.getSource().getName());
    }
    result.setMatches(matches);
    return result;
}
Also used : ArrayList(java.util.ArrayList) ComponentSearchResultDTO(org.apache.nifi.web.api.dto.search.ComponentSearchResultDTO) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) Connectable(org.apache.nifi.connectable.Connectable) Relationship(org.apache.nifi.processor.Relationship) ArrayList(java.util.ArrayList) List(java.util.List) FlowFilePrioritizer(org.apache.nifi.flowfile.FlowFilePrioritizer)

Example 7 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class StandardConnectionDAO method updateConnection.

@Override
public Connection updateConnection(final ConnectionDTO connectionDTO) {
    final Connection connection = locateConnection(connectionDTO.getId());
    final ProcessGroup group = connection.getProcessGroup();
    // ensure we can update
    verifyUpdate(connection, connectionDTO);
    final Collection<Relationship> newProcessorRelationships = new ArrayList<>();
    Connectable newDestination = null;
    // ensure that the source ID is correct, if specified.
    final Connectable existingSource = connection.getSource();
    if (isNotNull(connectionDTO.getSource()) && !existingSource.getIdentifier().equals(connectionDTO.getSource().getId())) {
        throw new IllegalStateException("Connection with ID " + connectionDTO.getId() + " has conflicting Source ID");
    }
    // determine any new relationships
    final Set<String> relationships = connectionDTO.getSelectedRelationships();
    if (isNotNull(relationships)) {
        if (relationships.isEmpty()) {
            throw new IllegalArgumentException("Cannot remove all relationships from Connection with ID " + connection.getIdentifier() + " -- remove the Connection instead");
        }
        if (existingSource == null) {
            throw new IllegalArgumentException("Cannot specify new relationships without including the source.");
        }
        for (final String relationship : relationships) {
            final Relationship processorRelationship = existingSource.getRelationship(relationship);
            if (processorRelationship == null) {
                throw new IllegalArgumentException("Unable to locate " + relationship + " relationship.");
            }
            newProcessorRelationships.add(processorRelationship);
        }
    }
    // determine if the destination changed
    final ConnectableDTO proposedDestination = connectionDTO.getDestination();
    if (proposedDestination != null) {
        final Connectable currentDestination = connection.getDestination();
        // handle remote input port differently
        if (ConnectableType.REMOTE_INPUT_PORT.name().equals(proposedDestination.getType())) {
            // the group id must be specified
            if (proposedDestination.getGroupId() == null) {
                throw new IllegalArgumentException("When the destination is a remote input port its group id is required.");
            }
            // if the current destination is a remote input port
            boolean isDifferentRemoteProcessGroup = false;
            if (currentDestination.getConnectableType() == ConnectableType.REMOTE_INPUT_PORT) {
                RemoteGroupPort remotePort = (RemoteGroupPort) currentDestination;
                if (!proposedDestination.getGroupId().equals(remotePort.getRemoteProcessGroup().getIdentifier())) {
                    isDifferentRemoteProcessGroup = true;
                }
            }
            // if the destination is changing or the previous destination was a different remote process group
            if (!proposedDestination.getId().equals(currentDestination.getIdentifier()) || isDifferentRemoteProcessGroup) {
                final ProcessGroup destinationParentGroup = locateProcessGroup(flowController, group.getIdentifier());
                final RemoteProcessGroup remoteProcessGroup = destinationParentGroup.getRemoteProcessGroup(proposedDestination.getGroupId());
                // ensure the remote process group was found
                if (remoteProcessGroup == null) {
                    throw new IllegalArgumentException("Unable to find the specified remote process group.");
                }
                final RemoteGroupPort remoteInputPort = remoteProcessGroup.getInputPort(proposedDestination.getId());
                // ensure the new destination was found
                if (remoteInputPort == null) {
                    throw new IllegalArgumentException("Unable to find the specified destination.");
                }
                // ensure the remote port actually exists
                if (!remoteInputPort.getTargetExists()) {
                    throw new IllegalArgumentException("The specified remote input port does not exist.");
                } else {
                    newDestination = remoteInputPort;
                }
            }
        } else {
            // if there is a different destination id
            if (!proposedDestination.getId().equals(currentDestination.getIdentifier())) {
                // if the destination connectable's group id has not been set, its inferred to be the current group
                if (proposedDestination.getGroupId() == null) {
                    proposedDestination.setGroupId(group.getIdentifier());
                }
                final ProcessGroup destinationGroup = locateProcessGroup(flowController, proposedDestination.getGroupId());
                newDestination = destinationGroup.getConnectable(proposedDestination.getId());
                // ensure the new destination was found
                if (newDestination == null) {
                    throw new IllegalArgumentException("Unable to find the specified destination.");
                }
            }
        }
    }
    // configure the connection
    configureConnection(connection, connectionDTO);
    // update the relationships if necessary
    if (!newProcessorRelationships.isEmpty()) {
        connection.setRelationships(newProcessorRelationships);
    }
    // update the destination if necessary
    if (isNotNull(newDestination)) {
        connection.setDestination(newDestination);
    }
    return connection;
}
Also used : RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) RemoteGroupPort(org.apache.nifi.remote.RemoteGroupPort) Connection(org.apache.nifi.connectable.Connection) ArrayList(java.util.ArrayList) Connectable(org.apache.nifi.connectable.Connectable) Relationship(org.apache.nifi.processor.Relationship) ProcessGroup(org.apache.nifi.groups.ProcessGroup) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO)

Example 8 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class StandardProcessorDAO method validateProposedConfiguration.

private List<String> validateProposedConfiguration(final ProcessorNode processorNode, final ProcessorConfigDTO config) {
    List<String> validationErrors = new ArrayList<>();
    // validate settings
    if (isNotNull(config.getPenaltyDuration())) {
        Matcher penaltyMatcher = FormatUtils.TIME_DURATION_PATTERN.matcher(config.getPenaltyDuration());
        if (!penaltyMatcher.matches()) {
            validationErrors.add("Penalty duration is not a valid time duration (ie 30 sec, 5 min)");
        }
    }
    if (isNotNull(config.getYieldDuration())) {
        Matcher yieldMatcher = FormatUtils.TIME_DURATION_PATTERN.matcher(config.getYieldDuration());
        if (!yieldMatcher.matches()) {
            validationErrors.add("Yield duration is not a valid time duration (ie 30 sec, 5 min)");
        }
    }
    if (isNotNull(config.getBulletinLevel())) {
        try {
            LogLevel.valueOf(config.getBulletinLevel());
        } catch (IllegalArgumentException iae) {
            validationErrors.add(String.format("Bulletin level: Value must be one of [%s]", StringUtils.join(LogLevel.values(), ", ")));
        }
    }
    if (isNotNull(config.getExecutionNode())) {
        try {
            ExecutionNode.valueOf(config.getExecutionNode());
        } catch (IllegalArgumentException iae) {
            validationErrors.add(String.format("Execution node: Value must be one of [%s]", StringUtils.join(ExecutionNode.values(), ", ")));
        }
    }
    // get the current scheduling strategy
    SchedulingStrategy schedulingStrategy = processorNode.getSchedulingStrategy();
    // validate the new scheduling strategy if appropriate
    if (isNotNull(config.getSchedulingStrategy())) {
        try {
            // this will be the new scheduling strategy so use it
            schedulingStrategy = SchedulingStrategy.valueOf(config.getSchedulingStrategy());
        } catch (IllegalArgumentException iae) {
            validationErrors.add(String.format("Scheduling strategy: Value must be one of [%s]", StringUtils.join(SchedulingStrategy.values(), ", ")));
        }
    }
    // validate the concurrent tasks based on the scheduling strategy
    if (isNotNull(config.getConcurrentlySchedulableTaskCount())) {
        switch(schedulingStrategy) {
            case TIMER_DRIVEN:
            case PRIMARY_NODE_ONLY:
                if (config.getConcurrentlySchedulableTaskCount() <= 0) {
                    validationErrors.add("Concurrent tasks must be greater than 0.");
                }
                break;
            case EVENT_DRIVEN:
                if (config.getConcurrentlySchedulableTaskCount() < 0) {
                    validationErrors.add("Concurrent tasks must be greater or equal to 0.");
                }
                break;
        }
    }
    // validate the scheduling period based on the scheduling strategy
    if (isNotNull(config.getSchedulingPeriod())) {
        switch(schedulingStrategy) {
            case TIMER_DRIVEN:
            case PRIMARY_NODE_ONLY:
                final Matcher schedulingMatcher = FormatUtils.TIME_DURATION_PATTERN.matcher(config.getSchedulingPeriod());
                if (!schedulingMatcher.matches()) {
                    validationErrors.add("Scheduling period is not a valid time duration (ie 30 sec, 5 min)");
                }
                break;
            case CRON_DRIVEN:
                try {
                    new CronExpression(config.getSchedulingPeriod());
                } catch (final ParseException pe) {
                    throw new IllegalArgumentException(String.format("Scheduling Period '%s' is not a valid cron expression: %s", config.getSchedulingPeriod(), pe.getMessage()));
                } catch (final Exception e) {
                    throw new IllegalArgumentException("Scheduling Period is not a valid cron expression: " + config.getSchedulingPeriod());
                }
                break;
        }
    }
    final Set<String> autoTerminatedRelationships = config.getAutoTerminatedRelationships();
    if (isNotNull(autoTerminatedRelationships)) {
        for (final String relationshipName : autoTerminatedRelationships) {
            final Relationship relationship = new Relationship.Builder().name(relationshipName).build();
            final Set<Connection> connections = processorNode.getConnections(relationship);
            if (isNotNull(connections) && !connections.isEmpty()) {
                validationErrors.add("Cannot automatically terminate '" + relationshipName + "' relationship because a Connection already exists with this relationship");
            }
        }
    }
    return validationErrors;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Connection(org.apache.nifi.connectable.Connection) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) ParseException(java.text.ParseException) ComponentLifeCycleException(org.apache.nifi.controller.exception.ComponentLifeCycleException) NiFiCoreException(org.apache.nifi.web.NiFiCoreException) ValidationException(org.apache.nifi.controller.exception.ValidationException) SchedulingStrategy(org.apache.nifi.scheduling.SchedulingStrategy) Relationship(org.apache.nifi.processor.Relationship) CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException)

Example 9 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class StandardProcessorDAO method configureProcessor.

private void configureProcessor(final ProcessorNode processor, final ProcessorDTO processorDTO) {
    final ProcessorConfigDTO config = processorDTO.getConfig();
    // ensure some configuration was specified
    if (isNotNull(config)) {
        // perform the configuration
        final String schedulingStrategy = config.getSchedulingStrategy();
        final String executionNode = config.getExecutionNode();
        final String comments = config.getComments();
        final String annotationData = config.getAnnotationData();
        final Integer maxTasks = config.getConcurrentlySchedulableTaskCount();
        final Map<String, String> configProperties = config.getProperties();
        final String schedulingPeriod = config.getSchedulingPeriod();
        final String penaltyDuration = config.getPenaltyDuration();
        final String yieldDuration = config.getYieldDuration();
        final Long runDurationMillis = config.getRunDurationMillis();
        final String bulletinLevel = config.getBulletinLevel();
        final Set<String> undefinedRelationshipsToTerminate = config.getAutoTerminatedRelationships();
        // ensure scheduling strategy is set first
        if (isNotNull(schedulingStrategy)) {
            processor.setSchedulingStrategy(SchedulingStrategy.valueOf(schedulingStrategy));
        }
        if (isNotNull(executionNode)) {
            processor.setExecutionNode(ExecutionNode.valueOf(executionNode));
        }
        if (isNotNull(comments)) {
            processor.setComments(comments);
        }
        if (isNotNull(annotationData)) {
            processor.setAnnotationData(annotationData);
        }
        if (isNotNull(maxTasks)) {
            processor.setMaxConcurrentTasks(maxTasks);
        }
        if (isNotNull(schedulingPeriod)) {
            processor.setScheduldingPeriod(schedulingPeriod);
        }
        if (isNotNull(penaltyDuration)) {
            processor.setPenalizationPeriod(penaltyDuration);
        }
        if (isNotNull(yieldDuration)) {
            processor.setYieldPeriod(yieldDuration);
        }
        if (isNotNull(runDurationMillis)) {
            processor.setRunDuration(runDurationMillis, TimeUnit.MILLISECONDS);
        }
        if (isNotNull(bulletinLevel)) {
            processor.setBulletinLevel(LogLevel.valueOf(bulletinLevel));
        }
        if (isNotNull(config.isLossTolerant())) {
            processor.setLossTolerant(config.isLossTolerant());
        }
        if (isNotNull(configProperties)) {
            processor.setProperties(configProperties);
        }
        if (isNotNull(undefinedRelationshipsToTerminate)) {
            final Set<Relationship> relationships = new HashSet<>();
            for (final String relName : undefinedRelationshipsToTerminate) {
                relationships.add(new Relationship.Builder().name(relName).build());
            }
            processor.setAutoTerminatedRelationships(relationships);
        }
    }
    // update processor settings
    if (isNotNull(processorDTO.getPosition())) {
        processor.setPosition(new Position(processorDTO.getPosition().getX(), processorDTO.getPosition().getY()));
    }
    if (isNotNull(processorDTO.getStyle())) {
        processor.setStyle(processorDTO.getStyle());
    }
    final String name = processorDTO.getName();
    if (isNotNull(name)) {
        processor.setName(name);
    }
}
Also used : ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) Position(org.apache.nifi.connectable.Position) Relationship(org.apache.nifi.processor.Relationship) HashSet(java.util.HashSet)

Example 10 with Relationship

use of org.apache.nifi.processor.Relationship in project nifi by apache.

the class TestStandardRemoteGroupPort method setupMockProcessSession.

private void setupMockProcessSession() {
    // Construct a RemoteGroupPort as a processor to use NiFi mock library.
    final Processor remoteGroupPort = mock(Processor.class);
    final Set<Relationship> relationships = new HashSet<>();
    relationships.add(Relationship.ANONYMOUS);
    when(remoteGroupPort.getRelationships()).thenReturn(relationships);
    when(remoteGroupPort.getIdentifier()).thenReturn("remote-group-port-id");
    sessionState = new SharedSessionState(remoteGroupPort, new AtomicLong(0));
    processSession = new MockProcessSession(sessionState, remoteGroupPort);
    processContext = new MockProcessContext(remoteGroupPort);
}
Also used : SharedSessionState(org.apache.nifi.util.SharedSessionState) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) Relationship(org.apache.nifi.processor.Relationship) MockProcessSession(org.apache.nifi.util.MockProcessSession) MockProcessContext(org.apache.nifi.util.MockProcessContext) HashSet(java.util.HashSet)

Aggregations

Relationship (org.apache.nifi.processor.Relationship)106 ArrayList (java.util.ArrayList)41 HashSet (java.util.HashSet)40 HashMap (java.util.HashMap)32 FlowFile (org.apache.nifi.flowfile.FlowFile)32 Map (java.util.Map)31 IOException (java.io.IOException)26 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)26 Test (org.junit.Test)23 List (java.util.List)20 Set (java.util.Set)19 Connection (org.apache.nifi.connectable.Connection)18 TestRunner (org.apache.nifi.util.TestRunner)18 ProcessException (org.apache.nifi.processor.exception.ProcessException)17 ProcessSession (org.apache.nifi.processor.ProcessSession)15 InputStream (java.io.InputStream)14 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)12 Processor (org.apache.nifi.processor.Processor)12 Collections (java.util.Collections)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)10