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();
}
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;
}
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);
}
}
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();
}
}
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();
}
}
Aggregations