Java Code Examples for org.apache.nifi.registry.flow.VersionedFlowSnapshot#setSnapshotMetadata()

The following examples show how to use org.apache.nifi.registry.flow.VersionedFlowSnapshot#setSnapshotMetadata() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRegistryService.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
private VersionedFlowSnapshot createSnapshot() {
    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setFlowIdentifier("flow1");
    snapshotMetadata.setVersion(1);
    snapshotMetadata.setComments("This is the first snapshot");
    snapshotMetadata.setBucketIdentifier("b1");
    snapshotMetadata.setAuthor("user1");

    final VersionedProcessGroup processGroup = new VersionedProcessGroup();
    processGroup.setIdentifier("pg1");
    processGroup.setName("My Process Group");

    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlowContents(processGroup);

    return snapshot;
}
 
Example 2
Source File: BucketFlowResource.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
private static void setSnaphotMetadataIfMissing(
        @NotNull String bucketIdParam,
        @NotNull String flowIdParam,
        @NotNull VersionedFlowSnapshot flowSnapshot) {

    VersionedFlowSnapshotMetadata metadata = flowSnapshot.getSnapshotMetadata();
    if (metadata == null) {
        metadata = new VersionedFlowSnapshotMetadata();
    }

    if (metadata.getBucketIdentifier() == null) {
        metadata.setBucketIdentifier(bucketIdParam);
    }

    if (metadata.getFlowIdentifier() == null) {
        metadata.setFlowIdentifier(flowIdParam);
    }

    flowSnapshot.setSnapshotMetadata(metadata);
}
 
Example 3
Source File: RegistryService.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private VersionedFlowSnapshot getVersionedFlowSnapshot(final BucketEntity bucketEntity, final FlowEntity flowEntity, final Integer version) {
    // ensure the snapshot exists
    final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowEntity.getId(), version);
    if (snapshotEntity == null) {
        LOGGER.warn("The specified flow snapshot id [{}] does not exist for version [{}].", flowEntity.getId(), version);
        throw new ResourceNotFoundException("The specified versioned flow snapshot does not exist for this flow.");
    }

    // get the serialized bytes of the snapshot
    final byte[] serializedSnapshot = flowPersistenceProvider.getFlowContent(bucketEntity.getId(), flowEntity.getId(), version);

    if (serializedSnapshot == null || serializedSnapshot.length == 0) {
        throw new IllegalStateException("No serialized content found for snapshot with flow identifier "
                + flowEntity.getId() + " and version " + version);
    }

    // deserialize the content
    final InputStream input = new ByteArrayInputStream(serializedSnapshot);
    final VersionedFlowSnapshot snapshot = deserializeFlowContent(input);

    // map entities to data model
    final Bucket bucket = BucketMappings.map(bucketEntity);
    final VersionedFlow versionedFlow = FlowMappings.map(bucketEntity, flowEntity);
    final VersionedFlowSnapshotMetadata snapshotMetadata = FlowMappings.map(bucketEntity, snapshotEntity);

    // create the snapshot to return
    registryUrlAliasService.setExternal(snapshot.getFlowContents());
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlow(versionedFlow);
    snapshot.setBucket(bucket);
    return snapshot;
}
 
Example 4
Source File: TestRestAPI.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private static VersionedFlowSnapshot createSnapshot(Client client, VersionedFlow flow, int num) {
    final VersionedFlowSnapshotMetadata snapshotMetadata1 = new VersionedFlowSnapshotMetadata();
    snapshotMetadata1.setBucketIdentifier(flow.getBucketIdentifier());
    snapshotMetadata1.setFlowIdentifier(flow.getIdentifier());
    snapshotMetadata1.setVersion(num);
    snapshotMetadata1.setComments("This is snapshot #" + num);

    final VersionedProcessGroup snapshotContents1 = new VersionedProcessGroup();
    snapshotContents1.setIdentifier("pg1");
    snapshotContents1.setName("Process Group 1");

    final VersionedFlowSnapshot snapshot1 = new VersionedFlowSnapshot();
    snapshot1.setSnapshotMetadata(snapshotMetadata1);
    snapshot1.setFlowContents(snapshotContents1);

    final VersionedFlowSnapshot createdSnapshot = client.target(REGISTRY_API_BUCKETS_URL)
            .path("{bucketId}/flows/{flowId}/versions")
            .resolveTemplate("bucketId", flow.getBucketIdentifier())
            .resolveTemplate("flowId", flow.getIdentifier())
            .request()
            .post(
                    Entity.entity(snapshot1, MediaType.APPLICATION_JSON_TYPE),
                    VersionedFlowSnapshot.class
            );

    return createdSnapshot;
}
 
