Search in sources :

Example 6 with ControllerServiceReferencingComponentsEntity

use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.

the class StandardNiFiServiceFacade method updateControllerServiceReferencingComponents.

@Override
public ControllerServiceReferencingComponentsEntity updateControllerServiceReferencingComponents(final Map<String, Revision> referenceRevisions, final String controllerServiceId, final ScheduledState scheduledState, final ControllerServiceState controllerServiceState) {
    final RevisionClaim claim = new StandardRevisionClaim(referenceRevisions.values());
    final NiFiUser user = NiFiUserUtils.getNiFiUser();
    final RevisionUpdate<ControllerServiceReferencingComponentsEntity> update = revisionManager.updateRevision(claim, user, new UpdateRevisionTask<ControllerServiceReferencingComponentsEntity>() {

        @Override
        public RevisionUpdate<ControllerServiceReferencingComponentsEntity> update() {
            final Set<ConfiguredComponent> updated = controllerServiceDAO.updateControllerServiceReferencingComponents(controllerServiceId, scheduledState, controllerServiceState);
            final ControllerServiceReference updatedReference = controllerServiceDAO.getControllerService(controllerServiceId).getReferences();
            // get the revisions of the updated components
            final Map<String, Revision> updatedRevisions = new HashMap<>();
            for (final ConfiguredComponent component : updated) {
                final Revision currentRevision = revisionManager.getRevision(component.getIdentifier());
                final Revision requestRevision = referenceRevisions.get(component.getIdentifier());
                updatedRevisions.put(component.getIdentifier(), currentRevision.incrementRevision(requestRevision.getClientId()));
            }
            // ensure the revision for all referencing components is included regardless of whether they were updated in this request
            for (final ConfiguredComponent component : findAllReferencingComponents(updatedReference)) {
                updatedRevisions.putIfAbsent(component.getIdentifier(), revisionManager.getRevision(component.getIdentifier()));
            }
            final ControllerServiceReferencingComponentsEntity entity = createControllerServiceReferencingComponentsEntity(updatedReference, updatedRevisions);
            return new StandardRevisionUpdate<>(entity, null, new HashSet<>(updatedRevisions.values()));
        }
    });
    return update.getComponent();
}
Also used : ControllerServiceReferencingComponentsEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity) HashSet(java.util.HashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ConfiguredComponent(org.apache.nifi.controller.ConfiguredComponent) RevisionUpdate(org.apache.nifi.web.revision.RevisionUpdate) StandardRevisionUpdate(org.apache.nifi.web.revision.StandardRevisionUpdate) ControllerServiceReference(org.apache.nifi.controller.service.ControllerServiceReference) StandardRevisionClaim(org.apache.nifi.web.revision.StandardRevisionClaim) RevisionClaim(org.apache.nifi.web.revision.RevisionClaim) StandardRevisionClaim(org.apache.nifi.web.revision.StandardRevisionClaim) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) StateMap(org.apache.nifi.components.state.StateMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 7 with ControllerServiceReferencingComponentsEntity

use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.

the class StandardNiFiServiceFacade method createControllerServiceReferencingComponentsEntity.

/**
 * Creates entities for components referencing a ControllerServcie using the specified revisions.
 *
 * @param reference ControllerServiceReference
 * @param revisions The revisions
 * @param visited   Which services we've already considered (in case of cycle)
 * @return The entity
 */
private ControllerServiceReferencingComponentsEntity createControllerServiceReferencingComponentsEntity(final ControllerServiceReference reference, final Map<String, Revision> revisions, final Set<ControllerServiceNode> visited) {
    final String modifier = NiFiUserUtils.getNiFiUserIdentity();
    final Set<ConfiguredComponent> referencingComponents = reference.getReferencingComponents();
    final Set<ControllerServiceReferencingComponentEntity> componentEntities = new HashSet<>();
    for (final ConfiguredComponent refComponent : referencingComponents) {
        PermissionsDTO permissions = null;
        if (refComponent instanceof Authorizable) {
            permissions = dtoFactory.createPermissionsDto(refComponent);
        }
        final Revision revision = revisions.get(refComponent.getIdentifier());
        final FlowModification flowMod = new FlowModification(revision, modifier);
        final RevisionDTO revisionDto = dtoFactory.createRevisionDTO(flowMod);
        final ControllerServiceReferencingComponentDTO dto = dtoFactory.createControllerServiceReferencingComponentDTO(refComponent);
        if (refComponent instanceof ControllerServiceNode) {
            final ControllerServiceNode node = (ControllerServiceNode) refComponent;
            // indicate if we've hit a cycle
            dto.setReferenceCycle(visited.contains(node));
            // mark node as visited before building the reference cycle
            visited.add(node);
            // if we haven't encountered this service before include it's referencing components
            if (!dto.getReferenceCycle()) {
                final ControllerServiceReference refReferences = node.getReferences();
                final Map<String, Revision> referencingRevisions = new HashMap<>(revisions);
                for (final ConfiguredComponent component : refReferences.getReferencingComponents()) {
                    referencingRevisions.putIfAbsent(component.getIdentifier(), revisionManager.getRevision(component.getIdentifier()));
                }
                final ControllerServiceReferencingComponentsEntity references = createControllerServiceReferencingComponentsEntity(refReferences, referencingRevisions, visited);
                dto.setReferencingComponents(references.getControllerServiceReferencingComponents());
            }
        }
        componentEntities.add(entityFactory.createControllerServiceReferencingComponentEntity(dto, revisionDto, permissions));
    }
    final ControllerServiceReferencingComponentsEntity entity = new ControllerServiceReferencingComponentsEntity();
    entity.setControllerServiceReferencingComponents(componentEntities);
    return entity;
}
Also used : ControllerServiceReferencingComponentsEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity) ControllerServiceReferencingComponentEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentEntity) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ConfiguredComponent(org.apache.nifi.controller.ConfiguredComponent) PermissionsDTO(org.apache.nifi.web.api.dto.PermissionsDTO) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) ControllerServiceReferencingComponentDTO(org.apache.nifi.web.api.dto.ControllerServiceReferencingComponentDTO) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ControllerServiceReference(org.apache.nifi.controller.service.ControllerServiceReference) Authorizable(org.apache.nifi.authorization.resource.Authorizable) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 8 with ControllerServiceReferencingComponentsEntity

