Search in sources :

Example 41 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class NiFiControllerServicesRestClientV1 method create.

@Nonnull
@Override
public ControllerServiceDTO create(@Nonnull ControllerServiceDTO controllerService) {
    final ControllerServiceEntity entity = new ControllerServiceEntity();
    entity.setComponent(controllerService);
    final RevisionDTO revision = new RevisionDTO();
    revision.setVersion(0L);
    entity.setRevision(revision);
    return client.post("/process-groups/root/controller-services", entity, ControllerServiceEntity.class).getComponent();
}
Also used : ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Nonnull(javax.annotation.Nonnull)

Example 42 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class NiFiPortsRestClientV1 method updateInputPortEntity.

private PortEntity updateInputPortEntity(@Nonnull final String processGroupId, @Nonnull final PortDTO inputPort, Integer retryAttempt) {
    // Get revision
    PortEntity current;
    try {
        current = client.get("/input-ports/" + inputPort.getId(), null, PortEntity.class);
    } catch (NotFoundException e) {
        throw new NifiComponentNotFoundException(inputPort.getId(), NifiConstants.NIFI_COMPONENT_TYPE.INPUT_PORT, e);
    }
    if (current == null || (current != null && isModified(current.getComponent(), inputPort))) {
        boolean update = true;
        // if we are trying to do anything but disable the port and the port is currently invalid prevent it from updating.
        if (current != null && current.getStatus().getRunStatus().equalsIgnoreCase("invalid") && !inputPort.getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.DISABLED.name())) {
            update = false;
        } else if (StringUtils.isNotBlank(inputPort.getId()) && NifiProcessUtil.PROCESS_STATE.RUNNING.name().equals(inputPort.getState())) {
            // only mark input port as running if we have connections to it
            // NIFI bug
            Set<ConnectionDTO> connectionDTOS = client.connections().findConnectionsToEntity(processGroupId, inputPort.getId());
            // find this ports processgroup parent
            Optional<ProcessGroupDTO> group = client.processGroups().findById(processGroupId, false, false);
            if (group.isPresent() && StringUtils.isNotBlank(group.get().getParentGroupId())) {
                Set<ConnectionDTO> upstreamConnections = client.connections().findConnectionsToEntity(group.get().getParentGroupId(), inputPort.getId());
                if (upstreamConnections != null && !upstreamConnections.isEmpty()) {
                    if (connectionDTOS == null) {
                        connectionDTOS = new HashSet<>();
                    }
                    connectionDTOS = Stream.concat(connectionDTOS.stream(), upstreamConnections.stream()).collect(Collectors.toSet());
                }
            }
            boolean isRootProcessGroup = client.processGroups().isRoot(processGroupId);
            // always update root input portsgroups as they will be needed to be running if used by Remote Process Groups
            if (!isRootProcessGroup && (connectionDTOS == null || connectionDTOS.isEmpty() || connectionDTOS.stream().noneMatch(connectionDTO -> connectionDTO.getDestination() != null && connectionDTO.getDestination().getId().equals(inputPort.getId())))) {
                log.warn("System will not start the input port [{}] [{}] in the process group [{}] since there are no upstream connections to it ", inputPort.getId(), inputPort.getName(), processGroupId);
                update = false;
            }
        }
        if (update) {
            if (StringUtils.isNotBlank(inputPort.getState())) {
                // if trying to make a DISABLED port RUNNING you need to make it STOPPED first and then mark it as RUNNING
                if (current != null && current.getComponent().getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.DISABLED.name()) && inputPort.getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.RUNNING.name())) {
                    // first need to make it ENABLED
                    inputPort.setState(NifiProcessUtil.PROCESS_STATE.STOPPED.name());
                    current = updateInputPortEntity(processGroupId, inputPort, retryAttempt);
                    inputPort.setState(NifiProcessUtil.PROCESS_STATE.RUNNING.name());
                }
                // if trying to make a RUNNING port DISABLED you need to make it STOPPED first and then mark it as DISABLED
                if (current != null && current.getComponent().getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.RUNNING.name()) && inputPort.getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.DISABLED.name())) {
                    // first need to make it ENABLED
                    inputPort.setState(NifiProcessUtil.PROCESS_STATE.STOPPED.name());
                    current = updateInputPortEntity(processGroupId, inputPort, retryAttempt);
                    inputPort.setState(NifiProcessUtil.PROCESS_STATE.DISABLED.name());
                }
            }
            // Update input port
            final PortEntity entity = new PortEntity();
            entity.setComponent(inputPort);
            final RevisionDTO revision = new RevisionDTO();
            revision.setVersion(current.getRevision().getVersion());
            entity.setRevision(revision);
            try {
                return client.put("/input-ports/" + inputPort.getId(), entity, PortEntity.class);
            } catch (final NotFoundException e) {
                throw new NifiComponentNotFoundException(inputPort.getId(), NifiConstants.NIFI_COMPONENT_TYPE.INPUT_PORT, e);
            } catch (Exception e) {
                if (retryAttempt < 3) {
                    retryAttempt++;
                    log.info("An exception occurred attempting to update input port {}.  Retrying update {}/3", inputPort.getId(), retryAttempt);
                    delay(500L);
                    return updateInputPortEntity(processGroupId, inputPort, retryAttempt);
                } else {
                    log.info("An exception occurred attempting to update input port {}. Max retries of 3 reached.", inputPort.getId(), e);
                    throw e;
                }
            }
        }
    }
    if (current == null) {
        PortEntity entity = new PortEntity();
        entity.setComponent(inputPort);
        entity.setPortType(inputPort.getType());
        return entity;
    } else {
        return current;
    }
}
Also used : NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) HashSet(java.util.HashSet) Set(java.util.Set) Optional(java.util.Optional) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) PortEntity(org.apache.nifi.web.api.entity.PortEntity) HashSet(java.util.HashSet)