Example 5
Source File: DBFlowStorageIT.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private VersionedFlowSnapshot createSnapshot(final Bucket bucket, final VersionedFlow flow, final int version, final String rootPgName) {
    final VersionedProcessGroup rootPg = new VersionedProcessGroup();
    rootPg.setName(rootPgName);

    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setBucketIdentifier(bucket.getIdentifier());
    snapshotMetadata.setFlowIdentifier(flow.getIdentifier());
    snapshotMetadata.setVersion(version);
    snapshotMetadata.setComments("comments");

    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setFlowContents(rootPg);
    snapshot.setSnapshotMetadata(snapshotMetadata);
    return snapshot;
}
 
Example 6
Source File: TestRegistryService.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test(expected = ConstraintViolationException.class)
public void testCreateSnapshotNullMetadata() {
    final VersionedFlowSnapshot snapshot = createSnapshot();
    snapshot.setSnapshotMetadata(null);
    registryService.createFlowSnapshot(snapshot);
}
 
Example 7
Source File: TestEventFactory.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    bucket = new Bucket();
    bucket.setName("Bucket1");
    bucket.setIdentifier(UUID.randomUUID().toString());
    bucket.setCreatedTimestamp(System.currentTimeMillis());

    versionedFlow = new VersionedFlow();
    versionedFlow.setIdentifier(UUID.randomUUID().toString());
    versionedFlow.setName("Flow 1");
    versionedFlow.setBucketIdentifier(bucket.getIdentifier());
    versionedFlow.setBucketName(bucket.getName());

    VersionedFlowSnapshotMetadata metadata = new VersionedFlowSnapshotMetadata();
    metadata.setAuthor("user1");
    metadata.setComments("This is flow 1");
    metadata.setVersion(1);
    metadata.setBucketIdentifier(bucket.getIdentifier());
    metadata.setFlowIdentifier(versionedFlow.getIdentifier());

    versionedFlowSnapshot = new VersionedFlowSnapshot();
    versionedFlowSnapshot.setSnapshotMetadata(metadata);
    versionedFlowSnapshot.setFlowContents(new VersionedProcessGroup());

    bundle = new Bundle();
    bundle.setIdentifier(UUID.randomUUID().toString());
    bundle.setBucketIdentifier(bucket.getIdentifier());
    bundle.setBundleType(BundleType.NIFI_NAR);
    bundle.setGroupId("org.apache.nifi");
    bundle.setArtifactId("nifi-foo-nar");

    final BundleVersionMetadata bundleVersionMetadata = new BundleVersionMetadata();
    bundleVersionMetadata.setId(UUID.randomUUID().toString());
    bundleVersionMetadata.setVersion("1.0.0");
    bundleVersionMetadata.setBucketId(bucket.getIdentifier());
    bundleVersionMetadata.setBundleId(bundle.getIdentifier());

    bundleVersion = new BundleVersion();
    bundleVersion.setVersionMetadata(bundleVersionMetadata);
    bundleVersion.setBundle(bundle);
    bundleVersion.setBucket(bucket);
}
 