use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.

the class ControllerServiceReferenceEndpointMerger method merge.

@Override
public NodeResponse merge(URI uri, String method, Set<NodeResponse> successfulResponses, Set<NodeResponse> problematicResponses, NodeResponse clientResponse) {
    if (!canHandle(uri, method)) {
        throw new IllegalArgumentException("Cannot use Endpoint Mapper of type " + getClass().getSimpleName() + " to map responses for URI " + uri + ", HTTP Method " + method);
    }
    final ControllerServiceReferencingComponentsEntity responseEntity = clientResponse.getClientResponse().readEntity(ControllerServiceReferencingComponentsEntity.class);
    final Set<ControllerServiceReferencingComponentEntity> referencingComponents = responseEntity.getControllerServiceReferencingComponents();
    final Map<NodeIdentifier, Set<ControllerServiceReferencingComponentEntity>> resultsMap = new HashMap<>();
    for (final NodeResponse nodeResponse : successfulResponses) {
        final ControllerServiceReferencingComponentsEntity nodeResponseEntity = nodeResponse == clientResponse ? responseEntity : nodeResponse.getClientResponse().readEntity(ControllerServiceReferencingComponentsEntity.class);
        final Set<ControllerServiceReferencingComponentEntity> nodeReferencingComponents = nodeResponseEntity.getControllerServiceReferencingComponents();
        resultsMap.put(nodeResponse.getNodeId(), nodeReferencingComponents);
    }
    ControllerServiceEntityMerger.mergeControllerServiceReferences(referencingComponents, resultsMap);
    return new NodeResponse(clientResponse, responseEntity);
}
Also used : ControllerServiceReferencingComponentsEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity) Set(java.util.Set) ControllerServiceReferencingComponentEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentEntity) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse)

Example 9 with ControllerServiceReferencingComponentsEntity

use of org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity in project nifi by apache.

the class ControllerServiceResource method getControllerServiceReferences.

/**
 * Retrieves the references of the specified controller service.
 *
 * @param id The id of the controller service to retrieve
 * @return A controllerServiceEntity.
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/references")
@ApiOperation(value = "Gets a controller service", response = ControllerServiceReferencingComponentsEntity.class, authorizations = { @Authorization(value = "Read - /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 getControllerServiceReferences(@ApiParam(value = "The controller service id.", required = true) @PathParam("id") final String id) {
    if (isReplicateRequest()) {
        return replicate(HttpMethod.GET);
    }
    // authorize access
    serviceFacade.authorizeAccess(lookup -> {
        final Authorizable controllerService = lookup.getControllerService(id).getAuthorizable();
        controllerService.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
    });
    // get the controller service
    final ControllerServiceReferencingComponentsEntity entity = serviceFacade.getControllerServiceReferencingComponents(id);
    return generateOkResponse(entity).build();
}
Also used : ControllerServiceReferencingComponentsEntity(org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ControllerServiceReferencingComponentsEntity (org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentsEntity)9 HashMap (java.util.HashMap)6 Set (java.util.Set)6 HashSet (java.util.HashSet)5 List (java.util.List)5 Map (java.util.Map)5 Authorizable (org.apache.nifi.authorization.resource.Authorizable)5 RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)5 ControllerServiceReferencingComponentEntity (org.apache.nifi.web.api.entity.ControllerServiceReferencingComponentEntity)5 ControllerServiceDTO (org.apache.nifi.web.api.dto.ControllerServiceDTO)4 ControllerServiceReferencingComponentDTO (org.apache.nifi.web.api.dto.ControllerServiceReferencingComponentDTO)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 ConfiguredComponent (org.apache.nifi.controller.ConfiguredComponent)3