Example 43 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class NiFiPortsRestClientV1 method updateOutputPortEntity.

@Nonnull
private PortEntity updateOutputPortEntity(@Nonnull final String processGroupId, @Nonnull final PortDTO outputPort, Integer retryAttempt) {
    // Get revision
    PortEntity current;
    try {
        current = client.get("/output-ports/" + outputPort.getId(), null, PortEntity.class);
    } catch (NotFoundException e) {
        throw new NifiComponentNotFoundException(outputPort.getId(), NifiConstants.NIFI_COMPONENT_TYPE.OUTPUT_PORT, e);
    }
    PortDTO currentPort = current != null ? current.getComponent() : null;
    boolean update = true;
    // if we are trying to do anything but disable the port and the port is currently invalid prevent it from updating.
    if (current != null && current.getStatus().getRunStatus().equalsIgnoreCase("invalid") && !outputPort.getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.DISABLED.name())) {
        update = false;
    } else if (StringUtils.isNotBlank(outputPort.getId()) && NifiProcessUtil.PROCESS_STATE.RUNNING.name().equals(outputPort.getState())) {
        // only mark port as running if we have connections to it
        // NIFI bug
        Set<ConnectionDTO> connectionDTOS = client.connections().findConnectionsToEntity(processGroupId, outputPort.getId());
        if (connectionDTOS == null || connectionDTOS.isEmpty() || connectionDTOS.stream().noneMatch(connectionDTO -> (connectionDTO.getSource() != null && connectionDTO.getSource().getId().equals(outputPort.getId())) || (connectionDTO.getDestination() != null && connectionDTO.getDestination().getId().equalsIgnoreCase(outputPort.getId())))) {
            log.warn("System will not start the output port [{}] [{}] in the process group [{}] since there are no connections to it ", outputPort.getId(), outputPort.getName(), processGroupId);
            update = false;
        }
    }
    if (update) {
        if (StringUtils.isNotBlank(outputPort.getState())) {
            // if trying to make a DISABLED port RUNNING you need to make it STOPPED first and then mark it as RUNNING
            if (current != null && current.getComponent().getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.DISABLED.name()) && outputPort.getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.RUNNING.name())) {
                // first need to make it ENABLED
                outputPort.setState(NifiProcessUtil.PROCESS_STATE.STOPPED.name());
                current = updateOutputPortEntity(processGroupId, outputPort, retryAttempt);
                delay(500L);
                outputPort.setState(NifiProcessUtil.PROCESS_STATE.RUNNING.name());
            }
            // if trying to make a RUNNING port DISABLED you need to make it STOPPED first and then mark it as DISABLED
            if (current != null && current.getComponent().getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.RUNNING.name()) && outputPort.getState().equalsIgnoreCase(NifiProcessUtil.PROCESS_STATE.DISABLED.name())) {
                // first need to make it ENABLED
                outputPort.setState(NifiProcessUtil.PROCESS_STATE.STOPPED.name());
                current = updateOutputPortEntity(processGroupId, outputPort, retryAttempt);
                delay(500L);
                outputPort.setState(NifiProcessUtil.PROCESS_STATE.DISABLED.name());
            }
        }
        // Update output port
        final PortEntity entity = new PortEntity();
        entity.setComponent(outputPort);
        final RevisionDTO revision = new RevisionDTO();
        revision.setVersion(current.getRevision().getVersion());
        entity.setRevision(revision);
        try {
            return client.put("/output-ports/" + outputPort.getId(), entity, PortEntity.class);
        } catch (final NotFoundException e) {
            throw new NifiComponentNotFoundException(outputPort.getId(), NifiConstants.NIFI_COMPONENT_TYPE.OUTPUT_PORT, e);
        } catch (Exception e) {
            if (retryAttempt < 3) {
                retryAttempt++;
                log.info("An exception occurred attempting to update output port {}.  Retrying update {}/3", outputPort.getId(), retryAttempt);
                delay(500L);
                return updateOutputPortEntity(processGroupId, outputPort, retryAttempt);
            } else {
                log.info("An exception occurred attempting to update output port {}. Max retries of 3 reached.", outputPort.getId(), e);
                throw e;
            }
        }
    }
    if (current == null) {
        PortEntity entity = new PortEntity();
        entity.setComponent(outputPort);
        entity.setPortType(outputPort.getType());
        return entity;
    } else {
        return current;
    }
}
Also used : NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) HashSet(java.util.HashSet) Set(java.util.Set) PortDTO(org.apache.nifi.web.api.dto.PortDTO) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) PortEntity(org.apache.nifi.web.api.entity.PortEntity) Nonnull(javax.annotation.Nonnull)

