Search in sources :

Example 1 with ReferenceResolver

use of org.apache.nifi.toolkit.cli.api.ReferenceResolver in project nifi by apache.

the class CommandProcessor method resolveBackReferences.

/**
 * Finds any args that indicate a back-reference and replaces the value of the arg with the
 * resolved back-reference.
 *
 * If the reference does not resolve, or non-numeric position is given, then the arg is left unchanged.
 *
 * @param args the args to process
 */
private void resolveBackReferences(final String[] args) {
    final ReferenceResolver referenceResolver = backReferenceHolder.get();
    if (referenceResolver == null) {
        return;
    }
    final List<ResolvedReference> resolvedReferences = new ArrayList<>();
    for (int i = 0; i < args.length; i++) {
        final String arg = args[i];
        if (arg == null || !arg.startsWith(BACK_REF_INDICATOR)) {
            continue;
        }
        try {
            // attempt to determine which option is using the back-ref
            CommandOption option = null;
            if (i > 0) {
                String prevArg = args[i - 1];
                if (prevArg.startsWith("--")) {
                    prevArg = prevArg.substring(2);
                } else if (prevArg.startsWith("-")) {
                    prevArg = prevArg.substring(1);
                }
                for (CommandOption opt : CommandOption.values()) {
                    if (opt.getShortName().equals(prevArg) || opt.getLongName().equals(prevArg)) {
                        option = opt;
                        break;
                    }
                }
            }
            // use the option and position to resolve the back-ref, and if it resolves then replace the arg
            final Integer pos = Integer.valueOf(arg.substring(1));
            final ResolvedReference resolvedReference = referenceResolver.resolve(option, pos);
            if (resolvedReference != null) {
                args[i] = resolvedReference.getResolvedValue();
                resolvedReferences.add(resolvedReference);
            }
        } catch (Exception e) {
        // skip
        }
    }
    if (context.isInteractive()) {
        for (ResolvedReference resolvedRef : resolvedReferences) {
            out.println();
            out.printf("Using a positional back-reference for '%s'%n", resolvedRef.getDisplayName());
        }
    }
}
Also used : ResolvedReference(org.apache.nifi.toolkit.cli.api.ResolvedReference) ArrayList(java.util.ArrayList) ReferenceResolver(org.apache.nifi.toolkit.cli.api.ReferenceResolver) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) ParseException(org.apache.commons.cli.ParseException)

Example 2 with ReferenceResolver

use of org.apache.nifi.toolkit.cli.api.ReferenceResolver in project nifi by apache.

the class BucketsResult method createReferenceResolver.

