Search in sources :

Example 26 with VersionedFlowSnapshotMetadata

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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Bucket(org.apache.nifi.registry.bucket.Bucket) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Example 27 with VersionedFlowSnapshotMetadata

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());
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test)

Example 28 with VersionedFlowSnapshotMetadata

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);
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test)

Example 29 with VersionedFlowSnapshotMetadata

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());
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) HashSet(java.util.HashSet) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test)

Example 30 with VersionedFlowSnapshotMetadata

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();
}
Also used : VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)30 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)14 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)9 Test (org.junit.Test)9 Bucket (org.apache.nifi.registry.bucket.Bucket)8 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)8 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)8 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)8 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)8 FlowSnapshotClient (org.apache.nifi.registry.client.FlowSnapshotClient)6 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)6 Date (java.util.Date)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 IOException (java.io.IOException)4 Consumes (javax.ws.rs.Consumes)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3