Search in sources :

Example 1 with ConnectableType

use of org.apache.nifi.connectable.ConnectableType in project nifi by apache.

the class ConnectionResource method updateConnection.

/**
 * Updates the specified connection.
 *
 * @param httpServletRequest request
 * @param id                 The id of the connection.
 * @param requestConnectionEntity   A connectionEntity.
 * @return A connectionEntity.
 * @throws InterruptedException if interrupted
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
@ApiOperation(value = "Updates a connection", response = ConnectionEntity.class, authorizations = { @Authorization(value = "Write Source - /{component-type}/{uuid}"), @Authorization(value = "Write Destination - /{component-type}/{uuid}"), @Authorization(value = "Write New Destination - /{component-type}/{uuid} - if updating Destination"), @Authorization(value = "Write Process Group - /process-groups/{uuid} - if updating Destination") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response updateConnection(@Context HttpServletRequest httpServletRequest, @ApiParam(value = "The connection id.", required = true) @PathParam("id") final String id, @ApiParam(value = "The connection configuration details.", required = true) final ConnectionEntity requestConnectionEntity) throws InterruptedException {
    if (requestConnectionEntity == null || requestConnectionEntity.getComponent() == null) {
        throw new IllegalArgumentException("Connection details must be specified.");
    }
    if (requestConnectionEntity.getRevision() == null) {
        throw new IllegalArgumentException("Revision must be specified.");
    }
    // ensure the ids are the same
    final ConnectionDTO requestConnection = requestConnectionEntity.getComponent();
    if (!id.equals(requestConnection.getId())) {
        throw new IllegalArgumentException(String.format("The connection id " + "(%s) in the request body does not equal the connection id of the " + "requested resource (%s).", requestConnection.getId(), id));
    }
    if (requestConnection.getDestination() != null) {
        if (requestConnection.getDestination().getId() == null) {
            throw new IllegalArgumentException("When specifying a destination component, the destination id is required.");
        }
        if (requestConnection.getDestination().getType() == null) {
            throw new IllegalArgumentException("When specifying a destination component, the type of the destination is required.");
        }
    }
    final List<PositionDTO> proposedBends = requestConnection.getBends();
    if (proposedBends != null) {
        for (final PositionDTO proposedBend : proposedBends) {
            if (proposedBend.getX() == null || proposedBend.getY() == null) {
                throw new IllegalArgumentException("The x and y coordinate of the each bend must be specified.");
            }
        }
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.PUT, requestConnectionEntity);
    }
    final Revision requestRevision = getRevision(requestConnectionEntity, id);
    return withWriteLock(serviceFacade, requestConnectionEntity, requestRevision, lookup -> {
        // verifies write access to this connection (this checks the current source and destination)
        ConnectionAuthorizable connAuth = lookup.getConnection(id);
        connAuth.getAuthorizable().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // if a destination has been specified and is different
        final Connectable currentDestination = connAuth.getDestination();
        if (requestConnection.getDestination() != null && !currentDestination.getIdentifier().equals(requestConnection.getDestination().getId())) {
            try {
                final ConnectableType destinationConnectableType = ConnectableType.valueOf(requestConnection.getDestination().getType());
                // explicitly handle RPGs differently as the connectable id can be ambiguous if self referencing
                final Authorizable newDestinationAuthorizable;
                if (ConnectableType.REMOTE_INPUT_PORT.equals(destinationConnectableType)) {
                    newDestinationAuthorizable = lookup.getRemoteProcessGroup(requestConnection.getDestination().getGroupId());
                } else {
                    newDestinationAuthorizable = lookup.getLocalConnectable(requestConnection.getDestination().getId());
                }
                // verify access of the new destination (current destination was already authorized as part of the connection check)
                newDestinationAuthorizable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
            } catch (final IllegalArgumentException e) {
                throw new IllegalArgumentException(String.format("Unrecognized destination type %s. Excepted values are [%s]", requestConnection.getDestination().getType(), StringUtils.join(ConnectableType.values(), ", ")));
            }
            // verify access of the parent group (this is the same check that is performed when creating the connection)
            connAuth.getParentGroup().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        }
    }, () -> serviceFacade.verifyUpdateConnection(requestConnection), (revision, connectionEntity) -> {
        final ConnectionDTO connection = connectionEntity.getComponent();
        final ConnectionEntity entity = serviceFacade.updateConnection(revision, connection);
        populateRemainingConnectionEntityContent(entity);
        // generate the response
        return generateOkResponse(entity).build();
    });
}
Also used : Revision(org.apache.nifi.web.Revision) Connectable(org.apache.nifi.connectable.Connectable) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) Authorizable(org.apache.nifi.authorization.resource.Authorizable) ConnectionAuthorizable(org.apache.nifi.authorization.ConnectionAuthorizable) ConnectableType(org.apache.nifi.connectable.ConnectableType) ConnectionAuthorizable(org.apache.nifi.authorization.ConnectionAuthorizable) ConnectionEntity(org.apache.nifi.web.api.entity.ConnectionEntity) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with ConnectableType

use of org.apache.nifi.connectable.ConnectableType in project nifi by apache.

the class ProcessGroupResource method createConnection.

// -----------
// connections
// -----------
/**
 * Creates a new connection.
 *
 * @param httpServletRequest request
 * @param groupId            The group id
 * @param requestConnectionEntity   A connectionEntity.
 * @return A connectionEntity.
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/connections")
@ApiOperation(value = "Creates a connection", response = ConnectionEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{uuid}"), @Authorization(value = "Write Source - /{component-type}/{uuid}"), @Authorization(value = "Write Destination - /{component-type}/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response createConnection(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, @ApiParam(value = "The connection configuration details.", required = true) final ConnectionEntity requestConnectionEntity) {
    if (requestConnectionEntity == null || requestConnectionEntity.getComponent() == null) {
        throw new IllegalArgumentException("Connection details must be specified.");
    }
    if (requestConnectionEntity.getRevision() == null || (requestConnectionEntity.getRevision().getVersion() == null || requestConnectionEntity.getRevision().getVersion() != 0)) {
        throw new IllegalArgumentException("A revision of 0 must be specified when creating a new Connection.");
    }
    if (requestConnectionEntity.getComponent().getId() != null) {
        throw new IllegalArgumentException("Connection ID cannot be specified.");
    }
    final List<PositionDTO> proposedBends = requestConnectionEntity.getComponent().getBends();
    if (proposedBends != null) {
        for (final PositionDTO proposedBend : proposedBends) {
            if (proposedBend.getX() == null || proposedBend.getY() == null) {
                throw new IllegalArgumentException("The x and y coordinate of the each bend must be specified.");
            }
        }
    }
    if (requestConnectionEntity.getComponent().getParentGroupId() != null && !groupId.equals(requestConnectionEntity.getComponent().getParentGroupId())) {
        throw new IllegalArgumentException(String.format("If specified, the parent process group id %s must be the same as specified in the URI %s", requestConnectionEntity.getComponent().getParentGroupId(), groupId));
    }
    requestConnectionEntity.getComponent().setParentGroupId(groupId);
    // get the connection
    final ConnectionDTO requestConnection = requestConnectionEntity.getComponent();
    if (requestConnection.getSource() == null || requestConnection.getSource().getId() == null) {
        throw new IllegalArgumentException("The source of the connection must be specified.");
    }
    if (requestConnection.getSource().getType() == null) {
        throw new IllegalArgumentException("The type of the source of the connection must be specified.");
    }
    final ConnectableType sourceConnectableType;
    try {
        sourceConnectableType = ConnectableType.valueOf(requestConnection.getSource().getType());
    } catch (final IllegalArgumentException e) {
        throw new IllegalArgumentException(String.format("Unrecognized source type %s. Expected values are [%s]", requestConnection.getSource().getType(), StringUtils.join(ConnectableType.values(), ", ")));
    }
    if (requestConnection.getDestination() == null || requestConnection.getDestination().getId() == null) {
        throw new IllegalArgumentException("The destination of the connection must be specified.");
    }
    if (requestConnection.getDestination().getType() == null) {
        throw new IllegalArgumentException("The type of the destination of the connection must be specified.");
    }
    final ConnectableType destinationConnectableType;
    try {
        destinationConnectableType = ConnectableType.valueOf(requestConnection.getDestination().getType());
    } catch (final IllegalArgumentException e) {
        throw new IllegalArgumentException(String.format("Unrecognized destination type %s. Expected values are [%s]", requestConnection.getDestination().getType(), StringUtils.join(ConnectableType.values(), ", ")));
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.POST, requestConnectionEntity);
    }
    return withWriteLock(serviceFacade, requestConnectionEntity, lookup -> {
        // ensure write access to the group
        final Authorizable processGroup = lookup.getProcessGroup(groupId).getAuthorizable();
        processGroup.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // explicitly handle RPGs differently as the connectable id can be ambiguous if self referencing
        final Authorizable source;
        if (ConnectableType.REMOTE_OUTPUT_PORT.equals(sourceConnectableType)) {
            source = lookup.getRemoteProcessGroup(requestConnection.getSource().getGroupId());
        } else {
            source = lookup.getLocalConnectable(requestConnection.getSource().getId());
        }
        // ensure write access to the source
        if (source == null) {
            throw new ResourceNotFoundException("Cannot find source component with ID [" + requestConnection.getSource().getId() + "]");
        }
        source.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // explicitly handle RPGs differently as the connectable id can be ambiguous if self referencing
        final Authorizable destination;
        if (ConnectableType.REMOTE_INPUT_PORT.equals(destinationConnectableType)) {
            destination = lookup.getRemoteProcessGroup(requestConnection.getDestination().getGroupId());
        } else {
            destination = lookup.getLocalConnectable(requestConnection.getDestination().getId());
        }
        // ensure write access to the destination
        if (destination == null) {
            throw new ResourceNotFoundException("Cannot find destination component with ID [" + requestConnection.getDestination().getId() + "]");
        }
        destination.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
    }, () -> serviceFacade.verifyCreateConnection(groupId, requestConnection), connectionEntity -> {
        final ConnectionDTO connection = connectionEntity.getComponent();
        // set the processor id as appropriate
        connection.setId(generateUuid());
        // create the new relationship target
        final Revision revision = getRevision(connectionEntity, connection.getId());
        final ConnectionEntity entity = serviceFacade.createConnection(revision, groupId, connection);
        connectionResource.populateRemainingConnectionEntityContent(entity);
        // extract the href and build the response
        String uri = entity.getUri();
        return generateCreatedResponse(URI.create(uri), entity).build();
    });
}
Also used : Revision(org.apache.nifi.web.Revision) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) SnippetAuthorizable(org.apache.nifi.authorization.SnippetAuthorizable) TemplateContentsAuthorizable(org.apache.nifi.authorization.TemplateContentsAuthorizable) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) ConnectableType(org.apache.nifi.connectable.ConnectableType) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) ConnectionEntity(org.apache.nifi.web.api.entity.ConnectionEntity) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ConnectableType

use of org.apache.nifi.connectable.ConnectableType in project nifi by apache.

the class StandardFlowSerializer method addConnection.

private void addConnection(final Element parentElement, final Connection connection) {
    final Document doc = parentElement.getOwnerDocument();
    final Element element = doc.createElement("connection");
    parentElement.appendChild(element);
    addTextElement(element, "id", connection.getIdentifier());
    addTextElement(element, "versionedComponentId", connection.getVersionedComponentId());
    addTextElement(element, "name", connection.getName());
    final Element bendPointsElement = doc.createElement("bendPoints");
    element.appendChild(bendPointsElement);
    for (final Position bendPoint : connection.getBendPoints()) {
        addPosition(bendPointsElement, bendPoint, "bendPoint");
    }
    addTextElement(element, "labelIndex", connection.getLabelIndex());
    addTextElement(element, "zIndex", connection.getZIndex());
    final String sourceId = connection.getSource().getIdentifier();
    final ConnectableType sourceType = connection.getSource().getConnectableType();
    final String sourceGroupId;
    if (sourceType == ConnectableType.REMOTE_OUTPUT_PORT) {
        sourceGroupId = ((RemoteGroupPort) connection.getSource()).getRemoteProcessGroup().getIdentifier();
    } else {
        sourceGroupId = connection.getSource().getProcessGroup().getIdentifier();
    }
    final ConnectableType destinationType = connection.getDestination().getConnectableType();
    final String destinationId = connection.getDestination().getIdentifier();
    final String destinationGroupId;
    if (destinationType == ConnectableType.REMOTE_INPUT_PORT) {
        destinationGroupId = ((RemoteGroupPort) connection.getDestination()).getRemoteProcessGroup().getIdentifier();
    } else {
        destinationGroupId = connection.getDestination().getProcessGroup().getIdentifier();
    }
    addTextElement(element, "sourceId", sourceId);
    addTextElement(element, "sourceGroupId", sourceGroupId);
    addTextElement(element, "sourceType", sourceType.toString());
    addTextElement(element, "destinationId", destinationId);
    addTextElement(element, "destinationGroupId", destinationGroupId);
    addTextElement(element, "destinationType", destinationType.toString());
    for (final Relationship relationship : connection.getRelationships()) {
        addTextElement(element, "relationship", relationship.getName());
    }
    addTextElement(element, "maxWorkQueueSize", connection.getFlowFileQueue().getBackPressureObjectThreshold());
    addTextElement(element, "maxWorkQueueDataSize", connection.getFlowFileQueue().getBackPressureDataSizeThreshold());
    addTextElement(element, "flowFileExpiration", connection.getFlowFileQueue().getFlowFileExpiration());
    for (final FlowFilePrioritizer comparator : connection.getFlowFileQueue().getPriorities()) {
        final String className = comparator.getClass().getCanonicalName();
        addTextElement(element, "queuePrioritizerClass", className);
    }
    parentElement.appendChild(element);
}
Also used : Position(org.apache.nifi.connectable.Position) RemoteGroupPort(org.apache.nifi.remote.RemoteGroupPort) Element(org.w3c.dom.Element) Relationship(org.apache.nifi.processor.Relationship) ConnectableType(org.apache.nifi.connectable.ConnectableType) FlowFilePrioritizer(org.apache.nifi.flowfile.FlowFilePrioritizer) Document(org.w3c.dom.Document)

Example 4 with ConnectableType

use of org.apache.nifi.connectable.ConnectableType in project nifi by apache.

the class TestStandardRemoteGroupPort method setupMock.

private void setupMock(final SiteToSiteTransportProtocol protocol, final TransferDirection direction, final SiteToSiteClientConfig siteToSiteClientConfig) throws Exception {
    processGroup = null;
    remoteGroup = mock(RemoteProcessGroup.class);
    scheduler = null;
    siteToSiteClient = mock(SiteToSiteClient.class);
    this.transaction = mock(Transaction.class);
    eventReporter = mock(EventReporter.class);
    final ConnectableType connectableType;
    switch(direction) {
        case SEND:
            connectableType = ConnectableType.REMOTE_INPUT_PORT;
            break;
        case RECEIVE:
            connectableType = ConnectableType.OUTPUT_PORT;
            break;
        default:
            connectableType = null;
            break;
    }
    port = spy(new StandardRemoteGroupPort(ID, ID, NAME, processGroup, remoteGroup, direction, connectableType, null, scheduler, NiFiProperties.createBasicNiFiProperties(null, null)));
    doReturn(true).when(remoteGroup).isTransmitting();
    doReturn(protocol).when(remoteGroup).getTransportProtocol();
    doReturn(REMOTE_CLUSTER_URL).when(remoteGroup).getTargetUri();
    doReturn(siteToSiteClient).when(port).getSiteToSiteClient();
    doReturn(transaction).when(siteToSiteClient).createTransaction(eq(direction));
    doReturn(siteToSiteClientConfig).when(siteToSiteClient).getConfig();
    doReturn(eventReporter).when(remoteGroup).getEventReporter();
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) ConnectableType(org.apache.nifi.connectable.ConnectableType) EventReporter(org.apache.nifi.events.EventReporter)

Aggregations

ConnectableType (org.apache.nifi.connectable.ConnectableType)4 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Consumes (javax.ws.rs.Consumes)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Authorizable (org.apache.nifi.authorization.resource.Authorizable)2 Revision (org.apache.nifi.web.Revision)2 ConnectionDTO (org.apache.nifi.web.api.dto.ConnectionDTO)2 PositionDTO (org.apache.nifi.web.api.dto.PositionDTO)2 ConnectionEntity (org.apache.nifi.web.api.entity.ConnectionEntity)2 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 ComponentAuthorizable (org.apache.nifi.authorization.ComponentAuthorizable)1 ConnectionAuthorizable (org.apache.nifi.authorization.ConnectionAuthorizable)1 ProcessGroupAuthorizable (org.apache.nifi.authorization.ProcessGroupAuthorizable)1 SnippetAuthorizable (org.apache.nifi.authorization.SnippetAuthorizable)1 TemplateContentsAuthorizable (org.apache.nifi.authorization.TemplateContentsAuthorizable)1 Connectable (org.apache.nifi.connectable.Connectable)1 Position (org.apache.nifi.connectable.Position)1