Search in sources :

Example 66 with Relationship

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

the class StandardFlowSerializer method addProcessor.

private void addProcessor(final Element parentElement, final ProcessorNode processor, final ScheduledStateLookup scheduledStateLookup) {
    final Document doc = parentElement.getOwnerDocument();
    final Element element = doc.createElement("processor");
    parentElement.appendChild(element);
    addTextElement(element, "id", processor.getIdentifier());
    addTextElement(element, "versionedComponentId", processor.getVersionedComponentId());
    addTextElement(element, "name", processor.getName());
    addPosition(element, processor.getPosition());
    addStyle(element, processor.getStyle());
    addTextElement(element, "comment", processor.getComments());
    addTextElement(element, "class", processor.getCanonicalClassName());
    addBundle(element, processor.getBundleCoordinate());
    addTextElement(element, "maxConcurrentTasks", processor.getMaxConcurrentTasks());
    addTextElement(element, "schedulingPeriod", processor.getSchedulingPeriod());
    addTextElement(element, "penalizationPeriod", processor.getPenalizationPeriod());
    addTextElement(element, "yieldPeriod", processor.getYieldPeriod());
    addTextElement(element, "bulletinLevel", processor.getBulletinLevel().toString());
    addTextElement(element, "lossTolerant", String.valueOf(processor.isLossTolerant()));
    addTextElement(element, "scheduledState", scheduledStateLookup.getScheduledState(processor).name());
    addTextElement(element, "schedulingStrategy", processor.getSchedulingStrategy().name());
    addTextElement(element, "executionNode", processor.getExecutionNode().name());
    addTextElement(element, "runDurationNanos", processor.getRunDuration(TimeUnit.NANOSECONDS));
    addConfiguration(element, processor.getProperties(), processor.getAnnotationData(), encryptor);
    for (final Relationship rel : processor.getAutoTerminatedRelationships()) {
        addTextElement(element, "autoTerminatedRelationship", rel.getName());
    }
}
Also used : Element(org.w3c.dom.Element) Relationship(org.apache.nifi.processor.Relationship) Document(org.w3c.dom.Document)

Example 67 with Relationship

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

the class ProcessorAuditor method extractConfiguredPropertyValues.

/**
 * Extracts the values for the configured properties from the specified Processor.
 */
private Map<String, String> extractConfiguredPropertyValues(ProcessorNode processor, ProcessorDTO processorDTO) {
    Map<String, String> values = new HashMap<>();
    if (processorDTO.getName() != null) {
        values.put(NAME, processor.getName());
    }
    if (processorDTO.getBundle() != null) {
        final BundleCoordinate bundle = processor.getBundleCoordinate();
        values.put(EXTENSION_VERSION, formatExtensionVersion(processor.getComponentType(), bundle));
    }
    if (processorDTO.getConfig() != null) {
        ProcessorConfigDTO newConfig = processorDTO.getConfig();
        if (newConfig.getConcurrentlySchedulableTaskCount() != null) {
            values.put(CONCURRENTLY_SCHEDULABLE_TASKS, String.valueOf(processor.getMaxConcurrentTasks()));
        }
        if (newConfig.getPenaltyDuration() != null) {
            values.put(PENALTY_DURATION, processor.getPenalizationPeriod());
        }
        if (newConfig.getYieldDuration() != null) {
            values.put(YIELD_DURATION, processor.getYieldPeriod());
        }
        if (newConfig.getBulletinLevel() != null) {
            values.put(BULLETIN_LEVEL, processor.getBulletinLevel().name());
        }
        if (newConfig.getAnnotationData() != null) {
            values.put(ANNOTATION_DATA, processor.getAnnotationData());
        }
        if (newConfig.getSchedulingPeriod() != null) {
            values.put(SCHEDULING_PERIOD, String.valueOf(processor.getSchedulingPeriod()));
        }
        if (newConfig.getAutoTerminatedRelationships() != null) {
            // get each of the auto terminated relationship names
            final Set<Relationship> autoTerminatedRelationships = processor.getAutoTerminatedRelationships();
            final List<String> autoTerminatedRelationshipNames = new ArrayList<>(autoTerminatedRelationships.size());
            for (final Relationship relationship : autoTerminatedRelationships) {
                autoTerminatedRelationshipNames.add(relationship.getName());
            }
            // sort them and include in the configuration
            Collections.sort(autoTerminatedRelationshipNames, Collator.getInstance(Locale.US));
            values.put(AUTO_TERMINATED_RELATIONSHIPS, StringUtils.join(autoTerminatedRelationshipNames, ", "));
        }
        if (newConfig.getProperties() != null) {
            // for each property specified, extract its configured value
            Map<String, String> properties = newConfig.getProperties();
            Map<PropertyDescriptor, String> configuredProperties = processor.getProperties();
            for (String propertyName : properties.keySet()) {
                // build a descriptor for getting the configured value
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor.Builder().name(propertyName).build();
                String configuredPropertyValue = configuredProperties.get(propertyDescriptor);
                // if the configured value couldn't be found, use the default value from the actual descriptor
                if (configuredPropertyValue == null) {
                    propertyDescriptor = locatePropertyDescriptor(configuredProperties.keySet(), propertyDescriptor);
                    configuredPropertyValue = propertyDescriptor.getDefaultValue();
                }
                values.put(propertyName, configuredPropertyValue);
            }
        }
        if (newConfig.getComments() != null) {
            values.put(COMMENTS, processor.getComments());
        }
        if (newConfig.getSchedulingStrategy() != null) {
            values.put(SCHEDULING_STRATEGY, processor.getSchedulingStrategy().name());
        }
        if (newConfig.getExecutionNode() != null) {
            values.put(EXECUTION_NODE, processor.getExecutionNode().name());
        }
    }
    return values;
}
Also used : ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) Relationship(org.apache.nifi.processor.Relationship) ArrayList(java.util.ArrayList) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate)

