You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by jo...@apache.org on 2020/09/02 17:59:19 UTC

[nifi] 09/16: NIFI-7661 Change validation of controller service against it's API to use comparison of inteface methods instead of bundle coordinates

This is an automated email from the ASF dual-hosted git repository.

joewitt pushed a commit to branch support/nifi-1.12.x
in repository https://gitbox.apache.org/repos/asf/nifi.git

commit f4bce4fead6adf78a8caa00bfcc2b5f71a26cfd0
Author: Bryan Bende <bb...@apache.org>
AuthorDate: Fri Jul 17 16:19:02 2020 -0400

    NIFI-7661 Change validation of controller service against it's API to use comparison of inteface methods instead of bundle coordinates
---
 .../nifi/controller/AbstractComponentNode.java     |  33 ++-
 .../controller/ControllerServiceApiMatcher.java    |  73 ++++++
 .../TestControllerServiceApiMatcher.java           | 283 +++++++++++++++++++++
 3 files changed, 377 insertions(+), 12 deletions(-)

diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractComponentNode.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractComponentNode.java
index a7db9ec..0643418 100644
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractComponentNode.java
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractComponentNode.java
@@ -727,6 +727,7 @@ public abstract class AbstractComponentNode implements ComponentNode {
         if (controllerServiceApiClass.equals(ControllerService.class)) {
             return null;
         }
+
         final ClassLoader controllerServiceApiClassLoader = controllerServiceApiClass.getClassLoader();
         final ExtensionManager extensionManager = serviceProvider.getExtensionManager();
 
@@ -745,20 +746,28 @@ public abstract class AbstractComponentNode implements ComponentNode {
         }
         final BundleCoordinate controllerServiceCoordinate = controllerServiceBundle.getBundleDetails().getCoordinate();
 
-        final boolean matchesApi = matchesApi(extensionManager, controllerServiceBundle, controllerServiceApiCoordinate);
+        final boolean matchesApiByBundleCoordinates = matchesApiBundleCoordinates(extensionManager, controllerServiceBundle, controllerServiceApiCoordinate);
+        if (!matchesApiByBundleCoordinates) {
+            final Class<? extends ControllerService> controllerServiceImplClass = controllerServiceNode.getControllerServiceImplementation().getClass();
+            logger.debug("Comparing methods from service api '{}' against service implementation '{}'",
+                    new Object[]{controllerServiceApiClass.getCanonicalName(), controllerServiceImplClass.getCanonicalName()});
+
+            final ControllerServiceApiMatcher controllerServiceApiMatcher = new ControllerServiceApiMatcher();
+            final boolean matchesApi = controllerServiceApiMatcher.matches(controllerServiceApiClass, controllerServiceImplClass);
 
-        if (!matchesApi) {
-            final String controllerServiceType = controllerServiceNode.getComponentType();
-            final String controllerServiceApiType = controllerServiceApiClass.getSimpleName();
+            if (!matchesApi) {
+                final String controllerServiceType = controllerServiceNode.getComponentType();
+                final String controllerServiceApiType = controllerServiceApiClass.getSimpleName();
 
-            final String explanation = new StringBuilder()
-                .append(controllerServiceType).append(" - ").append(controllerServiceCoordinate.getVersion())
-                .append(" from ").append(controllerServiceCoordinate.getGroup()).append(" - ").append(controllerServiceCoordinate.getId())
-                .append(" is not compatible with ").append(controllerServiceApiType).append(" - ").append(controllerServiceApiCoordinate.getVersion())
-                .append(" from ").append(controllerServiceApiCoordinate.getGroup()).append(" - ").append(controllerServiceApiCoordinate.getId())
-                .toString();
+                final String explanation = new StringBuilder()
+                        .append(controllerServiceType).append(" - ").append(controllerServiceCoordinate.getVersion())
+                        .append(" from ").append(controllerServiceCoordinate.getGroup()).append(" - ").append(controllerServiceCoordinate.getId())
+                        .append(" is not compatible with ").append(controllerServiceApiType).append(" - ").append(controllerServiceApiCoordinate.getVersion())
+                        .append(" from ").append(controllerServiceApiCoordinate.getGroup()).append(" - ").append(controllerServiceApiCoordinate.getId())
+                        .toString();
 
-            return createInvalidResult(serviceId, propertyName, explanation);
+                return createInvalidResult(serviceId, propertyName, explanation);
+            }
         }
 
         return null;
@@ -780,7 +789,7 @@ public abstract class AbstractComponentNode implements ComponentNode {
      * @param requiredApiCoordinate the controller service API required by the processor
      * @return true if the controller service node has the require API as an ancestor, false otherwise
      */
-    private boolean matchesApi(final ExtensionManager extensionManager, final Bundle controllerServiceImplBundle, final BundleCoordinate requiredApiCoordinate) {
+    private boolean matchesApiBundleCoordinates(final ExtensionManager extensionManager, final Bundle controllerServiceImplBundle, final BundleCoordinate requiredApiCoordinate) {
         // start with the coordinate of the controller service for cases where the API and service are in the same bundle
         BundleCoordinate controllerServiceDependencyCoordinate = controllerServiceImplBundle.getBundleDetails().getCoordinate();
 
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ControllerServiceApiMatcher.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ControllerServiceApiMatcher.java
new file mode 100644
index 0000000..5183617
--- /dev/null
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ControllerServiceApiMatcher.java
@@ -0,0 +1,73 @@
+/*
+ * 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.controller;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+
+public class ControllerServiceApiMatcher {
+
+    private static Logger LOGGER = LoggerFactory.getLogger(ControllerServiceApiMatcher.class);
+
+    /**
+     * Determines if all of the methods from the API class are present in the implementation class.
+     *
+     * @param serviceApi the controller service API class
+     * @param serviceImplementation the controller service implementation class
+     * @return true if all API methods exists in the implementation with the same name, parameters, and return type
+     */
+    public boolean matches(Class<? extends ControllerService> serviceApi, Class<? extends ControllerService> serviceImplementation) {
+        for (final Method apiMethod : serviceApi.getMethods()) {
+            boolean foundMatchingImplMethod = false;
+            for (final Method implMethod : serviceImplementation.getMethods()) {
+                if (!apiMethod.getName().equals(implMethod.getName())) {
+                    continue;
+                }
+
+                // if the service interface has new methods that the implementation doesn't implement,
+                // those methods still show up list of methods when calling impl getMethods(), but they
+                // are marked as abstract and will produce an AbstractMethodError at runtime if invoked
+                if (Modifier.isAbstract(implMethod.getModifiers())) {
+                    continue;
+                }
+
+                final boolean returnTypeMatches = apiMethod.getReturnType().equals(implMethod.getReturnType());
+                final boolean argsMatch = Arrays.equals(apiMethod.getParameterTypes(), implMethod.getParameterTypes());
+
+                if (returnTypeMatches && argsMatch) {
+                    foundMatchingImplMethod = true;
+                    break;
+                }
+            }
+
+            if (!foundMatchingImplMethod) {
+                if (LOGGER.isDebugEnabled()) {
+                    LOGGER.debug("{} does not implement the API method [{}] from {}",
+                            new Object[]{serviceImplementation.getCanonicalName(), apiMethod.toString(), serviceApi.getCanonicalName()});
+                }
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+}
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/test/java/org/apache/nifi/controller/TestControllerServiceApiMatcher.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/test/java/org/apache/nifi/controller/TestControllerServiceApiMatcher.java
new file mode 100644
index 0000000..4bc1849
--- /dev/null
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/test/java/org/apache/nifi/controller/TestControllerServiceApiMatcher.java
@@ -0,0 +1,283 @@
+/*
+ * 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.controller;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.reporting.InitializationException;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.List;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class TestControllerServiceApiMatcher {
+
+    private ControllerServiceApiMatcher serviceApiMatcher;
+
+    @Before
+    public void setup() {
+        this.serviceApiMatcher = new ControllerServiceApiMatcher();
+    }
+
+    @Test
+    public void testServiceImplementationMatchesRealServiceApi() {
+        assertTrue(serviceApiMatcher.matches(FooServiceApiV1.class, FooServiceImplementationV1.class));
+    }
+
+    @Test
+    public void testServiceImplementationMatchesCompatibleServiceApi() {
+        // v2 implementation should match the real v2 API that it implements
+        assertTrue(serviceApiMatcher.matches(FooServiceApiV2.class, FooServiceImplementationV2.class));
+        // v2 implementation should also match the v1 API since the methods are the same
+        assertTrue(serviceApiMatcher.matches(FooServiceApiV1.class, FooServiceImplementationV2.class));
+    }
+
+    @Test
+    public void testServiceImplementationDoesNotMatchIncompatibleApi() {
+        assertFalse(serviceApiMatcher.matches(FooServiceApiV3.class, FooServiceImplementationV1.class));
+        assertFalse(serviceApiMatcher.matches(FooServiceApiV3.class, FooServiceImplementationV2.class));
+    }
+
+    @Test
+    public void testServiceApiWithGenerics() {
+        // should match
+        assertTrue(serviceApiMatcher.matches(GenericServiceApiV1.class, GenericServiceImpl.class));
+        // should not match because method changed args
+        assertFalse(serviceApiMatcher.matches(GenericServiceApiV2.class, GenericServiceImpl.class));
+        // should not match because method changed return type
+        assertFalse(serviceApiMatcher.matches(GenericServiceApiV3.class, GenericServiceImpl.class));
+    }
+
+    // Interface for a result
+    private interface FooResult {
+        String getResult();
+    }
+
+    // Implementation for a result
+    private class FooResultImpl implements FooResult {
+        private final String result;
+
+        public FooResultImpl(final String result) {
+            this.result = result;
+        }
+
+        @Override
+        public String getResult() {
+            return result;
+        }
+    }
+
+    // Interface for an argument
+    private interface FooArg {
+        String getArg();
+    }
+
+    // Implementation for an argument
+    private static class FooArgImpl implements FooArg {
+        private final String arg;
+
+        public FooArgImpl(final String arg) {
+            this.arg = arg;
+        }
+
+        @Override
+        public String getArg() {
+            return arg;
+        }
+    }
+
+    // Service API v1
+    private interface FooServiceApiV1 extends ControllerService {
+        void execute();
+        String execute(String a);
+        FooResult executeWithReturn(FooArg a);
+    }
+
+    // Service API v2, unchanged from v1
+    private interface FooServiceApiV2 extends ControllerService {
+        void execute();
+        String execute(String a);
+        FooResult executeWithReturn(FooArg a);
+    }
+
+    // Service API v3, new method added since v1/v3
+    private interface FooServiceApiV3 extends ControllerService {
+        void execute();
+        String execute(String a);
+        FooResult executeWithReturn(FooArg a);
+        String someNewMethod();
+    }
+
+    // Service implementing v1 API
+    private static class FooServiceImplementationV1 implements FooServiceApiV1 {
+
+        @Override
+        public void execute() {
+
+        }
+
+        @Override
+        public String execute(String a) {
+            return null;
+        }
+
+        // Declare the impl result here to test comparing a more specific return type against the API
+        @Override
+        public FooResultImpl executeWithReturn(FooArg a) {
+            return null;
+        }
+
+        @Override
+        public void initialize(ControllerServiceInitializationContext context) throws InitializationException {
+
+        }
+
+        @Override
+        public Collection<ValidationResult> validate(ValidationContext context) {
+            return null;
+        }
+
+        @Override
+        public PropertyDescriptor getPropertyDescriptor(String name) {
+            return null;
+        }
+
+        @Override
+        public void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) {
+
+        }
+
+        @Override
+        public List<PropertyDescriptor> getPropertyDescriptors() {
+            return null;
+        }
+
+        @Override
+        public String getIdentifier() {
+            return null;
+        }
+    }
+
+    // Service implementing v2 API
+    private static class FooServiceImplementationV2 implements FooServiceApiV2 {
+
+        @Override
+        public void execute() {
+
+        }
+
+        @Override
+        public String execute(String a) {
+            return null;
+        }
+
+        @Override
+        public FooResult executeWithReturn(FooArg a) {
+            return null;
+        }
+
+        @Override
+        public void initialize(ControllerServiceInitializationContext context) throws InitializationException {
+
+        }
+
+        @Override
+        public Collection<ValidationResult> validate(ValidationContext context) {
+            return null;
+        }
+
+        @Override
+        public PropertyDescriptor getPropertyDescriptor(String name) {
+            return null;
+        }
+
+        @Override
+        public void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) {
+
+        }
+
+        @Override
+        public List<PropertyDescriptor> getPropertyDescriptors() {
+            return null;
+        }
+
+        @Override
+        public String getIdentifier() {
+            return null;
+        }
+    }
+
+    // Service API with generics v1
+    private interface GenericServiceApiV1<T extends FooResult, V extends FooArg> extends ControllerService {
+        T execute(V arg);
+    }
+
+    // Service API with generics v2
+    private interface GenericServiceApiV2<T extends FooResult, V extends FooArg> extends ControllerService {
+        T execute(V arg1, V arg2);
+    }
+
+    // Service API with generics v3
+    private interface GenericServiceApiV3<V extends FooArg> extends ControllerService {
+        String execute(V arg);
+    }
+
+    // Service implementation with generics
+    private static class GenericServiceImpl implements GenericServiceApiV1<FooResultImpl,FooArgImpl> {
+
+        @Override
+        public FooResultImpl execute(FooArgImpl arg) {
+            return null;
+        }
+
+        @Override
+        public void initialize(ControllerServiceInitializationContext context) throws InitializationException {
+
+        }
+
+        @Override
+        public Collection<ValidationResult> validate(ValidationContext context) {
+            return null;
+        }
+
+        @Override
+        public PropertyDescriptor getPropertyDescriptor(String name) {
+            return null;
+        }
+
+        @Override
+        public void onPropertyModified(PropertyDescriptor descriptor, String oldValue, String newValue) {
+
+        }
+
+        @Override
+        public List<PropertyDescriptor> getPropertyDescriptors() {
+            return null;
+        }
+
+        @Override
+        public String getIdentifier() {
+            return null;
+        }
+    }
+
+}