Search in sources :

Example 11 with ProcessorDefinition

use of com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorServiceImpl method updateProcessor.

@Override
@Transactional
public Processor updateProcessor(String bridgeId, String processorId, String customerId, ProcessorRequest processorRequest) {
    // Attempt to load the Bridge. We cannot update a Processor if the Bridge is not available.
    bridgesService.getReadyBridge(bridgeId, customerId);
    // Extract existing definition
    Processor existingProcessor = getProcessor(bridgeId, processorId, customerId);
    if (existingProcessor.getType() == ProcessorType.ERROR_HANDLER) {
        throw new BadRequestException("It is not possible to update this Processor.");
    }
    if (!isProcessorActionable(existingProcessor)) {
        throw new ProcessorLifecycleException(String.format("Processor with id '%s' for customer '%s' is not in an actionable state.", processorId, customerId));
    }
    ProcessorDefinition existingDefinition = existingProcessor.getDefinition();
    Action existingAction = existingDefinition.getRequestedAction();
    Action existingResolvedAction = existingDefinition.getResolvedAction();
    Source existingSource = existingDefinition.getRequestedSource();
    // Name cannot be updated.
    if (!Objects.equals(existingProcessor.getName(), processorRequest.getName())) {
        throw new BadRequestException("It is not possible to update the Processor's name.");
    }
    if (!Objects.equals(existingProcessor.getType(), processorRequest.getType())) {
        throw new BadRequestException("It is not possible to update the Processor's Type.");
    }
    // See https://issues.redhat.com/browse/MGDOBR-516 for updating Action support
    if (!Objects.equals(existingAction, processorRequest.getAction())) {
        throw new BadRequestException("It is not possible to update the Processor's Action.");
    }
    if (!Objects.equals(existingSource, processorRequest.getSource())) {
        throw new BadRequestException("It is not possible to update the Processor's Source.");
    }
    // Construct updated definition
    Set<BaseFilter> updatedFilters = processorRequest.getFilters();
    String updatedTransformationTemplate = processorRequest.getTransformationTemplate();
    ProcessorDefinition updatedDefinition = existingProcessor.getType() == ProcessorType.SOURCE ? new ProcessorDefinition(updatedFilters, updatedTransformationTemplate, existingSource, existingResolvedAction) : new ProcessorDefinition(updatedFilters, updatedTransformationTemplate, existingAction, existingResolvedAction);
    // No need to update CRD if the definition is unchanged
    if (existingDefinition.equals(updatedDefinition)) {
        return existingProcessor;
    }
    // Create new definition copying existing properties
    existingProcessor.setModifiedAt(ZonedDateTime.now());
    existingProcessor.setStatus(ManagedResourceStatus.ACCEPTED);
    existingProcessor.setDefinition(updatedDefinition);
    // Processor and Work should always be created in the same transaction
    // Since updates to the Action are unsupported we do not need to update the Connector record.
    workManager.schedule(existingProcessor);
    metricsService.onOperationStart(existingProcessor, MetricsOperation.MODIFY);
    LOGGER.info("Processor with id '{}' for customer '{}' on bridge '{}' has been marked for update", existingProcessor.getId(), existingProcessor.getBridge().getCustomerId(), existingProcessor.getBridge().getId());
    return existingProcessor;
}
Also used : ProcessorLifecycleException(com.redhat.service.smartevents.infra.exceptions.definitions.user.ProcessorLifecycleException) Action(com.redhat.service.smartevents.infra.models.gateways.Action) Processor(com.redhat.service.smartevents.manager.models.Processor) ProcessorDefinition(com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition) BadRequestException(com.redhat.service.smartevents.infra.exceptions.definitions.user.BadRequestException) Source(com.redhat.service.smartevents.infra.models.gateways.Source) BaseFilter(com.redhat.service.smartevents.infra.models.filters.BaseFilter) Transactional(javax.transaction.Transactional)

Example 12 with ProcessorDefinition

use of com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorServiceImpl method toResponse.

