Search in sources :

Example 26 with ConnectionDTO

use of org.apache.nifi.web.api.dto.ConnectionDTO in project kylo by Teradata.

the class ImportReusableTemplate method validateOutputPortConnections.

private boolean validateOutputPortConnections(NifiProcessGroup newTemplateInstance) {
    // Validate port connections
    newTemplateInstance.getProcessGroupEntity().getContents().getOutputPorts().stream().forEach(portDTO -> {
        if (portDTO.getValidationErrors() != null && !portDTO.getValidationErrors().isEmpty()) {
            importTemplate.setReusableFlowOutputPortConnectionsNeeded(true);
        }
        ReusableTemplateConnectionInfo connectionInfo = new ReusableTemplateConnectionInfo();
        connectionInfo.setFeedOutputPortName(portDTO.getName());
        // attempt to prefill it with the previous connection if it existed
        ConnectionDTO reusableTemplateInputPortConnection = findReusableTemplateInputPortConnectionForOutputPort(portDTO);
        if (reusableTemplateInputPortConnection != null) {
            connectionInfo.setInputPortDisplayName(reusableTemplateInputPortConnection.getSource().getName());
            connectionInfo.setReusableTemplateInputPortName(reusableTemplateInputPortConnection.getSource().getName());
            String processGroupName = findReusableTemplateProcessGroup(reusableTemplateInputPortConnection.getDestination().getGroupId()).map(processGroupDTO -> processGroupDTO.getName()).orElse(null);
            connectionInfo.setReusableTemplateProcessGroupName(processGroupName);
        }
        importTemplate.addReusableTemplateConnection(connectionInfo);
    });
    return importTemplate.isSuccess() && importTemplate.isValid() && !importTemplate.isReusableFlowOutputPortConnectionsNeeded();
}
Also used : UploadProgressService(com.thinkbiganalytics.feedmgr.service.UploadProgressService) VersionedProcessGroup(com.thinkbiganalytics.nifi.rest.model.VersionedProcessGroup) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) NifiError(com.thinkbiganalytics.nifi.rest.model.NifiError) LoggerFactory(org.slf4j.LoggerFactory) ReusableTemplateConnectionInfo(com.thinkbiganalytics.feedmgr.rest.model.ReusableTemplateConnectionInfo) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) NifiProcessUtil(com.thinkbiganalytics.nifi.rest.support.NifiProcessUtil) StringUtils(org.apache.commons.lang3.StringUtils) NiFiPropertyDescriptorTransform(com.thinkbiganalytics.nifi.rest.model.NiFiPropertyDescriptorTransform) ProcessGroupStatusSnapshotDTO(org.apache.nifi.web.api.dto.status.ProcessGroupStatusSnapshotDTO) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) NifiConnectionUtil(com.thinkbiganalytics.nifi.rest.support.NifiConnectionUtil) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) NifiPropertyUtil(com.thinkbiganalytics.nifi.rest.support.NifiPropertyUtil) Map(java.util.Map) ImportUtil(com.thinkbiganalytics.feedmgr.util.ImportUtil) AccessController(com.thinkbiganalytics.security.AccessController) NifiFlowUtil(com.thinkbiganalytics.nifi.rest.support.NifiFlowUtil) RemoteProcessGroupInputPort(com.thinkbiganalytics.feedmgr.rest.model.RemoteProcessGroupInputPort) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) ReusableTemplateCreationCallback(com.thinkbiganalytics.nifi.feedmgr.ReusableTemplateCreationCallback) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Collection(java.util.Collection) RemoteProcessGroupStatusSnapshotEntity(org.apache.nifi.web.api.entity.RemoteProcessGroupStatusSnapshotEntity) Set(java.util.Set) Collectors(java.util.stream.Collectors) UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) RegisteredTemplateCache(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateCache) PortDTO(org.apache.nifi.web.api.dto.PortDTO) List(java.util.List) Stream(java.util.stream.Stream) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) Optional(java.util.Optional) NiFiTemplateImport(com.thinkbiganalytics.feedmgr.service.template.importing.model.NiFiTemplateImport) ImportComponent(com.thinkbiganalytics.feedmgr.rest.ImportComponent) NifiProcessGroup(com.thinkbiganalytics.nifi.rest.model.NifiProcessGroup) ProcessGroupStatusDTO(org.apache.nifi.web.api.dto.status.ProcessGroupStatusDTO) HashMap(java.util.HashMap) ImportSection(com.thinkbiganalytics.feedmgr.rest.ImportSection) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) NiFiObjectCache(com.thinkbiganalytics.nifi.rest.NiFiObjectCache) ImportProperty(com.thinkbiganalytics.feedmgr.rest.model.ImportProperty) PropertyExpressionResolver(com.thinkbiganalytics.feedmgr.nifi.PropertyExpressionResolver) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) FlowDTO(org.apache.nifi.web.api.dto.flow.FlowDTO) NifiConstants(com.thinkbiganalytics.nifi.rest.support.NifiConstants) Nullable(javax.annotation.Nullable) RemoteInputPortService(com.thinkbiganalytics.feedmgr.service.template.RemoteInputPortService) NiFiClusterSummary(com.thinkbiganalytics.nifi.rest.model.NiFiClusterSummary) Logger(org.slf4j.Logger) TemplateCreationHelper(com.thinkbiganalytics.nifi.feedmgr.TemplateCreationHelper) NifiFlowCache(com.thinkbiganalytics.feedmgr.nifi.cache.NifiFlowCache) TemplateConnectionUtil(com.thinkbiganalytics.feedmgr.nifi.TemplateConnectionUtil) TemplateRemoteInputPortConnections(com.thinkbiganalytics.feedmgr.rest.model.TemplateRemoteInputPortConnections) Collections(java.util.Collections) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ReusableTemplateConnectionInfo(com.thinkbiganalytics.feedmgr.rest.model.ReusableTemplateConnectionInfo)

