use of org.apache.nifi.web.api.dto.PortDTO in project kylo by Teradata.
the class ImportReusableTemplate method createRemoteInputPorts.
private boolean createRemoteInputPorts(UploadProgressMessage remoteInputPortsMessage) {
ImportComponentOption remoteProcessGroupOption = importTemplateOptions.findImportComponentOption(ImportComponent.REMOTE_INPUT_PORT);
String rootProcessGroupId = templateConnectionUtil.getRootProcessGroup().getId();
String reusableTemplateProcessGroupId = templateConnectionUtil.getReusableTemplateProcessGroupId();
Map<String, PortDTO> reusableTemplateCategoryPorts = getReusableTemplateCategoryProcessGroup().getContents().getInputPorts().stream().collect(Collectors.toMap(p -> p.getName(), p -> p));
StringBuffer connectedStr = new StringBuffer("");
remoteProcessGroupOption.getRemoteProcessGroupInputPortsForTemplate(importTemplate.getTemplateName()).stream().filter(r -> r.isSelected() && !r.isExisting()).forEach(r -> {
// check/create the port at the parent canvas
PortDTO portDTO = new PortDTO();
portDTO.setName(r.getInputPortName());
portDTO.setType(NifiConstants.INPUT_PORT);
portDTO.setState(NifiProcessUtil.PROCESS_STATE.STOPPED.name());
PortDTO newInputPort = nifiRestClient.getNiFiRestClient().processGroups().createInputPort(rootProcessGroupId, portDTO);
getItemsCreated().addCreatedRemoteInputPort(newInputPort);
PortDTO reusableTemplatePort = reusableTemplateCategoryPorts.get(r.getInputPortName());
// connect this to the Reusable Template input port with the same name
ConnectableDTO source = new ConnectableDTO();
source.setGroupId(rootProcessGroupId);
source.setId(newInputPort.getId());
source.setName(newInputPort.getName());
source.setType(NifiConstants.NIFI_PORT_TYPE.INPUT_PORT.name());
ConnectableDTO dest = new ConnectableDTO();
dest.setGroupId(reusableTemplateProcessGroupId);
dest.setName(r.getInputPortName());
dest.setId(reusableTemplatePort.getId());
dest.setType(NifiConstants.NIFI_PORT_TYPE.INPUT_PORT.name());
ConnectionDTO connectionDTO = nifiRestClient.getNiFiRestClient().processGroups().createConnection(rootProcessGroupId, source, dest);
getItemsCreated().addCreatedRemoteInputPortConnection(connectionDTO);
if (connectedStr.length() != 0) {
connectedStr.append(",");
} else {
connectedStr.append("Created ");
}
connectedStr.append(r.getInputPortName());
remoteInputPortsMessage.update(connectedStr.toString());
});
if (connectedStr.length() != 0) {
connectedStr.append(" as remote input ports");
}
if (connectedStr.length() > 0) {
remoteInputPortsMessage.update(connectedStr.toString(), true);
} else {
remoteInputPortsMessage.complete(true);
}
return true;
}
use of org.apache.nifi.web.api.dto.PortDTO in project kylo by Teradata.
the class ImportReusableTemplate method removeConnectionsAndInputs.
private boolean removeConnectionsAndInputs() {
Optional<TemplateRemoteInputPortConnections> existingRemoteProcessInputPortInformation = getExistingRemoteProcessInputPortInformation();
if (existingRemoteProcessInputPortInformation.isPresent()) {
// find the input ports that are 'existing' but not 'selected' . These should be deleted
Set<String> inputPortNamesToRemove = remoteProcessGroupInputPortMap.values().stream().filter((remoteInputPort -> !remoteInputPort.isSelected() && existingRemoteProcessInputPortInformation.get().getExistingRemoteInputPortNames().contains(remoteInputPort.getInputPortName()))).map(remoteInputPort -> remoteInputPort.getInputPortName()).collect(Collectors.toSet());
// Find the connections that match the input ports that are to be removed
Set<ConnectionDTO> connectionsToRemove = existingRemoteProcessInputPortInformation.get().getExistingRemoteConnectionsToTemplate().stream().filter(connectionDTO -> inputPortNamesToRemove.contains(connectionDTO.getSource().getName())).collect(Collectors.toSet());
log.info("Removing input ports {}", inputPortNamesToRemove);
Set<ConnectionDTO> connectionsWithQueue = new HashSet<>();
// first validate the queues are empty ... if not warn user the following ports cant be deleted and rollback
connectionsToRemove.stream().forEach(connection -> {
Optional<ConnectionStatusEntity> connectionStatus = nifiRestClient.getNiFiRestClient().connections().getConnectionStatus(connection.getId());
if (connectionStatus.isPresent() && connectionStatus.get().getConnectionStatus().getAggregateSnapshot().getFlowFilesQueued() > 0) {
connectionsWithQueue.add(connection);
}
});
if (!connectionsWithQueue.isEmpty()) {
UploadProgressMessage importStatusMessage = uploadProgressService.addUploadStatus(importTemplateOptions.getUploadKey(), "Unable to remove inputPort and connection for :" + connectionsWithQueue.stream().map(c -> c.getSource().getName()).collect(Collectors.joining(",")) + ". The Queues are not empty. Failed to import template: " + importTemplate.getTemplateName(), true, false);
importTemplate.setValid(false);
importTemplate.setSuccess(false);
return false;
} else {
connectionsToRemove.stream().forEach(connection -> {
nifiRestClient.deleteConnection(connection, false);
getItemsCreated().addDeletedRemoteInputPortConnection(connection);
try {
PortDTO deletedPort = nifiRestClient.getNiFiRestClient().ports().deleteInputPort(connection.getSource().getId());
if (deletedPort != null) {
getItemsCreated().addDeletedRemoteInputPort(deletedPort);
}
} catch (NifiComponentNotFoundException e) {
// this is ok to catch as its deleted already
}
});
UploadProgressMessage importStatusMessage = uploadProgressService.addUploadStatus(importTemplateOptions.getUploadKey(), "Removed inputPort and connection for :" + connectionsToRemove.stream().map(c -> c.getSource().getName()).collect(Collectors.joining(",")) + " for template: " + importTemplate.getTemplateName(), true, true);
}
return true;
}
return true;
}
use of org.apache.nifi.web.api.dto.PortDTO in project kylo by Teradata.
the class ImportReusableTemplate method validateOutputPortConnections.
private boolean validateOutputPortConnections(NifiProcessGroup newTemplateInstance) {
// Validate port connections
newTemplateInstance.getProcessGroupEntity().getContents().getOutputPorts().stream().forEach(portDTO -> {
if (portDTO.getValidationErrors() != null && !portDTO.getValidationErrors().isEmpty()) {
importTemplate.setReusableFlowOutputPortConnectionsNeeded(true);
}
ReusableTemplateConnectionInfo connectionInfo = new ReusableTemplateConnectionInfo();
connectionInfo.setFeedOutputPortName(portDTO.getName());
// attempt to prefill it with the previous connection if it existed
ConnectionDTO reusableTemplateInputPortConnection = findReusableTemplateInputPortConnectionForOutputPort(portDTO);
if (reusableTemplateInputPortConnection != null) {
connectionInfo.setInputPortDisplayName(reusableTemplateInputPortConnection.getSource().getName());
connectionInfo.setReusableTemplateInputPortName(reusableTemplateInputPortConnection.getSource().getName());
String processGroupName = findReusableTemplateProcessGroup(reusableTemplateInputPortConnection.getDestination().getGroupId()).map(processGroupDTO -> processGroupDTO.getName()).orElse(null);
connectionInfo.setReusableTemplateProcessGroupName(processGroupName);
}
importTemplate.addReusableTemplateConnection(connectionInfo);
});
return importTemplate.isSuccess() && importTemplate.isValid() && !importTemplate.isReusableFlowOutputPortConnectionsNeeded();
}
use of org.apache.nifi.web.api.dto.PortDTO in project kylo by Teradata.
the class ImportReusableTemplate method recreateOutputPortConnections.
private void recreateOutputPortConnections(UploadProgressMessage importStatusMessage, NifiProcessGroup newTemplateInstance) {
VersionedProcessGroup versionedProcessGroup = newTemplateInstance.getVersionedProcessGroup();
String templateName = importTemplate.getTemplateName();
// Dest == input port in versioned off process group
if (versionedProcessGroup != null) {
String reusableTemplateProcessGroupId = getReusableTemplatesProcessGroupId();
if (reusableTemplateProcessGroupId != null) {
for (ConnectionDTO connectionDTO : versionedProcessGroup.getDeletedInputPortConnections()) {
if (connectionDTO.getSource().getType().equals(NifiConstants.OUTPUT_PORT)) {
// connect
PortDTO destPort = newTemplateInstance.getProcessGroupEntity().getContents().getInputPorts().stream().filter(portDTO -> portDTO.getName().equalsIgnoreCase(connectionDTO.getDestination().getName()) && connectionDTO.getDestination().getGroupId().equalsIgnoreCase(newTemplateInstance.getVersionedProcessGroup().getProcessGroupPriorToVersioning().getId())).findFirst().orElse(null);
if (destPort != null) {
// make the connection now from the output port to the 'connectionToUse' destination
ConnectableDTO source = NifiConnectionUtil.asNewConnectable(connectionDTO.getSource());
ConnectableDTO dest = NifiConnectionUtil.asConnectable(destPort);
ConnectionDTO newConnection = nifiRestClient.getNiFiRestClient().processGroups().createConnection(reusableTemplateProcessGroupId, source, dest);
connections.add(newConnection);
// possibly store the ports too?
log.info("Reconnected output port {} ({}) to this new process group input port: {} {{}) ", source.getName(), source.getId(), dest.getName(), dest.getId());
} else {
// ERROR cant recreate previous connections that were going into this reusable template
String msg = "Unable to recreate the connection for template: " + templateName + " that was previously connected to this template prior to the update. The following connection is missing: Connecting ['" + connectionDTO.getSource().getName() + "' to '" + connectionDTO.getDestination().getName() + "'].";
log.error(msg);
importTemplate.getTemplateResults().addError(NifiError.SEVERITY.FATAL, msg, "");
importStatusMessage.update("Unable to establish prior connection for reusable template: " + templateName + ". Connection: ['" + connectionDTO.getSource().getName() + "' to '" + connectionDTO.getDestination().getName() + "']", false);
break;
}
}
}
}
}
}
use of org.apache.nifi.web.api.dto.PortDTO in project kylo by Teradata.
the class ImportReusableTemplate method rollbackCreatedItems.
/**
* Rollback and delete any created items
* restore any items that were deleted
*/
private void rollbackCreatedItems() {
getItemsCreated().getCreatedRemoteInputPortConnections().stream().forEach(connection -> nifiRestClient.deleteConnection(connection, false));
getItemsCreated().getCreatedRemoteInputPorts().stream().forEach(portDTO -> nifiRestClient.getNiFiRestClient().ports().deleteInputPort(portDTO.getId()));
Map<String, String> oldToNewPortIdMap = new HashMap<>();
getItemsCreated().getDeletedRemoteInputPorts().stream().forEach(portDTO -> {
PortDTO createdPort = nifiRestClient.getNiFiRestClient().processGroups().createInputPort(portDTO.getParentGroupId(), portDTO);
oldToNewPortIdMap.put(portDTO.getId(), createdPort.getId());
});
// find matching connection
Map<String, String> newReusableTemplatePortNameToId = new HashMap<>();
String reusableTemplateProcessGroupId = templateConnectionUtil.getReusableTemplateProcessGroupId();
if (reusableTemplateProcessGroupId != null) {
ProcessGroupFlowDTO reusableTemplateFlow = nifiRestClient.getNiFiRestClient().processGroups().flow(reusableTemplateProcessGroupId);
String templateProcessGroupId = reusableTemplateFlow.getFlow().getProcessGroups().stream().filter(e -> e.getComponent().getName().equalsIgnoreCase(this.importTemplate.getTemplateName())).map(e -> e.getComponent().getId()).findFirst().orElse(null);
if (templateProcessGroupId != null) {
reusableTemplateFlow.getFlow().getConnections().stream().filter(connectionEntity -> connectionEntity.getComponent().getDestination().getGroupId().equalsIgnoreCase(templateProcessGroupId) && connectionEntity.getComponent().getSource().getType().equalsIgnoreCase(NifiConstants.INPUT_PORT)).forEach(connectionEntity -> {
newReusableTemplatePortNameToId.put(connectionEntity.getComponent().getSource().getName(), connectionEntity.getComponent().getSource().getId());
});
}
}
String rootProcessGroupId = templateConnectionUtil.getRootProcessGroup().getId();
getItemsCreated().getDeletedRemoteInputPortConnections().stream().forEach(connectionDTO -> {
String newId = oldToNewPortIdMap.get(connectionDTO.getSource().getId());
connectionDTO.getSource().setId(newId);
String newDestId = newReusableTemplatePortNameToId.get(connectionDTO.getSource().getName());
connectionDTO.getDestination().setId(newDestId);
ConnectionDTO restoredConnection = nifiRestClient.getNiFiRestClient().processGroups().createConnection(rootProcessGroupId, connectionDTO.getSource(), connectionDTO.getDestination());
});
}
Aggregations