@Override
public ReferenceResolver createReferenceResolver(final Context context) {
    final Map<Integer, Bucket> backRefs = new HashMap<>();
    final AtomicInteger position = new AtomicInteger(0);
    buckets.forEach(b -> backRefs.put(position.incrementAndGet(), b));
    return new ReferenceResolver() {

        @Override
        public ResolvedReference resolve(final CommandOption option, final Integer position) {
            final Bucket bucket = backRefs.get(position);
            if (bucket != null) {
                return new ResolvedReference(option, position, bucket.getName(), bucket.getIdentifier());
            } else {
                return null;
            }
        }

        @Override
        public boolean isEmpty() {
            return backRefs.isEmpty();
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResolvedReference(org.apache.nifi.toolkit.cli.api.ResolvedReference) CommandOption(org.apache.nifi.toolkit.cli.impl.command.CommandOption) Bucket(org.apache.nifi.registry.bucket.Bucket) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReferenceResolver(org.apache.nifi.toolkit.cli.api.ReferenceResolver)

Example 3 with ReferenceResolver

use of org.apache.nifi.toolkit.cli.api.ReferenceResolver in project nifi by apache.

the class ProcessGroupsResult method createReferenceResolver.

@Override
public ReferenceResolver createReferenceResolver(final Context context) {
    final Map<Integer, ProcessGroupDTO> backRefs = new HashMap<>();
    final AtomicInteger position = new AtomicInteger(0);
    processGroups.forEach(p -> backRefs.put(position.incrementAndGet(), p));
    return new ReferenceResolver() {

        @Override
        public ResolvedReference resolve(final CommandOption option, final Integer position) {
            final ProcessGroupDTO pg = backRefs.get(position);
            if (pg != null) {
                return new ResolvedReference(option, position, pg.getName(), pg.getId());
            } else {
                return null;
            }
        }

        @Override
        public boolean isEmpty() {
            return backRefs.isEmpty();
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResolvedReference(org.apache.nifi.toolkit.cli.api.ResolvedReference) CommandOption(org.apache.nifi.toolkit.cli.impl.command.CommandOption) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) ReferenceResolver(org.apache.nifi.toolkit.cli.api.ReferenceResolver)

Example 4 with ReferenceResolver

use of org.apache.nifi.toolkit.cli.api.ReferenceResolver in project nifi by apache.

the class VersionedFlowsResult method createReferenceResolver.

@Override
public ReferenceResolver createReferenceResolver(final Context context) {
    final Map<Integer, VersionedFlow> backRefs = new HashMap<>();
    final AtomicInteger position = new AtomicInteger(0);
    versionedFlows.forEach(f -> backRefs.put(position.incrementAndGet(), f));
    return new ReferenceResolver() {

        @Override
        public ResolvedReference resolve(final CommandOption option, final Integer position) {
            final VersionedFlow versionedFlow = backRefs.get(position);
            if (versionedFlow != null) {
                if (option != null && option == CommandOption.BUCKET_ID) {
                    return new ResolvedReference(option, position, versionedFlow.getBucketName(), versionedFlow.getBucketIdentifier());
                } else {
                    return new ResolvedReference(option, position, versionedFlow.getName(), versionedFlow.getIdentifier());
                }
            } else {
                return null;
            }
        }

        @Override
        public boolean isEmpty() {
            return backRefs.isEmpty();
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResolvedReference(org.apache.nifi.toolkit.cli.api.ResolvedReference) CommandOption(org.apache.nifi.toolkit.cli.impl.command.CommandOption) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) ReferenceResolver(org.apache.nifi.toolkit.cli.api.ReferenceResolver)

Example 5 with ReferenceResolver

use of org.apache.nifi.toolkit.cli.api.ReferenceResolver in project nifi by apache.

the class TestVersionedFlowsResult method testReferenceResolver.

@Test
public void testReferenceResolver() {
    final VersionedFlowsResult result = new VersionedFlowsResult(ResultType.SIMPLE, flows);
    final ReferenceResolver resolver = result.createReferenceResolver(Mockito.mock(Context.class));
    // should default to flow id when no option is specified
    Assert.assertEquals("ea752054-22c6-4fc0-b851-967d9a3837cb", resolver.resolve(null, 1).getResolvedValue());
    Assert.assertEquals("ddf5f289-7502-46df-9798-4b0457c1816b", resolver.resolve(null, 2).getResolvedValue());
    // should use flow id when flow id is specified
    Assert.assertEquals("ea752054-22c6-4fc0-b851-967d9a3837cb", resolver.resolve(CommandOption.FLOW_ID, 1).getResolvedValue());
    Assert.assertEquals("ddf5f289-7502-46df-9798-4b0457c1816b", resolver.resolve(CommandOption.FLOW_ID, 2).getResolvedValue());
    // should resolve the bucket id when bucket id option is used
    Assert.assertEquals("b1", resolver.resolve(CommandOption.BUCKET_ID, 1).getResolvedValue());
    Assert.assertEquals("b2", resolver.resolve(CommandOption.BUCKET_ID, 2).getResolvedValue());
    // should resolve to null when position doesn't exist
    Assert.assertEquals(null, resolver.resolve(CommandOption.FLOW_ID, 3));
}
Also used : Context(org.apache.nifi.toolkit.cli.api.Context) ReferenceResolver(org.apache.nifi.toolkit.cli.api.ReferenceResolver) Test(org.junit.Test)

Aggregations

ReferenceResolver (org.apache.nifi.toolkit.cli.api.ReferenceResolver)6 ResolvedReference (org.apache.nifi.toolkit.cli.api.ResolvedReference)4 HashMap (java.util.HashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CommandOption (org.apache.nifi.toolkit.cli.impl.command.CommandOption)3 ParseException (org.apache.commons.cli.ParseException)2 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)2 ArrayList (java.util.ArrayList)1 CommandLine (org.apache.commons.cli.CommandLine)1 Bucket (org.apache.nifi.registry.bucket.Bucket)1 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)1 Context (org.apache.nifi.toolkit.cli.api.Context)1 Referenceable (org.apache.nifi.toolkit.cli.api.Referenceable)1 Result (org.apache.nifi.toolkit.cli.api.Result)1 WritableResult (org.apache.nifi.toolkit.cli.api.WritableResult)1 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)1 Test (org.junit.Test)1