Example 27 with ConnectionDTO

use of org.apache.nifi.web.api.dto.ConnectionDTO in project kylo by Teradata.

the class ImportReusableTemplate method recreateOutputPortConnections.

private void recreateOutputPortConnections(UploadProgressMessage importStatusMessage, NifiProcessGroup newTemplateInstance) {
    VersionedProcessGroup versionedProcessGroup = newTemplateInstance.getVersionedProcessGroup();
    String templateName = importTemplate.getTemplateName();
    // Dest == input port in versioned off process group
    if (versionedProcessGroup != null) {
        String reusableTemplateProcessGroupId = getReusableTemplatesProcessGroupId();
        if (reusableTemplateProcessGroupId != null) {
            for (ConnectionDTO connectionDTO : versionedProcessGroup.getDeletedInputPortConnections()) {
                if (connectionDTO.getSource().getType().equals(NifiConstants.OUTPUT_PORT)) {
                    // connect
                    PortDTO destPort = newTemplateInstance.getProcessGroupEntity().getContents().getInputPorts().stream().filter(portDTO -> portDTO.getName().equalsIgnoreCase(connectionDTO.getDestination().getName()) && connectionDTO.getDestination().getGroupId().equalsIgnoreCase(newTemplateInstance.getVersionedProcessGroup().getProcessGroupPriorToVersioning().getId())).findFirst().orElse(null);
                    if (destPort != null) {
                        // make the connection now from the output port to the 'connectionToUse' destination
                        ConnectableDTO source = NifiConnectionUtil.asNewConnectable(connectionDTO.getSource());
                        ConnectableDTO dest = NifiConnectionUtil.asConnectable(destPort);
                        ConnectionDTO newConnection = nifiRestClient.getNiFiRestClient().processGroups().createConnection(reusableTemplateProcessGroupId, source, dest);
                        connections.add(newConnection);
                        // possibly store the ports too?
                        log.info("Reconnected output port {} ({}) to this new process group input port:  {} {{}) ", source.getName(), source.getId(), dest.getName(), dest.getId());
                    } else {
                        // ERROR cant recreate previous connections that were going into this reusable template
                        String msg = "Unable to recreate the connection for template: " + templateName + " that was previously connected to this template prior to the update. The following connection is missing:  Connecting ['" + connectionDTO.getSource().getName() + "' to '" + connectionDTO.getDestination().getName() + "'].";
                        log.error(msg);
                        importTemplate.getTemplateResults().addError(NifiError.SEVERITY.FATAL, msg, "");
                        importStatusMessage.update("Unable to establish prior connection for reusable template: " + templateName + ".  Connection:  ['" + connectionDTO.getSource().getName() + "' to '" + connectionDTO.getDestination().getName() + "']", false);
                        break;
                    }
                }
            }
        }
    }
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) PortDTO(org.apache.nifi.web.api.dto.PortDTO) VersionedProcessGroup(com.thinkbiganalytics.nifi.rest.model.VersionedProcessGroup) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO)