Example 68 with Relationship

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

the class RelationshipAuditor method updateConnectionAdvice.

/**
 * Audits the creation and removal of relationships via updateConnection().
 *
 * @param proceedingJoinPoint join point
 * @param connectionDTO dto
 * @param connectionDAO dao
 * @return connection
 * @throws Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.ConnectionDAO+) && " + "execution(org.apache.nifi.connectable.Connection updateConnection(org.apache.nifi.web.api.dto.ConnectionDTO)) && " + "args(connectionDTO) && " + "target(connectionDAO)")
public Connection updateConnectionAdvice(ProceedingJoinPoint proceedingJoinPoint, ConnectionDTO connectionDTO, ConnectionDAO connectionDAO) throws Throwable {
    // get the previous configuration
    Connection connection = connectionDAO.getConnection(connectionDTO.getId());
    Connectable previousDestination = connection.getDestination();
    Collection<Relationship> previousRelationships = connection.getRelationships();
    Map<String, String> values = extractConfiguredPropertyValues(connection, connectionDTO);
    // perform the underlying operation
    connection = (Connection) proceedingJoinPoint.proceed();
    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();
    // ensure the user was found
    if (user != null) {
        Collection<Action> actions = new ArrayList<>();
        Map<String, String> updatedValues = extractConfiguredPropertyValues(connection, connectionDTO);
        // get the source
        Connectable source = connection.getSource();
        // get the current target
        Connectable destination = connection.getDestination();
        // determine if a new target was specified in the initial request
        if (destination != null && !previousDestination.getIdentifier().equals(destination.getIdentifier())) {
            // record the removal of all relationships from the previous target
            final ConnectDetails disconnectDetails = createConnectDetails(connection, source, previousRelationships, previousDestination);
            actions.add(generateAuditRecordForConnection(connection, Operation.Disconnect, disconnectDetails));
            // record the addition of all relationships to the new target
            final ConnectDetails connectDetails = createConnectDetails(connection, connection.getRelationships());
            actions.add(generateAuditRecordForConnection(connection, Operation.Connect, connectDetails));
        }
        // audit and relationships added/removed
        Collection<Relationship> newRelationships = connection.getRelationships();
        // identify any relationships that were added
        if (newRelationships != null) {
            List<Relationship> relationshipsToAdd = new ArrayList<>(newRelationships);
            if (previousRelationships != null) {
                relationshipsToAdd.removeAll(previousRelationships);
            }
            if (!relationshipsToAdd.isEmpty()) {
                final ConnectDetails connectDetails = createConnectDetails(connection, relationshipsToAdd);
                actions.add(generateAuditRecordForConnection(connection, Operation.Connect, connectDetails));
            }
        }
        // identify any relationships that were removed
        if (previousRelationships != null) {
            List<Relationship> relationshipsToRemove = new ArrayList<>(previousRelationships);
            if (newRelationships != null) {
                relationshipsToRemove.removeAll(newRelationships);
            }
            if (!relationshipsToRemove.isEmpty()) {
                final ConnectDetails connectDetails = createConnectDetails(connection, relationshipsToRemove);
                actions.add(generateAuditRecordForConnection(connection, Operation.Disconnect, connectDetails));
            }
        }
        // go through each updated value
        Date actionTimestamp = new Date();
        for (String property : updatedValues.keySet()) {
            String newValue = updatedValues.get(property);
            String oldValue = values.get(property);
            // ensure the value is changing
            if (oldValue == null || newValue == null || !newValue.equals(oldValue)) {
                // create the config details
                FlowChangeConfigureDetails configurationDetails = new FlowChangeConfigureDetails();
                configurationDetails.setName(property);
                configurationDetails.setValue(newValue);
                configurationDetails.setPreviousValue(oldValue);
                // create a configuration action
                FlowChangeAction configurationAction = new FlowChangeAction();
                configurationAction.setUserIdentity(user.getIdentity());
                configurationAction.setOperation(Operation.Configure);
                configurationAction.setTimestamp(actionTimestamp);
                configurationAction.setSourceId(connection.getIdentifier());
                configurationAction.setSourceName(connection.getName());
                configurationAction.setSourceType(Component.Connection);
                configurationAction.setActionDetails(configurationDetails);
                actions.add(configurationAction);
            }
        }
        // save the actions
        if (!actions.isEmpty()) {
            saveActions(actions, logger);
        }
    }
    return connection;
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ConnectDetails(org.apache.nifi.action.details.ConnectDetails) FlowChangeConnectDetails(org.apache.nifi.action.details.FlowChangeConnectDetails) Connection(org.apache.nifi.connectable.Connection) ArrayList(java.util.ArrayList) Date(java.util.Date) Connectable(org.apache.nifi.connectable.Connectable) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) Relationship(org.apache.nifi.processor.Relationship) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Around(org.aspectj.lang.annotation.Around)

Example 69 with Relationship

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

the class RelationshipAuditor method generateAuditRecordForConnection.

/**
 * Generates the audit records for the specified connection.
 *
 * @param connection connection
 * @param operation operation
 * @param actionDetails details
 * @return action
 */
