Search in sources :

Example 21 with ProcessorDTO

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

the class ProcessGroupResource method createProcessor.

// ----------
// processors
// ----------
/**
 * Creates a new processor.
 *
 * @param httpServletRequest request
 * @param groupId            The group id
 * @param requestProcessorEntity    A processorEntity.
 * @return A processorEntity.
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/processors")
@ApiOperation(value = "Creates a new processor", response = ProcessorEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{uuid}"), @Authorization(value = "Read - any referenced Controller Services - /controller-services/{uuid}"), @Authorization(value = "Write - if the Processor is restricted - /restricted-components") })
@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 createProcessor(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, @ApiParam(value = "The processor configuration details.", required = true) final ProcessorEntity requestProcessorEntity) {
    if (requestProcessorEntity == null || requestProcessorEntity.getComponent() == null) {
        throw new IllegalArgumentException("Processor details must be specified.");
    }
    if (requestProcessorEntity.getRevision() == null || (requestProcessorEntity.getRevision().getVersion() == null || requestProcessorEntity.getRevision().getVersion() != 0)) {
        throw new IllegalArgumentException("A revision of 0 must be specified when creating a new Processor.");
    }
    final ProcessorDTO requestProcessor = requestProcessorEntity.getComponent();
    if (requestProcessor.getId() != null) {
        throw new IllegalArgumentException("Processor ID cannot be specified.");
    }
    if (StringUtils.isBlank(requestProcessor.getType())) {
        throw new IllegalArgumentException("The type of processor to create must be specified.");
    }
    final PositionDTO proposedPosition = requestProcessor.getPosition();
    if (proposedPosition != null) {
        if (proposedPosition.getX() == null || proposedPosition.getY() == null) {
            throw new IllegalArgumentException("The x and y coordinate of the proposed position must be specified.");
        }
    }
    if (requestProcessor.getParentGroupId() != null && !groupId.equals(requestProcessor.getParentGroupId())) {
        throw new IllegalArgumentException(String.format("If specified, the parent process group id %s must be the same as specified in the URI %s", requestProcessor.getParentGroupId(), groupId));
    }
    requestProcessor.setParentGroupId(groupId);
    if (isReplicateRequest()) {
        return replicate(HttpMethod.POST, requestProcessorEntity);
    }
    return withWriteLock(serviceFacade, requestProcessorEntity, lookup -> {
        final NiFiUser user = NiFiUserUtils.getNiFiUser();
        final Authorizable processGroup = lookup.getProcessGroup(groupId).getAuthorizable();
        processGroup.authorize(authorizer, RequestAction.WRITE, user);
        ComponentAuthorizable authorizable = null;
        try {
            authorizable = lookup.getConfigurableComponent(requestProcessor.getType(), requestProcessor.getBundle());
            if (authorizable.isRestricted()) {
                authorizeRestrictions(authorizer, authorizable);
            }
            final ProcessorConfigDTO config = requestProcessor.getConfig();
            if (config != null && config.getProperties() != null) {
                AuthorizeControllerServiceReference.authorizeControllerServiceReferences(config.getProperties(), authorizable, authorizer, lookup);
            }
        } finally {
            if (authorizable != null) {
                authorizable.cleanUpResources();
            }
        }
    }, () -> serviceFacade.verifyCreateProcessor(requestProcessor), processorEntity -> {
        final ProcessorDTO processor = processorEntity.getComponent();
        // set the processor id as appropriate
        processor.setId(generateUuid());
        // create the new processor
        final Revision revision = getRevision(processorEntity, processor.getId());
        final ProcessorEntity entity = serviceFacade.createProcessor(revision, groupId, processor);
        processorResource.populateRemainingProcessorEntityContent(entity);
        // generate a 201 created response
        String uri = entity.getUri();
        return generateCreatedResponse(URI.create(uri), entity).build();
    });
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) Revision(org.apache.nifi.web.Revision) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) 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) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) 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 22 with ProcessorDTO

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

the class ProcessorResource method updateProcessor.

/**
 * Updates the specified processor with the specified values.
 *
 * @param httpServletRequest request
 * @param id                 The id of the processor to update.
 * @param requestProcessorEntity    A processorEntity.
 * @return A processorEntity.
 * @throws InterruptedException if interrupted
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
@ApiOperation(value = "Updates a processor", response = ProcessorEntity.class, authorizations = { @Authorization(value = "Write - /processors/{uuid}"), @Authorization(value = "Read - any referenced Controller Services if this request changes the reference - /controller-services/{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 updateProcessor(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The processor id.", required = true) @PathParam("id") final String id, @ApiParam(value = "The processor configuration details.", required = true) final ProcessorEntity requestProcessorEntity) throws InterruptedException {
    if (requestProcessorEntity == null || requestProcessorEntity.getComponent() == null) {
        throw new IllegalArgumentException("Processor details must be specified.");
    }
    if (requestProcessorEntity.getRevision() == null) {
        throw new IllegalArgumentException("Revision must be specified.");
    }
    // ensure the same id is being used
    final ProcessorDTO requestProcessorDTO = requestProcessorEntity.getComponent();
    if (!id.equals(requestProcessorDTO.getId())) {
        throw new IllegalArgumentException(String.format("The processor id (%s) in the request body does " + "not equal the processor id of the requested resource (%s).", requestProcessorDTO.getId(), id));
    }
    final PositionDTO proposedPosition = requestProcessorDTO.getPosition();
    if (proposedPosition != null) {
        if (proposedPosition.getX() == null || proposedPosition.getY() == null) {
            throw new IllegalArgumentException("The x and y coordinate of the proposed position must be specified.");
        }
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.PUT, requestProcessorEntity);
    }
    // handle expects request (usually from the cluster manager)
    final Revision requestRevision = getRevision(requestProcessorEntity, id);
    return withWriteLock(serviceFacade, requestProcessorEntity, requestRevision, lookup -> {
        final NiFiUser user = NiFiUserUtils.getNiFiUser();
        final ComponentAuthorizable authorizable = lookup.getProcessor(id);
        authorizable.getAuthorizable().authorize(authorizer, RequestAction.WRITE, user);
        final ProcessorConfigDTO config = requestProcessorDTO.getConfig();
        if (config != null) {
            AuthorizeControllerServiceReference.authorizeControllerServiceReferences(config.getProperties(), authorizable, authorizer, lookup);
        }
    }, () -> serviceFacade.verifyUpdateProcessor(requestProcessorDTO), (revision, processorEntity) -> {
        final ProcessorDTO processorDTO = processorEntity.getComponent();
        // update the processor
        final ProcessorEntity entity = serviceFacade.updateProcessor(revision, processorDTO);
        populateRemainingProcessorEntityContent(entity);
        return generateOkResponse(entity).build();
    });
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) Revision(org.apache.nifi.web.Revision) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) 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 23 with ProcessorDTO

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

the class TemplateSerializerTest method validateDiffWithChangingComponentIdAndAdditionalElements.

@Test
public void validateDiffWithChangingComponentIdAndAdditionalElements() throws Exception {
    // Create initial template (TemplateDTO)
    FlowSnippetDTO snippet = new FlowSnippetDTO();
    Set<ProcessorDTO> procs = new HashSet<>();
    for (int i = 4; i > 0; i--) {
        ProcessorDTO procDTO = new ProcessorDTO();
        procDTO.setType("Processor" + i + ".class");
        procDTO.setId(ComponentIdGenerator.generateId().toString());
        procs.add(procDTO);
    }
    snippet.setProcessors(procs);
    TemplateDTO origTemplate = new TemplateDTO();
    origTemplate.setDescription("MyTemplate");
    origTemplate.setId("MyTemplate");
    origTemplate.setSnippet(snippet);
    byte[] serTemplate = TemplateSerializer.serialize(origTemplate);
    // Deserialize Template into TemplateDTO
    ByteArrayInputStream in = new ByteArrayInputStream(serTemplate);
    JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    XMLStreamReader xsr = XmlUtils.createSafeReader(in);
    JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
    TemplateDTO deserTemplate = templateElement.getValue();
    // Modify deserialized template
    FlowSnippetDTO deserSnippet = deserTemplate.getSnippet();
    Set<ProcessorDTO> deserProcs = deserSnippet.getProcessors();
    int c = 0;
    for (ProcessorDTO processorDTO : deserProcs) {
        if (c % 2 == 0) {
            processorDTO.setName("Hello-" + c);
        }
        c++;
    }
    // add new Processor
    ProcessorDTO procDTO = new ProcessorDTO();
    procDTO.setType("ProcessorNew" + ".class");
    procDTO.setId(ComponentIdGenerator.generateId().toString());
    deserProcs.add(procDTO);
    // Serialize modified template
    byte[] serTemplate2 = TemplateSerializer.serialize(deserTemplate);
    RawText rt1 = new RawText(serTemplate);
    RawText rt2 = new RawText(serTemplate2);
    EditList diffList = new EditList();
    diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (DiffFormatter diff = new DiffFormatter(out)) {
        diff.format(diffList, rt1, rt2);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), StandardCharsets.UTF_8));
        List<String> changes = reader.lines().peek(System.out::println).filter(line -> line.startsWith("+") || line.startsWith("-")).collect(Collectors.toList());
        assertEquals("+            <name>Hello-0</name>", changes.get(0));
        assertEquals("+            <name>Hello-2</name>", changes.get(1));
        assertEquals("+        <processors>", changes.get(2));
        assertEquals("+            <type>ProcessorNew.class</type>", changes.get(4));
        assertEquals("+        </processors>", changes.get(5));
    }
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) XmlUtils(org.apache.nifi.security.xml.XmlUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) RawText(org.eclipse.jgit.diff.RawText) JAXBContext(javax.xml.bind.JAXBContext) ComponentIdGenerator(org.apache.nifi.util.ComponentIdGenerator) Unmarshaller(javax.xml.bind.Unmarshaller) EditList(org.eclipse.jgit.diff.EditList) JAXBElement(javax.xml.bind.JAXBElement) Set(java.util.Set) Test(org.junit.Test) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawText(org.eclipse.jgit.diff.RawText) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) BufferedReader(java.io.BufferedReader) EditList(org.eclipse.jgit.diff.EditList) Unmarshaller(javax.xml.bind.Unmarshaller) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with ProcessorDTO

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

the class TestFlowController method testInstantiateSnippetWithDisabledProcessor.

@Test
public void testInstantiateSnippetWithDisabledProcessor() throws ProcessorInstantiationException {
    final String id = UUID.randomUUID().toString();
    final BundleCoordinate coordinate = systemBundle.getBundleDetails().getCoordinate();
    final ProcessorNode processorNode = controller.createProcessor(DummyProcessor.class.getName(), id, coordinate);
    processorNode.disable();
    // create a processor dto
    final ProcessorDTO processorDTO = new ProcessorDTO();
    // use a different id here
    processorDTO.setId(UUID.randomUUID().toString());
    processorDTO.setPosition(new PositionDTO(new Double(0), new Double(0)));
    processorDTO.setStyle(processorNode.getStyle());
    processorDTO.setParentGroupId("1234");
    processorDTO.setInputRequirement(processorNode.getInputRequirement().name());
    processorDTO.setPersistsState(processorNode.getProcessor().getClass().isAnnotationPresent(Stateful.class));
    processorDTO.setRestricted(processorNode.isRestricted());
    processorDTO.setExtensionMissing(processorNode.isExtensionMissing());
    processorDTO.setType(processorNode.getCanonicalClassName());
    processorDTO.setBundle(new BundleDTO(coordinate.getGroup(), coordinate.getId(), coordinate.getVersion()));
    processorDTO.setName(processorNode.getName());
    processorDTO.setState(processorNode.getScheduledState().toString());
    processorDTO.setRelationships(new ArrayList<>());
    processorDTO.setDescription("description");
    processorDTO.setSupportsParallelProcessing(!processorNode.isTriggeredSerially());
    processorDTO.setSupportsEventDriven(processorNode.isEventDrivenSupported());
    processorDTO.setSupportsBatching(processorNode.isSessionBatchingSupported());
    ProcessorConfigDTO configDTO = new ProcessorConfigDTO();
    configDTO.setSchedulingPeriod(processorNode.getSchedulingPeriod());
    configDTO.setPenaltyDuration(processorNode.getPenalizationPeriod());
    configDTO.setYieldDuration(processorNode.getYieldPeriod());
    configDTO.setRunDurationMillis(processorNode.getRunDuration(TimeUnit.MILLISECONDS));
    configDTO.setConcurrentlySchedulableTaskCount(processorNode.getMaxConcurrentTasks());
    configDTO.setLossTolerant(processorNode.isLossTolerant());
    configDTO.setComments(processorNode.getComments());
    configDTO.setBulletinLevel(processorNode.getBulletinLevel().name());
    configDTO.setSchedulingStrategy(processorNode.getSchedulingStrategy().name());
    configDTO.setExecutionNode(processorNode.getExecutionNode().name());
    configDTO.setAnnotationData(processorNode.getAnnotationData());
    processorDTO.setConfig(configDTO);
    // create the snippet with the processor
    final FlowSnippetDTO flowSnippetDTO = new FlowSnippetDTO();
    flowSnippetDTO.setProcessors(Collections.singleton(processorDTO));
    // instantiate the snippet
    assertEquals(0, controller.getRootGroup().getProcessors().size());
    controller.instantiateSnippet(controller.getRootGroup(), flowSnippetDTO);
    assertEquals(1, controller.getRootGroup().getProcessors().size());
    assertTrue(controller.getRootGroup().getProcessors().iterator().next().getScheduledState().equals(ScheduledState.DISABLED));
}
Also used : Stateful(org.apache.nifi.annotation.behavior.Stateful) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) DummyProcessor(org.apache.nifi.controller.service.mock.DummyProcessor) BundleDTO(org.apache.nifi.web.api.dto.BundleDTO) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) Test(org.junit.Test)

Example 25 with ProcessorDTO

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

the class StandardSnippetDAO method lookupSensitiveProcessorProperties.

private void lookupSensitiveProcessorProperties(final Set<ProcessorDTO> processors) {
    final ProcessGroup rootGroup = flowController.getGroup(flowController.getRootGroupId());
    // go through each processor
    for (final ProcessorDTO processorDTO : processors) {
        final ProcessorConfigDTO processorConfig = processorDTO.getConfig();
        // ensure that some property configuration have been specified
        if (processorConfig != null && processorConfig.getProperties() != null) {
            final Map<String, String> processorProperties = processorConfig.getProperties();
            // find the corresponding processor
            final ProcessorNode processorNode = rootGroup.findProcessor(processorDTO.getId());
            if (processorNode == null) {
                throw new IllegalArgumentException(String.format("Unable to create snippet because Processor '%s' could not be found", processorDTO.getId()));
            }
            // look for sensitive properties get the actual value
            for (Entry<PropertyDescriptor, String> entry : processorNode.getProperties().entrySet()) {
                final PropertyDescriptor descriptor = entry.getKey();
                if (descriptor.isSensitive()) {
                    processorProperties.put(descriptor.getName(), entry.getValue());
                }
            }
        }
    }
}
Also used : ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) ProcessorNode(org.apache.nifi.controller.ProcessorNode) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ProcessGroup(org.apache.nifi.groups.ProcessGroup)

Aggregations

ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)69 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)24 ArrayList (java.util.ArrayList)21 ConnectionDTO (org.apache.nifi.web.api.dto.ConnectionDTO)19 ProcessorConfigDTO (org.apache.nifi.web.api.dto.ProcessorConfigDTO)19 HashMap (java.util.HashMap)17 HashSet (java.util.HashSet)16 FlowSnippetDTO (org.apache.nifi.web.api.dto.FlowSnippetDTO)15 PortDTO (org.apache.nifi.web.api.dto.PortDTO)15 ProcessorEntity (org.apache.nifi.web.api.entity.ProcessorEntity)15 RemoteProcessGroupDTO (org.apache.nifi.web.api.dto.RemoteProcessGroupDTO)13 Map (java.util.Map)11 Response (javax.ws.rs.core.Response)11 ControllerServiceDTO (org.apache.nifi.web.api.dto.ControllerServiceDTO)11 RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)11 NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)10 List (java.util.List)10 Set (java.util.Set)10 Collectors (java.util.stream.Collectors)9 ConnectableDTO (org.apache.nifi.web.api.dto.ConnectableDTO)9