simonbence commented on code in PR #8377:
URL: https://github.com/apache/nifi/pull/8377#discussion_r1531531217


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/registry/flow/FlowAnalyzingRegistryClientNode.java:
##########
@@ -0,0 +1,452 @@
+/*
+ * 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.registry.flow;
+
+import org.apache.nifi.authorization.Resource;
+import org.apache.nifi.authorization.resource.Authorizable;
+import org.apache.nifi.bundle.BundleCoordinate;
+import org.apache.nifi.components.ConfigurableComponent;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.components.validation.ValidationState;
+import org.apache.nifi.components.validation.ValidationStatus;
+import org.apache.nifi.controller.LoggableComponent;
+import org.apache.nifi.controller.PropertyConfiguration;
+import org.apache.nifi.controller.TerminationAwareLogger;
+import org.apache.nifi.controller.flow.FlowManager;
+import org.apache.nifi.controller.flowanalysis.FlowAnalyzer;
+import org.apache.nifi.controller.service.ControllerServiceProvider;
+import org.apache.nifi.flow.ExternalControllerServiceReference;
+import org.apache.nifi.flow.ParameterProviderReference;
+import org.apache.nifi.flow.VersionedParameterContext;
+import org.apache.nifi.flow.VersionedProcessGroup;
+import org.apache.nifi.groups.ProcessGroup;
+import org.apache.nifi.parameter.ParameterContext;
+import org.apache.nifi.parameter.ParameterLookup;
+import org.apache.nifi.parameter.ParameterUpdate;
+import org.apache.nifi.registry.flow.mapping.InstantiatedVersionedProcessGroup;
+import org.apache.nifi.registry.flow.mapping.NiFiRegistryFlowMapper;
+import org.apache.nifi.validation.RuleViolation;
+import org.apache.nifi.validation.RuleViolationsManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+public final class FlowAnalyzingRegistryClientNode implements 
FlowRegistryClientNode {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(FlowAnalyzingRegistryClientNode.class);
+
+    private final FlowRegistryClientNode node;
+    private final ControllerServiceProvider serviceProvider;
+    private final FlowAnalyzer flowAnalyzer;
+    private final RuleViolationsManager ruleViolationsManager;
+    private final FlowManager flowManager;
+    private final Supplier<NiFiRegistryFlowMapper> flowMapperSupplier;
+
+    public FlowAnalyzingRegistryClientNode(
+            final FlowRegistryClientNode node,
+            final ControllerServiceProvider serviceProvider,
+            final FlowAnalyzer flowAnalyzer,
+            final RuleViolationsManager ruleViolationsManager,
+            final FlowManager flowManager,
+            final Supplier<NiFiRegistryFlowMapper> flowMapperSupplier
+    ) {
+        this.node = Objects.requireNonNull(node);
+        this.serviceProvider = Objects.requireNonNull(serviceProvider);
+        this.flowAnalyzer = Objects.requireNonNull(flowAnalyzer);
+        this.ruleViolationsManager = 
Objects.requireNonNull(ruleViolationsManager);
+        this.flowManager = Objects.requireNonNull(flowManager);
+        this.flowMapperSupplier = Objects.requireNonNull(flowMapperSupplier);
+    }
+
+    @Override
+    public RegisteredFlowSnapshot registerFlowSnapshot(
+            final FlowRegistryClientUserContext context,
+            final RegisteredFlow flow,
+            final VersionedProcessGroup snapshot,
+            final Map<String, ExternalControllerServiceReference> 
externalControllerServices,
+            final Map<String, VersionedParameterContext> parameterContexts,
+            final Map<String, ParameterProviderReference> 
parameterProviderReferences,
+            final String comments,
+            final int expectedVersion
+    ) throws FlowRegistryException, IOException {
+        if (LOGGER.isTraceEnabled()) {
+            LOGGER.trace("Snapshot for flow {} is checked for violations 
before commit", snapshot.getInstanceIdentifier());
+        }
+
+        if (analyzeProcessGroupToRegister(snapshot)) {
+            return node.registerFlowSnapshot(context, flow, snapshot, 
externalControllerServices, parameterContexts, parameterProviderReferences, 
comments, expectedVersion);
+        } else {
+            throw new ViolatingFlowException("Cannot store flow version to 
registry due to rules violations");
+        }
+    }
+
+    private boolean analyzeProcessGroupToRegister(final VersionedProcessGroup 
snapshot) {
+        final NiFiRegistryFlowMapper mapper = flowMapperSupplier.get();
+        final InstantiatedVersionedProcessGroup nonVersionedProcessGroup = 
mapper.mapNonVersionedProcessGroup(flowManager.getGroup(snapshot.getInstanceIdentifier()),
 serviceProvider);
+
+        flowAnalyzer.analyzeProcessGroup(nonVersionedProcessGroup);
+        final Collection<RuleViolation> ruleViolations = 
ruleViolationsManager.getRuleViolationsForGroup(snapshot.getInstanceIdentifier());

Review Comment:
   Good catch. What do you think, from rules and violation perspective, would 
it make sense to allow users to configure the level or violations which can 
prevents commit?



-- 
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