Example 8
Source File: SecureNiFiRegistryClientIT.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrudOperations() throws IOException, NiFiRegistryException {
    final Bucket bucket = new Bucket();
    bucket.setName("Bucket 1 " + System.currentTimeMillis());
    bucket.setDescription("This is bucket 1");
    bucket.setRevision(new RevisionInfo(null, 0L));

    final BucketClient bucketClient = client.getBucketClient();
    final Bucket createdBucket = bucketClient.create(bucket);
    Assert.assertNotNull(createdBucket);
    Assert.assertNotNull(createdBucket.getIdentifier());
    Assert.assertNotNull(createdBucket.getRevision());

    final List<Bucket> buckets = bucketClient.getAll();
    Assert.assertEquals(4, buckets.size());
    buckets.forEach(b -> Assert.assertNotNull(b.getRevision()));

    final VersionedFlow flow = new VersionedFlow();
    flow.setBucketIdentifier(createdBucket.getIdentifier());
    flow.setName("Flow 1 - " + System.currentTimeMillis());
    flow.setRevision(new RevisionInfo(null, 0L));

    final FlowClient flowClient = client.getFlowClient();
    final VersionedFlow createdFlow = flowClient.create(flow);
    Assert.assertNotNull(createdFlow);
    Assert.assertNotNull(createdFlow.getIdentifier());
    Assert.assertNotNull(createdFlow.getRevision());

    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setBucketIdentifier(createdFlow.getBucketIdentifier());
    snapshotMetadata.setFlowIdentifier(createdFlow.getIdentifier());
    snapshotMetadata.setVersion(1);
    snapshotMetadata.setComments("This is snapshot #1");

    final VersionedProcessGroup rootProcessGroup = new VersionedProcessGroup();
    rootProcessGroup.setIdentifier("root-pg");
    rootProcessGroup.setName("Root Process Group");

    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlowContents(rootProcessGroup);

    final FlowSnapshotClient snapshotClient = client.getFlowSnapshotClient();
    final VersionedFlowSnapshot createdSnapshot = snapshotClient.create(snapshot);
    Assert.assertNotNull(createdSnapshot);
    Assert.assertEquals("CN=user1, OU=nifi", createdSnapshot.getSnapshotMetadata().getAuthor());
}
 
Example 9
Source File: UnsecuredNiFiRegistryClientIT.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
private static VersionedFlowSnapshot buildSnapshot(VersionedFlow flow, int num) {
    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setBucketIdentifier(flow.getBucketIdentifier());
    snapshotMetadata.setFlowIdentifier(flow.getIdentifier());
    snapshotMetadata.setVersion(num);
    snapshotMetadata.setComments("This is snapshot #" + num);

    final VersionedProcessGroup rootProcessGroup = new VersionedProcessGroup();
    rootProcessGroup.setIdentifier("root-pg");
    rootProcessGroup.setName("Root Process Group");

    final VersionedProcessGroup subProcessGroup = new VersionedProcessGroup();
    subProcessGroup.setIdentifier("sub-pg");
    subProcessGroup.setName("Sub Process Group");
    rootProcessGroup.getProcessGroups().add(subProcessGroup);

    final Map<String,String> processorProperties = new HashMap<>();
    processorProperties.put("Prop 1", "Val 1");
    processorProperties.put("Prop 2", "Val 2");

    final Map<String, VersionedPropertyDescriptor> propertyDescriptors = new HashMap<>();

    final VersionedProcessor processor1 = new VersionedProcessor();
    processor1.setIdentifier("p1");
    processor1.setName("Processor 1");
    processor1.setProperties(processorProperties);
    processor1.setPropertyDescriptors(propertyDescriptors);

    final VersionedProcessor processor2 = new VersionedProcessor();
    processor2.setIdentifier("p2");
    processor2.setName("Processor 2");
    processor2.setProperties(processorProperties);
    processor2.setPropertyDescriptors(propertyDescriptors);

    subProcessGroup.getProcessors().add(processor1);
    subProcessGroup.getProcessors().add(processor2);

    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlowContents(rootProcessGroup);
    return snapshot;
}
 
