use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.
the class RegistryService method getFlowSnapshot.
public VersionedFlowSnapshot getFlowSnapshot(final String bucketIdentifier, final String flowIdentifier, final Integer version) {
if (StringUtils.isBlank(bucketIdentifier)) {
throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
}
if (StringUtils.isBlank(flowIdentifier)) {
throw new IllegalArgumentException("Flow identifier cannot be null or blank");
}
if (version == null) {
throw new IllegalArgumentException("Version cannot be null or blank");
}
readLock.lock();
try {
final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
if (existingBucket == null) {
LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
}
// we need to populate the version count here so we have to do this retrieval instead of snapshotEntity.getFlow()
final FlowEntity flowEntityWithCount = metadataService.getFlowByIdWithSnapshotCounts(flowIdentifier);
if (flowEntityWithCount == null) {
LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
}
if (!existingBucket.getId().equals(flowEntityWithCount.getBucketId())) {
throw new IllegalStateException("The requested flow is not located in the given bucket");
}
// ensure the snapshot exists
final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowIdentifier, version);
if (snapshotEntity == null) {
LOGGER.warn("The specified flow snapshot id [{}] does not exist for version [{}].", flowIdentifier, version);
throw new ResourceNotFoundException("The specified versioned flow snapshot does not exist for this flow.");
}
// get the serialized bytes of the snapshot
final byte[] serializedSnapshot = flowPersistenceProvider.getFlowContent(bucketIdentifier, flowIdentifier, version);
if (serializedSnapshot == null || serializedSnapshot.length == 0) {
throw new IllegalStateException("No serialized content found for snapshot with flow identifier " + flowIdentifier + " and version " + version);
}
// deserialize the contents
final InputStream input = new ByteArrayInputStream(serializedSnapshot);
final VersionedProcessGroup flowContents = processGroupSerializer.deserialize(input);
// map entities to data model
final Bucket bucket = DataModelMapper.map(existingBucket);
final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, flowEntityWithCount);
final VersionedFlowSnapshotMetadata snapshotMetadata = DataModelMapper.map(existingBucket, snapshotEntity);
// create the snapshot to return
final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
snapshot.setFlowContents(flowContents);
snapshot.setSnapshotMetadata(snapshotMetadata);
snapshot.setFlow(versionedFlow);
snapshot.setBucket(bucket);
return snapshot;
} finally {
readLock.unlock();
}
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.
the class TestRegistryService method testGetLatestSnapshotMetadataWhenVersionsExist.
@Test
public void testGetLatestSnapshotMetadataWhenVersionsExist() {
final BucketEntity existingBucket = new BucketEntity();
existingBucket.setId("b1");
existingBucket.setName("My Bucket");
existingBucket.setDescription("This is my bucket");
existingBucket.setCreated(new Date());
when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
// return a flow with the existing snapshot when getFlowById is called
final FlowEntity existingFlow = new FlowEntity();
existingFlow.setId("flow1");
existingFlow.setName("My Flow");
existingFlow.setDescription("This is my flow.");
existingFlow.setCreated(new Date());
existingFlow.setModified(new Date());
existingFlow.setBucketId(existingBucket.getId());
when(metadataService.getFlowById(existingFlow.getId())).thenReturn(existingFlow);
final FlowSnapshotEntity existingSnapshot1 = new FlowSnapshotEntity();
existingSnapshot1.setVersion(1);
existingSnapshot1.setFlowId(existingFlow.getId());
existingSnapshot1.setCreatedBy("user1");
existingSnapshot1.setCreated(new Date());
existingSnapshot1.setComments("This is snapshot 1");
when(metadataService.getLatestSnapshot(existingFlow.getId())).thenReturn(existingSnapshot1);
VersionedFlowSnapshotMetadata latestMetadata = registryService.getLatestFlowSnapshotMetadata(existingBucket.getId(), existingFlow.getId());
assertNotNull(latestMetadata);
assertEquals(1, latestMetadata.getVersion());
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.
the class TestRegistryService method testDeleteSnapshotExists.
@Test
public void testDeleteSnapshotExists() {
final BucketEntity existingBucket = createBucketEntity("b1");
final FlowEntity existingFlow = createFlowEntity(existingBucket.getId());
final FlowSnapshotEntity existingSnapshot = createFlowSnapshotEntity(existingFlow.getId());
when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
when(metadataService.getFlowById(existingFlow.getId())).thenReturn(existingFlow);
when(metadataService.getFlowSnapshot(existingSnapshot.getFlowId(), existingSnapshot.getVersion())).thenReturn(existingSnapshot);
final VersionedFlowSnapshotMetadata deletedSnapshot = registryService.deleteFlowSnapshot(existingBucket.getId(), existingSnapshot.getFlowId(), existingSnapshot.getVersion());
assertNotNull(deletedSnapshot);
assertEquals(existingSnapshot.getFlowId(), deletedSnapshot.getFlowIdentifier());
verify(flowPersistenceProvider, times(1)).deleteFlowContent(existingBucket.getId(), existingSnapshot.getFlowId(), existingSnapshot.getVersion());
verify(metadataService, times(1)).deleteFlowSnapshot(existingSnapshot);
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.
the class TestRegistryService method testGetFlowSnapshotsWhenNoSnapshots.
@Test
public void testGetFlowSnapshotsWhenNoSnapshots() {
final BucketEntity existingBucket = new BucketEntity();
existingBucket.setId("b1");
existingBucket.setName("My Bucket");
existingBucket.setDescription("This is my bucket");
existingBucket.setCreated(new Date());
when(metadataService.getBucketById(existingBucket.getId())).thenReturn(existingBucket);
// return a flow with the existing snapshot when getFlowById is called
final FlowEntity existingFlow = new FlowEntity();
existingFlow.setId("flow1");
existingFlow.setName("My Flow");
existingFlow.setDescription("This is my flow.");
existingFlow.setCreated(new Date());
existingFlow.setModified(new Date());
existingFlow.setBucketId(existingBucket.getId());
final Set<FlowSnapshotEntity> snapshots = new HashSet<>();
when(metadataService.getFlowById(existingFlow.getId())).thenReturn(existingFlow);
final SortedSet<VersionedFlowSnapshotMetadata> retrievedSnapshots = registryService.getFlowSnapshots(existingBucket.getId(), existingFlow.getId());
assertNotNull(retrievedSnapshots);
assertEquals(0, retrievedSnapshots.size());
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.
the class BucketFlowResource method getLatestFlowVersionMetadata.
@GET
@Path("{flowId}/versions/latest/metadata")
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get the metadata for the latest version of a flow", response = VersionedFlowSnapshotMetadata.class, extensions = { @Extension(name = "access-policy", properties = { @ExtensionProperty(name = "action", value = "read"), @ExtensionProperty(name = "resource", value = "/buckets/{bucketId}") }) })
@ApiResponses({ @ApiResponse(code = 401, message = HttpStatusMessages.MESSAGE_401), @ApiResponse(code = 403, message = HttpStatusMessages.MESSAGE_403), @ApiResponse(code = 404, message = HttpStatusMessages.MESSAGE_404), @ApiResponse(code = 409, message = HttpStatusMessages.MESSAGE_409) })
public Response getLatestFlowVersionMetadata(@PathParam("bucketId") @ApiParam("The bucket identifier") final String bucketId, @PathParam("flowId") @ApiParam("The flow identifier") final String flowId) {
authorizeBucketAccess(RequestAction.READ, bucketId);
final VersionedFlowSnapshotMetadata latest = registryService.getLatestFlowSnapshotMetadata(bucketId, flowId);
linkService.populateSnapshotLinks(latest);
return Response.status(Response.Status.OK).entity(latest).build();
}
Aggregations