Example 28 with ConnectionDTO

use of org.apache.nifi.web.api.dto.ConnectionDTO in project kylo by Teradata.

the class TemplateCreationHelper method versionProcessGroup.

/**
 * Version a ProcessGroup renaming it with the name - {timestamp millis}.
 * If {@code removeIfInactive} is true it will not version but just delete it
 *
 * @param processGroup the group to version
 */
public VersionedProcessGroup versionProcessGroup(ProcessGroupDTO processGroup, String versionIdentifier) {
    log.info("Versioning Process Group {} ", processGroup.getName());
    VersionedProcessGroup versionedProcessGroup = new VersionedProcessGroup();
    versionedProcessGroup.setProcessGroupPriorToVersioning(processGroup);
    versionedProcessGroup.setProcessGroupName(processGroup.getName());
    List<ProcessorDTO> inputProcessorsPriorToDisabling = restClient.disableAllInputProcessors(processGroup.getId());
    versionedProcessGroup.setInputProcessorsPriorToDisabling(inputProcessorsPriorToDisabling);
    log.info("Disabled Inputs for {} ", processGroup.getName());
    // attempt to stop all processors
    try {
        restClient.stopInputs(processGroup.getId());
        log.info("Stopped Input Ports for {}, ", processGroup.getName());
    } catch (Exception e) {
        log.error("Error trying to stop Input Ports for {} while creating a new version ", processGroup.getName());
    }
    // delete input connections
    try {
        List<ConnectionDTO> deletedConnections = deleteInputPortConnections(processGroup);
        versionedProcessGroup.setDeletedInputPortConnections(deletedConnections);
    } catch (NifiClientRuntimeException e) {
        log.error("Error trying to delete input port connections for Process Group {} while creating a new version. ", processGroup.getName(), e);
        getErrors().add(new NifiError(NifiError.SEVERITY.FATAL, "The input port connections to the process group " + processGroup.getName() + " could not be deleted. Please delete them manually " + "in NiFi and try again."));
    }
    String versionedProcessGroupName = getVersionedProcessGroupName(processGroup.getName(), versionIdentifier);
    versionedProcessGroup.setVersionedProcessGroupName(versionedProcessGroupName);
    // rename the feedGroup to be name+timestamp
    processGroup.setName(versionedProcessGroupName);
    restClient.updateProcessGroup(processGroup);
    log.info("Renamed ProcessGroup to  {}, ", processGroup.getName());
    versionedProcessGroup.setVersionedProcessGroup(processGroup);
    return versionedProcessGroup;
}
Also used : NifiError(com.thinkbiganalytics.nifi.rest.model.NifiError) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) VersionedProcessGroup(com.thinkbiganalytics.nifi.rest.model.VersionedProcessGroup) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 29 with ConnectionDTO

use of org.apache.nifi.web.api.dto.ConnectionDTO in project kylo by Teradata.

the class TemplateCreationHelper method deleteInputPortConnections.

/**
 * Deletes the input port connections to the specified process group.
 *
 * <p>When versioning we want to delete only the input port connections. Keep output port connections in place as they may still have data running through them that should flow through the
 * system.</p>
 *
 * @param processGroup the process group with input port connections
 * @throws NifiClientRuntimeException if a connection cannot be deleted
 */
