mcgilman commented on code in PR #10756:
URL: https://github.com/apache/nifi/pull/10756#discussion_r2708776799


##########
nifi-commons/nifi-connector-utils/src/main/java/org/apache/nifi/components/connector/util/VersionedFlowUtils.java:
##########
@@ -363,4 +368,70 @@ private static void 
removeUnreferencedControllerServices(final VersionedProcessG
             removeUnreferencedControllerServices(childGroup, 
referencedServiceIds);
         }
     }
+
+    public static void updateToLatestBundles(final VersionedProcessGroup 
processGroup, final ComponentBundleLookup componentBundleLookup) {

Review Comment:
   Can we add some test coverage to these new `updateToLatest` methods?



##########
nifi-commons/nifi-connector-utils/src/main/java/org/apache/nifi/components/connector/util/VersionedFlowUtils.java:
##########
@@ -363,4 +368,70 @@ private static void 
removeUnreferencedControllerServices(final VersionedProcessG
             removeUnreferencedControllerServices(childGroup, 
referencedServiceIds);
         }
     }
+
+    public static void updateToLatestBundles(final VersionedProcessGroup 
processGroup, final ComponentBundleLookup componentBundleLookup) {
+        for (final VersionedProcessor processor : 
processGroup.getProcessors()) {
+            updateToLatestBundle(processor, componentBundleLookup);
+        }
+
+        for (final VersionedControllerService service : 
processGroup.getControllerServices()) {
+            updateToLatestBundle(service, componentBundleLookup);
+        }
+
+        for (final VersionedProcessGroup childGroup : 
processGroup.getProcessGroups()) {
+            updateToLatestBundles(childGroup, componentBundleLookup);
+        }
+    }
+
+    public static void updateToLatestBundle(final VersionedProcessor 
processor, final ComponentBundleLookup componentBundleLookup) {
+        final Bundle latest = getLatestBundle(processor, 
componentBundleLookup);
+        processor.setBundle(latest);
+    }
+
+    public static void updateToLatestBundle(final VersionedControllerService 
service, final ComponentBundleLookup componentBundleLookup) {
+        final Bundle latest = getLatestBundle(service, componentBundleLookup);
+        service.setBundle(latest);
+    }
+
+    private static Bundle getLatestBundle(final VersionedConfigurableExtension 
versionedComponent, final ComponentBundleLookup componentBundleLookup) {
+        final String type = versionedComponent.getType();
+        final List<Bundle> bundles = 
componentBundleLookup.getAvailableBundles(type);
+        final List<Bundle> includingExisting = new ArrayList<>(bundles);
+        if (versionedComponent.getBundle() != null && 
!includingExisting.contains(versionedComponent.getBundle())) {
+            includingExisting.add(versionedComponent.getBundle());
+        }
+
+        return getLatest(includingExisting);
+    }
+
+    public static Bundle getLatest(final List<Bundle> bundles) {
+        Bundle latest = null;
+        for (final Bundle bundle : bundles) {
+            if (latest == null || compareVersion(bundle.getVersion(), 
latest.getVersion()) > 0) {
+                latest = bundle;
+            }
+        }
+
+        return latest;
+    }
+
+    private static int compareVersion(final String v1, final String v2) {

Review Comment:
   The logic in this method doesn't work for versions with characters and 
trailing numbers. For instance, both `2.0.0-M1` and `2.0.0-RC1` are considered 
greater than `2.0.0`. Maybe this isn't something we're trying to tackle but 
just thought I'd raise it.
   



##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/components/connector/StandardConnectorInitializationContext.java:
##########
@@ -121,6 +128,12 @@ public Builder assetManager(final AssetManager 
assetManager) {
             return this;
         }
 
+        @Override
+        public Builder componentBundleLookup(final ComponentBundleLookup 
bundleLookup) {

Review Comment:
   I don't see this being called any place yet. A Connector calling 
`getComponentBundleLookup` would get `null`.



##########
nifi-commons/nifi-connector-utils/src/main/java/org/apache/nifi/components/connector/util/VersionedFlowUtils.java:
##########
@@ -363,4 +368,70 @@ private static void 
removeUnreferencedControllerServices(final VersionedProcessG
             removeUnreferencedControllerServices(childGroup, 
referencedServiceIds);
         }
     }
+
+    public static void updateToLatestBundles(final VersionedProcessGroup 
processGroup, final ComponentBundleLookup componentBundleLookup) {
+        for (final VersionedProcessor processor : 
processGroup.getProcessors()) {
+            updateToLatestBundle(processor, componentBundleLookup);
+        }
+
+        for (final VersionedControllerService service : 
processGroup.getControllerServices()) {
+            updateToLatestBundle(service, componentBundleLookup);
+        }
+
+        for (final VersionedProcessGroup childGroup : 
processGroup.getProcessGroups()) {
+            updateToLatestBundles(childGroup, componentBundleLookup);
+        }
+    }
+
+    public static void updateToLatestBundle(final VersionedProcessor 
processor, final ComponentBundleLookup componentBundleLookup) {
+        final Bundle latest = getLatestBundle(processor, 
componentBundleLookup);
+        processor.setBundle(latest);
+    }
+
+    public static void updateToLatestBundle(final VersionedControllerService 
service, final ComponentBundleLookup componentBundleLookup) {
+        final Bundle latest = getLatestBundle(service, componentBundleLookup);
+        service.setBundle(latest);
+    }
+
+    private static Bundle getLatestBundle(final VersionedConfigurableExtension 
versionedComponent, final ComponentBundleLookup componentBundleLookup) {
+        final String type = versionedComponent.getType();
+        final List<Bundle> bundles = 
componentBundleLookup.getAvailableBundles(type);
+        final List<Bundle> includingExisting = new ArrayList<>(bundles);
+        if (versionedComponent.getBundle() != null && 
!includingExisting.contains(versionedComponent.getBundle())) {
+            includingExisting.add(versionedComponent.getBundle());
+        }
+
+        return getLatest(includingExisting);
+    }
+
+    public static Bundle getLatest(final List<Bundle> bundles) {
+        Bundle latest = null;
+        for (final Bundle bundle : bundles) {
+            if (latest == null || compareVersion(bundle.getVersion(), 
latest.getVersion()) > 0) {
+                latest = bundle;
+            }
+        }
+
+        return latest;
+    }
+
+    private static int compareVersion(final String v1, final String v2) {
+        final String[] parts1 = VERSION_SEPARATOR_PATTERN.split(v1);
+        final String[] parts2 = VERSION_SEPARATOR_PATTERN.split(v2);
+
+        final int length = Math.max(parts1.length, parts2.length);
+        for (int i = 0; i < length; i++) {
+            final int part1 = i < parts1.length ? Integer.parseInt(parts1[i]) 
: 0;
+            final int part2 = i < parts2.length ? Integer.parseInt(parts2[i]) 
: 0;

Review Comment:
   Do we need any additional validation to avoid `NumberFormatException`?



##########
nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/components/connector/StandardConnectorInitializationContext.java:
##########
@@ -90,6 +96,7 @@ public static class Builder implements 
FrameworkConnectorInitializationContextBu
         private ComponentLog componentLog;
         private SecretsManager secretsManager;
         private AssetManager assetManager;
+        private ComponentBundleLookup componentBundleLookup;

Review Comment:
   `updateParameterContext` above is private and unused.



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