public Action generateAuditRecordForConnection(Connection connection, Operation operation, ActionDetails actionDetails) {
    FlowChangeAction action = null;
    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();
    // ensure the user was found
    if (user != null) {
        // determine the source details
        final String connectionId = connection.getIdentifier();
        String connectionName = connection.getName();
        if (StringUtils.isBlank(connectionName)) {
            Collection<String> relationshipNames = new HashSet<>(connection.getRelationships().size());
            for (final Relationship relationship : connection.getRelationships()) {
                relationshipNames.add(relationship.getName());
            }
            connectionName = StringUtils.join(relationshipNames, ", ");
        }
        // go through each relationship added
        Date actionTimestamp = new Date();
        // create a new relationship action
        action = new FlowChangeAction();
        action.setUserIdentity(user.getIdentity());
        action.setOperation(operation);
        action.setTimestamp(actionTimestamp);
        action.setSourceId(connectionId);
        action.setSourceName(connectionName);
        action.setSourceType(Component.Connection);
        if (actionDetails != null) {
            action.setActionDetails(actionDetails);
        }
    }
    return action;
}
Also used : NiFiUser(org.apache.nifi.authorization.user.NiFiUser) Relationship(org.apache.nifi.processor.Relationship) Date(java.util.Date) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) HashSet(java.util.HashSet)

Example 70 with Relationship

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

the class StandardProcessorNode method getUndefinedRelationships.

public Set<Relationship> getUndefinedRelationships() {
    final Set<Relationship> undefined = new HashSet<>();
    final Set<Relationship> relationships;
    final Processor processor = processorRef.get().getProcessor();
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(processor.getClass(), processor.getIdentifier())) {
        relationships = processor.getRelationships();
    }
    if (relationships == null) {
        return undefined;
    }
    for (final Relationship relation : relationships) {
        final Set<Connection> connectionSet = this.connections.get(relation);
        if (connectionSet == null || connectionSet.isEmpty()) {
            undefined.add(relation);
        }
    }
    return undefined;
}
Also used : NarCloseable(org.apache.nifi.nar.NarCloseable) Processor(org.apache.nifi.processor.Processor) Relationship(org.apache.nifi.processor.Relationship) Connection(org.apache.nifi.connectable.Connection) 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