@Override
public ProcessorResponse toResponse(Processor processor) {
    ProcessorResponse processorResponse = new ProcessorResponse();
    processorResponse.setType(processor.getType());
    processorResponse.setId(processor.getId());
    processorResponse.setName(processor.getName());
    processorResponse.setStatus(processor.getStatus());
    processorResponse.setPublishedAt(processor.getPublishedAt());
    processorResponse.setSubmittedAt(processor.getSubmittedAt());
    processorResponse.setOwner(processor.getOwner());
    if (processor.getDefinition() != null) {
        ProcessorDefinition definition = processor.getDefinition();
        processorResponse.setFilters(definition.getFilters());
        processorResponse.setTransformationTemplate(definition.getTransformationTemplate());
        processorResponse.setAction(definition.getRequestedAction());
        processorResponse.setSource(definition.getRequestedSource());
    }
    if (processor.getBridge() != null) {
        processorResponse.setHref(APIConstants.USER_API_BASE_PATH + processor.getBridge().getId() + "/processors/" + processor.getId());
    }
    return processorResponse;
}
Also used : ProcessorResponse(com.redhat.service.smartevents.manager.api.models.responses.ProcessorResponse) ProcessorDefinition(com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition)

Example 13 with ProcessorDefinition

use of com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorServiceImpl method doCreateProcessor.

private Processor doCreateProcessor(Bridge bridge, String customerId, String owner, ProcessorType processorType, ProcessorRequest processorRequest) {
    String bridgeId = bridge.getId();
    if (processorDAO.findByBridgeIdAndName(bridgeId, processorRequest.getName()) != null) {
        throw new AlreadyExistingItemException("Processor with name '" + processorRequest.getName() + "' already exists for bridge with id '" + bridgeId + "' for customer '" + customerId + "'");
    }
    Processor newProcessor = new Processor();
    newProcessor.setType(processorType);
    newProcessor.setName(processorRequest.getName());
    newProcessor.setSubmittedAt(ZonedDateTime.now());
    newProcessor.setStatus(ManagedResourceStatus.ACCEPTED);
    newProcessor.setBridge(bridge);
    newProcessor.setShardId(shardService.getAssignedShardId(newProcessor.getId()));
    newProcessor.setOwner(owner);
    Set<BaseFilter> requestedFilters = processorRequest.getFilters();
    String requestedTransformationTemplate = processorRequest.getTransformationTemplate();
    Action resolvedAction = processorType == ProcessorType.SOURCE ? resolveSource(processorRequest.getSource(), customerId, bridge.getId(), newProcessor.getId()) : resolveAction(processorRequest.getAction(), customerId, bridge.getId(), newProcessor.getId());
    ProcessorDefinition definition = processorType == ProcessorType.SOURCE ? new ProcessorDefinition(requestedFilters, requestedTransformationTemplate, processorRequest.getSource(), resolvedAction) : new ProcessorDefinition(requestedFilters, requestedTransformationTemplate, processorRequest.getAction(), resolvedAction);
    newProcessor.setDefinition(definition);
    // Processor, Connector and Work should always be created in the same transaction
    processorDAO.persist(newProcessor);
    connectorService.createConnectorEntity(newProcessor);
    workManager.schedule(newProcessor);
    metricsService.onOperationStart(newProcessor, MetricsOperation.PROVISION);
    LOGGER.info("Processor with id '{}' for customer '{}' on bridge '{}' has been marked for creation", newProcessor.getId(), newProcessor.getBridge().getCustomerId(), newProcessor.getBridge().getId());
    return newProcessor;
}
Also used : Action(com.redhat.service.smartevents.infra.models.gateways.Action) Processor(com.redhat.service.smartevents.manager.models.Processor) ProcessorDefinition(com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition) BaseFilter(com.redhat.service.smartevents.infra.models.filters.BaseFilter) AlreadyExistingItemException(com.redhat.service.smartevents.infra.exceptions.definitions.user.AlreadyExistingItemException)

Example 14 with ProcessorDefinition

use of com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorServiceTest method doAssertProcessorCreation.

