Search in sources :

Example 1 with NifiConnectionException

use of com.thinkbiganalytics.nifi.rest.client.NifiConnectionException in project kylo by Teradata.

the class DefaultTemplateExporter method zip.

private byte[] zip(RegisteredTemplate template, String nifiTemplateXml, List<String> reusableTemplateXmls, Set<ReusableTemplateConnectionInfo> outputPortMetadata, Set<RemoteProcessGroupInputPort> reusableTemplateRemoteInputPorts) {
    if (nifiTemplateXml == null) {
        throw new NifiConnectionException("Attempt to export using invalid template");
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        ZipEntry entry = new ZipEntry(ImportTemplate.NIFI_TEMPLATE_XML_FILE);
        zos.putNextEntry(entry);
        zos.write(nifiTemplateXml.getBytes());
        zos.closeEntry();
        int reusableTemplateNumber = 0;
        for (String reusableTemplateXml : reusableTemplateXmls) {
            entry = new ZipEntry(String.format("%s_%s.xml", ImportTemplate.NIFI_CONNECTING_REUSABLE_TEMPLATE_XML_FILE, reusableTemplateNumber++));
            zos.putNextEntry(entry);
            zos.write(reusableTemplateXml.getBytes());
            zos.closeEntry();
        }
        entry = new ZipEntry(ImportTemplate.TEMPLATE_JSON_FILE);
        zos.putNextEntry(entry);
        String json = ObjectMapperSerializer.serialize(template);
        zos.write(json.getBytes());
        zos.closeEntry();
        if (outputPortMetadata != null && !outputPortMetadata.isEmpty()) {
            entry = new ZipEntry(ImportTemplate.REUSABLE_TEMPLATE_OUTPUT_CONNECTION_FILE);
            zos.putNextEntry(entry);
            json = ObjectMapperSerializer.serialize(outputPortMetadata);
            zos.write(json.getBytes());
            zos.closeEntry();
        }
        if (reusableTemplateRemoteInputPorts != null && !reusableTemplateRemoteInputPorts.isEmpty()) {
            entry = new ZipEntry(ImportTemplate.REUSABLE_TEMPLATE_REMOTE_INPUT_PORT_JSON_FILE);
            zos.putNextEntry(entry);
            json = ObjectMapperSerializer.serialize(reusableTemplateRemoteInputPorts);
            zos.write(json.getBytes());
            zos.closeEntry();
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    return baos.toByteArray();
}
Also used : NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with NifiConnectionException

use of com.thinkbiganalytics.nifi.rest.client.NifiConnectionException in project kylo by Teradata.

the class TemplateImporter method validate.

public ImportTemplate validate() {
    try {
        init();
        AbstractValidateImportTemplate validateImportTemplate = validateImportTemplateFactory.apply(this.importTemplate, this.importTemplateOptions, importType);
        validateImportTemplate.validate();
    } catch (NifiConnectionException e) {
        throw e;
    } catch (Exception e) {
        this.overallStatusMessage.update("An Error occurred " + e.getMessage(), false);
        throw new TemplateImportException("Error importing template  " + fileName + ".  " + e.getMessage());
    }
    overallStatusMessage.update("Validated template for import ", this.importTemplate.isValid());
    return this.importTemplate;
}
Also used : NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) AbstractValidateImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.validation.AbstractValidateImportTemplate) TemplateImportException(com.thinkbiganalytics.nifi.feedmgr.TemplateImportException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) IOException(java.io.IOException) TemplateImportException(com.thinkbiganalytics.nifi.feedmgr.TemplateImportException)

Example 3 with NifiConnectionException

use of com.thinkbiganalytics.nifi.rest.client.NifiConnectionException in project kylo by Teradata.

the class DefaultTemplateExporter method export.

