Search in sources :

Example 1 with VariableRegistryEntity

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

the class EntityFactory method createVariableRegistryEntity.

public VariableRegistryEntity createVariableRegistryEntity(final VariableRegistryDTO dto, final RevisionDTO revision, final PermissionsDTO permissions) {
    final VariableRegistryEntity entity = new VariableRegistryEntity();
    entity.setProcessGroupRevision(revision);
    if (dto != null) {
        if (permissions != null && permissions.getCanRead()) {
            entity.setVariableRegistry(dto);
        }
    }
    return entity;
}
Also used : VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity)

Example 2 with VariableRegistryEntity

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

the class ProcessGroupResource method applyVariableRegistryUpdate.

private void applyVariableRegistryUpdate(final String groupId, final URI originalUri, final VariableRegistryUpdateRequest updateRequest, final VariableRegistryEntity updateEntity) throws InterruptedException, IOException {
    // convert request accordingly
    URI applyUpdatesUri;
    try {
        applyUpdatesUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/process-groups/" + groupId + "/variable-registry", 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, applyUpdatesUri, updateEntity, headers).awaitMergedResponse();
    } else {
        clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), HttpMethod.PUT, applyUpdatesUri, updateEntity, headers).awaitMergedResponse();
    }
    final int applyUpdatesStatus = clusterResponse.getStatus();
    updateRequest.setLastUpdated(new Date());
    updateRequest.getApplyUpdatesStep().setComplete(true);
    if (applyUpdatesStatus == Status.OK.getStatusCode()) {
        // grab the current process group revision
        final VariableRegistryEntity entity = getResponseEntity(clusterResponse, VariableRegistryEntity.class);
        updateRequest.setProcessGroupRevision(entity.getProcessGroupRevision());
    } else {
        final String message = getResponseEntity(clusterResponse, String.class);
        // update the request progress
        updateRequest.getApplyUpdatesStep().setFailureReason("Failed to apply updates to the Variable Registry: " + message);
        updateRequest.setComplete(true);
        updateRequest.setFailureReason("Failed to apply updates to the Variable Registry: " + message);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Date(java.util.Date)

Example 3 with VariableRegistryEntity

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

the class PGSetVar 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 String varName = getRequiredArg(properties, CommandOption.PG_VAR_NAME);
    final String varVal = getRequiredArg(properties, CommandOption.PG_VAR_VALUE);
    final ProcessGroupClient pgClient = client.getProcessGroupClient();
    final VariableRegistryEntity variableRegistry = pgClient.getVariables(pgId);
    final VariableRegistryDTO variableRegistryDTO = variableRegistry.getVariableRegistry();
    final VariableDTO variableDTO = new VariableDTO();
    variableDTO.setName(varName);
    variableDTO.setValue(varVal);
    final VariableEntity variableEntity = new VariableEntity();
    variableEntity.setVariable(variableDTO);
    // take the existing DTO and set only the requested variable for this command
    variableRegistryDTO.setVariables(Collections.singleton(variableEntity));
    // initiate the update request by posting the updated variable registry
    final VariableRegistryUpdateRequestEntity createdUpdateRequest = pgClient.updateVariableRegistry(pgId, variableRegistry);
    // poll the update request for up to 30 seconds to see if it has completed
    // if it doesn't complete then an exception will be thrown, but in either case the request will be deleted
    final String updateRequestId = createdUpdateRequest.getRequest().getRequestId();
    try {
        boolean completed = false;
        for (int i = 0; i < 30; i++) {
            final VariableRegistryUpdateRequestEntity updateRequest = pgClient.getVariableRegistryUpdateRequest(pgId, updateRequestId);
            if (updateRequest != null && updateRequest.getRequest().isComplete()) {
                completed = true;
                break;
            } else {
                try {
                    if (getContext().isInteractive()) {
                        println("Waiting for update request to complete...");
                    }
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!completed) {
            throw new NiFiClientException("Unable to update variables of process group, cancelling request");
        }
    } finally {
        pgClient.deleteVariableRegistryUpdateRequest(pgId, updateRequestId);
    }
    return VoidResult.getInstance();
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) VariableEntity(org.apache.nifi.web.api.entity.VariableEntity) VariableRegistryUpdateRequestEntity(org.apache.nifi.web.api.entity.VariableRegistryUpdateRequestEntity) VariableRegistryDTO(org.apache.nifi.web.api.dto.VariableRegistryDTO) VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity) VariableDTO(org.apache.nifi.web.api.dto.VariableDTO) ProcessGroupClient(org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient)

Example 4 with VariableRegistryEntity

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

the class VariableRegistryEndpointMerger method mergeResponses.

@Override
protected void mergeResponses(final VariableRegistryEntity clientEntity, final Map<NodeIdentifier, VariableRegistryEntity> entityMap, final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses) {
    final VariableRegistryDTO clientVariableRegistry = clientEntity.getVariableRegistry();
    final Set<VariableEntity> clientVariableEntities = clientVariableRegistry.getVariables();
    if (clientVariableEntities != null) {
        for (final Iterator<VariableEntity> i = clientVariableEntities.iterator(); i.hasNext(); ) {
            final VariableEntity clientVariableEntity = i.next();
            final VariableDTO clientVariable = clientVariableEntity.getVariable();
            final Map<NodeIdentifier, Set<AffectedComponentEntity>> nodeAffectedComponentEntities = new HashMap<>();
            boolean retainClientVariable = true;
            for (final Map.Entry<NodeIdentifier, VariableRegistryEntity> nodeEntry : entityMap.entrySet()) {
                final VariableRegistryEntity nodeVariableRegistry = nodeEntry.getValue();
                final Set<VariableEntity> nodeVariableEntities = nodeVariableRegistry.getVariableRegistry().getVariables();
                // if this node has no variables, then the current client variable should be removed
                if (nodeVariableEntities == null) {
                    retainClientVariable = false;
                    break;
                }
                boolean variableFound = false;
                for (final VariableEntity nodeVariableEntity : nodeVariableEntities) {
                    final VariableDTO nodeVariable = nodeVariableEntity.getVariable();
                    // identify the current clientVariable for each node
                    if (clientVariable.getProcessGroupId().equals(nodeVariable.getProcessGroupId()) && clientVariable.getName().equals(nodeVariable.getName())) {
                        variableFound = true;
                        if (Boolean.FALSE.equals(nodeVariableEntity.getCanWrite())) {
                            clientVariableEntity.setCanWrite(false);
                        }
                        nodeAffectedComponentEntities.put(nodeEntry.getKey(), nodeVariableEntity.getVariable().getAffectedComponents());
                        break;
                    }
                }
                if (!variableFound) {
                    retainClientVariable = false;
                    break;
                }
            }
            if (!retainClientVariable) {
                i.remove();
            } else {
                final Set<AffectedComponentEntity> clientAffectedComponentEntities = clientVariableEntity.getVariable().getAffectedComponents();
                affectedComponentEntityMerger.mergeAffectedComponents(clientAffectedComponentEntities, nodeAffectedComponentEntities);
            }
        }
    }
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) VariableRegistryDTO(org.apache.nifi.web.api.dto.VariableRegistryDTO) VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity) VariableEntity(org.apache.nifi.web.api.entity.VariableEntity) VariableDTO(org.apache.nifi.web.api.dto.VariableDTO) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity)

