Search in sources :

Example 26 with FlowEntity

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

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

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

use of org.apache.nifi.registry.db.entity.FlowEntity 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)

Example 30 with FlowEntity

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

the class RegistryService method getFlows.

public List<VersionedFlow> getFlows(final String bucketId) {
    if (StringUtils.isBlank(bucketId)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null");
    }
    readLock.lock();
    try {
        final BucketEntity existingBucket = metadataService.getBucketById(bucketId);
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucketId);
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // return non-verbose set of flows for the given bucket
        final List<FlowEntity> flows = metadataService.getFlowsByBucket(existingBucket.getId());
        return flows.stream().map(f -> DataModelMapper.map(existingBucket, f)).collect(Collectors.toList());
    } finally {
        readLock.unlock();
    }
}
Also used : SortedSet(java.util.SortedSet) FlowComparison(org.apache.nifi.registry.flow.diff.FlowComparison) LoggerFactory(org.slf4j.LoggerFactory) StandardFlowComparator(org.apache.nifi.registry.flow.diff.StandardFlowComparator) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) Map(java.util.Map) VersionedComponent(org.apache.nifi.registry.flow.VersionedComponent) ConstraintViolation(javax.validation.ConstraintViolation) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Validator(javax.validation.Validator) Set(java.util.Set) FlowSnapshotContext(org.apache.nifi.registry.flow.FlowSnapshotContext) UUID(java.util.UUID) Serializer(org.apache.nifi.registry.serialization.Serializer) Collectors(java.util.stream.Collectors) BucketItem(org.apache.nifi.registry.bucket.BucketItem) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) List(java.util.List) VersionedFlowDifference(org.apache.nifi.registry.diff.VersionedFlowDifference) FlowComparator(org.apache.nifi.registry.flow.diff.FlowComparator) Bucket(org.apache.nifi.registry.bucket.Bucket) StandardComparableDataFlow(org.apache.nifi.registry.flow.diff.StandardComparableDataFlow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) ConciseEvolvingDifferenceDescriptor(org.apache.nifi.registry.flow.diff.ConciseEvolvingDifferenceDescriptor) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) BucketItemEntity(org.apache.nifi.registry.db.entity.BucketItemEntity) ObjectUtils(org.apache.commons.lang3.ObjectUtils) ComparableDataFlow(org.apache.nifi.registry.flow.diff.ComparableDataFlow) Service(org.springframework.stereotype.Service) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) ComponentDifferenceGroup(org.apache.nifi.registry.diff.ComponentDifferenceGroup) Logger(org.slf4j.Logger) StandardFlowSnapshotContext(org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext) FlowPersistenceProvider(org.apache.nifi.registry.flow.FlowPersistenceProvider) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) FlowDifference(org.apache.nifi.registry.flow.diff.FlowDifference) Lock(java.util.concurrent.locks.Lock) Validate(org.apache.commons.lang3.Validate) ConstraintViolationException(javax.validation.ConstraintViolationException) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) InputStream(java.io.InputStream) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Aggregations

FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)46 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)30 Test (org.junit.Test)27 Date (java.util.Date)20 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)16 ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)12 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)10 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)9 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)7 ArrayList (java.util.ArrayList)5 Bucket (org.apache.nifi.registry.bucket.Bucket)5 BucketItemEntity (org.apache.nifi.registry.db.entity.BucketItemEntity)5 InputStream (java.io.InputStream)3 TreeSet (java.util.TreeSet)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)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 Nullable (org.springframework.lang.Nullable)2