Example 10
Source File: NoRevisionsIT.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoRevisions() {
    // Create a bucket...

    final Bucket bucket = new Bucket();
    bucket.setName("Integration Test Bucket");
    bucket.setDescription("A bucket created by an integration test.");

    final Bucket createdBucket = client
            .target(createURL("buckets"))
            .request()
            .post(Entity.entity(bucket, MediaType.APPLICATION_JSON), Bucket.class);

    assertBucketsEqual(bucket, createdBucket, false);
    assertNotNull(createdBucket.getIdentifier());

    // Update bucket...

    createdBucket.setName("Renamed Bucket");
    createdBucket.setDescription("This bucket has been updated by an integration test.");

    final Bucket updatedBucket = client
            .target(createURL("buckets/" + createdBucket.getIdentifier()))
            .request()
            .put(Entity.entity(createdBucket, MediaType.APPLICATION_JSON), Bucket.class);

    assertBucketsEqual(updatedBucket, createdBucket, true);

    // Create a flow...

    final VersionedFlow flow = new VersionedFlow();
    flow.setIdentifier(UUID.randomUUID().toString()); // Simulate NiFi sending an identifier
    flow.setBucketIdentifier(createdBucket.getIdentifier());
    flow.setName("Test Flow");
    flow.setDescription("This is a flow created by an integration test.");

    final VersionedFlow createdFlow = client
            .target(createURL("buckets/{bucketId}/flows"))
            .resolveTemplate("bucketId", flow.getBucketIdentifier())
            .request()
            .post(Entity.entity(flow, MediaType.APPLICATION_JSON), VersionedFlow.class);

    assertFlowsEqual(flow, createdFlow, false);
    assertNotNull(createdFlow.getIdentifier());

    // Update flow...

    createdFlow.setName("Renamed Flow");
    createdFlow.setDescription("This flow has been updated by an integration test.");

    final VersionedFlow updatedFlow = client
            .target(createURL("buckets/{bucketId}/flows/{flowId}"))
            .resolveTemplate("bucketId",flow.getBucketIdentifier())
            .resolveTemplate("flowId", createdFlow.getIdentifier())
            .request()
            .put(Entity.entity(createdFlow, MediaType.APPLICATION_JSON), VersionedFlow.class);

    assertTrue(updatedFlow.getModifiedTimestamp() > createdFlow.getModifiedTimestamp());

    // Create a version of a flow...

    final VersionedFlowSnapshotMetadata flowSnapshotMetadata = new VersionedFlowSnapshotMetadata();
    flowSnapshotMetadata.setVersion(1);
    flowSnapshotMetadata.setBucketIdentifier(createdFlow.getBucketIdentifier());
    flowSnapshotMetadata.setFlowIdentifier(createdFlow.getIdentifier());
    flowSnapshotMetadata.setComments("This is snapshot 1, created by an integration test.");

    final VersionedFlowSnapshot flowSnapshot = new VersionedFlowSnapshot();
    flowSnapshot.setSnapshotMetadata(flowSnapshotMetadata);
    flowSnapshot.setFlowContents(new VersionedProcessGroup()); // an empty root process group

    final VersionedFlowSnapshot createdFlowSnapshot = client
            .target(createURL("buckets/{bucketId}/flows/{flowId}/versions"))
            .resolveTemplate("bucketId", flowSnapshotMetadata.getBucketIdentifier())
            .resolveTemplate("flowId", flowSnapshotMetadata.getFlowIdentifier())
            .request()
            .post(Entity.entity(flowSnapshot, MediaType.APPLICATION_JSON), VersionedFlowSnapshot.class);

    assertFlowSnapshotsEqual(flowSnapshot, createdFlowSnapshot, false);

    // Delete flow...

    final VersionedFlow deletedFlow = client
            .target(createURL("buckets/{bucketId}/flows/{flowId}"))
            .resolveTemplate("bucketId", createdFlow.getBucketIdentifier())
            .resolveTemplate("flowId", createdFlow.getIdentifier())
            .request()
            .delete(VersionedFlow.class);

    assertNotNull(deletedFlow);

    // Delete bucket...

    final Bucket deletedBucket = client
            .target(createURL("buckets/" + createdBucket.getIdentifier()))
            .request()
            .delete(Bucket.class);

    assertNotNull(deletedBucket);
}
 
Example 11
Source File: FlowsIT.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateFlowVersionGetFlowVersion() throws Exception {
    final RevisionInfo initialRevision = new RevisionInfo("FlowsIT", 0L);

    // Given: an empty Bucket "3" (see FlowsIT.sql) with a newly created flow

    long testStartTime = System.currentTimeMillis();
    final String bucketId = "2";
    final VersionedFlow flow = new VersionedFlow();
    flow.setBucketIdentifier(bucketId);
    flow.setName("Test Flow for creating snapshots");
    flow.setDescription("This is a randomly named flow created by an integration test for the purpose of holding snapshots.");
    flow.setRevision(initialRevision);

    final VersionedFlow createdFlow = client
            .target(createURL("buckets/{bucketId}/flows"))
            .resolveTemplate("bucketId", bucketId)
            .request()
            .post(Entity.entity(flow, MediaType.APPLICATION_JSON), VersionedFlow.class);
    final String flowId = createdFlow.getIdentifier();

    // When: an initial flow snapshot is created *without* a version

    final VersionedFlowSnapshotMetadata flowSnapshotMetadata = new VersionedFlowSnapshotMetadata();
    flowSnapshotMetadata.setBucketIdentifier("2");
    flowSnapshotMetadata.setFlowIdentifier(flowId);
    flowSnapshotMetadata.setComments("This is snapshot 1, created by an integration test.");
    final VersionedFlowSnapshot flowSnapshot = new VersionedFlowSnapshot();
    flowSnapshot.setSnapshotMetadata(flowSnapshotMetadata);
    flowSnapshot.setFlowContents(new VersionedProcessGroup()); // an empty root process group

    WebTarget clientRequestTarget = client
            .target(createURL("buckets/{bucketId}/flows/{flowId}/versions"))
            .resolveTemplate("bucketId", bucketId)
            .resolveTemplate("flowId", flowId);
    final Response response =
            clientRequestTarget.request().post(Entity.entity(flowSnapshot, MediaType.APPLICATION_JSON), Response.class);

    // Then: an error is returned because version != 1

    assertEquals(400, response.getStatus());

    // But When: an initial flow snapshot is created with version == 1

    flowSnapshot.getSnapshotMetadata().setVersion(1);
    final VersionedFlowSnapshot createdFlowSnapshot =
            clientRequestTarget.request().post(Entity.entity(flowSnapshot, MediaType.APPLICATION_JSON), VersionedFlowSnapshot.class);

    // Then: the server returns the created flow snapshot, with server-set fields populated correctly :)

    assertFlowSnapshotsEqual(flowSnapshot, createdFlowSnapshot, false);
    assertTrue(createdFlowSnapshot.getSnapshotMetadata().getTimestamp() - testStartTime > 0L); // both server and client in same JVM, so there shouldn't be skew
    assertEquals("anonymous", createdFlowSnapshot.getSnapshotMetadata().getAuthor());
    assertNotNull(createdFlowSnapshot.getSnapshotMetadata().getLink());
    assertNotNull(createdFlowSnapshot.getSnapshotMetadata().getLink().getUri());
    assertNotNull(createdFlowSnapshot.getFlow());
    assertEquals(1, createdFlowSnapshot.getFlow().getVersionCount());
    assertNotNull(createdFlowSnapshot.getBucket());

    // And when .../flows/{id}/versions is queried, then the newly created flow snapshot is returned in the list

    final VersionedFlowSnapshotMetadata[] versionedFlowSnapshots =
            clientRequestTarget.request().get(VersionedFlowSnapshotMetadata[].class);
    assertNotNull(versionedFlowSnapshots);
    assertEquals(1, versionedFlowSnapshots.length);
    assertFlowSnapshotMetadataEqual(createdFlowSnapshot.getSnapshotMetadata(), versionedFlowSnapshots[0], true);

    // And when the link URI is queried, then the newly created flow snapshot is returned

    final VersionedFlowSnapshot flowSnapshotByLink = client
            .target(createURL(versionedFlowSnapshots[0].getLink().getUri().toString()))
            .request()
            .get(VersionedFlowSnapshot.class);
    assertFlowSnapshotsEqual(createdFlowSnapshot, flowSnapshotByLink, true);
    assertNotNull(flowSnapshotByLink.getFlow());
    assertNotNull(flowSnapshotByLink.getBucket());

    // And when the bucket is queried by .../versions/{v}, then the newly created flow snapshot is returned

    final VersionedFlowSnapshot flowSnapshotByVersionNumber = clientRequestTarget.path("/1").request().get(VersionedFlowSnapshot.class);
    assertFlowSnapshotsEqual(createdFlowSnapshot, flowSnapshotByVersionNumber, true);
    assertNotNull(flowSnapshotByVersionNumber.getFlow());
    assertNotNull(flowSnapshotByVersionNumber.getBucket());

    // And when the latest URI is queried, then the newly created flow snapshot is returned

    final VersionedFlowSnapshot flowSnapshotByLatest = clientRequestTarget.path("/latest").request().get(VersionedFlowSnapshot.class);
    assertFlowSnapshotsEqual(createdFlowSnapshot, flowSnapshotByLatest, true);
    assertNotNull(flowSnapshotByLatest.getFlow());
    assertNotNull(flowSnapshotByLatest.getBucket());

}
 