Example 44 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class NiFiProcessGroupsRestClientV1 method create.

public ProcessGroupDTO create(@Nonnull String parentProcessGroupId, @Nonnull String name, @Nullable Double x, @Nullable Double y) {
    final ProcessGroupEntity entity = new ProcessGroupEntity();
    final ProcessGroupDTO processGroup = new ProcessGroupDTO();
    processGroup.setName(name);
    if (x != null && y != null) {
        processGroup.setPosition(new PositionDTO(x, y));
    }
    entity.setComponent(processGroup);
    final RevisionDTO revision = new RevisionDTO();
    revision.setVersion(0L);
    entity.setRevision(revision);
    try {
        return getClient().post(BASE_PATH + parentProcessGroupId + "/process-groups", entity, ProcessGroupEntity.class).getComponent();
    } catch (final NotFoundException e) {
        throw new NifiComponentNotFoundException(parentProcessGroupId, NifiConstants.NIFI_COMPONENT_TYPE.PROCESS_GROUP, e);
    }
}
Also used : NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) RemoteProcessGroupEntity(org.apache.nifi.web.api.entity.RemoteProcessGroupEntity) ProcessGroupEntity(org.apache.nifi.web.api.entity.ProcessGroupEntity) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO)

Example 45 with RevisionDTO

use of org.apache.nifi.web.api.dto.RevisionDTO in project kylo by Teradata.

the class NiFiPortsRestClientV1 method updateOutputPort.

@Nonnull
@Override
public PortDTO updateOutputPort(@Nonnull final String processGroupId, @Nonnull final PortDTO outputPort) {
    // Get revision
    final PortEntity current;
    try {
        current = client.get("/output-ports/" + outputPort.getId(), null, PortEntity.class);
    } catch (NotFoundException e) {
        throw new NifiComponentNotFoundException(outputPort.getId(), NifiConstants.NIFI_COMPONENT_TYPE.OUTPUT_PORT, e);
    }
    // Update output port
    final PortEntity entity = new PortEntity();
    entity.setComponent(outputPort);
    final RevisionDTO revision = new RevisionDTO();
    revision.setVersion(current.getRevision().getVersion());
    entity.setRevision(revision);
    try {
        return client.put("/output-ports/" + outputPort.getId(), entity, PortEntity.class).getComponent();
    } catch (final NotFoundException e) {
        throw new NifiComponentNotFoundException(outputPort.getId(), NifiConstants.NIFI_COMPONENT_TYPE.OUTPUT_PORT, e);
    }
}
Also used : NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) PortEntity(org.apache.nifi.web.api.entity.PortEntity) Nonnull(javax.annotation.Nonnull)

Aggregations

RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)96 Response (javax.ws.rs.core.Response)45 Authorizable (org.apache.nifi.authorization.resource.Authorizable)30 PermissionsDTO (org.apache.nifi.web.api.dto.PermissionsDTO)29 PortEntity (org.apache.nifi.web.api.entity.PortEntity)27 ProcessGroupEntity (org.apache.nifi.web.api.entity.ProcessGroupEntity)27 ProcessorEntity (org.apache.nifi.web.api.entity.ProcessorEntity)26 HashMap (java.util.HashMap)24 Set (java.util.Set)24 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)24 Map (java.util.Map)23 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)23 HashSet (java.util.HashSet)22 Collectors (java.util.stream.Collectors)22 ControllerServiceEntity (org.apache.nifi.web.api.entity.ControllerServiceEntity)22 ScheduledState (org.apache.nifi.controller.ScheduledState)21 ControllerServiceState (org.apache.nifi.controller.service.ControllerServiceState)21 VersionControlInformationDTO (org.apache.nifi.web.api.dto.VersionControlInformationDTO)21 Authorizer (org.apache.nifi.authorization.Authorizer)19 RequestAction (org.apache.nifi.authorization.RequestAction)19