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

The following examples show how to use org.apache.nifi.registry.flow.VersionedFlowSnapshot#setFlowContents() . 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: RegistryService.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
private VersionedFlowSnapshot deserializeFlowContent(final InputStream input) {
    // attempt to read the version header from the serialized content
    final int dataModelVersion = flowContentSerializer.readDataModelVersion(input);

    // determine how to do deserialize based on the data model version
    if (flowContentSerializer.isProcessGroupVersion(dataModelVersion)) {
        final VersionedProcessGroup processGroup = flowContentSerializer.deserializeProcessGroup(dataModelVersion, input);
        final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
        snapshot.setFlowContents(processGroup);
        return snapshot;
    } else {
        final FlowContent flowContent = flowContentSerializer.deserializeFlowContent(dataModelVersion, input);
        return flowContent.getFlowSnapshot();
    }
}
 
Example 3
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 4
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 5
Source File: TestFlowContentSerializer.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testSerializeDeserializeFlowContent() {
    final VersionedProcessor processor1 = new VersionedProcessor();
    processor1.setIdentifier("processor1");
    processor1.setName("My Processor 1");

    final VersionedProcessGroup processGroup1 = new VersionedProcessGroup();
    processGroup1.setIdentifier("pg1");
    processGroup1.setName("My Process Group");
    processGroup1.getProcessors().add(processor1);

    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setFlowContents(processGroup1);

    final FlowContent flowContent = new FlowContent();
    flowContent.setFlowSnapshot(snapshot);

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    serializer.serializeFlowContent(flowContent, out);

    //final String json = new String(out.toByteArray(), StandardCharsets.UTF_8);
    //System.out.println(json);

    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

    // make sure we can read the version from the input stream and it should be the current version
    final Integer version = serializer.readDataModelVersion(in);
    assertEquals(serializer.getCurrentDataModelVersion(), version);
    assertEquals(false, serializer.isProcessGroupVersion(version));

    // make sure we can deserialize back to FlowContent
    final FlowContent deserializedFlowContent = serializer.deserializeFlowContent(version, in);
    assertNotNull(deserializedFlowContent);

    final VersionedFlowSnapshot deserializedSnapshot = deserializedFlowContent.getFlowSnapshot();
    assertNotNull(deserializedSnapshot);

    final VersionedProcessGroup deserializedProcessGroup1 = deserializedSnapshot.getFlowContents();
    assertNotNull(deserializedProcessGroup1);
    assertEquals(processGroup1.getIdentifier(), deserializedProcessGroup1.getIdentifier());
    assertEquals(processGroup1.getName(), deserializedProcessGroup1.getName());

    assertEquals(1, deserializedProcessGroup1.getProcessors().size());

    final VersionedProcessor deserializedProcessor1 = deserializedProcessGroup1.getProcessors().iterator().next();
    assertEquals(processor1.getIdentifier(), deserializedProcessor1.getIdentifier());
    assertEquals(processor1.getName(), deserializedProcessor1.getName());
}
 
Example 6
Source File: TestFlowContentSerializer.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testSerializeDeserializeWithExternalServices() throws SerializationException {
    final VersionedProcessGroup processGroup1 = new VersionedProcessGroup();
    processGroup1.setIdentifier("pg1");
    processGroup1.setName("My Process Group");

    final ExternalControllerServiceReference serviceReference1 = new ExternalControllerServiceReference();
    serviceReference1.setIdentifier("1");
    serviceReference1.setName("Service 1");

    final ExternalControllerServiceReference serviceReference2 = new ExternalControllerServiceReference();
    serviceReference2.setIdentifier("2");
    serviceReference2.setName("Service 2");

    final Map<String,ExternalControllerServiceReference> serviceReferences = new HashMap<>();
    serviceReferences.put(serviceReference1.getIdentifier(), serviceReference1);
    serviceReferences.put(serviceReference2.getIdentifier(), serviceReference2);

    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setFlowContents(processGroup1);
    snapshot.setExternalControllerServices(serviceReferences);

    final FlowContent flowContent = new FlowContent();
    flowContent.setFlowSnapshot(snapshot);

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    serializer.serializeFlowContent(flowContent, out);

    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

    // make sure we can read the version from the input stream and it should be the current version
    final Integer version = serializer.readDataModelVersion(in);
    assertEquals(serializer.getCurrentDataModelVersion(), version);

    // make sure we can deserialize back to FlowContent
    final FlowContent deserializedFlowContent = serializer.deserializeFlowContent(version, in);
    assertNotNull(deserializedFlowContent);

    final VersionedFlowSnapshot deserializedSnapshot = deserializedFlowContent.getFlowSnapshot();
    assertNotNull(deserializedSnapshot);

    final VersionedProcessGroup deserializedProcessGroup = deserializedSnapshot.getFlowContents();
    assertEquals(processGroup1.getIdentifier(), deserializedProcessGroup.getIdentifier());
    assertEquals(processGroup1.getName(), deserializedProcessGroup.getName());

    final Map<String,ExternalControllerServiceReference> deserializedServiceReferences = deserializedSnapshot.getExternalControllerServices();
    assertNotNull(deserializedServiceReferences);
    assertEquals(2, deserializedServiceReferences.size());

    final ExternalControllerServiceReference deserializedServiceReference1 = deserializedServiceReferences.get(serviceReference1.getIdentifier());
    assertNotNull(deserializedServiceReference1);
    assertEquals(serviceReference1.getIdentifier(), deserializedServiceReference1.getIdentifier());
    assertEquals(serviceReference1.getName(), deserializedServiceReference1.getName());
}
 
Example 7
Source File: TestRegistryService.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test(expected = ConstraintViolationException.class)
public void testCreateSnapshotInvalidFlowContents() {
    final VersionedFlowSnapshot snapshot = createSnapshot();
    snapshot.setFlowContents(null);
    registryService.createFlowSnapshot(snapshot);
}
 
Example 8
Source File: TestRegistryService.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Test(expected = ConstraintViolationException.class)
public void testCreateSnapshotNullFlowContents() {
    final VersionedFlowSnapshot snapshot = createSnapshot();
    snapshot.setFlowContents(null);
    registryService.createFlowSnapshot(snapshot);
}
 
Example 9
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 10
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 11
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 12
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 13
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 14
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 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;
}