Search in sources :

Example 1 with FlowSnapshotContext

use of org.apache.nifi.registry.flow.FlowSnapshotContext in project nifi-registry by apache.

the class TestFileSystemFlowPersistenceProvider method createAndSaveSnapshot.

private void createAndSaveSnapshot(final FlowPersistenceProvider flowPersistenceProvider, final String bucketId, final String flowId, final int version, final String contentString) throws IOException {
    final FlowSnapshotContext context = Mockito.mock(FlowSnapshotContext.class);
    when(context.getBucketId()).thenReturn(bucketId);
    when(context.getFlowId()).thenReturn(flowId);
    when(context.getVersion()).thenReturn(version);
    final byte[] content = contentString.getBytes(StandardCharsets.UTF_8);
    flowPersistenceProvider.saveFlowContent(context, content);
}
Also used : FlowSnapshotContext(org.apache.nifi.registry.flow.FlowSnapshotContext)

Example 2 with FlowSnapshotContext

use of org.apache.nifi.registry.flow.FlowSnapshotContext in project nifi-registry by apache.

the class TestFileSystemFlowPersistenceProvider method testSaveWithExistingVersion.

@Test
public void testSaveWithExistingVersion() throws IOException {
    final FlowSnapshotContext context = Mockito.mock(FlowSnapshotContext.class);
    when(context.getBucketId()).thenReturn("bucket1");
    when(context.getFlowId()).thenReturn("flow1");
    when(context.getVersion()).thenReturn(1);
    final byte[] content = "flow1v1".getBytes(StandardCharsets.UTF_8);
    fileSystemFlowProvider.saveFlowContent(context, content);
    // save new content for an existing version
    final byte[] content2 = "XXX".getBytes(StandardCharsets.UTF_8);
    try {
        fileSystemFlowProvider.saveFlowContent(context, content2);
        Assert.fail("Should have thrown exception");
    } catch (Exception e) {
    }
    // verify the new content wasn't written
    final File flowSnapshotFile = new File(flowStorageDir, "bucket1/flow1/1/1" + FileSystemFlowPersistenceProvider.SNAPSHOT_EXTENSION);
    try (InputStream in = new FileInputStream(flowSnapshotFile)) {
        Assert.assertEquals("flow1v1", IOUtils.toString(in, StandardCharsets.UTF_8));
    }
}
Also used : FlowSnapshotContext(org.apache.nifi.registry.flow.FlowSnapshotContext) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with FlowSnapshotContext

use of org.apache.nifi.registry.flow.FlowSnapshotContext in project nifi-registry by apache.

the class RegistryService method createFlowSnapshot.