private ExportTemplate export(String templateId) {
    RegisteredTemplate template = registeredTemplateService.findRegisteredTemplate(new RegisteredTemplateRequest.Builder().templateId(templateId).nifiTemplateId(templateId).includeSensitiveProperties(true).build());
    if (template != null) {
        List<String> connectingReusableTemplates = new ArrayList<>();
        Set<String> connectedTemplateIds = new HashSet<>();
        Set<ReusableTemplateConnectionInfo> outputPortConnectionMetadata = new HashSet<>();
        Set<RemoteProcessGroupInputPort> templateRemoteInputPorts = new HashSet<>();
        List<ReusableTemplateConnectionInfo> reusableTemplateConnectionInfos = null;
        if (template.usesReusableTemplate()) {
            reusableTemplateConnectionInfos = template.getReusableTemplateConnections();
        }
        List<ReusableTemplateConnectionInfo> remoteProcessGroupConnectionInfo = getRemoteProcessGroupConnectionInfo(template.getNifiTemplate());
        if (reusableTemplateConnectionInfos != null) {
            reusableTemplateConnectionInfos.addAll(remoteProcessGroupConnectionInfo);
        } else {
            reusableTemplateConnectionInfos = remoteProcessGroupConnectionInfo;
        }
        if (reusableTemplateConnectionInfos != null && !reusableTemplateConnectionInfos.isEmpty()) {
            ProcessGroupFlowDTO reusableTemplateFlow = templateConnectionUtil.getReusableTemplateCategoryProcessGroupFlow();
            Map<String, PortDTOWithGroupInfo> reusableTemplatePorts = templateConnectionUtil.getReusableFeedInputPorts(reusableTemplateFlow).stream().collect(Collectors.toMap(port -> port.getName(), port -> port));
            reusableTemplateConnectionInfos.stream().filter(connectionInfo -> StringUtils.isBlank(connectionInfo.getReusableTemplateProcessGroupName())).forEach(connectionInfo -> {
                PortDTOWithGroupInfo port = reusableTemplatePorts.get(connectionInfo.getReusableTemplateInputPortName());
                if (port != null) {
                    connectionInfo.setReusableTemplateProcessGroupName(port.getDestinationProcessGroupName());
                }
            });
            // Get flow information for the 'reusable_templates' process group in NiFi
            if (reusableTemplateFlow != null) {
                gatherConnectedReusableTemplates(connectingReusableTemplates, connectedTemplateIds, outputPortConnectionMetadata, reusableTemplateConnectionInfos, reusableTemplateFlow);
            }
            // Only gather remote input ports on the reusable templates if enabled
            if (isRemoteProcessGroupsEnabled()) {
                // for all the reusable templates used gather any that have remote input ports
                reusableTemplateConnectionInfos.stream().forEach(connectionInfo -> {
                    Set<RemoteProcessGroupInputPort> remoteProcessGroupInputPorts = findReusableTemplateRemoteInputPorts(reusableTemplateFlow, connectionInfo.getReusableTemplateProcessGroupName());
                    templateRemoteInputPorts.addAll(remoteProcessGroupInputPorts);
                });
            }
        }
        String templateXml = null;
        try {
            if (template != null) {
                try {
                    templateXml = nifiRestClient.getTemplateXml(template.getNifiTemplateId());
                } catch (NifiClientRuntimeException e) {
                    TemplateDTO templateDTO = nifiRestClient.getTemplateByName(template.getTemplateName());
                    if (templateDTO != null) {
                        templateXml = nifiRestClient.getTemplateXml(templateDTO.getId());
                    }
                }
            }
        } catch (NifiConnectionException e) {
            throw e;
        } catch (Exception e) {
            throw new TemplateExportException("Unable to find Nifi Template for " + templateId);
        }
        // create a zip file with the template and xml
        byte[] zipFile = zip(template, templateXml, connectingReusableTemplates, outputPortConnectionMetadata, templateRemoteInputPorts);
        return new ExportTemplate(SystemNamingService.generateSystemName(template.getTemplateName()) + ".template.zip", template.getTemplateName(), template.getDescription(), template.isStream(), zipFile);
    } else {
        throw new TemplateExportException("Unable to find Template for " + templateId);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PortDTOWithGroupInfo(com.thinkbiganalytics.feedmgr.rest.model.PortDTOWithGroupInfo) ReusableTemplateConnectionInfo(com.thinkbiganalytics.feedmgr.rest.model.ReusableTemplateConnectionInfo) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) StringUtils(org.apache.commons.lang3.StringUtils) RegisteredTemplateRequest(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplateRequest) NifiRemoteProcessGroupUtil(com.thinkbiganalytics.nifi.rest.support.NifiRemoteProcessGroupUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ObjectMapperSerializer(com.thinkbiganalytics.json.ObjectMapperSerializer) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) TemplateExportException(com.thinkbiganalytics.nifi.feedmgr.TemplateExportException) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) TemplateAccessControl(com.thinkbiganalytics.metadata.api.template.security.TemplateAccessControl) Map(java.util.Map) AccessController(com.thinkbiganalytics.security.AccessController) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) ZipEntry(java.util.zip.ZipEntry) NifiConstants(com.thinkbiganalytics.nifi.rest.support.NifiConstants) RemoteProcessGroupInputPort(com.thinkbiganalytics.feedmgr.rest.model.RemoteProcessGroupInputPort) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) TemplateExporter(com.thinkbiganalytics.metadata.api.template.export.TemplateExporter) Set(java.util.Set) IOException(java.io.IOException) SystemNamingService(com.thinkbiganalytics.feedmgr.rest.support.SystemNamingService) Collectors(java.util.stream.Collectors) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) List(java.util.List) Optional(java.util.Optional) TemplateConnectionUtil(com.thinkbiganalytics.feedmgr.nifi.TemplateConnectionUtil) TemplateRemoteInputPortConnections(com.thinkbiganalytics.feedmgr.rest.model.TemplateRemoteInputPortConnections) Collections(java.util.Collections) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) ArrayList(java.util.ArrayList) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) TemplateExportException(com.thinkbiganalytics.nifi.feedmgr.TemplateExportException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) IOException(java.io.IOException) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) RemoteProcessGroupInputPort(com.thinkbiganalytics.feedmgr.rest.model.RemoteProcessGroupInputPort) TemplateExportException(com.thinkbiganalytics.nifi.feedmgr.TemplateExportException) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) RegisteredTemplateRequest(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplateRequest) ReusableTemplateConnectionInfo(com.thinkbiganalytics.feedmgr.rest.model.ReusableTemplateConnectionInfo) PortDTOWithGroupInfo(com.thinkbiganalytics.feedmgr.rest.model.PortDTOWithGroupInfo) HashSet(java.util.HashSet)

