use of org.apache.nifi.web.api.entity.ProcessorEntity in project kylo by Teradata.
the class AbstractNiFiControllerServicesRestClient method updateServiceAndReferencingComponents.
/**
* Updates a controller service
* This will first disable the service, stop all referencing components, enable the service, reset the state of the components back to their prior state
*
* @param controllerService the service to update with the updated properties
* @return the updates service
*/
@Override
public ControllerServiceDTO updateServiceAndReferencingComponents(ControllerServiceDTO controllerService) {
String id = controllerService.getId();
// Get all references to this controller service. this will include processors, other controller services, and reporting tasks
Optional<ControllerServiceReferencingComponentsEntity> references = getReferences(id);
// state of the processors prior to update
Map<String, String> previousProcessorState = null;
// revisions for the processor references
Map<String, RevisionDTO> processorRevisions = null;
// revisions for the controller service references
Map<String, RevisionDTO> controllerServiceRevisions = null;
// a map of component id to reference entity. this will include all referencing processors, controller services, and reporting tasks
Map<String, ControllerServiceReferencingComponentEntity> referencingComponentEntityMap = null;
// Stop all processor references and also disable any other controller service references
if (references.isPresent()) {
// build the reference state and revision maps prior to making this update
referencingComponentEntityMap = getReferencingComponents(references.get().getControllerServiceReferencingComponents());
previousProcessorState = referencingComponentEntityMap.values().stream().filter(e -> e.getComponent().getReferenceType().equalsIgnoreCase("PROCESSOR")).map(c -> c.getComponent()).collect(Collectors.toMap(ControllerServiceReferencingComponentDTO::getId, ControllerServiceReferencingComponentDTO::getState));
processorRevisions = referencingComponentEntityMap.values().stream().filter(e -> e.getComponent().getReferenceType().equalsIgnoreCase("PROCESSOR")).collect(Collectors.toMap(ControllerServiceReferencingComponentEntity::getId, ControllerServiceReferencingComponentEntity::getRevision));
controllerServiceRevisions = referencingComponentEntityMap.values().stream().filter(e -> e.getComponent().getReferenceType().equalsIgnoreCase("ControllerService")).collect(Collectors.toMap(ControllerServiceReferencingComponentEntity::getId, ControllerServiceReferencingComponentEntity::getRevision));
// Stop the referencing processors and ensure they are in the stopped state
if (!processorRevisions.isEmpty()) {
String state = NifiProcessUtil.PROCESS_STATE.STOPPED.name();
log.info("Stopping all component references to controller service {} ", controllerService.getName());
UpdateControllerServiceReferenceRequestEntity stopComponentsRequest = new UpdateControllerServiceReferenceRequestEntity();
stopComponentsRequest.setState(state);
stopComponentsRequest.setId(controllerService.getId());
stopComponentsRequest.setReferencingComponentRevisions(processorRevisions);
updateReferences(id, stopComponentsRequest);
boolean updatedReferences = ensureComponentsAreOfState(id, "Processor", state, 5, 500, TimeUnit.MILLISECONDS);
if (!updatedReferences) {
// error unable to change the state of the references. ... error
throw new NifiClientRuntimeException("Unable to stop processor references to this controller service " + controllerService.getName() + " before making the update");
}
}
// disable any controller service references
if (!controllerServiceRevisions.isEmpty()) {
UpdateControllerServiceReferenceRequestEntity stopComponentsRequest = new UpdateControllerServiceReferenceRequestEntity();
stopComponentsRequest.setState("DISABLED");
stopComponentsRequest.setId(controllerService.getId());
stopComponentsRequest.setReferencingComponentRevisions(controllerServiceRevisions);
updateReferences(id, stopComponentsRequest);
boolean updatedReferences = ensureComponentsAreOfState(id, "ControllerService", "DISABLED", 5, 500, TimeUnit.MILLISECONDS);
if (!updatedReferences) {
// error unable to change the state of the references. ... error
throw new NifiClientRuntimeException("Unable to disable other controller service references to this controller service " + controllerService.getName() + " before making the update");
}
}
}
// mark this controller service as disabled
log.info("Disabling the controller service {} ", controllerService.getName());
// update the service and mark it disabled. this will throw a RuntimeException if it is not successful
ControllerServiceDTO updatedService = updateStateByIdWithRetries(controllerService.getId(), "DISABLED", 5, 500, TimeUnit.MILLISECONDS);
// Perform the update to this controller service
log.info("Updating the controller service {} ", controllerService.getName());
updatedService = update(controllerService);
// Enable the service
updateStateById(controllerService.getId(), NiFiControllerServicesRestClient.State.ENABLED);
// Enable any controller service references
if (!controllerServiceRevisions.isEmpty()) {
log.info("Enabling other controller services referencing this controller service {} ", controllerService.getName());
UpdateControllerServiceReferenceRequestEntity enableReferenceServicesRequest = new UpdateControllerServiceReferenceRequestEntity();
enableReferenceServicesRequest.setId(controllerService.getId());
enableReferenceServicesRequest.setState(NiFiControllerServicesRestClient.State.ENABLED.name());
enableReferenceServicesRequest.setReferencingComponentRevisions(controllerServiceRevisions);
updateReferences(id, enableReferenceServicesRequest);
boolean updatedReferences = ensureComponentsAreOfState(id, "ControllerService", NiFiControllerServicesRestClient.State.ENABLED.name(), 5, 500, TimeUnit.MILLISECONDS);
if (!updatedReferences) {
// error unable to change the state of the references. ... error
throw new NifiClientRuntimeException("The controller service " + controllerService.getName() + " was updated, but it was unable to enable other controller service references to this controller service. Please visit NiFi to reconcile and fix any controller service issues. " + controllerService.getName());
}
}
if (references.isPresent() && previousProcessorState != null) {
// reset the processor state
Map<String, RevisionDTO> finalComponentRevisions = processorRevisions;
// if all referencing processors of of the same state we can call the quick rest endpoint to update all of them
Set<String> distinctStates = previousProcessorState.values().stream().collect(Collectors.toSet());
if (distinctStates.size() > 0) {
if (distinctStates.size() == 1) {
String state = new ArrayList<>(distinctStates).get(0);
if (!NifiProcessUtil.PROCESS_STATE.STOPPED.name().equals(state)) {
UpdateControllerServiceReferenceRequestEntity startComponentsRequest = new UpdateControllerServiceReferenceRequestEntity();
startComponentsRequest.setState(state);
startComponentsRequest.setReferencingComponentRevisions(finalComponentRevisions);
startComponentsRequest.setId(controllerService.getId());
updateReferences(id, startComponentsRequest);
log.info("Updating all component references to be {} for controller service {} ", state, controllerService.getName());
boolean updatedReferences = ensureComponentsAreOfState(id, "Processor", state, 5, 500, TimeUnit.MILLISECONDS);
if (!updatedReferences) {
// error unable to change the state of the references. ... error
throw new NifiClientRuntimeException("The controller service {} was updated, but it was unable to update the state of the processors as " + state + ". Please visit NiFi to reconcile and fix any controller service issues. " + controllerService.getName());
}
log.info("Successfully updated controller service {}", controllerService.getName());
}
} else {
log.info("The controller service component references ({} total) had mixed states prior to updating {}. Going through each processor/component and setting its state back to what it was prior to the update.", previousProcessorState.size(), distinctStates);
// update references back to previous states
List<ProcessorEntity> updatedProcessors = references.get().getControllerServiceReferencingComponents().stream().filter(c -> c.getComponent().getReferenceType().equalsIgnoreCase("PROCESSOR")).filter(c -> !c.getComponent().getState().equals(NifiProcessUtil.PROCESS_STATE.STOPPED.name())).map(referencingComponentEntity -> referencingComponentEntity.getComponent()).map(c -> {
ProcessorEntity entity = new ProcessorEntity();
entity.setRevision(finalComponentRevisions.get(c.getId()));
entity.setId(c.getId());
ProcessorDTO processorDTO = new ProcessorDTO();
processorDTO.setId(c.getId());
processorDTO.setState(c.getState());
entity.setComponent(processorDTO);
return entity;
}).collect(Collectors.toList());
updatedProcessors.stream().forEach(p -> getClient().processors().update(p));
log.info("Successfully updated controller service {} and updated {} components back to their prior state ", controllerService.getName(), previousProcessorState.size());
}
}
}
return updatedService;
}
use of org.apache.nifi.web.api.entity.ProcessorEntity in project kylo by Teradata.
the class NiFiProcessGroupsRestClientV1 method toFlowSnippet.
/**
* Converts the specified flow to a flow snippet.
*
* @param flow the flow
* @return the flow snippet
*/
@Nonnull
private FlowSnippetDTO toFlowSnippet(@Nonnull final FlowDTO flow, boolean recursive) {
final FlowSnippetDTO snippet = new FlowSnippetDTO();
snippet.setConnections(flow.getConnections().stream().map(ConnectionEntity::getComponent).collect(Collectors.toSet()));
snippet.setControllerServices(Collections.emptySet());
snippet.setFunnels(flow.getFunnels().stream().map(FunnelEntity::getComponent).collect(Collectors.toSet()));
snippet.setInputPorts(flow.getInputPorts().stream().map(PortEntity::getComponent).collect(Collectors.toSet()));
snippet.setLabels(flow.getLabels().stream().map(LabelEntity::getComponent).collect(Collectors.toSet()));
snippet.setOutputPorts(flow.getOutputPorts().stream().map(PortEntity::getComponent).collect(Collectors.toSet()));
snippet.setProcessGroups(flow.getProcessGroups().stream().map(ProcessGroupEntity::getComponent).collect(Collectors.toSet()));
snippet.setProcessors(flow.getProcessors().stream().map(ProcessorEntity::getComponent).collect(Collectors.toSet()));
snippet.setRemoteProcessGroups(flow.getRemoteProcessGroups().stream().map(RemoteProcessGroupEntity::getComponent).collect(Collectors.toSet()));
// Add flow for child process groups
if (recursive) {
for (final ProcessGroupDTO processGroup : snippet.getProcessGroups()) {
processGroup.setContents(getFlowSnippet(processGroup.getId(), true));
}
}
return snippet;
}
use of org.apache.nifi.web.api.entity.ProcessorEntity in project kylo by Teradata.
the class NiFiProcessorsRestClientV1 method update.
@Nonnull
@Override
public ProcessorDTO update(@Nonnull final ProcessorDTO processor) {
final ProcessorEntity entity = new ProcessorEntity();
entity.setComponent(processor);
return update(entity).orElseThrow(() -> new NifiComponentNotFoundException(processor.getId(), NifiConstants.NIFI_COMPONENT_TYPE.PROCESSOR, null));
}
use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.
the class StandardNiFiServiceFacade method createProcessor.
@Override
public ProcessorEntity createProcessor(final Revision revision, final String groupId, final ProcessorDTO processorDTO) {
final RevisionUpdate<ProcessorDTO> snapshot = createComponent(revision, processorDTO, () -> processorDAO.createProcessor(groupId, processorDTO), processor -> dtoFactory.createProcessorDto(processor));
final ProcessorNode processor = processorDAO.getProcessor(processorDTO.getId());
final PermissionsDTO permissions = dtoFactory.createPermissionsDto(processor);
final ProcessorStatusDTO status = dtoFactory.createProcessorStatusDto(controllerFacade.getProcessorStatus(processorDTO.getId()));
final List<BulletinDTO> bulletins = dtoFactory.createBulletinDtos(bulletinRepository.findBulletinsForSource(processorDTO.getId()));
final List<BulletinEntity> bulletinEntities = bulletins.stream().map(bulletin -> entityFactory.createBulletinEntity(bulletin, permissions.getCanRead())).collect(Collectors.toList());
return entityFactory.createProcessorEntity(snapshot.getComponent(), dtoFactory.createRevisionDTO(snapshot.getLastModification()), permissions, status, bulletinEntities);
}
use of org.apache.nifi.web.api.entity.ProcessorEntity in project nifi by apache.
the class StandardNiFiServiceFacade method createProcessorEntity.
private ProcessorEntity createProcessorEntity(final ProcessorNode processor, final NiFiUser user) {
final RevisionDTO revision = dtoFactory.createRevisionDTO(revisionManager.getRevision(processor.getIdentifier()));
final PermissionsDTO permissions = dtoFactory.createPermissionsDto(processor, user);
final ProcessorStatusDTO status = dtoFactory.createProcessorStatusDto(controllerFacade.getProcessorStatus(processor.getIdentifier()));
final List<BulletinDTO> bulletins = dtoFactory.createBulletinDtos(bulletinRepository.findBulletinsForSource(processor.getIdentifier()));
final List<BulletinEntity> bulletinEntities = bulletins.stream().map(bulletin -> entityFactory.createBulletinEntity(bulletin, permissions.getCanRead())).collect(Collectors.toList());
return entityFactory.createProcessorEntity(dtoFactory.createProcessorDto(processor), revision, permissions, status, bulletinEntities);
}
Aggregations