|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.nifi.processors.tests.system; |
| 19 | + |
| 20 | +import org.apache.nifi.annotation.behavior.TriggerSerially; |
| 21 | +import org.apache.nifi.annotation.documentation.CapabilityDescription; |
| 22 | +import org.apache.nifi.components.PropertyDescriptor; |
| 23 | +import org.apache.nifi.expression.ExpressionLanguageScope; |
| 24 | +import org.apache.nifi.flowfile.FlowFile; |
| 25 | +import org.apache.nifi.processor.AbstractProcessor; |
| 26 | +import org.apache.nifi.processor.ProcessContext; |
| 27 | +import org.apache.nifi.processor.ProcessSession; |
| 28 | +import org.apache.nifi.processor.Relationship; |
| 29 | +import org.apache.nifi.processor.exception.ProcessException; |
| 30 | +import org.apache.nifi.processor.util.StandardValidators; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +@TriggerSerially |
| 36 | +@CapabilityDescription(""" |
| 37 | + Pulls one FlowFile at a time from the incoming queue and routes it to 'sequential' if its Value to Consider is exactly one greater than the |
| 38 | + Value to Consider of the previous FlowFile that this Processor received. Otherwise, the FlowFile is routed to 'failure'. The first FlowFile |
| 39 | + that the Processor receives is always routed to 'sequential'. |
| 40 | + """) |
| 41 | +public class VerifySequentialOrder extends AbstractProcessor { |
| 42 | + |
| 43 | + static final PropertyDescriptor VALUE_TO_CONSIDER = new PropertyDescriptor.Builder() |
| 44 | + .name("Value to Consider") |
| 45 | + .description("The whole number to compare against the Value to Consider of the previous FlowFile") |
| 46 | + .required(true) |
| 47 | + .addValidator(StandardValidators.LONG_VALIDATOR) |
| 48 | + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) |
| 49 | + .build(); |
| 50 | + |
| 51 | + static final Relationship REL_SEQUENTIAL = new Relationship.Builder() |
| 52 | + .name("sequential") |
| 53 | + .description("The first FlowFile, and any FlowFile whose Value to Consider is exactly one greater than that of the previous FlowFile") |
| 54 | + .build(); |
| 55 | + |
| 56 | + static final Relationship REL_FAILURE = new Relationship.Builder() |
| 57 | + .name("failure") |
| 58 | + .description("Any FlowFile whose Value to Consider is not exactly one greater than that of the previous FlowFile, or is not a whole number") |
| 59 | + .build(); |
| 60 | + |
| 61 | + private static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = List.of(VALUE_TO_CONSIDER); |
| 62 | + private static final Set<Relationship> RELATIONSHIPS = Set.of(REL_SEQUENTIAL, REL_FAILURE); |
| 63 | + |
| 64 | + private volatile Long previousValue; |
| 65 | + |
| 66 | + @Override |
| 67 | + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { |
| 68 | + return PROPERTY_DESCRIPTORS; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Set<Relationship> getRelationships() { |
| 73 | + return RELATIONSHIPS; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { |
| 78 | + final FlowFile flowFile = session.get(); |
| 79 | + if (flowFile == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + final String value = context.getProperty(VALUE_TO_CONSIDER).evaluateAttributeExpressions(flowFile).getValue(); |
| 84 | + final long currentValue; |
| 85 | + try { |
| 86 | + currentValue = Long.parseLong(value); |
| 87 | + } catch (final NumberFormatException e) { |
| 88 | + getLogger().error("Routing {} to failure because its Value to Consider [{}] is not a whole number", flowFile, value); |
| 89 | + session.transfer(flowFile, REL_FAILURE); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + final Long lastValue = previousValue; |
| 94 | + previousValue = currentValue; |
| 95 | + |
| 96 | + if (lastValue == null || currentValue == lastValue + 1) { |
| 97 | + session.transfer(flowFile, REL_SEQUENTIAL); |
| 98 | + } else { |
| 99 | + getLogger().warn("Routing {} to failure because its Value to Consider is {} but the previous FlowFile's value was {}", flowFile, currentValue, lastValue); |
| 100 | + session.transfer(flowFile, REL_FAILURE); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments