Search in sources :

Example 1 with ComponentDTO

use of org.apache.nifi.web.api.dto.ComponentDTO in project nifi by apache.

the class SnippetUtils method normalizeCoordinates.

/**
 * Will normalize the coordinates of the components to ensure their
 * consistency across exports. It will do so by fist calculating the
 * smallest X and smallest Y and then subtracting it from all X's and Y's of
 * each component ensuring that coordinates are consistent across export
 * while preserving relative locations set by the user.
 */
private void normalizeCoordinates(Collection<? extends ComponentDTO> components) {
    // determine the smallest x,y coordinates in the collection of components
    double smallestX = Double.MAX_VALUE;
    double smallestY = Double.MAX_VALUE;
    for (ComponentDTO component : components) {
        // to check those bend points for the smallest x,y coordinates
        if (component instanceof ConnectionDTO) {
            final ConnectionDTO connection = (ConnectionDTO) component;
            for (final PositionDTO position : connection.getBends()) {
                smallestX = Math.min(smallestX, position.getX());
                smallestY = Math.min(smallestY, position.getY());
            }
        } else {
            smallestX = Math.min(smallestX, component.getPosition().getX());
            smallestY = Math.min(smallestY, component.getPosition().getY());
        }
    }
    // position the components accordingly
    for (ComponentDTO component : components) {
        if (component instanceof ConnectionDTO) {
            final ConnectionDTO connection = (ConnectionDTO) component;
            for (final PositionDTO position : connection.getBends()) {
                position.setX(position.getX() - smallestX);
                position.setY(position.getY() - smallestY);
            }
        } else {
            component.getPosition().setX(component.getPosition().getX() - smallestX);
            component.getPosition().setY(component.getPosition().getY() - smallestY);
        }
    }
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ComponentDTO(org.apache.nifi.web.api.dto.ComponentDTO) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO)

Example 2 with ComponentDTO

use of org.apache.nifi.web.api.dto.ComponentDTO in project nifi by apache.

the class SnippetUtils method applyUpdatedPositions.

/**
 * Applies the updated positions to the corresponding components.
 *
 * @param componentPositionLookup  lookup
 * @param connectionPositionLookup lookup
 */
private static void applyUpdatedPositions(final Map<ComponentDTO, PositionDTO> componentPositionLookup, final Map<ConnectionDTO, List<PositionDTO>> connectionPositionLookup) {
    for (final Map.Entry<ComponentDTO, PositionDTO> entry : componentPositionLookup.entrySet()) {
        final ComponentDTO component = entry.getKey();
        final PositionDTO position = entry.getValue();
        component.setPosition(position);
    }
    for (final Map.Entry<ConnectionDTO, List<PositionDTO>> entry : connectionPositionLookup.entrySet()) {
        final ConnectionDTO connection = entry.getKey();
        final List<PositionDTO> bends = entry.getValue();
        connection.setBends(bends);
    }
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ComponentDTO(org.apache.nifi.web.api.dto.ComponentDTO) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO)

Example 3 with ComponentDTO

use of org.apache.nifi.web.api.dto.ComponentDTO in project nifi by apache.

the class SnippetUtils method scaleSnippet.

/**
 * Scales the placement of individual components of the snippet by the
 * given factorX and factorY. Does not scale components in child process groups.
 *
 * @param snippet snippet
 * @param factorX x location scaling factor
 * @param factorY y location scaling factor
 */
public static void scaleSnippet(FlowSnippetDTO snippet, double factorX, double factorY) {
    // get the connections
    final Collection<ConnectionDTO> connections = getConnections(snippet);
    // get the components and their positions from the template contents
    final Collection<ComponentDTO> components = getComponents(snippet);
    // only perform the operation if there are components in this snippet
    if (connections.isEmpty() && components.isEmpty()) {
        return;
    }
    // get the component positions from the snippet contents
    final Map<ComponentDTO, PositionDTO> componentPositionLookup = getPositionLookup(components);
    final Map<ConnectionDTO, List<PositionDTO>> connectionPositionLookup = getConnectionPositionLookup(connections);
    // adjust all component positions
    for (final PositionDTO position : componentPositionLookup.values()) {
        position.setX(position.getX() * factorX);
        position.setY(position.getY() * factorY);
    }
    // adjust all connection positions
    for (final List<PositionDTO> bends : connectionPositionLookup.values()) {
        for (final PositionDTO bend : bends) {
            bend.setX(bend.getX() * factorX);
            bend.setY(bend.getY() * factorY);
        }
    }
    // apply the updated positions
    applyUpdatedPositions(componentPositionLookup, connectionPositionLookup);
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ComponentDTO(org.apache.nifi.web.api.dto.ComponentDTO) ArrayList(java.util.ArrayList) List(java.util.List) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO)

Example 4 with ComponentDTO

use of org.apache.nifi.web.api.dto.ComponentDTO in project nifi-minifi by apache.

the class FlowSnippetDTOEnricher method enrich.

public void enrich(FlowSnippetDTO flowSnippetDTO, final String encodingVersion) {
    List<FlowSnippetDTO> allFlowSnippets = getAllFlowSnippets(flowSnippetDTO);
    Set<RemoteProcessGroupDTO> remoteProcessGroups = getAll(allFlowSnippets, FlowSnippetDTO::getRemoteProcessGroups).collect(Collectors.toSet());
    Map<String, String> connectableNameMap = getAll(allFlowSnippets, FlowSnippetDTO::getProcessors).collect(Collectors.toMap(ComponentDTO::getId, ProcessorDTO::getName));
    Map<String, String> rpgIdToTargetIdMap = new HashMap<>();
    for (RemoteProcessGroupDTO remoteProcessGroupDTO : remoteProcessGroups) {
        final RemoteProcessGroupContentsDTO contents = remoteProcessGroupDTO.getContents();
        final Set<RemoteProcessGroupPortDTO> rpgInputPortDtos = nullToEmpty(contents.getInputPorts());
        final Set<RemoteProcessGroupPortDTO> rpgOutputPortDtos = nullToEmpty(contents.getOutputPorts());
        switch(encodingVersion) {
            case "1.2":
                // Map all port DTOs to their respective targetIds
                rpgIdToTargetIdMap.putAll(Stream.concat(rpgInputPortDtos.stream(), rpgOutputPortDtos.stream()).collect(Collectors.toMap(RemoteProcessGroupPortDTO::getId, RemoteProcessGroupPortDTO::getTargetId)));
                break;
            default:
                break;
        }
        addConnectables(connectableNameMap, rpgInputPortDtos, RemoteProcessGroupPortDTO::getId, RemoteProcessGroupPortDTO::getId);
        addConnectables(connectableNameMap, rpgOutputPortDtos, RemoteProcessGroupPortDTO::getId, RemoteProcessGroupPortDTO::getId);
    }
    addConnectables(connectableNameMap, getAll(allFlowSnippets, FlowSnippetDTO::getInputPorts).collect(Collectors.toList()), PortDTO::getId, PortDTO::getName);
    addConnectables(connectableNameMap, getAll(allFlowSnippets, FlowSnippetDTO::getOutputPorts).collect(Collectors.toList()), PortDTO::getId, PortDTO::getName);
    final Set<ConnectionDTO> connections = getAll(allFlowSnippets, FlowSnippetDTO::getConnections).collect(Collectors.toSet());
    // Enrich connection endpoints using known names and overriding with targetIds for remote ports
    for (ConnectionDTO connection : connections) {
        setName(connectableNameMap, connection.getSource(), rpgIdToTargetIdMap);
        setName(connectableNameMap, connection.getDestination(), rpgIdToTargetIdMap);
    }
    // Override any ids that are for Remote Ports to use their target Ids where available
    connections.stream().flatMap(connectionDTO -> Stream.of(connectionDTO.getSource(), connectionDTO.getDestination())).filter(connectable -> connectable.getType().equals(ConnectableType.REMOTE_OUTPUT_PORT.toString()) || connectable.getType().equals(ConnectableType.REMOTE_INPUT_PORT.toString())).forEach(connectable -> connectable.setId(Optional.ofNullable(rpgIdToTargetIdMap.get(connectable.getId())).orElse(connectable.getId())));
    // Establish unique names for connections
    for (ConnectionDTO connection : connections) {
        if (StringUtil.isNullOrEmpty(connection.getName())) {
            StringBuilder name = new StringBuilder();
            ConnectableDTO connectionSource = connection.getSource();
            name.append(determineValueForConnectable(connectionSource, rpgIdToTargetIdMap));
            name.append("/");
            if (connection.getSelectedRelationships() != null && connection.getSelectedRelationships().size() > 0) {
                name.append(connection.getSelectedRelationships().iterator().next());
            }
            name.append("/");
            ConnectableDTO connectionDestination = connection.getDestination();
            name.append(determineValueForConnectable(connectionDestination, rpgIdToTargetIdMap));
            connection.setName(name.toString());
        }
    }
    nullToEmpty(flowSnippetDTO.getProcessGroups()).stream().map(ProcessGroupDTO::getContents).forEach(snippetDTO -> enrich(snippetDTO, encodingVersion));
}
Also used : RemoteProcessGroupDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupDTO) RemoteProcessGroupContentsDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupContentsDTO) ConnectableType(org.apache.nifi.connectable.ConnectableType) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) StringUtil(org.apache.nifi.minifi.commons.schema.common.StringUtil) Map(java.util.Map) Collection(java.util.Collection) Set(java.util.Set) RemoteProcessGroupPortDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupPortDTO) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ComponentDTO(org.apache.nifi.web.api.dto.ComponentDTO) 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) Optional(java.util.Optional) CollectionUtil.nullToEmpty(org.apache.nifi.minifi.commons.schema.common.CollectionUtil.nullToEmpty) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) HashMap(java.util.HashMap) RemoteProcessGroupPortDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupPortDTO) PortDTO(org.apache.nifi.web.api.dto.PortDTO) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) RemoteProcessGroupContentsDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupContentsDTO) RemoteProcessGroupDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupDTO) RemoteProcessGroupPortDTO(org.apache.nifi.web.api.dto.RemoteProcessGroupPortDTO) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO)

Example 5 with ComponentDTO

use of org.apache.nifi.web.api.dto.ComponentDTO in project nifi by apache.

the class SnippetUtils method moveAndScaleSnippet.

/**
 * Moves the content of the specified snippet around the specified location
 * and scales the placement of individual components of the template by the
 * given factorX and factorY.  Does not scale components in child process groups.
 *
 * @param snippet snippet
 * @param x       x location
 * @param y       y location
 * @param factorX x location scaling factor
 * @param factorY y location scaling factor
 */
public static void moveAndScaleSnippet(FlowSnippetDTO snippet, Double x, Double y, double factorX, double factorY) {
    // ensure the point is specified
    if (x != null && y != null) {
        final PositionDTO origin = new PositionDTO(x, y);
        // get the connections
        final Collection<ConnectionDTO> connections = getConnections(snippet);
        // get the components and their positions from the template contents
        final Collection<ComponentDTO> components = getComponents(snippet);
        // only perform the operation if there are components in this snippet
        if (connections.isEmpty() && components.isEmpty()) {
            return;
        }
        // get the component positions from the snippet contents
        final Map<ComponentDTO, PositionDTO> componentPositionLookup = getPositionLookup(components);
        final Map<ConnectionDTO, List<PositionDTO>> connectionPositionLookup = getConnectionPositionLookup(connections);
        final PositionDTO currentOrigin = getOrigin(componentPositionLookup.values(), connectionPositionLookup.values());
        // adjust all component positions
        for (final PositionDTO position : componentPositionLookup.values()) {
            position.setX(origin.getX() + ((position.getX() - currentOrigin.getX()) * factorX));
            position.setY(origin.getY() + ((position.getY() - currentOrigin.getY()) * factorY));
        }
        // adjust all connection positions
        for (final List<PositionDTO> bends : connectionPositionLookup.values()) {
            for (final PositionDTO bend : bends) {
                bend.setX(origin.getX() + ((bend.getX() - currentOrigin.getX()) * factorX));
                bend.setY(origin.getY() + ((bend.getY() - currentOrigin.getY()) * factorY));
            }
        }
        // apply the updated positions
        applyUpdatedPositions(componentPositionLookup, connectionPositionLookup);
    }
}
Also used : ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ComponentDTO(org.apache.nifi.web.api.dto.ComponentDTO) ArrayList(java.util.ArrayList) List(java.util.List) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO)

Aggregations

ComponentDTO (org.apache.nifi.web.api.dto.ComponentDTO)6 ConnectionDTO (org.apache.nifi.web.api.dto.ConnectionDTO)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 PositionDTO (org.apache.nifi.web.api.dto.PositionDTO)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Collection (java.util.Collection)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 ConnectableType (org.apache.nifi.connectable.ConnectableType)2 ConnectableDTO (org.apache.nifi.web.api.dto.ConnectableDTO)2 StandardCharsets (java.nio.charset.StandardCharsets)1 SecureRandom (java.security.SecureRandom)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1