refactor: remove mqtt legacy endpoint fallback
This commit is contained in:
@@ -18,9 +18,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
@@ -70,9 +68,7 @@ public class MqttInboundAutoConfiguration {
|
|||||||
public MqttEndpointManager mqttEndpointManager(MqttInboundProperties props,
|
public MqttEndpointManager mqttEndpointManager(MqttInboundProperties props,
|
||||||
MqttProfileRegistry profileRegistry,
|
MqttProfileRegistry profileRegistry,
|
||||||
VehicleIdentityResolver identityResolver,
|
VehicleIdentityResolver identityResolver,
|
||||||
Dispatcher dispatcher,
|
Dispatcher dispatcher) {
|
||||||
Environment environment) {
|
|
||||||
applyLegacySingleEndpointFallback(props, environment);
|
|
||||||
return new MqttEndpointManager(props, profileRegistry, identityResolver, dispatcher);
|
return new MqttEndpointManager(props, profileRegistry, identityResolver, dispatcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,133 +80,4 @@ public class MqttInboundAutoConfiguration {
|
|||||||
public ApplicationRunner mqttEndpointStartupRunner(MqttEndpointManager manager) {
|
public ApplicationRunner mqttEndpointStartupRunner(MqttEndpointManager manager) {
|
||||||
return args -> manager.start();
|
return args -> manager.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void applyLegacySingleEndpointFallback(MqttInboundProperties props, Environment env) {
|
|
||||||
if (!props.getEndpoints().isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String uri = firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].uri",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_URI",
|
|
||||||
"MQTT_URI",
|
|
||||||
"YUTONG_MQTT_URI");
|
|
||||||
if (uri.isBlank() || uri.contains("${")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
MqttInboundProperties.Endpoint endpoint = new MqttInboundProperties.Endpoint();
|
|
||||||
endpoint.setName(defaultText(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].name",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_NAME",
|
|
||||||
"MQTT_ENDPOINT_NAME",
|
|
||||||
"YUTONG_MQTT_ENDPOINT_NAME"), "yutong"));
|
|
||||||
endpoint.setUri(uri);
|
|
||||||
endpoint.setTopic(defaultText(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].topic",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_TOPIC",
|
|
||||||
"MQTT_TOPIC",
|
|
||||||
"YUTONG_MQTT_TOPIC"), "#"));
|
|
||||||
endpoint.setQos(intValue(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].qos",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_QOS",
|
|
||||||
"MQTT_QOS",
|
|
||||||
"YUTONG_MQTT_QOS"), 1));
|
|
||||||
endpoint.setClientId(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].client-id",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_CLIENT_ID",
|
|
||||||
"MQTT_CLIENT_ID",
|
|
||||||
"YUTONG_MQTT_CLIENT_ID"));
|
|
||||||
endpoint.setUsername(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].username",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_USERNAME",
|
|
||||||
"MQTT_USERNAME",
|
|
||||||
"YUTONG_MQTT_USERNAME"));
|
|
||||||
endpoint.setPassword(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].password",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_PASSWORD",
|
|
||||||
"MQTT_PASSWORD",
|
|
||||||
"YUTONG_MQTT_PASSWORD"));
|
|
||||||
endpoint.setCleanSession(booleanValue(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].clean-session",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_CLEAN_SESSION",
|
|
||||||
"MQTT_CLEAN_SESSION",
|
|
||||||
"YUTONG_MQTT_CLEAN_SESSION"), false));
|
|
||||||
endpoint.setKeepAliveSeconds(intValue(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].keep-alive-seconds",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_KEEP_ALIVE_SECONDS",
|
|
||||||
"MQTT_KEEP_ALIVE_SECONDS",
|
|
||||||
"YUTONG_MQTT_KEEP_ALIVE_SECONDS"), 20));
|
|
||||||
endpoint.setConnectionTimeoutSeconds(intValue(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].connection-timeout-seconds",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_CONNECTION_TIMEOUT_SECONDS",
|
|
||||||
"MQTT_CONNECTION_TIMEOUT_SECONDS",
|
|
||||||
"YUTONG_MQTT_CONNECTION_TIMEOUT_SECONDS"), 10));
|
|
||||||
endpoint.setProfile(defaultText(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].profile",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_PROFILE",
|
|
||||||
"MQTT_PROFILE",
|
|
||||||
"YUTONG_MQTT_PROFILE"), "yutong"));
|
|
||||||
MqttInboundProperties.Tls tls = endpoint.getTls();
|
|
||||||
tls.setCaPem(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].tls.ca-pem",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_TLS_CA_PEM",
|
|
||||||
"MQTT_TLS_CA_PEM",
|
|
||||||
"YUTONG_MQTT_TLS_CA_PEM"));
|
|
||||||
tls.setClientPem(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].tls.client-pem",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_TLS_CLIENT_PEM",
|
|
||||||
"MQTT_TLS_CLIENT_PEM",
|
|
||||||
"YUTONG_MQTT_TLS_CLIENT_PEM"));
|
|
||||||
tls.setClientKey(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].tls.client-key",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_TLS_CLIENT_KEY",
|
|
||||||
"MQTT_TLS_CLIENT_KEY",
|
|
||||||
"YUTONG_MQTT_TLS_CLIENT_KEY"));
|
|
||||||
tls.setHostnameVerificationEnabled(booleanValue(firstText(env,
|
|
||||||
"lingniu.ingest.mqtt.endpoints[0].tls.hostname-verification-enabled",
|
|
||||||
"LINGNIU_INGEST_MQTT_ENDPOINTS_0_TLS_HOSTNAME_VERIFICATION_ENABLED",
|
|
||||||
"MQTT_TLS_HOSTNAME_VERIFICATION_ENABLED",
|
|
||||||
"YUTONG_MQTT_TLS_HOSTNAME_VERIFICATION_ENABLED"), true));
|
|
||||||
props.setEndpoints(new ArrayList<>(List.of(endpoint)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String firstText(Environment env, String... keys) {
|
|
||||||
for (String key : keys) {
|
|
||||||
String value = env.getProperty(key);
|
|
||||||
if (value != null && !value.isBlank() && !value.contains("${")) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String defaultText(String value, String fallback) {
|
|
||||||
return value == null || value.isBlank() ? fallback : value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int intValue(String value, int fallback) {
|
|
||||||
if (value == null || value.isBlank()) {
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return Integer.parseInt(value.trim());
|
|
||||||
} catch (NumberFormatException ex) {
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean booleanValue(String value, boolean fallback) {
|
|
||||||
if (value == null || value.isBlank()) {
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
String normalized = value.trim().toLowerCase();
|
|
||||||
if ("true".equals(normalized) || "1".equals(normalized) || "yes".equals(normalized)
|
|
||||||
|| "on".equals(normalized)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ("false".equals(normalized) || "0".equals(normalized) || "no".equals(normalized)
|
|
||||||
|| "off".equals(normalized)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.lingniu.ingest.inbound.mqtt.config;
|
||||||
|
|
||||||
|
import com.lingniu.ingest.core.dispatcher.Dispatcher;
|
||||||
|
import com.lingniu.ingest.identity.VehicleIdentity;
|
||||||
|
import com.lingniu.ingest.identity.VehicleIdentitySource;
|
||||||
|
import com.lingniu.ingest.inbound.mqtt.profile.MqttProfileRegistry;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.core.env.MapPropertySource;
|
||||||
|
import org.springframework.core.env.StandardEnvironment;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class MqttInboundAutoConfigurationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doesNotCreateEndpointFromLegacySingleEndpointEnvironmentKeys() {
|
||||||
|
MqttInboundProperties props = new MqttInboundProperties();
|
||||||
|
StandardEnvironment environment = new StandardEnvironment();
|
||||||
|
environment.getPropertySources().addFirst(new MapPropertySource("test", Map.of(
|
||||||
|
"MQTT_URI", "tcp://127.0.0.1:1883",
|
||||||
|
"MQTT_TOPIC", "/legacy/#",
|
||||||
|
"YUTONG_MQTT_ENDPOINT_NAME", "legacy-yutong")));
|
||||||
|
|
||||||
|
new MqttInboundAutoConfiguration().mqttEndpointManager(
|
||||||
|
props,
|
||||||
|
new MqttProfileRegistry(List.of()),
|
||||||
|
lookup -> new VehicleIdentity("unknown", false, VehicleIdentitySource.UNKNOWN),
|
||||||
|
new Dispatcher(null, null, null, null, null));
|
||||||
|
|
||||||
|
assertThat(props.getEndpoints()).isEmpty();
|
||||||
|
assertThat(environment.getProperty("MQTT_URI")).isEqualTo("tcp://127.0.0.1:1883");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user