use of org.apache.nifi.web.api.entity.ScheduleComponentsEntity in project nifi by apache.
the class FlowResource method activateControllerServices.
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("process-groups/{id}/controller-services")
@ApiOperation(value = "Enable or disable Controller Services in the specified Process Group.", response = ActivateControllerServicesEntity.class, authorizations = { @Authorization(value = "Read - /flow"), @Authorization(value = "Write - /{component-type}/{uuid} - For every service being enabled/disabled") })
@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 activateControllerServices(@Context HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") String id, @ApiParam(value = "The request to schedule or unschedule. If the comopnents in the request are not specified, all authorized components will be considered.", required = true) final ActivateControllerServicesEntity requestEntity) {
// ensure the same id is being used
if (!id.equals(requestEntity.getId())) {
throw new IllegalArgumentException(String.format("The process group id (%s) in the request body does " + "not equal the process group id of the requested resource (%s).", requestEntity.getId(), id));
}
final ControllerServiceState state;
if (requestEntity.getState() == null) {
throw new IllegalArgumentException("The controller service state must be specified.");
} else {
try {
state = ControllerServiceState.valueOf(requestEntity.getState());
} catch (final IllegalArgumentException iae) {
throw new IllegalArgumentException(String.format("The controller service state must be one of [%s].", StringUtils.join(EnumSet.of(ControllerServiceState.ENABLED, ControllerServiceState.DISABLED), ", ")));
}
}
// ensure its a supported scheduled state
if (ControllerServiceState.DISABLING.equals(state) || ControllerServiceState.ENABLING.equals(state)) {
throw new IllegalArgumentException(String.format("The scheduled must be one of [%s].", StringUtils.join(EnumSet.of(ControllerServiceState.ENABLED, ControllerServiceState.DISABLED), ", ")));
}
// if the components are not specified, gather all components and their current revision
if (requestEntity.getComponents() == null) {
// get the current revisions for the components being updated
final Set<Revision> revisions = serviceFacade.getRevisionsFromGroup(id, group -> {
final Set<String> componentIds = new HashSet<>();
final Predicate<ControllerServiceNode> filter;
if (ControllerServiceState.ENABLED.equals(state)) {
filter = service -> !service.isActive() && service.isValid();
} else {
filter = service -> service.isActive();
}
group.findAllControllerServices().stream().filter(filter).filter(service -> service.isAuthorized(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser())).forEach(service -> componentIds.add(service.getIdentifier()));
return componentIds;
});
// build the component mapping
final Map<String, RevisionDTO> componentsToSchedule = new HashMap<>();
revisions.forEach(revision -> {
final RevisionDTO dto = new RevisionDTO();
dto.setClientId(revision.getClientId());
dto.setVersion(revision.getVersion());
componentsToSchedule.put(revision.getComponentId(), dto);
});
// set the components and their current revision
requestEntity.setComponents(componentsToSchedule);
}
if (isReplicateRequest()) {
return replicate(HttpMethod.PUT, requestEntity);
}
final Map<String, RevisionDTO> requestComponentsToSchedule = requestEntity.getComponents();
final Map<String, Revision> requestComponentRevisions = requestComponentsToSchedule.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getRevision(e.getValue(), e.getKey())));
final Set<Revision> requestRevisions = new HashSet<>(requestComponentRevisions.values());
return withWriteLock(serviceFacade, requestEntity, requestRevisions, lookup -> {
// ensure access to the flow
authorizeFlow();
// ensure access to every component being scheduled
requestComponentsToSchedule.keySet().forEach(componentId -> {
final Authorizable authorizable = lookup.getControllerService(componentId).getAuthorizable();
authorizable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
});
}, () -> serviceFacade.verifyActivateControllerServices(id, state, requestComponentRevisions.keySet()), (revisions, scheduleComponentsEntity) -> {
final ControllerServiceState serviceState = ControllerServiceState.valueOf(scheduleComponentsEntity.getState());
final Map<String, RevisionDTO> componentsToSchedule = scheduleComponentsEntity.getComponents();
final Map<String, Revision> componentRevisions = componentsToSchedule.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getRevision(e.getValue(), e.getKey())));
// update the controller services
final ActivateControllerServicesEntity entity = serviceFacade.activateControllerServices(id, serviceState, componentRevisions);
return generateOkResponse(entity).build();
});
}
use of org.apache.nifi.web.api.entity.ScheduleComponentsEntity in project nifi by apache.
the class ProcessGroupResource method scheduleProcessors.
private void scheduleProcessors(final String groupId, final URI originalUri, final VariableRegistryUpdateRequest updateRequest, final Pause pause, final Collection<AffectedComponentDTO> affectedProcessors, final ScheduledState desiredState, final VariableRegistryUpdateStep updateStep) throws InterruptedException {
final Set<String> affectedProcessorIds = affectedProcessors.stream().map(component -> component.getId()).collect(Collectors.toSet());
final Map<String, Revision> processorRevisionMap = getRevisions(groupId, affectedProcessorIds);
final Map<String, RevisionDTO> processorRevisionDtoMap = processorRevisionMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> dtoFactory.createRevisionDTO(entry.getValue())));
final ScheduleComponentsEntity scheduleProcessorsEntity = new ScheduleComponentsEntity();
scheduleProcessorsEntity.setComponents(processorRevisionDtoMap);
scheduleProcessorsEntity.setId(groupId);
scheduleProcessorsEntity.setState(desiredState.name());
URI scheduleGroupUri;
try {
scheduleGroupUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/flow/process-groups/" + groupId, null, originalUri.getFragment());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
final Map<String, String> headers = new HashMap<>();
headers.put("content-type", MediaType.APPLICATION_JSON);
// 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(HttpMethod.PUT, scheduleGroupUri, scheduleProcessorsEntity, headers).awaitMergedResponse();
} else {
clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), HttpMethod.PUT, scheduleGroupUri, scheduleProcessorsEntity, headers).awaitMergedResponse();
}
final int stopProcessorStatus = clusterResponse.getStatus();
if (stopProcessorStatus != Status.OK.getStatusCode()) {
updateRequest.getStopProcessorsStep().setFailureReason("Failed while " + updateStep.getDescription());
updateStep.setComplete(true);
updateRequest.setFailureReason("Failed while " + updateStep.getDescription());
return;
}
updateRequest.setLastUpdated(new Date());
final boolean processorsTransitioned = waitForProcessorStatus(originalUri, groupId, affectedProcessorIds, desiredState, updateRequest, pause);
updateStep.setComplete(true);
if (!processorsTransitioned) {
updateStep.setFailureReason("Failed while " + updateStep.getDescription());
updateRequest.setComplete(true);
updateRequest.setFailureReason("Failed while " + updateStep.getDescription());
}
}
use of org.apache.nifi.web.api.entity.ScheduleComponentsEntity in project kylo by Teradata.
the class NiFiProcessGroupsRestClientV1 method schedule.
@Override
public void schedule(@Nonnull final String processGroupId, @Nonnull final String parentGroupId, @Nonnull final NiFiComponentState state) {
// find the entity and see if there are RemoteProcessGroups associated with it. If so those need to be disabled/enabled
Optional<ProcessGroupDTO> group = findById(processGroupId, false, true);
if (group.isPresent() && group.get().getContents().getRemoteProcessGroups() != null && !group.get().getContents().getRemoteProcessGroups().isEmpty()) {
group.get().getContents().getRemoteProcessGroups().stream().forEach(remoteProcessGroupDTO -> {
if (NiFiComponentState.RUNNING == state) {
client.remoteProcessGroups().enable(remoteProcessGroupDTO.getId());
} else if (NiFiComponentState.STOPPED == state) {
client.remoteProcessGroups().disable(remoteProcessGroupDTO.getId());
}
});
}
final ScheduleComponentsEntity entity = new ScheduleComponentsEntity();
entity.setId(processGroupId);
entity.setState(state.toString());
try {
getClient().put("/flow/process-groups/" + processGroupId, entity, ScheduleComponentsEntity.class);
} catch (final NotFoundException e) {
throw new NifiComponentNotFoundException(processGroupId, NifiConstants.NIFI_COMPONENT_TYPE.PROCESS_GROUP, e);
}
}
use of org.apache.nifi.web.api.entity.ScheduleComponentsEntity in project nifi by apache.
the class PGStart method doExecute.
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final ScheduleComponentsEntity entity = new ScheduleComponentsEntity();
entity.setId(pgId);
entity.setState(ScheduleComponentsEntity.STATE_RUNNING);
final FlowClient flowClient = client.getFlowClient();
final ScheduleComponentsEntity resultEntity = flowClient.scheduleProcessGroupComponents(pgId, entity);
return VoidResult.getInstance();
}
use of org.apache.nifi.web.api.entity.ScheduleComponentsEntity in project nifi by apache.
the class PGStop method doExecute.
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final ScheduleComponentsEntity entity = new ScheduleComponentsEntity();
entity.setId(pgId);
entity.setState(ScheduleComponentsEntity.STATE_STOPPED);
final FlowClient flowClient = client.getFlowClient();
final ScheduleComponentsEntity resultEntity = flowClient.scheduleProcessGroupComponents(pgId, entity);
return VoidResult.getInstance();
}
Aggregations