private void doAssertProcessorCreation(Processor processor, ProcessorRequest request, ProcessorType type) {
    assertThat(processor.getBridge().getId()).isEqualTo(DEFAULT_BRIDGE_ID);
    assertThat(processor.getType()).isEqualTo(type);
    assertThat(processor.getName()).isEqualTo(request.getName());
    assertThat(processor.getStatus()).isEqualTo(ACCEPTED);
    assertThat(processor.getSubmittedAt()).isNotNull();
    assertThat(processor.getDefinition()).isNotNull();
    ArgumentCaptor<Processor> processorCaptor1 = ArgumentCaptor.forClass(Processor.class);
    verify(processorDAO, times(1)).persist(processorCaptor1.capture());
    assertThat(processorCaptor1.getValue()).isEqualTo(processor);
    ArgumentCaptor<Processor> processorCaptor2 = ArgumentCaptor.forClass(Processor.class);
    verify(connectorServiceMock, times(1)).createConnectorEntity(processorCaptor2.capture());
    assertThat(processorCaptor2.getValue()).isEqualTo(processor);
    assertThat(processorCaptor2.getValue().getDefinition().getRequestedAction()).isEqualTo(request.getAction());
    ArgumentCaptor<Processor> processorCaptor3 = ArgumentCaptor.forClass(Processor.class);
    verify(workManagerMock, times(1)).schedule(processorCaptor3.capture());
    assertThat(processorCaptor3.getValue()).isEqualTo(processor);
    ProcessorDefinition definition = processor.getDefinition();
    assertThat(definition.getTransformationTemplate()).isEqualTo(request.getTransformationTemplate());
}
Also used : Processor(com.redhat.service.smartevents.manager.models.Processor) ProcessorDefinition(com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition)

Example 15 with ProcessorDefinition

use of com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorServiceTest method createReadyProcessorFromRequest.

private static Processor createReadyProcessorFromRequest(ProcessorRequest request) {
    ProcessorDefinition definition = request.getType() == ProcessorType.SOURCE ? new ProcessorDefinition(request.getFilters(), request.getTransformationTemplate(), request.getSource(), null) : new ProcessorDefinition(request.getFilters(), request.getTransformationTemplate(), request.getAction(), null);
    Processor processor = Fixtures.createProcessor(createReadyBridge(), READY);
    processor.setId(DEFAULT_PROCESSOR_ID);
    processor.setType(request.getType());
    processor.setName(request.getName());
    processor.setDefinition(definition);
    return processor;
}
Also used : Processor(com.redhat.service.smartevents.manager.models.Processor) ProcessorDefinition(com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition)

Aggregations

ProcessorDefinition (com.redhat.service.smartevents.infra.models.processors.ProcessorDefinition)16 Action (com.redhat.service.smartevents.infra.models.gateways.Action)12 Processor (com.redhat.service.smartevents.manager.models.Processor)9 KafkaTopicAction (com.redhat.service.smartevents.processor.actions.kafkatopic.KafkaTopicAction)7 HashMap (java.util.HashMap)5 WebhookAction (com.redhat.service.smartevents.processor.actions.webhook.WebhookAction)4 ProcessorDTO (com.redhat.service.smartevents.infra.models.dto.ProcessorDTO)3 BaseFilter (com.redhat.service.smartevents.infra.models.filters.BaseFilter)3 SendToBridgeAction (com.redhat.service.smartevents.processor.actions.sendtobridge.SendToBridgeAction)3 Source (com.redhat.service.smartevents.infra.models.gateways.Source)2 ProcessorResponse (com.redhat.service.smartevents.manager.api.models.responses.ProcessorResponse)2 Bridge (com.redhat.service.smartevents.manager.models.Bridge)2 AlreadyExistingItemException (com.redhat.service.smartevents.infra.exceptions.definitions.user.AlreadyExistingItemException)1 BadRequestException (com.redhat.service.smartevents.infra.exceptions.definitions.user.BadRequestException)1 ProcessorLifecycleException (com.redhat.service.smartevents.infra.exceptions.definitions.user.ProcessorLifecycleException)1 StringEquals (com.redhat.service.smartevents.infra.models.filters.StringEquals)1 SlackAction (com.redhat.service.smartevents.processor.actions.slack.SlackAction)1 SlackSource (com.redhat.service.smartevents.processor.sources.slack.SlackSource)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 HashSet (java.util.HashSet)1