Search in sources :

Example 11 with ProcessorEntity

use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.

the class ProcessorResource method clearState.

/**
 * Clears the state for a processor.
 *
 * @param httpServletRequest servlet request
 * @param id                 The id of the processor
 * @return a componentStateEntity
 * @throws InterruptedException if interrupted
 */
@POST
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/state/clear-requests")
@ApiOperation(value = "Clears the state for a processor", response = ComponentStateEntity.class, authorizations = { @Authorization(value = "Write - /processors/{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 clearState(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The processor id.", required = true) @PathParam("id") final String id) throws InterruptedException {
    if (isReplicateRequest()) {
        return replicate(HttpMethod.POST);
    }
    final ProcessorEntity requestProcessorEntity = new ProcessorEntity();
    requestProcessorEntity.setId(id);
    return withWriteLock(serviceFacade, requestProcessorEntity, lookup -> {
        final Authorizable processor = lookup.getProcessor(id).getAuthorizable();
        processor.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
    }, () -> serviceFacade.verifyCanClearProcessorState(id), (processorEntity) -> {
        // get the component state
        serviceFacade.clearProcessorState(processorEntity.getId());
        // generate the response entity
        final ComponentStateEntity entity = new ComponentStateEntity();
        // generate the response
        return generateOkResponse(entity).build();
    });
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) ComponentStateEntity(org.apache.nifi.web.api.entity.ComponentStateEntity) 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 12 with ProcessorEntity

use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.

the class EntityFactory method createProcessorEntity.

public ProcessorEntity createProcessorEntity(final ProcessorDTO dto, final RevisionDTO revision, final PermissionsDTO permissions, final ProcessorStatusDTO status, final List<BulletinEntity> bulletins) {
    final ProcessorEntity entity = new ProcessorEntity();
    entity.setRevision(revision);
    if (dto != null) {
        entity.setPermissions(permissions);
        entity.setStatus(status);
        entity.setId(dto.getId());
        entity.setInputRequirement(dto.getInputRequirement());
        entity.setPosition(dto.getPosition());
        if (permissions != null && permissions.getCanRead()) {
            entity.setComponent(dto);
            entity.setBulletins(bulletins);
        }
    }
    return entity;
}
Also used : ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity)

Example 13 with ProcessorEntity

use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.

the class ClusterReplicationComponentLifecycle method waitForProcessorStatus.

/**
 * Periodically polls the process group with the given ID, waiting for all processors whose ID's are given to have the given Scheduled State.
 *
 * @param user the user making the request
 * @param originalUri the original uri
 * @param groupId the ID of the Process Group to poll
 * @param processors the Processors whose state should be equal to the given desired state
 * @param desiredState the desired state for all processors with the ID's given
 * @param pause the Pause that can be used to wait between polling
 * @return <code>true</code> if successful, <code>false</code> if unable to wait for processors to reach the desired state
 */
private boolean waitForProcessorStatus(final NiFiUser user, final URI originalUri, final String groupId, final Map<String, AffectedComponentEntity> processors, final ScheduledState desiredState, final Pause pause) throws InterruptedException {
    URI groupUri;
    try {
        groupUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/process-groups/" + groupId + "/processors", "includeDescendantGroups=true", originalUri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    final MultivaluedMap<String, String> requestEntity = new MultivaluedHashMap<>();
    boolean continuePolling = true;
    while (continuePolling) {
        // Determine whether we should replicate only to the cluster coordinator, or if we should replicate directly to the cluster nodes themselves.
        final NodeResponse clusterResponse;
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            clusterResponse = getRequestReplicator().replicate(user, HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        } else {
            clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), user, HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        }
        if (clusterResponse.getStatus() != Status.OK.getStatusCode()) {
            return false;
        }
        final ProcessorsEntity processorsEntity = getResponseEntity(clusterResponse, ProcessorsEntity.class);
        final Set<ProcessorEntity> processorEntities = processorsEntity.getProcessors();
        if (isProcessorActionComplete(processorEntities, processors, desiredState)) {
            logger.debug("All {} processors of interest now have the desired state of {}", processors.size(), desiredState);
            return true;
        }
        // Not all of the processors are in the desired state. Pause for a bit and poll again.
        continuePolling = pause.pause();
    }
    return false;
}
Also used : HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) URISyntaxException(java.net.URISyntaxException) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity)

Example 14 with ProcessorEntity

use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.

the class NiFiWebApiTest method populateFlow.

