use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.
the class StandardNiFiServiceFacade method updateControllerService.
@Override
public ControllerServiceEntity updateControllerService(final Revision revision, final ControllerServiceDTO controllerServiceDTO) {
// get the component, ensure we have access to it, and perform the update request
final ControllerServiceNode controllerService = controllerServiceDAO.getControllerService(controllerServiceDTO.getId());
final RevisionUpdate<ControllerServiceDTO> snapshot = updateComponent(revision, controllerService, () -> controllerServiceDAO.updateControllerService(controllerServiceDTO), cs -> {
final ControllerServiceDTO dto = dtoFactory.createControllerServiceDto(cs);
final ControllerServiceReference ref = controllerService.getReferences();
final ControllerServiceReferencingComponentsEntity referencingComponentsEntity = createControllerServiceReferencingComponentsEntity(ref, Sets.newHashSet(controllerService.getIdentifier()));
dto.setReferencingComponents(referencingComponentsEntity.getControllerServiceReferencingComponents());
return dto;
});
final PermissionsDTO permissions = dtoFactory.createPermissionsDto(controllerService);
final List<BulletinDTO> bulletins = dtoFactory.createBulletinDtos(bulletinRepository.findBulletinsForSource(controllerServiceDTO.getId()));
final List<BulletinEntity> bulletinEntities = bulletins.stream().map(bulletin -> entityFactory.createBulletinEntity(bulletin, permissions.getCanRead())).collect(Collectors.toList());
return entityFactory.createControllerServiceEntity(snapshot.getComponent(), dtoFactory.createRevisionDTO(snapshot.getLastModification()), permissions, bulletinEntities);
}
use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.
the class ControllerServiceResource method updateControllerServiceReferences.
/**
* Updates the references of the specified controller service.
*
* @param httpServletRequest request
* @param requestUpdateReferenceRequest The update request
* @return A controllerServiceReferencingComponentsEntity.
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/references")
@ApiOperation(value = "Updates a controller services references", response = ControllerServiceReferencingComponentsEntity.class, authorizations = { @Authorization(value = "Write - /{component-type}/{uuid} - For each referencing component specified") })
@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 updateControllerServiceReferences(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The controller service id.", required = true) @PathParam("id") final String id, @ApiParam(value = "The controller service request update request.", required = true) final UpdateControllerServiceReferenceRequestEntity requestUpdateReferenceRequest) {
if (requestUpdateReferenceRequest.getId() == null) {
throw new IllegalArgumentException("The controller service identifier must be specified.");
}
if (requestUpdateReferenceRequest.getReferencingComponentRevisions() == null) {
throw new IllegalArgumentException("The controller service referencing components revisions must be specified.");
}
// parse the state to determine the desired action
// need to consider controller service state first as it shares a state with
// scheduled state (disabled) which is applicable for referencing services
// but not referencing schedulable components
ControllerServiceState requestControllerServiceState = null;
try {
requestControllerServiceState = ControllerServiceState.valueOf(requestUpdateReferenceRequest.getState());
} catch (final IllegalArgumentException iae) {
// ignore
}
ScheduledState requestScheduledState = null;
try {
requestScheduledState = ScheduledState.valueOf(requestUpdateReferenceRequest.getState());
} catch (final IllegalArgumentException iae) {
// ignore
}
// ensure an action has been specified
if (requestScheduledState == null && requestControllerServiceState == null) {
throw new IllegalArgumentException("Must specify the updated state. To update referencing Processors " + "and Reporting Tasks the state should be RUNNING or STOPPED. To update the referencing Controller Services the " + "state should be ENABLED or DISABLED.");
}
// ensure the controller service state is not ENABLING or DISABLING
if (requestControllerServiceState != null && (ControllerServiceState.ENABLING.equals(requestControllerServiceState) || ControllerServiceState.DISABLING.equals(requestControllerServiceState))) {
throw new IllegalArgumentException("Cannot set the referencing services to ENABLING or DISABLING");
}
if (isReplicateRequest()) {
return replicate(HttpMethod.PUT, requestUpdateReferenceRequest);
}
// convert the referencing revisions
final Map<String, Revision> requestReferencingRevisions = requestUpdateReferenceRequest.getReferencingComponentRevisions().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
final RevisionDTO rev = e.getValue();
return new Revision(rev.getVersion(), rev.getClientId(), e.getKey());
}));
final Set<Revision> requestRevisions = new HashSet<>(requestReferencingRevisions.values());
final ScheduledState verifyScheduledState = requestScheduledState;
final ControllerServiceState verifyControllerServiceState = requestControllerServiceState;
return withWriteLock(serviceFacade, requestUpdateReferenceRequest, requestRevisions, lookup -> {
requestReferencingRevisions.entrySet().stream().forEach(e -> {
final Authorizable controllerService = lookup.getControllerServiceReferencingComponent(id, e.getKey());
controllerService.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
});
}, () -> serviceFacade.verifyUpdateControllerServiceReferencingComponents(requestUpdateReferenceRequest.getId(), verifyScheduledState, verifyControllerServiceState), (revisions, updateReferenceRequest) -> {
ScheduledState scheduledState = null;
try {
scheduledState = ScheduledState.valueOf(updateReferenceRequest.getState());
} catch (final IllegalArgumentException e) {
// ignore
}
ControllerServiceState controllerServiceState = null;
try {
controllerServiceState = ControllerServiceState.valueOf(updateReferenceRequest.getState());
} catch (final IllegalArgumentException iae) {
// ignore
}
final Map<String, Revision> referencingRevisions = updateReferenceRequest.getReferencingComponentRevisions().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
final RevisionDTO rev = e.getValue();
return new Revision(rev.getVersion(), rev.getClientId(), e.getKey());
}));
// update the controller service references
final ControllerServiceReferencingComponentsEntity entity = serviceFacade.updateControllerServiceReferencingComponents(referencingRevisions, updateReferenceRequest.getId(), scheduledState, controllerServiceState);
return generateOkResponse(entity).build();
});
}
use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project kylo by Teradata.
the class NifiIntegrationRestController method getControllerServiceReferencesMap.
@GET
@Path("/controller-services/{serviceId}/references")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets a controller service references in a map by component type. (i.e. Processor -> list, Controller Service -> list ...)", notes = "returns a map of the type and reference objects")
@ApiResponses({ @ApiResponse(code = 200, message = "returns a map of the type and reference objects", response = ControllerServiceDTO.class), @ApiResponse(code = 500, message = "Unable to find the controller service", response = RestResponseStatus.class) })
public Response getControllerServiceReferencesMap(@PathParam("serviceId") String serviceId) {
Map<String, List<ControllerServiceReferencingComponentDTO>> map = null;
try {
final ControllerServiceDTO controllerService = legacyNifiRestClient.getControllerService(null, serviceId);
if (controllerService != null) {
Optional<ControllerServiceReferencingComponentsEntity> optional = legacyNifiRestClient.getNiFiRestClient().controllerServices().getReferences(serviceId);
if (optional.isPresent()) {
ControllerServiceReferencingComponentsEntity entity = optional.get();
map = getReferencingComponents(entity.getControllerServiceReferencingComponents()).values().stream().map(c -> c.getComponent()).collect(Collectors.groupingBy(x -> x.getReferenceType()));
} else {
map = Collections.emptyMap();
}
}
return Response.ok(map).build();
} catch (Exception e) {
RestResponseStatus error = new RestResponseStatus.ResponseStatusBuilder().message("Unable to find controller service references for " + serviceId).buildError();
return Response.ok(error).build();
}
}
use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project kylo by Teradata.
the class AbstractNiFiControllerServicesRestClient method updateServiceAndReferencingComponents.
/**
* Updates a controller service.
*
* <p>This will first stop all referencing components, disable the service, update the service, enable the service, then reset the state of the components back to their prior state.</p>
*
* @param controllerService the service to update with the updated properties
* @return the updated service
*/
@Nonnull
@Override
public ControllerServiceDTO updateServiceAndReferencingComponents(@Nonnull final ControllerServiceDTO controllerService) {
// Recursively get all references to this controller service. This will include processors, other controller services, and reporting tasks.
final Optional<ControllerServiceReferencingComponentsEntity> referencesEntity = getReferences(controllerService.getId());
final Set<ControllerServiceReferencingComponentEntity> references = referencesEntity.isPresent() ? flattenReferencingComponents(referencesEntity.get()).collect(Collectors.toSet()) : Collections.emptySet();
// build the reference state and revision maps prior to making this update
final Map<String, RevisionDTO> referencingSchedulableComponentRevisions = new HashMap<>();
final Map<String, RevisionDTO> referencingServiceRevisions = new HashMap<>();
references.forEach(reference -> {
if (REFERENCE_TYPE_CONTROLLER_SERVICE.equals(reference.getComponent().getReferenceType())) {
referencingServiceRevisions.put(reference.getId(), reference.getRevision());
} else {
referencingSchedulableComponentRevisions.put(reference.getId(), reference.getRevision());
}
});
// Update service and referencing components
ControllerServiceDTO updatedService = null;
Exception updateException = null;
try {
// Stop the referencing processors and ensure they are in the stopped state
log.info("Stopping all component references to controller service {} ", controllerService.getName());
if (!referencingSchedulableComponentRevisions.isEmpty() && !updateReferencingSchedulableComponents(controllerService, referencingSchedulableComponentRevisions, false)) {
// 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 (!referencingServiceRevisions.isEmpty() && !updateReferencingServices(controllerService, referencingServiceRevisions, false)) {
// 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");
}
// Update the service and mark it disabled. This will throw a RuntimeException if it is not successful.
log.info("Disabling the controller service {} ", controllerService.getName());
updateStateByIdWithRetries(controllerService.getId(), State.DISABLED.name(), 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(), State.ENABLED);
} catch (final Exception e) {
log.error("Reverting changes after controller service {} [{}] failed to update: ", controllerService.getId(), controllerService.getName(), e);
updateException = e;
}
// Enable any controller service references
boolean servicesUpdate;
try {
log.info("Enabling other controller services referencing this controller service {} ", controllerService.getName());
servicesUpdate = updateReferencingServices(controllerService, referencingServiceRevisions, true);
} catch (final Exception e) {
log.debug("Failed to restore referencing service states for controller service {} [{}]: {}", controllerService.getId(), controllerService.getName(), e, e);
servicesUpdate = false;
}
// Update references back to previous states
boolean componentsUpdate;
try {
final Set<ControllerServiceReferencingComponentEntity> runningComponents = references.stream().filter(reference -> !REFERENCE_TYPE_CONTROLLER_SERVICE.equals(reference.getComponent().getReferenceType())).filter(reference -> NifiProcessUtil.PROCESS_STATE.RUNNING.name().equals(reference.getComponent().getState())).collect(Collectors.toSet());
if (runningComponents.size() == referencingSchedulableComponentRevisions.size()) {
log.info("Updating all component references to be RUNNING for controller service {} ", controllerService.getName());
componentsUpdate = updateReferencingSchedulableComponents(controllerService, referencingSchedulableComponentRevisions, true);
} 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.", referencingSchedulableComponentRevisions.size());
componentsUpdate = startSchedulableComponents(runningComponents);
log.info("Successfully updated controller service {} and updated {} components back to their prior state ", controllerService.getName(), runningComponents.size());
}
} catch (final Exception e) {
log.debug("Failed to restore referencing component states for controller service {} [{}]: {}", controllerService.getId(), controllerService.getName(), e, e);
componentsUpdate = false;
}
// Verify final state
if (updateException != null) {
throw Throwables.propagate(updateException);
}
if (!servicesUpdate) {
// error unable to change the state of the references. ... error
throw new NifiClientRuntimeException("Kylo was unable to enable other controller service references to the " + controllerService.getName() + " controller service. Please visit NiFi" + " to reconcile and fix any controller service issues.");
}
if (!componentsUpdate) {
// error unable to change the state of the references. ... error
throw new NifiClientRuntimeException("Kylo was unable to update the state of the processors as RUNNING. Please visit NiFi to reconcile and fix any controller service issues. " + controllerService.getName());
}
log.info("Successfully updated controller service {}", controllerService.getName());
return updatedService;
}
use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.
the class StandardNiFiServiceFacade method createControllerServiceEntity.
private ControllerServiceEntity createControllerServiceEntity(final ControllerServiceNode serviceNode, final Set<String> serviceIds, final NiFiUser user) {
final ControllerServiceDTO dto = dtoFactory.createControllerServiceDto(serviceNode);
final ControllerServiceReference ref = serviceNode.getReferences();
final ControllerServiceReferencingComponentsEntity referencingComponentsEntity = createControllerServiceReferencingComponentsEntity(ref, serviceIds);
dto.setReferencingComponents(referencingComponentsEntity.getControllerServiceReferencingComponents());
final RevisionDTO revision = dtoFactory.createRevisionDTO(revisionManager.getRevision(serviceNode.getIdentifier()));
final PermissionsDTO permissions = dtoFactory.createPermissionsDto(serviceNode, user);
final List<BulletinDTO> bulletins = dtoFactory.createBulletinDtos(bulletinRepository.findBulletinsForSource(serviceNode.getIdentifier()));
final List<BulletinEntity> bulletinEntities = bulletins.stream().map(bulletin -> entityFactory.createBulletinEntity(bulletin, permissions.getCanRead())).collect(Collectors.toList());
return entityFactory.createControllerServiceEntity(dto, revision, permissions, bulletinEntities);
}
Aggregations