Example 5 with VariableRegistryEntity

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

the class PGGetVars method doExecute.

@Override
public VariableRegistryResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
    final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
    final ProcessGroupClient pgClient = client.getProcessGroupClient();
    final VariableRegistryEntity varEntity = pgClient.getVariables(pgId);
    return new VariableRegistryResult(getResultType(properties), varEntity);
}
Also used : VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity) ProcessGroupClient(org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient) VariableRegistryResult(org.apache.nifi.toolkit.cli.impl.result.VariableRegistryResult)

Aggregations

VariableRegistryEntity (org.apache.nifi.web.api.entity.VariableRegistryEntity)9 VariableRegistryDTO (org.apache.nifi.web.api.dto.VariableRegistryDTO)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 ComponentAuthorizable (org.apache.nifi.authorization.ComponentAuthorizable)3 ProcessGroupAuthorizable (org.apache.nifi.authorization.ProcessGroupAuthorizable)3 SnippetAuthorizable (org.apache.nifi.authorization.SnippetAuthorizable)3 TemplateContentsAuthorizable (org.apache.nifi.authorization.TemplateContentsAuthorizable)3 Authorizable (org.apache.nifi.authorization.resource.Authorizable)3 Revision (org.apache.nifi.web.Revision)3 VariableRegistryUpdateRequestEntity (org.apache.nifi.web.api.entity.VariableRegistryUpdateRequestEntity)3 IOException (java.io.IOException)2 Map (java.util.Map)2