Example 4 with NifiConnectionException

use of com.thinkbiganalytics.nifi.rest.client.NifiConnectionException in project kylo by Teradata.

the class FeedRestController method saveDraftFeed.

@POST
@Path("/{feedId}/versions/draft/entity")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Creates or saves a feed as draft version.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the feed metadata.", response = NifiFeed.class), @ApiResponse(code = 500, message = "An internal error occurred.", response = RestResponseStatus.class) })
@Nonnull
public Response saveDraftFeed(@Nonnull final FeedMetadata feedMetadata) {
    try {
        FeedMetadata update = getMetadataService().saveDraftFeed(feedMetadata);
        NifiFeed feed = new NifiFeed(update, null);
        feed.setSuccess(true);
        return Response.ok(feed).build();
    } catch (DuplicateFeedNameException e) {
        log.info("Failed to create a new feed due to another feed having the same category/feed name: " + feedMetadata.getCategoryAndFeedDisplayName());
        // Create an error message
        String msg = "A feed already exists in the category \"" + e.getCategoryName() + "\" with name name \"" + e.getFeedName() + "\"";
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.CONFLICT).entity(feed).build();
    } catch (NifiConnectionException e) {
        throw e;
    } catch (Exception e) {
        log.error("Failed to create a new feed.", e);
        // Create an error message
        String msg = (e.getMessage() != null) ? "Error creating Feed: " + e.getMessage() : "An unknown error occurred while saving the feed.";
        if (e.getCause() instanceof JDBCException) {
            msg += ". " + ((JDBCException) e).getSQLException();
        }
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(feed).build();
    }
}
Also used : JDBCException(org.hibernate.JDBCException) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) NifiFeed(com.thinkbiganalytics.feedmgr.rest.model.NifiFeed) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) FeedCleanupTimeoutException(com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException) FeedCleanupFailedException(com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException) WebApplicationException(javax.ws.rs.WebApplicationException) AccessDeniedException(java.nio.file.AccessDeniedException) DeployFeedException(com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) IOException(java.io.IOException) FeedCurrentlyRunningException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException) ClientErrorException(javax.ws.rs.ClientErrorException) JDBCException(org.hibernate.JDBCException) FeedHistoryDataReindexingNotEnabledException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) AccessControlException(java.security.AccessControlException) DataAccessException(org.springframework.dao.DataAccessException) VersionNotFoundException(com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException) FeedNotFoundException(com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Nonnull(javax.annotation.Nonnull) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with NifiConnectionException

use of com.thinkbiganalytics.nifi.rest.client.NifiConnectionException in project kylo by Teradata.

the class FeedRestController method createDraftFeed.

@POST
@Path("/draft/entity")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Creates a new feed as draft version.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the feed metadata.", response = NifiFeed.class), @ApiResponse(code = 500, message = "An internal error occurred.", response = RestResponseStatus.class) })
@Nonnull
public Response createDraftFeed(@Nonnull final FeedMetadata feedMetadata) {
    try {
        FeedMetadata update = getMetadataService().saveDraftFeed(feedMetadata);
        NifiFeed feed = new NifiFeed(update, null);
        feed.setSuccess(true);
        return Response.ok(feed).build();
    } catch (DuplicateFeedNameException e) {
        log.info("Failed to create a new feed due to another feed having the same category/feed name: " + feedMetadata.getCategoryAndFeedDisplayName());
        // Create an error message
        String msg = "A feed already exists in the category \"" + e.getCategoryName() + "\" with name name \"" + e.getFeedName() + "\"";
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.CONFLICT).entity(feed).build();
    } catch (NifiConnectionException e) {
        throw e;
    } catch (Exception e) {
        log.error("Failed to create a new feed.", e);
        // Create an error message
        String msg = (e.getMessage() != null) ? "Error creating Feed: " + e.getMessage() : "An unknown error occurred while saving the feed.";
        if (e.getCause() instanceof JDBCException) {
            msg += ". " + ((JDBCException) e).getSQLException();
        }
        // Add error message to feed
        NifiFeed feed = new NifiFeed(feedMetadata, null);
        feed.addErrorMessage(msg);
        feed.setSuccess(false);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(feed).build();
    }
}
Also used : JDBCException(org.hibernate.JDBCException) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) NifiFeed(com.thinkbiganalytics.feedmgr.rest.model.NifiFeed) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) FeedCleanupTimeoutException(com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException) FeedCleanupFailedException(com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException) WebApplicationException(javax.ws.rs.WebApplicationException) AccessDeniedException(java.nio.file.AccessDeniedException) DeployFeedException(com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException) DuplicateFeedNameException(com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException) IOException(java.io.IOException) FeedCurrentlyRunningException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException) ClientErrorException(javax.ws.rs.ClientErrorException) JDBCException(org.hibernate.JDBCException) FeedHistoryDataReindexingNotEnabledException(com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) AccessControlException(java.security.AccessControlException) DataAccessException(org.springframework.dao.DataAccessException) VersionNotFoundException(com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException) FeedNotFoundException(com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException) NifiConnectionException(com.thinkbiganalytics.nifi.rest.client.NifiConnectionException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Nonnull(javax.annotation.Nonnull) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

NifiConnectionException (com.thinkbiganalytics.nifi.rest.client.NifiConnectionException)5 IOException (java.io.IOException)5 NifiClientRuntimeException (com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException)4 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)2 NifiFeed (com.thinkbiganalytics.feedmgr.rest.model.NifiFeed)2 FeedCleanupFailedException (com.thinkbiganalytics.feedmgr.service.FeedCleanupFailedException)2 FeedCleanupTimeoutException (com.thinkbiganalytics.feedmgr.service.FeedCleanupTimeoutException)2 DeployFeedException (com.thinkbiganalytics.feedmgr.service.feed.DeployFeedException)2 DuplicateFeedNameException (com.thinkbiganalytics.feedmgr.service.feed.DuplicateFeedNameException)2 FeedCurrentlyRunningException (com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedCurrentlyRunningException)2 FeedHistoryDataReindexingNotEnabledException (com.thinkbiganalytics.feedmgr.service.feed.reindexing.FeedHistoryDataReindexingNotEnabledException)2 FeedNotFoundException (com.thinkbiganalytics.metadata.api.feed.FeedNotFoundException)2 VersionNotFoundException (com.thinkbiganalytics.metadata.api.versioning.VersionNotFoundException)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 AccessDeniedException (java.nio.file.AccessDeniedException)2 AccessControlException (java.security.AccessControlException)2 Nonnull (javax.annotation.Nonnull)2 ClientErrorException (javax.ws.rs.ClientErrorException)2 Consumes (javax.ws.rs.Consumes)2