private List<ConnectionDTO> deleteInputPortConnections(@Nonnull final ProcessGroupDTO processGroup) throws NifiClientRuntimeException {
    // Get the list of incoming connections coming from some source to this process group
    List<ConnectionDTO> deletedConnections = new ArrayList<>();
    final Set<ConnectionDTO> connectionsEntity = nifiObjectCache.isCacheConnections() ? nifiObjectCache.getConnections(processGroup.getParentGroupId()) : restClient.getProcessGroupConnections(processGroup.getParentGroupId());
    if (connectionsEntity == null) {
        return deletedConnections;
    }
    final List<ConnectionDTO> connections = NifiConnectionUtil.findConnectionsMatchingDestinationGroupId(connectionsEntity, processGroup.getId());
    if (connections == null) {
        return deletedConnections;
    }
    Set<String> removedConnections = new HashSet<>();
    // Delete the connections
    for (ConnectionDTO connection : connections) {
        final String type = connection.getSource().getType();
        log.info("Found connection {} matching source type {} and destination group {}.", connection.getId(), type, connection.getDestination().getId());
        // Stop the port
        if (NifiConstants.NIFI_PORT_TYPE.INPUT_PORT.name().equalsIgnoreCase(type)) {
            try {
                restClient.stopInputPort(connection.getSource().getGroupId(), connection.getSource().getId());
                log.info("Stopped input port {} for connection: {} ", connection.getSource().getId(), connection.getId());
            } catch (Exception e) {
                log.error("Failed to stop input port for connection: {}", connection.getId(), e);
                throw new NifiClientRuntimeException("Error stopping the input port " + connection.getSource().getId() + " for connection " + connection.getId() + " prior to deleting the " + "connection.");
            }
        }
        // Delete the connection
        try {
            restClient.deleteConnection(connection, false);
            removedConnections.add(connection.getId());
            deletedConnections.add(connection);
        } catch (Exception e) {
            log.error("Failed to delete the connection: {}", connection.getId(), e);
            final String source = (connection.getSource() != null) ? connection.getSource().getName() : null;
            final String destination = (connection.getDestination() != null) ? connection.getDestination().getName() : null;
            throw new NifiClientRuntimeException("Error deleting the connection " + connection.getId() + " with source " + source + " and destination " + destination + ".");
        }
    }
    nifiObjectCache.removeConnections(processGroup.getParentGroupId(), removedConnections);
    return deletedConnections;
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ArrayList(java.util.ArrayList) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) HashSet(java.util.HashSet)

Example 30 with ConnectionDTO

use of org.apache.nifi.web.api.dto.ConnectionDTO in project kylo by Teradata.

the class RemoteProcessGroupValidator method updateRemoteConnection.

/**
 * When updating the connections to remote process groups the system needs to detect if the RemoteProcessGroup is valid (has no issues connecting to the targetURI prior to making the connection)
 * This call will try and sleep and retry if the remoteProcessGroup has connection issues
 *
 * @param remoteProcessGroupConnectionDTO the connection to update
 * @param validation                      the holder of validation success/failures
 * @param retryCount                      the number of retries already attempted for this connection
 * @return true if successful, false if not.  The Validation object will also be populated with the validation information
 */
