From 76d127eec32b5315bf27366ccea931ffa980a3d5 Mon Sep 17 00:00:00 2001 From: yannick Date: Thu, 19 Sep 2024 14:30:46 +0200 Subject: [PATCH] improve fallback to legacy config when config cannot be parsed --- .../SimulationProtocolAdapterFactory.java | 35 +++++++++++++++--- .../etherip/EipProtocolAdapterFactory.java | 30 +++++++++++++-- .../file/FileProtocolAdapterFactory.java | 30 +++++++++++++-- .../http/HttpProtocolAdapterFactory.java | 23 +++++++++--- .../modbus/ModbusProtocolAdapterFactory.java | 37 +++++++++++++++---- .../opcua/OpcUaProtocolAdapterFactory.java | 30 +++++++++++++-- .../types/ads/ADSProtocolAdapterFactory.java | 31 +++++++++++++--- 7 files changed, 180 insertions(+), 36 deletions(-) diff --git a/hivemq-edge/src/main/java/com/hivemq/edge/modules/adapters/simulation/SimulationProtocolAdapterFactory.java b/hivemq-edge/src/main/java/com/hivemq/edge/modules/adapters/simulation/SimulationProtocolAdapterFactory.java index 1592ebe4e1..41ebf795be 100644 --- a/hivemq-edge/src/main/java/com/hivemq/edge/modules/adapters/simulation/SimulationProtocolAdapterFactory.java +++ b/hivemq-edge/src/main/java/com/hivemq/edge/modules/adapters/simulation/SimulationProtocolAdapterFactory.java @@ -24,7 +24,9 @@ import com.hivemq.edge.modules.adapters.simulation.config.SimulationToMqttConfig; import com.hivemq.edge.modules.adapters.simulation.config.SimulationToMqttMapping; import com.hivemq.edge.modules.adapters.simulation.config.legacy.LegacySimulationAdapterConfig; -import com.hivemq.extension.sdk.api.annotations.NotNull; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -32,6 +34,8 @@ public class SimulationProtocolAdapterFactory implements ProtocolAdapterFactory { + private static final @NotNull Logger log = LoggerFactory.getLogger(SimulationProtocolAdapterFactory.class); + @Override public @NotNull ProtocolAdapterInformation getInformation() { return SimulationProtocolAdapterInformation.INSTANCE; @@ -51,16 +55,35 @@ public class SimulationProtocolAdapterFactory implements ProtocolAdapterFactory< @Override public @NotNull SimulationAdapterConfig convertConfigObject( - final @NotNull ObjectMapper objectMapper, final @NotNull Map config) { - if (config.get("simulationToMqtt") != null || config.get("mqttToSimulation") != null) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { + try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); - } else { - return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception currentConfigFailedException) { + try { + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + SimulationProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } + return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + SimulationProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } + //we rethrow the exception from the current config conversation, to have a correct rest response. + throw currentConfigFailedException; + } } } private static @NotNull SimulationAdapterConfig tryConvertLegacyConfig( - final @NotNull ObjectMapper objectMapper, final @NotNull Map config) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { final LegacySimulationAdapterConfig legacySimulationAdapterConfig = objectMapper.convertValue(config, LegacySimulationAdapterConfig.class); diff --git a/modules/hivemq-edge-module-etherip/src/main/java/com/hivemq/edge/adapters/etherip/EipProtocolAdapterFactory.java b/modules/hivemq-edge-module-etherip/src/main/java/com/hivemq/edge/adapters/etherip/EipProtocolAdapterFactory.java index 2fbe82cf01..5e6413537d 100644 --- a/modules/hivemq-edge-module-etherip/src/main/java/com/hivemq/edge/adapters/etherip/EipProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-etherip/src/main/java/com/hivemq/edge/adapters/etherip/EipProtocolAdapterFactory.java @@ -25,6 +25,8 @@ import com.hivemq.edge.adapters.etherip.config.EipToMqttMapping; import com.hivemq.edge.adapters.etherip.config.legacy.LegacyEipAdapterConfig; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -32,6 +34,8 @@ public class EipProtocolAdapterFactory implements ProtocolAdapterFactory { + private static final @NotNull Logger log = LoggerFactory.getLogger(EipProtocolAdapterFactory.class); + @Override public @NotNull ProtocolAdapterInformation getInformation() { return EipProtocolAdapterInformation.INSTANCE; @@ -46,11 +50,29 @@ public class EipProtocolAdapterFactory implements ProtocolAdapterFactory config) { - if (config.get("eipToMqtt") != null || config.get("mqttToEip") != null) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { + try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); - } else { - return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception currentConfigFailedException) { + try { + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + EipProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } + return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + EipProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } + //we rethrow the exception from the current config conversation, to have a correct rest response. + throw currentConfigFailedException; + } } } diff --git a/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/FileProtocolAdapterFactory.java b/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/FileProtocolAdapterFactory.java index 3d762268a8..7c0235a263 100644 --- a/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/FileProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-file/src/main/java/com/hivemq/edge/adapters/file/FileProtocolAdapterFactory.java @@ -25,6 +25,8 @@ import com.hivemq.edge.adapters.file.config.FileToMqttMapping; import com.hivemq.edge.adapters.file.config.legacy.LegacyFileAdapterConfig; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -32,6 +34,8 @@ public class FileProtocolAdapterFactory implements ProtocolAdapterFactory { + private static final @NotNull Logger log = LoggerFactory.getLogger(FileProtocolAdapterFactory.class); + @Override public @NotNull ProtocolAdapterInformation getInformation() { return FileProtocolAdapterInformation.INSTANCE; @@ -46,11 +50,29 @@ public class FileProtocolAdapterFactory implements ProtocolAdapterFactory config) { - if (config.get("fileToMqtt") != null || config.get("mqttToFile") != null) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { + try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); - } else { - return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception currentConfigFailedException) { + try { + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + FileProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } + return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + FileProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } + //we rethrow the exception from the current config conversation, to have a correct rest response. + throw currentConfigFailedException; + } } } diff --git a/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java b/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java index 8c8b842c11..744b459dd3 100644 --- a/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-http/src/main/java/com/hivemq/edge/adapters/http/HttpProtocolAdapterFactory.java @@ -52,20 +52,30 @@ public class HttpProtocolAdapterFactory implements ProtocolAdapterFactory config) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); } catch (final Exception currentConfigFailedException) { try { - log.warn("Could not load http adapter configuration, trying to load legacy configuration."); + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + HttpProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } return tryConvertLegacyConfig(objectMapper, config); - } catch (final Exception ignored) { - log.warn("Could not load legacy http adapter configuration."); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + HttpProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } //we rethrow the exception from the current config conversation, to have a correct rest response. throw currentConfigFailedException; } } - } @Override @@ -74,7 +84,8 @@ public class HttpProtocolAdapterFactory implements ProtocolAdapterFactory config) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { final LegacyHttpAdapterConfig legacyHttpAdapterConfig = objectMapper.convertValue(config, LegacyHttpAdapterConfig.class); diff --git a/modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapterFactory.java b/modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapterFactory.java index fde990b8b7..95bebf7e57 100644 --- a/modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapterFactory.java @@ -18,14 +18,15 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.hivemq.adapter.sdk.api.ProtocolAdapter; import com.hivemq.adapter.sdk.api.ProtocolAdapterInformation; -import com.hivemq.adapter.sdk.api.config.MqttUserProperty; import com.hivemq.adapter.sdk.api.factories.ProtocolAdapterFactory; import com.hivemq.adapter.sdk.api.model.ProtocolAdapterInput; -import com.hivemq.edge.adapters.modbus.config.legacy.LegacyModbusAdapterConfig; import com.hivemq.edge.adapters.modbus.config.ModbusAdapterConfig; import com.hivemq.edge.adapters.modbus.config.ModbusToMqttConfig; import com.hivemq.edge.adapters.modbus.config.ModbusToMqttMapping; +import com.hivemq.edge.adapters.modbus.config.legacy.LegacyModbusAdapterConfig; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -33,6 +34,8 @@ public class ModbusProtocolAdapterFactory implements ProtocolAdapterFactory { + private static final @NotNull Logger log = LoggerFactory.getLogger(ModbusProtocolAdapterFactory.class); + @Override public @NotNull ProtocolAdapterInformation getInformation() { return ModbusProtocolAdapterInformation.INSTANCE; @@ -52,15 +55,35 @@ public class ModbusProtocolAdapterFactory implements ProtocolAdapterFactory config) { - if(config.get("modbusToMqtt") != null || config.get("mqttToModbus") != null) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { + try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); - } else { - return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception currentConfigFailedException) { + try { + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + ModbusProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } + return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + ModbusProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } + //we rethrow the exception from the current config conversation, to have a correct rest response. + throw currentConfigFailedException; + } } } - private static @NotNull ModbusAdapterConfig tryConvertLegacyConfig(final @NotNull ObjectMapper objectMapper, final @NotNull Map config) { + private static @NotNull ModbusAdapterConfig tryConvertLegacyConfig( + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { final LegacyModbusAdapterConfig legacyModbusAdapterConfig = objectMapper.convertValue(config, LegacyModbusAdapterConfig.class); diff --git a/modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/OpcUaProtocolAdapterFactory.java b/modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/OpcUaProtocolAdapterFactory.java index dc59809a5b..fef70fee16 100644 --- a/modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/OpcUaProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-opcua/src/main/java/com/hivemq/edge/adapters/opcua/OpcUaProtocolAdapterFactory.java @@ -25,6 +25,8 @@ import com.hivemq.edge.adapters.opcua.config.OpcUaToMqttMapping; import com.hivemq.edge.adapters.opcua.config.legacy.LegacyOpcUaAdapterConfig; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -32,6 +34,8 @@ public class OpcUaProtocolAdapterFactory implements ProtocolAdapterFactory { + private static final @NotNull Logger log = LoggerFactory.getLogger(OpcUaProtocolAdapterFactory.class); + @Override public @NotNull ProtocolAdapterInformation getInformation() { return OpcUaProtocolAdapterInformation.INSTANCE; @@ -51,11 +55,29 @@ public class OpcUaProtocolAdapterFactory implements ProtocolAdapterFactory config) { - if (config.get("opcuaToMqtt") != null || config.get("mqttToOpcua") != null) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { + try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); - } else { - return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception currentConfigFailedException) { + try { + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + OpcUaProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } + return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + OpcUaProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } + //we rethrow the exception from the current config conversation, to have a correct rest response. + throw currentConfigFailedException; + } } } diff --git a/modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/ads/ADSProtocolAdapterFactory.java b/modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/ads/ADSProtocolAdapterFactory.java index 520f6538df..edc62556f7 100644 --- a/modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/ads/ADSProtocolAdapterFactory.java +++ b/modules/hivemq-edge-module-plc4x/src/main/java/com/hivemq/edge/adapters/plc4x/types/ads/ADSProtocolAdapterFactory.java @@ -18,7 +18,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.hivemq.adapter.sdk.api.ProtocolAdapter; import com.hivemq.adapter.sdk.api.ProtocolAdapterInformation; -import com.hivemq.adapter.sdk.api.config.MqttUserProperty; import com.hivemq.adapter.sdk.api.factories.ProtocolAdapterFactory; import com.hivemq.adapter.sdk.api.model.ProtocolAdapterInput; import com.hivemq.edge.adapters.plc4x.config.Plc4xToMqttMapping; @@ -26,6 +25,8 @@ import com.hivemq.edge.adapters.plc4x.types.ads.config.ADSToMqttConfig; import com.hivemq.edge.adapters.plc4x.types.ads.config.legacy.LegacyADSAdapterConfig; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -36,6 +37,8 @@ */ public class ADSProtocolAdapterFactory implements ProtocolAdapterFactory { + private static final @NotNull Logger log = LoggerFactory.getLogger(ADSProtocolAdapterFactory.class); + @Override public @NotNull ProtocolAdapterInformation getInformation() { return ADSProtocolAdapterInformation.INSTANCE; @@ -55,11 +58,29 @@ public class ADSProtocolAdapterFactory implements ProtocolAdapterFactory config) { - if (config.get("adsToMqtt") != null || config.get("mqttToAds") != null) { + final @NotNull ObjectMapper objectMapper, + final @NotNull Map config) { + try { return ProtocolAdapterFactory.super.convertConfigObject(objectMapper, config); - } else { - return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception currentConfigFailedException) { + try { + log.warn("Could not load '{}' configuration, trying to load legacy configuration. Because: '{}'", + ADSProtocolAdapterInformation.INSTANCE.getDisplayName(), + currentConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", currentConfigFailedException); + } + return tryConvertLegacyConfig(objectMapper, config); + } catch (final Exception legacyConfigFailedException) { + log.warn("Could not load legacy '{}' configuration. Because: '{}'", + ADSProtocolAdapterInformation.INSTANCE.getDisplayName(), + legacyConfigFailedException.getMessage()); + if (log.isDebugEnabled()) { + log.debug("Original Exception:", legacyConfigFailedException); + } + //we rethrow the exception from the current config conversation, to have a correct rest response. + throw currentConfigFailedException; + } } }