Example 12
Source File: TransferFlowVersion.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public StringResult doExecute(final NiFiRegistryClient client, final Properties properties)
        throws IOException, NiFiRegistryException, ParseException {

    final String srcPropsValue = getArg(properties, CommandOption.SRC_PROPS);
    final String srcFlowId  = getRequiredArg(properties, CommandOption.SRC_FLOW_ID);
    final Integer srcFlowVersion = getIntArg(properties, CommandOption.SRC_FLOW_VERSION);
    final String destFlowId = getRequiredArg(properties, CommandOption.FLOW_ID);

    final NiFiRegistryClient srcClient = getSourceClient(client, srcPropsValue);

    // get the snapshot of the source flow, either the version specified or the latest
    final VersionedFlowSnapshot srcSnapshot;
    if (srcFlowVersion == null) {
        srcSnapshot = srcClient.getFlowSnapshotClient().getLatest(srcFlowId);
    } else {
        srcSnapshot = srcClient.getFlowSnapshotClient().get(srcFlowId, srcFlowVersion);
    }

    final Integer srcSnapshotFlowVersion = srcSnapshot.getSnapshotMetadata().getVersion();

    // get the destination flow
    final VersionedFlow destFlow;
    try {
        destFlow = client.getFlowClient().get(destFlowId);
    } catch (Exception e) {
        throw new NiFiRegistryException("Error retrieving destination flow : " + e.getMessage(), e);
    }

    // determine the next version number for the destination flow
    final List<Integer> destVersions = getVersions(client, destFlow.getIdentifier());
    final Integer destFlowVersion = destVersions.isEmpty() ? 1 : destVersions.get(0) + 1;

    // create the new metadata for the destination snapshot
    final VersionedFlowSnapshotMetadata destMetadata = new VersionedFlowSnapshotMetadata();
    destMetadata.setBucketIdentifier(destFlow.getBucketIdentifier());
    destMetadata.setFlowIdentifier(destFlowId);
    destMetadata.setVersion(destFlowVersion);
    destMetadata.setComments(srcSnapshot.getSnapshotMetadata().getComments());

    // update the source snapshot with the destination metadata
    srcSnapshot.setFlow(null);
    srcSnapshot.setBucket(null);
    srcSnapshot.setSnapshotMetadata(destMetadata);

    // create the destination snapshot
    client.getFlowSnapshotClient().create(srcSnapshot);

    if (getContext().isInteractive()) {
        println();
        println("Transferred version " + srcSnapshotFlowVersion
                + " of source flow to version " + destFlowVersion + " of destination flow");
    }

    return new OkResult(getContext().isInteractive());
}
 
