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;
}
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;
}
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;
}
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);
}
}
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);
}
Aggregations