markobean commented on code in PR #9685:
URL: https://github.com/apache/nifi/pull/9685#discussion_r1941803790


##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-rules/src/main/java/org/apache/nifi/flowanalysis/rules/RestrictLoadBalancing.java:
##########
@@ -0,0 +1,266 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.flowanalysis.rules;
+
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.controller.queue.LoadBalanceCompression;
+import org.apache.nifi.controller.queue.LoadBalanceStrategy;
+import org.apache.nifi.flow.VersionedProcessGroup;
+import org.apache.nifi.flowanalysis.AbstractFlowAnalysisRule;
+import org.apache.nifi.flowanalysis.FlowAnalysisRuleContext;
+import org.apache.nifi.flowanalysis.GroupAnalysisResult;
+import org.apache.nifi.flowanalysis.rules.util.ConnectionViolation;
+import org.apache.nifi.flowanalysis.rules.util.FlowAnalysisRuleUtils;
+import org.apache.nifi.flowanalysis.rules.util.ViolationType;
+import org.apache.nifi.processor.util.StandardValidators;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+@Tags({"connection", "load", "balance", "strategy", "compression"})
+@CapabilityDescription("This rule generates a violation when the Load Balance 
Strategy setting of a connection is set to a strategy which is not allowed. "
+        + "This rule may be useful for preventing certain strategy types or 
load-balancing compression. It may be implemented to assist in locating "
+        + "all connections which have load balancing enabled.")
+public class RestrictLoadBalancing extends AbstractFlowAnalysisRule {
+
+    static final String CONFIGURE_STRATEGY_ERROR_MESSAGE = "at least one load 
balance strategy property must be set to 'true'";
+    static final String COMPRESSION_CONFIGURATION_ERROR_MESSAGE =
+            "there is no compression configuration for content only without 
including attributes. If content compression is allowed, attribute compression 
must also be allowed.";
+
+    public static final PropertyDescriptor ALLOW_DO_NOT_LOAD_BALANCE = new 
PropertyDescriptor.Builder()
+            .name("Allow 'Do not load balance' Strategy")
+            .description("If set to true, connections may be configured with 
the load balancing strategy of 'Do not load balance'. This is the default "
+                    + "setting for new connections.")
+            .required(true)
+            .allowableValues("true", "false")
+            .defaultValue("true")
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .build();
+
+    public static final PropertyDescriptor ALLOW_PARTITION = new 
PropertyDescriptor.Builder()
+            .name("Allow 'Partition by attribute' Strategy")
+            .description("If set to true, connections may be configured with 
the load balancing strategy of 'Partition by attribute'.")
+            .required(true)
+            .allowableValues("true", "false")
+            .defaultValue("false")
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .build();
+
+    public static final PropertyDescriptor ALLOW_ROUND_ROBIN = new 
PropertyDescriptor.Builder()
+            .name("Allow 'Round robin' Strategy")
+            .description("If set to true, connections may be configured with 
the load balancing strategy of 'Round robin'.")
+            .required(true)
+            .allowableValues("true", "false")
+            .defaultValue("false")
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .build();
+
+    public static final PropertyDescriptor ALLOW_SINGLE_NODE = new 
PropertyDescriptor.Builder()
+            .name("Allow 'Single node' Strategy")
+            .description("If set to true, connections may be configured with 
the load balancing strategy of 'Single node'.")
+            .required(true)
+            .allowableValues("true", "false")
+            .defaultValue("false")
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .build();
+
+    public static final PropertyDescriptor ALLOW_ATTRIBUTE_COMPRESSION = new 
PropertyDescriptor.Builder()
+            .name("Allow Attribute Compression")
+            .description("If set to true, connections which are configured to 
load balance may compress attributes. "
+                    + "This property is ignored if the only allowed Load 
Balance Strategy is 'Do not load balance'.")
+            .required(false)
+            .allowableValues("true", "false")
+            .defaultValue("true")
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .build();
+    public static final PropertyDescriptor ALLOW_CONTENT_COMPRESSION = new 
PropertyDescriptor.Builder()
+            .name("Allow Content Compression")
+            .description("If set to true, connections which are configured to 
load balance may compress attributes and content. "
+                    + "This property is ignored if the only allowed Load 
Balance Strategy is 'Do not load balance'. "
+                    + "When set to true, 'Allow Attribute Compression' must 
also be set to true")
+            .required(false)
+            .allowableValues("true", "false")
+            .defaultValue("true")
+            .addValidator(StandardValidators.BOOLEAN_VALIDATOR)
+            .build();
+
+    private static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = 
List.of(
+            ALLOW_DO_NOT_LOAD_BALANCE,
+            ALLOW_PARTITION,
+            ALLOW_ROUND_ROBIN,
+            ALLOW_SINGLE_NODE,
+            ALLOW_ATTRIBUTE_COMPRESSION,
+            ALLOW_CONTENT_COMPRESSION
+    );
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        return PROPERTY_DESCRIPTORS;
+    }
+
+    @Override
+    protected Collection<ValidationResult> customValidate(ValidationContext 
validationContext) {
+        final List<ValidationResult> results = new ArrayList<>();
+
+        // For this flow analysis rule to be valid, at least one load 
balancing strategy must be allowed
+        if 
(!validationContext.getProperty(ALLOW_DO_NOT_LOAD_BALANCE).asBoolean()
+                && !isLoadBalancingAllowed(validationContext)) {
+            results.add(new ValidationResult.Builder()
+                    .subject("Load Balance Strategy Properties")
+                    .valid(false)
+                    .explanation(CONFIGURE_STRATEGY_ERROR_MESSAGE)
+                    .build());
+        }
+
+        // The only forms of compression are "attributes only" and "both 
attributes and content".
+        // Therefore, if content compression is allowed, attribute compression 
must also be allowed.
+        // It is also valid to have only attribute compression or no 
compression at all allowed.
+        // In addition, this is only relevant if a load balance strategy is 
allowed, i.e. if the only allowed strategy is "Do not load balance",
+        // then any combination of compression is allowed because it will be 
ignored.
+        if (isLoadBalancingAllowed(validationContext)
+                && 
validationContext.getProperty(ALLOW_CONTENT_COMPRESSION).isSet() && 
validationContext.getProperty(ALLOW_CONTENT_COMPRESSION).asBoolean()
+                && 
validationContext.getProperty(ALLOW_ATTRIBUTE_COMPRESSION).isSet() && 
!validationContext.getProperty(ALLOW_ATTRIBUTE_COMPRESSION).asBoolean()) {

Review Comment:
   Good catch. Removed the `isSet()` check.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to