briansolo1985 commented on code in PR #8393: URL: https://github.com/apache/nifi/pull/8393#discussion_r1489093457
########## minifi/minifi-toolkit/minifi-toolkit-configuration/src/main/java/org/apache/nifi/minifi/toolkit/configuration/json/TransformCommandFactory.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.minifi.toolkit.configuration.json; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.nifi.controller.flow.VersionedDataflow; +import org.apache.nifi.minifi.toolkit.configuration.ConfigMain; +import org.apache.nifi.minifi.toolkit.configuration.ConfigTransformException; +import org.apache.nifi.minifi.toolkit.configuration.PathInputStreamFactory; +import org.apache.nifi.minifi.toolkit.configuration.PathOutputStreamFactory; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; + +public class TransformCommandFactory { + + public static final String TRANSFORM = "transform"; + + private static final String COMMAND_DESCRIPTION = "Transform NiFi flow JSON format into MiNifi flow JSON format"; + private static final String PROPERTY_KEY_VALUE_DELIMITER = "="; Review Comment: This member is not used, and can be safely removed. ########## minifi/minifi-toolkit/minifi-toolkit-configuration/src/main/java/org/apache/nifi/minifi/toolkit/configuration/json/TransformCommandFactory.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.minifi.toolkit.configuration.json; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.nifi.controller.flow.VersionedDataflow; +import org.apache.nifi.minifi.toolkit.configuration.ConfigMain; +import org.apache.nifi.minifi.toolkit.configuration.ConfigTransformException; +import org.apache.nifi.minifi.toolkit.configuration.PathInputStreamFactory; +import org.apache.nifi.minifi.toolkit.configuration.PathOutputStreamFactory; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; + +public class TransformCommandFactory { + + public static final String TRANSFORM = "transform"; + + private static final String COMMAND_DESCRIPTION = "Transform NiFi flow JSON format into MiNifi flow JSON format"; + private static final String PROPERTY_KEY_VALUE_DELIMITER = "="; + + private final PathInputStreamFactory pathInputStreamFactory; + private final PathOutputStreamFactory pathOutputStreamFactory; + + public TransformCommandFactory(PathInputStreamFactory pathInputStreamFactory, + PathOutputStreamFactory pathOutputStreamFactory) { + this.pathInputStreamFactory = pathInputStreamFactory; + this.pathOutputStreamFactory = pathOutputStreamFactory; + } + + public ConfigMain.Command create() { + return new ConfigMain.Command(this::transformToJson, COMMAND_DESCRIPTION); + } + + private int transformToJson(String[] args) { + if (args.length != 3) { + printTransformUsage(); + return ConfigMain.ERR_INVALID_ARGS; + } + + String sourceNiFiJsonPath = args[1]; + String targetMiNiFiJsonPath = args[2]; + + try { + RegisteredFlowSnapshot registeredFlowSnapshot = readNifiFlow(sourceNiFiJsonPath); + VersionedDataflow versionedDataflow = new VersionedDataflow(); + versionedDataflow.setRootGroup(registeredFlowSnapshot.getFlowContents()); + persistFlowJson(versionedDataflow, targetMiNiFiJsonPath); + } catch (ConfigTransformException e) { + System.out.println("Unable to convert NiFi JSON to MiNiFi flow JSON: " + e); + return e.getErrorCode(); + } + return ConfigMain.SUCCESS; + } + + private void printTransformUsage() { + System.out.println("Transform Usage:"); + System.out.println(); + System.out.println(" transform SOURCE_NIFI_JSON_FLOW_FILE TARGET_MINIFI_JSON_FLOW_FILE"); + System.out.println(); + } + + private RegisteredFlowSnapshot readNifiFlow(String sourceNiFiJsonPath) throws ConfigTransformException { + try (InputStream inputStream = pathInputStreamFactory.create(sourceNiFiJsonPath)) { + ObjectMapper objectMapper = createObjectMapper(); + final RegisteredFlowSnapshot registeredFlowSnapshot = objectMapper.readValue(inputStream, + RegisteredFlowSnapshot.class); + return registeredFlowSnapshot; + + } catch (IOException e) { + throw new ConfigTransformException("Error when reading NiFi flow json file", + ConfigMain.ERR_UNABLE_TO_OPEN_INPUT, e); + } + } + + private void persistFlowJson(VersionedDataflow flow, String flowJsonPath) throws ConfigTransformException { + try (OutputStream outputStream = pathOutputStreamFactory.create(flowJsonPath)) { + ObjectMapper objectMapper = createObjectMapper(); + objectMapper.writeValue(outputStream, flow); + } catch (IOException e) { + throw new ConfigTransformException("Error when persisting flow JSON file: " + flowJsonPath, + ConfigMain.ERR_UNABLE_TO_SAVE_CONFIG, e); + } + } + + private ObjectMapper createObjectMapper() { Review Comment: This is a duplication of TransformCommandFactory.createObjectMapper method. We should create a utility class with moving the this method there and making it static. Then we could use it from TransformCommandFactory and TransformYamlCommandFactory, avoiding code duplication. ########## minifi/minifi-toolkit/minifi-toolkit-configuration/src/main/java/org/apache/nifi/minifi/toolkit/configuration/json/TransformCommandFactory.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.minifi.toolkit.configuration.json; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.nifi.controller.flow.VersionedDataflow; +import org.apache.nifi.minifi.toolkit.configuration.ConfigMain; +import org.apache.nifi.minifi.toolkit.configuration.ConfigTransformException; +import org.apache.nifi.minifi.toolkit.configuration.PathInputStreamFactory; +import org.apache.nifi.minifi.toolkit.configuration.PathOutputStreamFactory; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; + +public class TransformCommandFactory { + + public static final String TRANSFORM = "transform"; + + private static final String COMMAND_DESCRIPTION = "Transform NiFi flow JSON format into MiNifi flow JSON format"; + private static final String PROPERTY_KEY_VALUE_DELIMITER = "="; + + private final PathInputStreamFactory pathInputStreamFactory; + private final PathOutputStreamFactory pathOutputStreamFactory; + + public TransformCommandFactory(PathInputStreamFactory pathInputStreamFactory, + PathOutputStreamFactory pathOutputStreamFactory) { + this.pathInputStreamFactory = pathInputStreamFactory; + this.pathOutputStreamFactory = pathOutputStreamFactory; + } + + public ConfigMain.Command create() { + return new ConfigMain.Command(this::transformToJson, COMMAND_DESCRIPTION); + } + + private int transformToJson(String[] args) { + if (args.length != 3) { + printTransformUsage(); + return ConfigMain.ERR_INVALID_ARGS; + } + + String sourceNiFiJsonPath = args[1]; + String targetMiNiFiJsonPath = args[2]; + + try { + RegisteredFlowSnapshot registeredFlowSnapshot = readNifiFlow(sourceNiFiJsonPath); + VersionedDataflow versionedDataflow = new VersionedDataflow(); + versionedDataflow.setRootGroup(registeredFlowSnapshot.getFlowContents()); + persistFlowJson(versionedDataflow, targetMiNiFiJsonPath); + } catch (ConfigTransformException e) { Review Comment: We should also copy the parameter contexts from the exported flow. Without this if an exported flow contains parameter referenes, then the transformed flow will fail as it won't find the related parameter context. ``` versionedDataflow.setParameterContexts(new ArrayList<>(registeredFlowSnapshot.getParameterContexts().values())); ``` ########## minifi/minifi-toolkit/minifi-toolkit-configuration/src/main/java/org/apache/nifi/minifi/toolkit/configuration/json/TransformCommandFactory.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.minifi.toolkit.configuration.json; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.nifi.controller.flow.VersionedDataflow; +import org.apache.nifi.minifi.toolkit.configuration.ConfigMain; +import org.apache.nifi.minifi.toolkit.configuration.ConfigTransformException; +import org.apache.nifi.minifi.toolkit.configuration.PathInputStreamFactory; +import org.apache.nifi.minifi.toolkit.configuration.PathOutputStreamFactory; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; + +public class TransformCommandFactory { + + public static final String TRANSFORM = "transform"; Review Comment: I was wondering whether it would worth to use a more meaningful name here, for example: transformNifiFlow If you agree, please rename the class as well ########## minifi/minifi-toolkit/minifi-toolkit-configuration/src/main/java/org/apache/nifi/minifi/toolkit/configuration/json/TransformCommandFactory.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.minifi.toolkit.configuration.json; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.nifi.controller.flow.VersionedDataflow; +import org.apache.nifi.minifi.toolkit.configuration.ConfigMain; +import org.apache.nifi.minifi.toolkit.configuration.ConfigTransformException; +import org.apache.nifi.minifi.toolkit.configuration.PathInputStreamFactory; +import org.apache.nifi.minifi.toolkit.configuration.PathOutputStreamFactory; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; + +public class TransformCommandFactory { + + public static final String TRANSFORM = "transform"; + + private static final String COMMAND_DESCRIPTION = "Transform NiFi flow JSON format into MiNifi flow JSON format"; + private static final String PROPERTY_KEY_VALUE_DELIMITER = "="; + + private final PathInputStreamFactory pathInputStreamFactory; + private final PathOutputStreamFactory pathOutputStreamFactory; + + public TransformCommandFactory(PathInputStreamFactory pathInputStreamFactory, + PathOutputStreamFactory pathOutputStreamFactory) { + this.pathInputStreamFactory = pathInputStreamFactory; + this.pathOutputStreamFactory = pathOutputStreamFactory; + } + + public ConfigMain.Command create() { + return new ConfigMain.Command(this::transformToJson, COMMAND_DESCRIPTION); + } + + private int transformToJson(String[] args) { + if (args.length != 3) { + printTransformUsage(); + return ConfigMain.ERR_INVALID_ARGS; + } + + String sourceNiFiJsonPath = args[1]; + String targetMiNiFiJsonPath = args[2]; + + try { + RegisteredFlowSnapshot registeredFlowSnapshot = readNifiFlow(sourceNiFiJsonPath); + VersionedDataflow versionedDataflow = new VersionedDataflow(); + versionedDataflow.setRootGroup(registeredFlowSnapshot.getFlowContents()); + persistFlowJson(versionedDataflow, targetMiNiFiJsonPath); + } catch (ConfigTransformException e) { + System.out.println("Unable to convert NiFi JSON to MiNiFi flow JSON: " + e); + return e.getErrorCode(); + } + return ConfigMain.SUCCESS; + } + + private void printTransformUsage() { + System.out.println("Transform Usage:"); + System.out.println(); + System.out.println(" transform SOURCE_NIFI_JSON_FLOW_FILE TARGET_MINIFI_JSON_FLOW_FILE"); + System.out.println(); + } + + private RegisteredFlowSnapshot readNifiFlow(String sourceNiFiJsonPath) throws ConfigTransformException { + try (InputStream inputStream = pathInputStreamFactory.create(sourceNiFiJsonPath)) { + ObjectMapper objectMapper = createObjectMapper(); + final RegisteredFlowSnapshot registeredFlowSnapshot = objectMapper.readValue(inputStream, + RegisteredFlowSnapshot.class); + return registeredFlowSnapshot; + Review Comment: The empty line could be removed ########## minifi/minifi-toolkit/minifi-toolkit-configuration/src/main/java/org/apache/nifi/minifi/toolkit/configuration/json/TransformCommandFactory.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.minifi.toolkit.configuration.json; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.nifi.controller.flow.VersionedDataflow; +import org.apache.nifi.minifi.toolkit.configuration.ConfigMain; +import org.apache.nifi.minifi.toolkit.configuration.ConfigTransformException; +import org.apache.nifi.minifi.toolkit.configuration.PathInputStreamFactory; +import org.apache.nifi.minifi.toolkit.configuration.PathOutputStreamFactory; +import org.apache.nifi.registry.flow.RegisteredFlowSnapshot; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; + +public class TransformCommandFactory { + + public static final String TRANSFORM = "transform"; + + private static final String COMMAND_DESCRIPTION = "Transform NiFi flow JSON format into MiNifi flow JSON format"; + private static final String PROPERTY_KEY_VALUE_DELIMITER = "="; + + private final PathInputStreamFactory pathInputStreamFactory; + private final PathOutputStreamFactory pathOutputStreamFactory; + + public TransformCommandFactory(PathInputStreamFactory pathInputStreamFactory, + PathOutputStreamFactory pathOutputStreamFactory) { + this.pathInputStreamFactory = pathInputStreamFactory; + this.pathOutputStreamFactory = pathOutputStreamFactory; + } + + public ConfigMain.Command create() { + return new ConfigMain.Command(this::transformToJson, COMMAND_DESCRIPTION); + } + + private int transformToJson(String[] args) { + if (args.length != 3) { + printTransformUsage(); + return ConfigMain.ERR_INVALID_ARGS; + } + + String sourceNiFiJsonPath = args[1]; + String targetMiNiFiJsonPath = args[2]; + + try { + RegisteredFlowSnapshot registeredFlowSnapshot = readNifiFlow(sourceNiFiJsonPath); + VersionedDataflow versionedDataflow = new VersionedDataflow(); + versionedDataflow.setRootGroup(registeredFlowSnapshot.getFlowContents()); + persistFlowJson(versionedDataflow, targetMiNiFiJsonPath); + } catch (ConfigTransformException e) { + System.out.println("Unable to convert NiFi JSON to MiNiFi flow JSON: " + e); + return e.getErrorCode(); + } + return ConfigMain.SUCCESS; + } + + private void printTransformUsage() { + System.out.println("Transform Usage:"); + System.out.println(); + System.out.println(" transform SOURCE_NIFI_JSON_FLOW_FILE TARGET_MINIFI_JSON_FLOW_FILE"); + System.out.println(); + } + + private RegisteredFlowSnapshot readNifiFlow(String sourceNiFiJsonPath) throws ConfigTransformException { + try (InputStream inputStream = pathInputStreamFactory.create(sourceNiFiJsonPath)) { + ObjectMapper objectMapper = createObjectMapper(); + final RegisteredFlowSnapshot registeredFlowSnapshot = objectMapper.readValue(inputStream, Review Comment: Please remove the final keyword, as it does not really have a purpose here. -- 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]