Example 13
Source File: ImportFlowVersion.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public StringResult doExecute(final NiFiRegistryClient client, final Properties properties)
        throws ParseException, IOException, NiFiRegistryException {
    final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
    final String inputFile = getRequiredArg(properties, CommandOption.INPUT_SOURCE);

    final String contents = getInputSourceContent(inputFile);

    final FlowClient flowClient = client.getFlowClient();
    final FlowSnapshotClient snapshotClient = client.getFlowSnapshotClient();

    final ObjectMapper objectMapper = JacksonUtils.getObjectMapper();
    final VersionedFlowSnapshot deserializedSnapshot = objectMapper.readValue(contents, VersionedFlowSnapshot.class);
    if (deserializedSnapshot == null) {
        throw new IOException("Unable to deserialize flow version from " + inputFile);
    }

    final VersionedFlow versionedFlow = flowClient.get(flowId);

    // determine the latest existing version in the destination system
    Integer version;
    try {
        final VersionedFlowSnapshotMetadata latestMetadata = snapshotClient.getLatestMetadata(flowId);
        version = latestMetadata.getVersion() + 1;
    } catch (NiFiRegistryException e) {
        // when there are no versions it produces a 404 not found
        version = 1;
    }

    // create new metadata using the passed in bucket and flow in the target registry, keep comments
    final VersionedFlowSnapshotMetadata metadata = new VersionedFlowSnapshotMetadata();
    metadata.setBucketIdentifier(versionedFlow.getBucketIdentifier());
    metadata.setFlowIdentifier(flowId);
    metadata.setVersion(version);

    final VersionedFlowSnapshotMetadata deserializedSnapshotMetadata = deserializedSnapshot.getSnapshotMetadata();
    if (deserializedSnapshotMetadata != null) {
        metadata.setComments(deserializedSnapshotMetadata.getComments());
    }

    // create a new snapshot using the new metadata and the contents from the deserialized snapshot
    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(metadata);
    snapshot.setFlowContents(deserializedSnapshot.getFlowContents());
    snapshot.setExternalControllerServices(deserializedSnapshot.getExternalControllerServices());
    snapshot.setParameterContexts(deserializedSnapshot.getParameterContexts());
    snapshot.setFlowEncodingVersion(deserializedSnapshot.getFlowEncodingVersion());

    final VersionedFlowSnapshot createdSnapshot = snapshotClient.create(snapshot);
    final VersionedFlowSnapshotMetadata createdMetadata = createdSnapshot.getSnapshotMetadata();

    return new StringResult(String.valueOf(createdMetadata.getVersion()), getContext().isInteractive());
}
 
Example 14
Source File: VersionsResource.java    From nifi with Apache License 2.0 4 votes vote down vote up
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("process-groups/{id}/download")
@ApiOperation(
    value = "Gets the latest version of a Process Group for download",
    response = String.class,
    authorizations = {
        @Authorization(value = "Read - /process-groups/{uuid}")
    }
)
@ApiResponses(value = {
    @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."),
    @ApiResponse(code = 401, message = "Client could not be authenticated."),
    @ApiResponse(code = 403, message = "Client is not authorized to make this request."),
    @ApiResponse(code = 404, message = "The specified resource could not be found."),
    @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.")
})
public Response exportFlowVersion(@ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId) {
    // authorize access
    serviceFacade.authorizeAccess(lookup -> {
        final ProcessGroupAuthorizable groupAuthorizable = lookup.getProcessGroup(groupId);
        // ensure access to process groups (nested), encapsulated controller services and referenced parameter contexts
        authorizeProcessGroup(groupAuthorizable, authorizer, lookup, RequestAction.READ, true,
                false, true, false, true);
    });

    // get the versioned flow
    final VersionedFlowSnapshot versionedFlowSnapshot = serviceFacade.getVersionedFlowSnapshotByGroupId(groupId);

    final VersionedProcessGroup versionedProcessGroup = versionedFlowSnapshot.getFlowContents();
    final String flowName = versionedProcessGroup.getName();
    final int flowVersion = versionedFlowSnapshot.getSnapshotMetadata().getVersion();

    // clear top-level registry data which doesn't belong in versioned flow download
    versionedFlowSnapshot.setFlow(null);
    versionedFlowSnapshot.setBucket(null);
    versionedFlowSnapshot.setSnapshotMetadata(null);

    // clear nested process group registry data which doesn't belong in versioned flow download
    sanitizeRegistryInfo(versionedProcessGroup);

    // determine the name of the attachment - possible issues with spaces in file names
    final String filename = flowName.replaceAll("\\s", "_") + "_" + flowVersion + ".json";

    return generateOkResponse(versionedFlowSnapshot).header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filename)).build();
}
 
