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

The following examples show how to use org.apache.nifi.registry.flow.VersionedFlowSnapshot#setBucket() . 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: 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 2
Source File: ExportFlowVersion.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public VersionedFlowSnapshotResult doExecute(final NiFiRegistryClient client, final Properties properties)
        throws ParseException, IOException, NiFiRegistryException {
    final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
    final Integer version = getIntArg(properties, CommandOption.FLOW_VERSION);

    // if no version was provided then export the latest, otherwise use specific version
    final VersionedFlowSnapshot versionedFlowSnapshot;
    if (version == null) {
        versionedFlowSnapshot = client.getFlowSnapshotClient().getLatest(flowId);
    } else {
        versionedFlowSnapshot = client.getFlowSnapshotClient().get(flowId, version);
    }

    versionedFlowSnapshot.setFlow(null);
    versionedFlowSnapshot.setBucket(null);
    versionedFlowSnapshot.getSnapshotMetadata().setBucketIdentifier(null);
    versionedFlowSnapshot.getSnapshotMetadata().setFlowIdentifier(null);
    versionedFlowSnapshot.getSnapshotMetadata().setLink(null);

    // currently export doesn't use the ResultWriter concept, it always writes JSON
    // destination will be a file if outputFile is specified, otherwise it will be the output stream of the CLI
    final String outputFile;
    if (properties.containsKey(CommandOption.OUTPUT_FILE.getLongName())) {
        outputFile = properties.getProperty(CommandOption.OUTPUT_FILE.getLongName());
    } else {
        outputFile = null;
    }

    return new VersionedFlowSnapshotResult(versionedFlowSnapshot, outputFile);
}
 
Example 3
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 4
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 5
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;
}