private boolean updateRemoteConnection(UpdatedConnection updatedConnection, RemoteProcessGroupValidation validation, int retryCount) {
    // ensure we are not attempting to authorize
    // TODO pull timeouts to configurable parameters
    boolean success = false;
    final int sleepTimeMillis = templateConnectionUtil.getRemoteProcessGroupSleepTime();
    final int maxAttempts = templateConnectionUtil.getRemoteProcessGroupMaxAttempts();
    ConnectionDTO connectionDTO = updatedConnection.getNewConnection();
    // set the destination to that id
    if (updatedConnection.getRemoteProcessGroupDTO().hasRemoteProcessGroupAuthorizationIssues() || updatedConnection.getRemoteProcessGroupDTO().getRemoteProcessGroup().getInputPortCount() == 0) {
        // wait
        log.info("Authorization issue found when attempting to update Remote Process Group Port connection for {}.  Retry Attempt: {} ", connectionDTO.getDestination().getName(), retryCount);
        if (retryCount <= maxAttempts) {
            Uninterruptibles.sleepUninterruptibly(sleepTimeMillis, TimeUnit.MILLISECONDS);
            Optional<RemoteProcessGroupDTO> remoteProcessGroupDTO = restClient.getNiFiRestClient().remoteProcessGroups().findById(updatedConnection.getRemoteProcessGroupDTO().getRemoteProcessGroup().getId());
            if (remoteProcessGroupDTO.isPresent()) {
                updatedConnection.updateRemoteProcessGroup(remoteProcessGroupDTO.get());
                retryCount++;
                return updateRemoteConnection(updatedConnection, validation, retryCount);
            } else {
                validation.addInvalidConnection(connectionDTO);
                success = false;
            }
        } else {
            validation.addInvalidConnection(connectionDTO);
            success = false;
        }
    } else if (connectionDTO.getId() == null) {
        try {
            ConnectionDTO connection = new ConnectionDTO();
            connection.setSource(connectionDTO.getSource());
            connection.setDestination(connectionDTO.getDestination());
            connection.setParentGroupId(connectionDTO.getParentGroupId());
            connection.setSelectedRelationships(new HashSet<>());
            connection.getSelectedRelationships().add("success");
            connection.setName(StringUtils.isNotBlank(connectionDTO.getName()) ? connectionDTO.getName() : (connectionDTO.getSource().getName() + " - " + connectionDTO.getDestination().getName()));
            connection = restClient.getNiFiRestClient().processGroups().createConnection(connection);
            if (connection != null) {
                updatedConnection.setNewConnection(connection);
                updatedConnection.setUpdated(true);
                success = true;
            } else {
                success = false;
            }
        } catch (Exception e) {
            log.info("Error found attempting to create the new connection to the Remote Process Group for {} in Parent Process Group: {}.  Retry Attempt: {} ", connectionDTO.getDestination().getName(), connectionDTO.getParentGroupId(), retryCount);
            if (retryCount <= maxAttempts) {
                Uninterruptibles.sleepUninterruptibly(sleepTimeMillis, TimeUnit.MILLISECONDS);
                retryCount++;
                return updateRemoteConnection(updatedConnection, validation, retryCount);
            } else {
                // unable to create new connection
                validation.addInvalidConnection(connectionDTO);
                success = false;
            }
        }
    } else {
        log.info("Updating Remote Process Group Port connection for {} ", connectionDTO.getDestination().getName());
        try {
            Optional<ConnectionDTO> connection = restClient.getNiFiRestClient().connections().update(connectionDTO);
            if (connection.isPresent()) {
                validation.addUpdatedConnection(connection.get());
                success = true;
            } else {
                validation.addInvalidConnection(connectionDTO);
                success = false;
            }
        } catch (Exception e) {
            validation.addInvalidConnection(connectionDTO);
            success = false;
        }
    }
    return success;
}
Also used : Optional(java.util.Optional) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) RemoteProcessGroupDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupDTO) HashSet(java.util.HashSet)

Aggregations

ConnectionDTO (org.apache.nifi.web.api.dto.ConnectionDTO)66 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)35 ArrayList (java.util.ArrayList)32 ConnectableDTO (org.apache.nifi.web.api.dto.ConnectableDTO)32 HashSet (java.util.HashSet)30 PortDTO (org.apache.nifi.web.api.dto.PortDTO)28 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)25 List (java.util.List)24 HashMap (java.util.HashMap)20 Set (java.util.Set)20 Collectors (java.util.stream.Collectors)18 RemoteProcessGroupDTO (org.apache.nifi.web.api.dto.RemoteProcessGroupDTO)17 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 Map (java.util.Map)16 NifiClientRuntimeException (com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException)15 Inject (javax.inject.Inject)15 NifiConnectionUtil (com.thinkbiganalytics.nifi.rest.support.NifiConnectionUtil)14 Optional (java.util.Optional)13 StringUtils (org.apache.commons.lang3.StringUtils)13