Example 15
Source File: ImportFlowIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
private VersionedFlowSnapshot createFlowSnapshot(final List<ControllerServiceNode> controllerServices, final List<ProcessorNode> processors, final Set<Parameter> parameters) {
    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setAuthor("unit-test");
    snapshotMetadata.setBucketIdentifier("unit-test-bucket");
    snapshotMetadata.setFlowIdentifier("unit-test-flow");
    snapshotMetadata.setTimestamp(System.currentTimeMillis());
    snapshotMetadata.setVersion(1);

    final Bucket bucket = new Bucket();
    bucket.setCreatedTimestamp(System.currentTimeMillis());
    bucket.setIdentifier("unit-test-bucket");
    bucket.setName("Unit Test Bucket");

    final VersionedFlow flow = new VersionedFlow();
    flow.setBucketIdentifier("unit-test-bucket");
    flow.setBucketName("Unit Test Bucket");
    flow.setCreatedTimestamp(System.currentTimeMillis());
    flow.setIdentifier("unit-test-flow");
    flow.setName("Unit Test Flow");

    final BundleCoordinate coordinate = getSystemBundle().getBundleDetails().getCoordinate();
    final Bundle bundle = new Bundle();
    bundle.setArtifact(coordinate.getId());
    bundle.setGroup(coordinate.getGroup());
    bundle.setVersion(coordinate.getVersion());

    final NiFiRegistryFlowMapper flowMapper = new NiFiRegistryFlowMapper(getExtensionManager());

    final Set<VersionedProcessor> versionedProcessors = new HashSet<>();
    for (final ProcessorNode processor : processors) {
        final VersionedProcessor versionedProcessor = flowMapper.mapProcessor(processor, getFlowController().getControllerServiceProvider(), Collections.emptySet(), new HashMap<>());
        versionedProcessors.add(versionedProcessor);
        processor.setVersionedComponentId(versionedProcessor.getIdentifier());
    }

    final Set<VersionedControllerService> services = new HashSet<>();
    for (final ControllerServiceNode serviceNode : controllerServices) {
        final VersionedControllerService service = flowMapper.mapControllerService(serviceNode, getFlowController().getControllerServiceProvider(), Collections.emptySet(), new HashMap<>());
        services.add(service);
        serviceNode.setVersionedComponentId(service.getIdentifier());
    }

    final VersionedProcessGroup flowContents = new VersionedProcessGroup();
    flowContents.setIdentifier("unit-test-flow-contents");
    flowContents.setName("Unit Test");
    flowContents.setProcessors(versionedProcessors);
    flowContents.setControllerServices(services);

    final VersionedFlowSnapshot versionedFlowSnapshot = new VersionedFlowSnapshot();
    versionedFlowSnapshot.setSnapshotMetadata(snapshotMetadata);
    versionedFlowSnapshot.setBucket(bucket);
    versionedFlowSnapshot.setFlow(flow);
    versionedFlowSnapshot.setFlowContents(flowContents);

    if (parameters != null) {
        final Set<VersionedParameter> versionedParameters = new HashSet<>();
        for (final Parameter parameter : parameters) {
            final VersionedParameter versionedParameter = new VersionedParameter();
            versionedParameter.setName(parameter.getDescriptor().getName());
            versionedParameter.setValue(parameter.getValue());
            versionedParameter.setSensitive(parameter.getDescriptor().isSensitive());

            versionedParameters.add(versionedParameter);
        }

        final VersionedParameterContext versionedParameterContext = new VersionedParameterContext();
        versionedParameterContext.setName("Unit Test Context");
        versionedParameterContext.setParameters(versionedParameters);
        versionedFlowSnapshot.setParameterContexts(Collections.singletonMap(versionedParameterContext.getName(), versionedParameterContext));

        flowContents.setParameterContextName("Unit Test Context");
    }

    return versionedFlowSnapshot;
}