public static void populateFlow(Client client, String baseUrl, NiFiTestUser user, String clientId) throws Exception {
    // -----------------------------------------------
    // Create a source processor
    // -----------------------------------------------
    // create the local selection processor
    ProcessorDTO processorDTO = new ProcessorDTO();
    processorDTO.setName("Pick up");
    processorDTO.setType(SourceTestProcessor.class.getName());
    // create the revision
    final RevisionDTO revision = new RevisionDTO();
    revision.setClientId(clientId);
    revision.setVersion(0l);
    // create the local selection processor entity
    ProcessorEntity processorEntity = new ProcessorEntity();
    processorEntity.setRevision(revision);
    processorEntity.setComponent(processorDTO);
    // add the processor
    Response response = user.testPost(baseUrl + "/process-groups/root/processors", processorEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // get the processors id
    processorEntity = response.readEntity(ProcessorEntity.class);
    processorDTO = processorEntity.getComponent();
    String localSelectionId = processorDTO.getId();
    String localSelectionGroupId = processorDTO.getParentGroupId();
    // -----------------------------------------------
    // Create a termination processor
    // -----------------------------------------------
    // create the termination processor
    processorDTO = new ProcessorDTO();
    processorDTO.setName("End");
    processorDTO.setType(TerminationTestProcessor.class.getName());
    // create the termination processor entity
    processorEntity = new ProcessorEntity();
    processorEntity.setRevision(revision);
    processorEntity.setComponent(processorDTO);
    // add the processor
    response = user.testPost(baseUrl + "/process-groups/root/processors", processorEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // get the processors id
    processorEntity = response.readEntity(ProcessorEntity.class);
    processorDTO = processorEntity.getComponent();
    String terminationId = processorDTO.getId();
    String terminationGroupId = processorDTO.getParentGroupId();
    // -----------------------------------------------
    // Connect the two processors
    // -----------------------------------------------
    ConnectableDTO source = new ConnectableDTO();
    source.setId(localSelectionId);
    source.setGroupId(localSelectionGroupId);
    source.setType(ConnectableType.PROCESSOR.name());
    ConnectableDTO target = new ConnectableDTO();
    target.setId(terminationId);
    target.setGroupId(terminationGroupId);
    target.setType(ConnectableType.PROCESSOR.name());
    // create the relationships
    Set<String> relationships = new HashSet<>();
    relationships.add("success");
    // create the connection
    ConnectionDTO connectionDTO = new ConnectionDTO();
    connectionDTO.setSource(source);
    connectionDTO.setDestination(target);
    connectionDTO.setSelectedRelationships(relationships);
    // create the connection entity
    ConnectionEntity connectionEntity = new ConnectionEntity();
    connectionEntity.setRevision(revision);
    connectionEntity.setComponent(connectionDTO);
    // add the processor
    response = user.testPost(baseUrl + "/process-groups/root/connections", connectionEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // -----------------------------------------------
    // Create a label
    // -----------------------------------------------
    // create the label
    LabelDTO labelDTO = new LabelDTO();
    labelDTO.setLabel("Test label");
    // create the label entity
    LabelEntity labelEntity = new LabelEntity();
    labelEntity.setRevision(revision);
    labelEntity.setComponent(labelDTO);
    // add the label
    response = user.testPost(baseUrl + "/process-groups/root/labels", labelEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // -----------------------------------------------
    // Create a funnel
    // -----------------------------------------------
    // create the funnel
    FunnelDTO funnelDTO = new FunnelDTO();
    // create the funnel entity
    FunnelEntity funnelEntity = new FunnelEntity();
    funnelEntity.setRevision(revision);
    funnelEntity.setComponent(funnelDTO);
    // add the funnel
    response = user.testPost(baseUrl + "/process-groups/root/funnels", funnelEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // -----------------------------------------------
    // Create a process group
    // -----------------------------------------------
    // create the process group
    ProcessGroupDTO processGroup = new ProcessGroupDTO();
    processGroup.setName("group name");
    // create the process group entity
    ProcessGroupEntity processGroupEntity = new ProcessGroupEntity();
    processGroupEntity.setRevision(revision);
    processGroupEntity.setComponent(processGroup);
    // add the process group
    response = user.testPost(baseUrl + "/process-groups/root/process-groups", processGroupEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // -----------------------------------------------
    // Create an input port
    // -----------------------------------------------
    // create the input port
    PortDTO inputPort = new PortDTO();
    inputPort.setName("input");
    // create the input port entity
    PortEntity inputPortEntity = new PortEntity();
    inputPortEntity.setRevision(revision);
    inputPortEntity.setComponent(inputPort);
    // add the input port
    response = user.testPost(baseUrl + "/process-groups/root/input-ports", inputPortEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
    // -----------------------------------------------
    // Create a output ports
    // -----------------------------------------------
    // create the process group
    PortDTO outputPort = new PortDTO();
    outputPort.setName("output");
    // create the process group entity
    PortEntity outputPortEntity = new PortEntity();
    outputPortEntity.setRevision(revision);
    outputPortEntity.setComponent(outputPort);
    // add the output port
    response = user.testPost(baseUrl + "/process-groups/root/output-ports", outputPortEntity);
    // ensure a successful response
    if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
        // since it was unable to create the component attempt to extract an
        // error message from the response body
        final String responseEntity = response.readEntity(String.class);
        throw new Exception("Unable to populate initial flow: " + responseEntity);
    }
}
Also used : ProcessGroupEntity(org.apache.nifi.web.api.entity.ProcessGroupEntity) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) PortDTO(org.apache.nifi.web.api.dto.PortDTO) FunnelDTO(org.apache.nifi.web.api.dto.FunnelDTO) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Response(javax.ws.rs.core.Response) FunnelEntity(org.apache.nifi.web.api.entity.FunnelEntity) LabelEntity(org.apache.nifi.web.api.entity.LabelEntity) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) TerminationTestProcessor(org.apache.nifi.integration.util.TerminationTestProcessor) LabelDTO(org.apache.nifi.web.api.dto.LabelDTO) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) SourceTestProcessor(org.apache.nifi.integration.util.SourceTestProcessor) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO) ConnectionEntity(org.apache.nifi.web.api.entity.ConnectionEntity) HashSet(java.util.HashSet) PortEntity(org.apache.nifi.web.api.entity.PortEntity)

Example 15 with ProcessorEntity

use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.

the class ITConnectionAccessControl method createConnection.

private ConnectionEntity createConnection(final String name) throws Exception {
    String url = helper.getBaseUrl() + "/process-groups/root/connections";
    // get two processors
    final ProcessorEntity one = ITProcessorAccessControl.createProcessor(helper, "one");
    final ProcessorEntity two = ITProcessorAccessControl.createProcessor(helper, "two");
    // create the source connectable
    ConnectableDTO source = new ConnectableDTO();
    source.setId(one.getId());
    source.setGroupId(one.getComponent().getParentGroupId());
    source.setType(ConnectableType.PROCESSOR.name());
    // create the target connectable
    ConnectableDTO target = new ConnectableDTO();
    target.setId(two.getId());
    target.setGroupId(two.getComponent().getParentGroupId());
    target.setType(ConnectableType.PROCESSOR.name());
    // create the relationships
    Set<String> relationships = new HashSet<>();
    relationships.add("success");
    // create the connection
    ConnectionDTO connection = new ConnectionDTO();
    connection.setName(name);
    connection.setSource(source);
    connection.setDestination(target);
    connection.setSelectedRelationships(relationships);
    // create the revision
    final RevisionDTO revision = new RevisionDTO();
    revision.setClientId(READ_WRITE_CLIENT_ID);
    revision.setVersion(0L);
    // create the entity body
    ConnectionEntity entity = new ConnectionEntity();
    entity.setRevision(revision);
    entity.setComponent(connection);
    // perform the request
    Response response = helper.getReadWriteUser().testPost(url, entity);
    // ensure the request is successful
    assertEquals(201, response.getStatus());
    // get the entity body
    entity = response.readEntity(ConnectionEntity.class);
    // verify creation
    connection = entity.getComponent();
    assertEquals(name, connection.getName());
    // get the connection
    return entity;
}
Also used : Response(javax.ws.rs.core.Response) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) ConnectableDTO(org.apache.nifi.web.api.dto.ConnectableDTO) ConnectionEntity(org.apache.nifi.web.api.entity.ConnectionEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) HashSet(java.util.HashSet)

Aggregations

ProcessorEntity (org.apache.nifi.web.api.entity.ProcessorEntity)60 Response (javax.ws.rs.core.Response)29 Test (org.junit.Test)26 HashMap (java.util.HashMap)20 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)19 HashSet (java.util.HashSet)17 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)17 URI (java.net.URI)16 RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)16 Map (java.util.Map)15 NodeResponse (org.apache.nifi.cluster.manager.NodeResponse)12 NiFiUserDetails (org.apache.nifi.authorization.user.NiFiUserDetails)11 NiFiAuthenticationToken (org.apache.nifi.web.security.token.NiFiAuthenticationToken)11 Authentication (org.springframework.security.core.Authentication)11 Set (java.util.Set)10 Authorizable (org.apache.nifi.authorization.resource.Authorizable)10 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)10 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ClusterCoordinator (org.apache.nifi.cluster.coordination.ClusterCoordinator)8