use of org.apache.nifi.web.api.dto.ConnectionDTO in project nifi by apache.
the class StandardNiFiServiceFacade method deleteConnection.
@Override
public ConnectionEntity deleteConnection(final Revision revision, final String connectionId) {
final Connection connection = connectionDAO.getConnection(connectionId);
final PermissionsDTO permissions = dtoFactory.createPermissionsDto(connection);
final ConnectionDTO snapshot = deleteComponent(revision, connection.getResource(), () -> connectionDAO.deleteConnection(connectionId), // no policies to remove
false, dtoFactory.createConnectionDto(connection));
return entityFactory.createConnectionEntity(snapshot, null, permissions, null);
}
use of org.apache.nifi.web.api.dto.ConnectionDTO in project nifi by apache.
the class ConnectionResource method updateConnection.
/**
* Updates the specified connection.
*
* @param httpServletRequest request
* @param id The id of the connection.
* @param requestConnectionEntity A connectionEntity.
* @return A connectionEntity.
* @throws InterruptedException if interrupted
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
@ApiOperation(value = "Updates a connection", response = ConnectionEntity.class, authorizations = { @Authorization(value = "Write Source - /{component-type}/{uuid}"), @Authorization(value = "Write Destination - /{component-type}/{uuid}"), @Authorization(value = "Write New Destination - /{component-type}/{uuid} - if updating Destination"), @Authorization(value = "Write Process Group - /process-groups/{uuid} - if updating Destination") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response updateConnection(@Context HttpServletRequest httpServletRequest, @ApiParam(value = "The connection id.", required = true) @PathParam("id") final String id, @ApiParam(value = "The connection configuration details.", required = true) final ConnectionEntity requestConnectionEntity) throws InterruptedException {
if (requestConnectionEntity == null || requestConnectionEntity.getComponent() == null) {
throw new IllegalArgumentException("Connection details must be specified.");
}
if (requestConnectionEntity.getRevision() == null) {
throw new IllegalArgumentException("Revision must be specified.");
}
// ensure the ids are the same
final ConnectionDTO requestConnection = requestConnectionEntity.getComponent();
if (!id.equals(requestConnection.getId())) {
throw new IllegalArgumentException(String.format("The connection id " + "(%s) in the request body does not equal the connection id of the " + "requested resource (%s).", requestConnection.getId(), id));
}
if (requestConnection.getDestination() != null) {
if (requestConnection.getDestination().getId() == null) {
throw new IllegalArgumentException("When specifying a destination component, the destination id is required.");
}
if (requestConnection.getDestination().getType() == null) {
throw new IllegalArgumentException("When specifying a destination component, the type of the destination is required.");
}
}
final List<PositionDTO> proposedBends = requestConnection.getBends();
if (proposedBends != null) {
for (final PositionDTO proposedBend : proposedBends) {
if (proposedBend.getX() == null || proposedBend.getY() == null) {
throw new IllegalArgumentException("The x and y coordinate of the each bend must be specified.");
}
}
}
if (isReplicateRequest()) {
return replicate(HttpMethod.PUT, requestConnectionEntity);
}
final Revision requestRevision = getRevision(requestConnectionEntity, id);
return withWriteLock(serviceFacade, requestConnectionEntity, requestRevision, lookup -> {
// verifies write access to this connection (this checks the current source and destination)
ConnectionAuthorizable connAuth = lookup.getConnection(id);
connAuth.getAuthorizable().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
// if a destination has been specified and is different
final Connectable currentDestination = connAuth.getDestination();
if (requestConnection.getDestination() != null && !currentDestination.getIdentifier().equals(requestConnection.getDestination().getId())) {
try {
final ConnectableType destinationConnectableType = ConnectableType.valueOf(requestConnection.getDestination().getType());
// explicitly handle RPGs differently as the connectable id can be ambiguous if self referencing
final Authorizable newDestinationAuthorizable;
if (ConnectableType.REMOTE_INPUT_PORT.equals(destinationConnectableType)) {
newDestinationAuthorizable = lookup.getRemoteProcessGroup(requestConnection.getDestination().getGroupId());
} else {
newDestinationAuthorizable = lookup.getLocalConnectable(requestConnection.getDestination().getId());
}
// verify access of the new destination (current destination was already authorized as part of the connection check)
newDestinationAuthorizable.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Unrecognized destination type %s. Excepted values are [%s]", requestConnection.getDestination().getType(), StringUtils.join(ConnectableType.values(), ", ")));
}
// verify access of the parent group (this is the same check that is performed when creating the connection)
connAuth.getParentGroup().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
}
}, () -> serviceFacade.verifyUpdateConnection(requestConnection), (revision, connectionEntity) -> {
final ConnectionDTO connection = connectionEntity.getComponent();
final ConnectionEntity entity = serviceFacade.updateConnection(revision, connection);
populateRemainingConnectionEntityContent(entity);
// generate the response
return generateOkResponse(entity).build();
});
}
use of org.apache.nifi.web.api.dto.ConnectionDTO in project nifi by apache.
the class ProcessGroupResource method createConnection.
// -----------
// connections
// -----------
/**
* Creates a new connection.
*
* @param httpServletRequest request
* @param groupId The group id
* @param requestConnectionEntity A connectionEntity.
* @return A connectionEntity.
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}/connections")
@ApiOperation(value = "Creates a connection", response = ConnectionEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{uuid}"), @Authorization(value = "Write Source - /{component-type}/{uuid}"), @Authorization(value = "Write Destination - /{component-type}/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response createConnection(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, @ApiParam(value = "The connection configuration details.", required = true) final ConnectionEntity requestConnectionEntity) {
if (requestConnectionEntity == null || requestConnectionEntity.getComponent() == null) {
throw new IllegalArgumentException("Connection details must be specified.");
}
if (requestConnectionEntity.getRevision() == null || (requestConnectionEntity.getRevision().getVersion() == null || requestConnectionEntity.getRevision().getVersion() != 0)) {
throw new IllegalArgumentException("A revision of 0 must be specified when creating a new Connection.");
}
if (requestConnectionEntity.getComponent().getId() != null) {
throw new IllegalArgumentException("Connection ID cannot be specified.");
}
final List<PositionDTO> proposedBends = requestConnectionEntity.getComponent().getBends();
if (proposedBends != null) {
for (final PositionDTO proposedBend : proposedBends) {
if (proposedBend.getX() == null || proposedBend.getY() == null) {
throw new IllegalArgumentException("The x and y coordinate of the each bend must be specified.");
}
}
}
if (requestConnectionEntity.getComponent().getParentGroupId() != null && !groupId.equals(requestConnectionEntity.getComponent().getParentGroupId())) {
throw new IllegalArgumentException(String.format("If specified, the parent process group id %s must be the same as specified in the URI %s", requestConnectionEntity.getComponent().getParentGroupId(), groupId));
}
requestConnectionEntity.getComponent().setParentGroupId(groupId);
// get the connection
final ConnectionDTO requestConnection = requestConnectionEntity.getComponent();
if (requestConnection.getSource() == null || requestConnection.getSource().getId() == null) {
throw new IllegalArgumentException("The source of the connection must be specified.");
}
if (requestConnection.getSource().getType() == null) {
throw new IllegalArgumentException("The type of the source of the connection must be specified.");
}
final ConnectableType sourceConnectableType;
try {
sourceConnectableType = ConnectableType.valueOf(requestConnection.getSource().getType());
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Unrecognized source type %s. Expected values are [%s]", requestConnection.getSource().getType(), StringUtils.join(ConnectableType.values(), ", ")));
}
if (requestConnection.getDestination() == null || requestConnection.getDestination().getId() == null) {
throw new IllegalArgumentException("The destination of the connection must be specified.");
}
if (requestConnection.getDestination().getType() == null) {
throw new IllegalArgumentException("The type of the destination of the connection must be specified.");
}
final ConnectableType destinationConnectableType;
try {
destinationConnectableType = ConnectableType.valueOf(requestConnection.getDestination().getType());
} catch (final IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Unrecognized destination type %s. Expected values are [%s]", requestConnection.getDestination().getType(), StringUtils.join(ConnectableType.values(), ", ")));
}
if (isReplicateRequest()) {
return replicate(HttpMethod.POST, requestConnectionEntity);
}
return withWriteLock(serviceFacade, requestConnectionEntity, lookup -> {
// ensure write access to the group
final Authorizable processGroup = lookup.getProcessGroup(groupId).getAuthorizable();
processGroup.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
// explicitly handle RPGs differently as the connectable id can be ambiguous if self referencing
final Authorizable source;
if (ConnectableType.REMOTE_OUTPUT_PORT.equals(sourceConnectableType)) {
source = lookup.getRemoteProcessGroup(requestConnection.getSource().getGroupId());
} else {
source = lookup.getLocalConnectable(requestConnection.getSource().getId());
}
// ensure write access to the source
if (source == null) {
throw new ResourceNotFoundException("Cannot find source component with ID [" + requestConnection.getSource().getId() + "]");
}
source.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
// explicitly handle RPGs differently as the connectable id can be ambiguous if self referencing
final Authorizable destination;
if (ConnectableType.REMOTE_INPUT_PORT.equals(destinationConnectableType)) {
destination = lookup.getRemoteProcessGroup(requestConnection.getDestination().getGroupId());
} else {
destination = lookup.getLocalConnectable(requestConnection.getDestination().getId());
}
// ensure write access to the destination
if (destination == null) {
throw new ResourceNotFoundException("Cannot find destination component with ID [" + requestConnection.getDestination().getId() + "]");
}
destination.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
}, () -> serviceFacade.verifyCreateConnection(groupId, requestConnection), connectionEntity -> {
final ConnectionDTO connection = connectionEntity.getComponent();
// set the processor id as appropriate
connection.setId(generateUuid());
// create the new relationship target
final Revision revision = getRevision(connectionEntity, connection.getId());
final ConnectionEntity entity = serviceFacade.createConnection(revision, groupId, connection);
connectionResource.populateRemainingConnectionEntityContent(entity);
// extract the href and build the response
String uri = entity.getUri();
return generateCreatedResponse(URI.create(uri), entity).build();
});
}
use of org.apache.nifi.web.api.dto.ConnectionDTO in project nifi by apache.
the class SnippetUtils method normalizeCoordinates.
/**
* Will normalize the coordinates of the components to ensure their
* consistency across exports. It will do so by fist calculating the
* smallest X and smallest Y and then subtracting it from all X's and Y's of
* each component ensuring that coordinates are consistent across export
* while preserving relative locations set by the user.
*/
private void normalizeCoordinates(Collection<? extends ComponentDTO> components) {
// determine the smallest x,y coordinates in the collection of components
double smallestX = Double.MAX_VALUE;
double smallestY = Double.MAX_VALUE;
for (ComponentDTO component : components) {
// to check those bend points for the smallest x,y coordinates
if (component instanceof ConnectionDTO) {
final ConnectionDTO connection = (ConnectionDTO) component;
for (final PositionDTO position : connection.getBends()) {
smallestX = Math.min(smallestX, position.getX());
smallestY = Math.min(smallestY, position.getY());
}
} else {
smallestX = Math.min(smallestX, component.getPosition().getX());
smallestY = Math.min(smallestY, component.getPosition().getY());
}
}
// position the components accordingly
for (ComponentDTO component : components) {
if (component instanceof ConnectionDTO) {
final ConnectionDTO connection = (ConnectionDTO) component;
for (final PositionDTO position : connection.getBends()) {
position.setX(position.getX() - smallestX);
position.setY(position.getY() - smallestY);
}
} else {
component.getPosition().setX(component.getPosition().getX() - smallestX);
component.getPosition().setY(component.getPosition().getY() - smallestY);
}
}
}
use of org.apache.nifi.web.api.dto.ConnectionDTO in project nifi by apache.
the class NiFiWebApiTest method populateFlow.
public static void populateFlow(Client client, String baseUrl, NiFiTestUser user, String clientId) throws Exception {
// -----------------------------------------------
// Create a source processor
// -----------------------------------------------
// create the local selection processor
ProcessorDTO processorDTO = new ProcessorDTO();
processorDTO.setName("Pick up");
processorDTO.setType(SourceTestProcessor.class.getName());
// create the revision
final RevisionDTO revision = new RevisionDTO();
revision.setClientId(clientId);
revision.setVersion(0l);
// create the local selection processor entity
ProcessorEntity processorEntity = new ProcessorEntity();
processorEntity.setRevision(revision);
processorEntity.setComponent(processorDTO);
// add the processor
Response response = user.testPost(baseUrl + "/process-groups/root/processors", processorEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// get the processors id
processorEntity = response.readEntity(ProcessorEntity.class);
processorDTO = processorEntity.getComponent();
String localSelectionId = processorDTO.getId();
String localSelectionGroupId = processorDTO.getParentGroupId();
// -----------------------------------------------
// Create a termination processor
// -----------------------------------------------
// create the termination processor
processorDTO = new ProcessorDTO();
processorDTO.setName("End");
processorDTO.setType(TerminationTestProcessor.class.getName());
// create the termination processor entity
processorEntity = new ProcessorEntity();
processorEntity.setRevision(revision);
processorEntity.setComponent(processorDTO);
// add the processor
response = user.testPost(baseUrl + "/process-groups/root/processors", processorEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// get the processors id
processorEntity = response.readEntity(ProcessorEntity.class);
processorDTO = processorEntity.getComponent();
String terminationId = processorDTO.getId();
String terminationGroupId = processorDTO.getParentGroupId();
// -----------------------------------------------
// Connect the two processors
// -----------------------------------------------
ConnectableDTO source = new ConnectableDTO();
source.setId(localSelectionId);
source.setGroupId(localSelectionGroupId);
source.setType(ConnectableType.PROCESSOR.name());
ConnectableDTO target = new ConnectableDTO();
target.setId(terminationId);
target.setGroupId(terminationGroupId);
target.setType(ConnectableType.PROCESSOR.name());
// create the relationships
Set<String> relationships = new HashSet<>();
relationships.add("success");
// create the connection
ConnectionDTO connectionDTO = new ConnectionDTO();
connectionDTO.setSource(source);
connectionDTO.setDestination(target);
connectionDTO.setSelectedRelationships(relationships);
// create the connection entity
ConnectionEntity connectionEntity = new ConnectionEntity();
connectionEntity.setRevision(revision);
connectionEntity.setComponent(connectionDTO);
// add the processor
response = user.testPost(baseUrl + "/process-groups/root/connections", connectionEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// -----------------------------------------------
// Create a label
// -----------------------------------------------
// create the label
LabelDTO labelDTO = new LabelDTO();
labelDTO.setLabel("Test label");
// create the label entity
LabelEntity labelEntity = new LabelEntity();
labelEntity.setRevision(revision);
labelEntity.setComponent(labelDTO);
// add the label
response = user.testPost(baseUrl + "/process-groups/root/labels", labelEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// -----------------------------------------------
// Create a funnel
// -----------------------------------------------
// create the funnel
FunnelDTO funnelDTO = new FunnelDTO();
// create the funnel entity
FunnelEntity funnelEntity = new FunnelEntity();
funnelEntity.setRevision(revision);
funnelEntity.setComponent(funnelDTO);
// add the funnel
response = user.testPost(baseUrl + "/process-groups/root/funnels", funnelEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// -----------------------------------------------
// Create a process group
// -----------------------------------------------
// create the process group
ProcessGroupDTO processGroup = new ProcessGroupDTO();
processGroup.setName("group name");
// create the process group entity
ProcessGroupEntity processGroupEntity = new ProcessGroupEntity();
processGroupEntity.setRevision(revision);
processGroupEntity.setComponent(processGroup);
// add the process group
response = user.testPost(baseUrl + "/process-groups/root/process-groups", processGroupEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// -----------------------------------------------
// Create an input port
// -----------------------------------------------
// create the input port
PortDTO inputPort = new PortDTO();
inputPort.setName("input");
// create the input port entity
PortEntity inputPortEntity = new PortEntity();
inputPortEntity.setRevision(revision);
inputPortEntity.setComponent(inputPort);
// add the input port
response = user.testPost(baseUrl + "/process-groups/root/input-ports", inputPortEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
// -----------------------------------------------
// Create a output ports
// -----------------------------------------------
// create the process group
PortDTO outputPort = new PortDTO();
outputPort.setName("output");
// create the process group entity
PortEntity outputPortEntity = new PortEntity();
outputPortEntity.setRevision(revision);
outputPortEntity.setComponent(outputPort);
// add the output port
response = user.testPost(baseUrl + "/process-groups/root/output-ports", outputPortEntity);
// ensure a successful response
if (Response.Status.CREATED.getStatusCode() != response.getStatusInfo().getStatusCode()) {
// since it was unable to create the component attempt to extract an
// error message from the response body
final String responseEntity = response.readEntity(String.class);
throw new Exception("Unable to populate initial flow: " + responseEntity);
}
}
Aggregations