Search in sources :

Example 26 with BucketEntity

use of org.apache.nifi.registry.db.entity.BucketEntity 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 27 with BucketEntity

use of org.apache.nifi.registry.db.entity.BucketEntity in project nifi-registry by apache.

the class RegistryService method getFlow.

public VersionedFlow getFlow(final String bucketIdentifier, final String flowIdentifier) {
    if (StringUtils.isBlank(bucketIdentifier)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
    }
    if (StringUtils.isBlank(flowIdentifier)) {
        throw new IllegalArgumentException("Versioned flow identifier cannot be null or blank");
    }
    readLock.lock();
    try {
        // ensure the bucket exists
        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.");
        }
        final FlowEntity existingFlow = metadataService.getFlowByIdWithSnapshotCounts(flowIdentifier);
        if (existingFlow == 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(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        return DataModelMapper.map(existingBucket, existingFlow);
    } finally {
        readLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Example 28 with BucketEntity

use of org.apache.nifi.registry.db.entity.BucketEntity in project nifi-registry by apache.

the class RegistryService method updateFlow.

public VersionedFlow updateFlow(final VersionedFlow versionedFlow) {
    if (versionedFlow == null) {
        throw new IllegalArgumentException("Versioned flow cannot be null");
    }
    if (StringUtils.isBlank(versionedFlow.getIdentifier())) {
        throw new IllegalArgumentException("Versioned flow identifier cannot be null or blank");
    }
    if (StringUtils.isBlank(versionedFlow.getBucketIdentifier())) {
        throw new IllegalArgumentException("Versioned flow bucket identifier cannot be null or blank");
    }
    if (versionedFlow.getName() != null && StringUtils.isBlank(versionedFlow.getName())) {
        throw new IllegalArgumentException("Versioned flow name cannot be blank");
    }
    writeLock.lock();
    try {
        // ensure the bucket exists
        final BucketEntity existingBucket = metadataService.getBucketById(versionedFlow.getBucketIdentifier());
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", versionedFlow.getBucketIdentifier());
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        final FlowEntity existingFlow = metadataService.getFlowByIdWithSnapshotCounts(versionedFlow.getIdentifier());
        if (existingFlow == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", versionedFlow.getIdentifier());
            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");
        }
        // since we're allowing partial updates here, only check this if a non-null name is provided
        if (StringUtils.isNotBlank(versionedFlow.getName())) {
            final List<FlowEntity> flowsWithSameName = metadataService.getFlowsByName(existingBucket.getId(), versionedFlow.getName());
            if (flowsWithSameName != null) {
                for (final FlowEntity flowWithSameName : flowsWithSameName) {
                    if (!flowWithSameName.getId().equals(existingFlow.getId())) {
                        throw new IllegalStateException("A versioned flow with the same name already exists in the selected bucket");
                    }
                }
            }
        }
        // transfer over the new values to the existing flow
        if (StringUtils.isNotBlank(versionedFlow.getName())) {
            existingFlow.setName(versionedFlow.getName());
        }
        if (versionedFlow.getDescription() != null) {
            existingFlow.setDescription(versionedFlow.getDescription());
        }
        // perform the actual update
        final FlowEntity updatedFlow = metadataService.updateFlow(existingFlow);
        return DataModelMapper.map(existingBucket, updatedFlow);
    } finally {
        writeLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Example 29 with BucketEntity

use of org.apache.nifi.registry.db.entity.BucketEntity in project nifi-registry by apache.

the class RegistryService method updateBucket.

public Bucket updateBucket(final Bucket bucket) {
    if (bucket == null) {
        throw new IllegalArgumentException("Bucket cannot be null");
    }
    if (bucket.getIdentifier() == null) {
        throw new IllegalArgumentException("Bucket identifier cannot be null");
    }
    if (bucket.getName() != null && StringUtils.isBlank(bucket.getName())) {
        throw new IllegalArgumentException("Bucket name cannot be blank");
    }
    writeLock.lock();
    try {
        // ensure a bucket with the given id exists
        final BucketEntity existingBucketById = metadataService.getBucketById(bucket.getIdentifier());
        if (existingBucketById == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucket.getIdentifier());
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // since we're allowing partial updates here, only check this if a non-null name is provided
        if (StringUtils.isNotBlank(bucket.getName())) {
            final List<BucketEntity> bucketsWithSameName = metadataService.getBucketsByName(bucket.getName());
            if (bucketsWithSameName != null) {
                for (final BucketEntity bucketWithSameName : bucketsWithSameName) {
                    if (!bucketWithSameName.getId().equals(existingBucketById.getId())) {
                        throw new IllegalStateException("A bucket with the same name already exists - " + bucket.getName());
                    }
                }
            }
        }
        // transfer over the new values to the existing bucket
        if (StringUtils.isNotBlank(bucket.getName())) {
            existingBucketById.setName(bucket.getName());
        }
        if (bucket.getDescription() != null) {
            existingBucketById.setDescription(bucket.getDescription());
        }
        // perform the actual update
        final BucketEntity updatedBucket = metadataService.updateBucket(existingBucketById);
        return DataModelMapper.map(updatedBucket);
    } finally {
        writeLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Example 30 with BucketEntity

use of org.apache.nifi.registry.db.entity.BucketEntity in project nifi-registry by apache.

the class RegistryService method deleteFlow.

public VersionedFlow deleteFlow(final String bucketIdentifier, final String flowIdentifier) {
    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");
    }
    writeLock.lock();
    try {
        // ensure the bucket exists
        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.");
        }
        // ensure the flow exists
        final FlowEntity existingFlow = metadataService.getFlowById(flowIdentifier);
        if (existingFlow == 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(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // delete all snapshots from the flow persistence provider
        flowPersistenceProvider.deleteAllFlowContent(existingFlow.getBucketId(), existingFlow.getId());
        // now delete the flow from the metadata provider
        metadataService.deleteFlow(existingFlow);
        return DataModelMapper.map(existingBucket, existingFlow);
    } finally {
        writeLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Aggregations

BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)49 Test (org.junit.Test)32 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)31 Date (java.util.Date)26 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)16 ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)14 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)11 Bucket (org.apache.nifi.registry.bucket.Bucket)10 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)9 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)8 ArrayList (java.util.ArrayList)5 InputStream (java.io.InputStream)3 TreeSet (java.util.TreeSet)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BucketItem (org.apache.nifi.registry.bucket.BucketItem)2 BucketItemEntity (org.apache.nifi.registry.db.entity.BucketItemEntity)2 FlowSnapshotContext (org.apache.nifi.registry.flow.FlowSnapshotContext)2 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)2 StandardFlowSnapshotContext (org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext)2