// ---------------------- VersionedFlowSnapshot methods ---------------------------------------------
public VersionedFlowSnapshot createFlowSnapshot(final VersionedFlowSnapshot flowSnapshot) {
    if (flowSnapshot == null) {
        throw new IllegalArgumentException("Versioned flow snapshot cannot be null");
    }
    // validation will ensure that the metadata and contents are not null
    if (flowSnapshot.getSnapshotMetadata() != null) {
        flowSnapshot.getSnapshotMetadata().setTimestamp(System.currentTimeMillis());
    }
    // these fields aren't used for creation
    flowSnapshot.setFlow(null);
    flowSnapshot.setBucket(null);
    validate(flowSnapshot, "Cannot create versioned flow snapshot");
    writeLock.lock();
    try {
        final VersionedFlowSnapshotMetadata snapshotMetadata = flowSnapshot.getSnapshotMetadata();
        // ensure the bucket exists
        final BucketEntity existingBucket = metadataService.getBucketById(snapshotMetadata.getBucketIdentifier());
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", snapshotMetadata.getBucketIdentifier());
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // ensure the flow exists
        final FlowEntity existingFlow = metadataService.getFlowById(snapshotMetadata.getFlowIdentifier());
        if (existingFlow == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", snapshotMetadata.getFlowIdentifier());
            throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
        }
        if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // convert the set of FlowSnapshotEntity to set of VersionedFlowSnapshotMetadata
        final SortedSet<VersionedFlowSnapshotMetadata> sortedSnapshots = new TreeSet<>();
        final List<FlowSnapshotEntity> existingFlowSnapshots = metadataService.getSnapshots(existingFlow.getId());
        if (existingFlowSnapshots != null) {
            existingFlowSnapshots.stream().forEach(s -> sortedSnapshots.add(DataModelMapper.map(existingBucket, s)));
        }
        // if we already have snapshots we need to verify the new one has the correct version
        if (sortedSnapshots != null && sortedSnapshots.size() > 0) {
            final VersionedFlowSnapshotMetadata lastSnapshot = sortedSnapshots.last();
            if (snapshotMetadata.getVersion() <= lastSnapshot.getVersion()) {
                throw new IllegalStateException("A Versioned flow snapshot with the same version already exists: " + snapshotMetadata.getVersion());
            }
            if (snapshotMetadata.getVersion() > (lastSnapshot.getVersion() + 1)) {
                throw new IllegalStateException("Version must be a one-up number, last version was " + lastSnapshot.getVersion() + " and version for this snapshot was " + snapshotMetadata.getVersion());
            }
        } else if (snapshotMetadata.getVersion() != 1) {
            throw new IllegalStateException("Version of first snapshot must be 1");
        }
        // serialize the snapshot
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        processGroupSerializer.serialize(flowSnapshot.getFlowContents(), out);
        // save the serialized snapshot to the persistence provider
        final Bucket bucket = DataModelMapper.map(existingBucket);
        final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, existingFlow);
        final FlowSnapshotContext context = new StandardFlowSnapshotContext.Builder(bucket, versionedFlow, snapshotMetadata).build();
        flowPersistenceProvider.saveFlowContent(context, out.toByteArray());
        // create snapshot in the metadata provider
        metadataService.createFlowSnapshot(DataModelMapper.map(snapshotMetadata));
        // update the modified date on the flow
        metadataService.updateFlow(existingFlow);
        // get the updated flow, we need to use "with counts" here so we can return this is a part of the response
        final FlowEntity updatedFlow = metadataService.getFlowByIdWithSnapshotCounts(snapshotMetadata.getFlowIdentifier());
        if (updatedFlow == null) {
            throw new ResourceNotFoundException("Versioned flow does not exist for identifier " + snapshotMetadata.getFlowIdentifier());
        }
        final VersionedFlow updatedVersionedFlow = DataModelMapper.map(existingBucket, updatedFlow);
        flowSnapshot.setBucket(bucket);
        flowSnapshot.setFlow(updatedVersionedFlow);
        return flowSnapshot;
    } finally {
        writeLock.unlock();
    }
}
Also used : FlowSnapshotContext(org.apache.nifi.registry.flow.FlowSnapshotContext) StandardFlowSnapshotContext(org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Bucket(org.apache.nifi.registry.bucket.Bucket) TreeSet(java.util.TreeSet) StandardFlowSnapshotContext(org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Example 4 with FlowSnapshotContext

use of org.apache.nifi.registry.flow.FlowSnapshotContext in project nifi-registry by apache.

the class TestStandardFlowSnapshotContext method testBuilder.

@Test
public void testBuilder() {
    final String bucketId = "1234-1234-1234-1234";
    final String bucketName = "Some Bucket";
    final String flowId = "2345-2345-2345-2345";
    final String flowName = "Some Flow";
    final int version = 2;
    final String comments = "Some Comments";
    final long timestamp = System.currentTimeMillis();
    final FlowSnapshotContext context = new StandardFlowSnapshotContext.Builder().bucketId(bucketId).bucketName(bucketName).flowId(flowId).flowName(flowName).version(version).comments(comments).snapshotTimestamp(timestamp).build();
    Assert.assertEquals(bucketId, context.getBucketId());
    Assert.assertEquals(bucketName, context.getBucketName());
    Assert.assertEquals(flowId, context.getFlowId());
    Assert.assertEquals(flowName, context.getFlowName());
    Assert.assertEquals(version, context.getVersion());
    Assert.assertEquals(comments, context.getComments());
    Assert.assertEquals(timestamp, context.getSnapshotTimestamp());
}
Also used : StandardFlowSnapshotContext(org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext) FlowSnapshotContext(org.apache.nifi.registry.flow.FlowSnapshotContext) Test(org.junit.Test)

Aggregations

FlowSnapshotContext (org.apache.nifi.registry.flow.FlowSnapshotContext)4 StandardFlowSnapshotContext (org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TreeSet (java.util.TreeSet)1 Bucket (org.apache.nifi.registry.bucket.Bucket)1 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)1 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)1 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)1 ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)1 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)1 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)1