Search in sources :

Example 1 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class TestRegistryService method testGetLatestSnapshotMetadataWhenNoVersionsExist.

@Test
public void testGetLatestSnapshotMetadataWhenNoVersionsExist() {
    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(null);
    try {
        registryService.getLatestFlowSnapshotMetadata(existingBucket.getId(), existingFlow.getId());
        Assert.fail("Should have thrown exception");
    } catch (ResourceNotFoundException e) {
        assertEquals("The specified flow ID has no versions", e.getMessage());
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) Date(java.util.Date) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) Test(org.junit.Test)

Example 2 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method deleteBucket.

public Bucket deleteBucket(final String bucketIdentifier) {
    if (bucketIdentifier == null) {
        throw new IllegalArgumentException("Bucket identifier cannot be null");
    }
    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.");
        }
        // for each flow in the bucket, delete all snapshots from the flow persistence provider
        for (final FlowEntity flowEntity : metadataService.getFlowsByBucket(existingBucket.getId())) {
            flowPersistenceProvider.deleteAllFlowContent(bucketIdentifier, flowEntity.getId());
        }
        // now delete the bucket from the metadata provider, which deletes all flows referencing it
        metadataService.deleteBucket(existingBucket);
        return DataModelMapper.map(existingBucket);
    } 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 3 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method getFlowSnapshots.

/**
 * Returns all versions of a flow, sorted newest to oldest.
 *
 * @param bucketIdentifier the id of the bucket to search for the flowIdentifier
 * @param flowIdentifier the id of the flow to retrieve from the specified bucket
 * @return all versions of the specified flow, sorted newest to oldest
 */
public SortedSet<VersionedFlowSnapshotMetadata> getFlowSnapshots(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");
    }
    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.");
        }
        // 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");
        }
        // convert the set of FlowSnapshotEntity to set of VersionedFlowSnapshotMetadata, ordered by version descending
        final SortedSet<VersionedFlowSnapshotMetadata> sortedSnapshots = new TreeSet<>(Collections.reverseOrder());
        final List<FlowSnapshotEntity> existingFlowSnapshots = metadataService.getSnapshots(existingFlow.getId());
        if (existingFlowSnapshots != null) {
            existingFlowSnapshots.stream().forEach(s -> sortedSnapshots.add(DataModelMapper.map(existingBucket, s)));
        }
        return sortedSnapshots;
    } finally {
        readLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) TreeSet(java.util.TreeSet) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 4 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method getBucketItems.

// ---------------------- BucketItem methods ---------------------------------------------
public List<BucketItem> getBucketItems(final String bucketIdentifier) {
    if (bucketIdentifier == null) {
        throw new IllegalArgumentException("Bucket identifier cannot be null");
    }
    readLock.lock();
    try {
        final BucketEntity bucket = metadataService.getBucketById(bucketIdentifier);
        if (bucket == 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 List<BucketItem> bucketItems = new ArrayList<>();
        metadataService.getBucketItems(bucket.getId()).stream().forEach(b -> addBucketItem(bucketItems, b));
        return bucketItems;
    } finally {
        readLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) ArrayList(java.util.ArrayList) BucketItem(org.apache.nifi.registry.bucket.BucketItem) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Example 5 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method createFlow.

// ---------------------- VersionedFlow methods ---------------------------------------------
public VersionedFlow createFlow(final String bucketIdentifier, final VersionedFlow versionedFlow) {
    if (StringUtils.isBlank(bucketIdentifier)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
    }
    if (versionedFlow == null) {
        throw new IllegalArgumentException("Versioned flow cannot be null");
    }
    if (versionedFlow.getBucketIdentifier() != null && !bucketIdentifier.equals(versionedFlow.getBucketIdentifier())) {
        throw new IllegalArgumentException("Bucket identifiers must match");
    }
    if (versionedFlow.getBucketIdentifier() == null) {
        versionedFlow.setBucketIdentifier(bucketIdentifier);
    }
    versionedFlow.setIdentifier(UUID.randomUUID().toString());
    final long timestamp = System.currentTimeMillis();
    versionedFlow.setCreatedTimestamp(timestamp);
    versionedFlow.setModifiedTimestamp(timestamp);
    validate(versionedFlow, "Cannot create versioned flow");
    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 another flow with the same name doesn't exist
        final List<FlowEntity> flowsWithSameName = metadataService.getFlowsByName(existingBucket.getId(), versionedFlow.getName());
        if (flowsWithSameName != null && flowsWithSameName.size() > 0) {
            throw new IllegalStateException("A versioned flow with the same name already exists in the selected bucket");
        }
        // convert from dto to entity and set the bucket relationship
        final FlowEntity flowEntity = DataModelMapper.map(versionedFlow);
        flowEntity.setBucketId(existingBucket.getId());
        // persist the flow and return the created entity
        final FlowEntity createdFlow = metadataService.createFlow(flowEntity);
        return DataModelMapper.map(existingBucket, createdFlow);
    } 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

ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)23 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)14 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)12 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Consumes (javax.ws.rs.Consumes)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)7 GET (javax.ws.rs.GET)4 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)4 TreeSet (java.util.TreeSet)3 DELETE (javax.ws.rs.DELETE)3 User (org.apache.nifi.registry.authorization.User)3 Bucket (org.apache.nifi.registry.bucket.Bucket)3 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2