use of org.apache.nifi.registry.db.entity.FlowEntity in project nifi-registry by apache.
the class RegistryService method addBucketItem.
private void addBucketItem(final List<BucketItem> bucketItems, final BucketItemEntity itemEntity) {
if (itemEntity instanceof FlowEntity) {
final FlowEntity flowEntity = (FlowEntity) itemEntity;
// Currently we don't populate the bucket name for items
bucketItems.add(DataModelMapper.map(null, flowEntity));
} else {
LOGGER.error("Unknown type of BucketItemEntity: " + itemEntity.getClass().getCanonicalName());
}
}
use of org.apache.nifi.registry.db.entity.FlowEntity 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();
}
}
use of org.apache.nifi.registry.db.entity.FlowEntity 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();
}
}
use of org.apache.nifi.registry.db.entity.FlowEntity in project nifi-registry by apache.
the class TestDatabaseMetadataService method testGetItemsWithCountsFilteredByBuckets.
@Test
public void testGetItemsWithCountsFilteredByBuckets() {
final List<BucketItemEntity> items = metadataService.getBucketItems(Collections.singleton("1"));
assertNotNull(items);
// only 2 items in bucket 1
assertEquals(2, items.size());
final BucketItemEntity item1 = items.stream().filter(i -> i.getId().equals("1")).findFirst().orElse(null);
assertNotNull(item1);
assertEquals(BucketItemEntityType.FLOW, item1.getType());
final FlowEntity flowEntity = (FlowEntity) item1;
assertEquals(3, flowEntity.getSnapshotCount());
items.stream().forEach(i -> assertNotNull(i.getBucketName()));
}
use of org.apache.nifi.registry.db.entity.FlowEntity in project nifi-registry by apache.
the class TestDatabaseMetadataService method testGetFlowByIdWhenExists.
// ----------------- Flows ---------------------------------
@Test
public void testGetFlowByIdWhenExists() {
final FlowEntity flow = metadataService.getFlowById("1");
assertNotNull(flow);
assertEquals("1", flow.getId());
assertEquals("1", flow.getBucketId());
}
Aggregations