Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version to config #713

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ protected void validateAdapterSchema(

protocolAdapterManager.addAdapter(new ProtocolAdapterConfig(adapterId,
adapterType,
protocolAdapterInformation.get().getCurrentConfigVersion(),
protocolSpecificAdapterConfig,
southboundMappings,
northboundMappings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author Dominik Obermaier
Expand All @@ -44,6 +45,11 @@
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
public class HiveMQConfigEntity {

public static final int CURRENT_CONFIG_VERSION = 1;
DC2-DanielKrueger marked this conversation as resolved.
Show resolved Hide resolved

@XmlElement(name = "config-version", defaultValue = "" + CURRENT_CONFIG_VERSION)
private int version = CURRENT_CONFIG_VERSION;

@XmlElementWrapper(name = "mqtt-listeners", required = true)
@XmlElementRef(required = false)
private @NotNull List<ListenerEntity> mqttListeners = new ArrayList<>();
Expand Down Expand Up @@ -95,11 +101,12 @@ public class HiveMQConfigEntity {
private final @NotNull InternalConfigEntity internal = new InternalConfigEntity();

// no-arg constructor as JaxB does need one
public HiveMQConfigEntity(){
public HiveMQConfigEntity() {

}

public HiveMQConfigEntity(
final @NotNull Integer version,
final @NotNull AdminApiEntity api,
final @NotNull DynamicConfigEntity gateway,
final @NotNull Map<String, Object> moduleConfigs,
Expand All @@ -114,6 +121,7 @@ public HiveMQConfigEntity(
final @NotNull SecurityConfigEntity security,
final @NotNull UnsConfigEntity uns,
final @NotNull UsageTrackingConfigEntity usageTracking) {
this.version = Objects.requireNonNullElse(version, CURRENT_CONFIG_VERSION);
this.api = api;
this.gateway = gateway;
this.moduleConfigs = moduleConfigs;
Expand Down Expand Up @@ -174,9 +182,13 @@ public HiveMQConfigEntity(
return moduleConfigs;
}

public @NotNull UnsConfigEntity getUns() { return uns; }
public @NotNull UnsConfigEntity getUns() {
return uns;
}

public @NotNull DynamicConfigEntity getGatewayConfig() { return gateway;}
public @NotNull DynamicConfigEntity getGatewayConfig() {
return gateway;
}

public @NotNull UsageTrackingConfigEntity getUsageTracking() {
return usageTracking;
Expand All @@ -185,4 +197,8 @@ public HiveMQConfigEntity(
public @NotNull InternalConfigEntity getInternal() {
return internal;
}

public int getVersion() {
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hivemq.configuration.reader.ArbitraryValuesMapAdapter;
import org.jetbrains.annotations.NotNull;
import com.hivemq.protocols.ProtocolAdapterConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.annotation.XmlElement;
Expand All @@ -30,6 +31,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@SuppressWarnings({"FieldMayBeFinal", "unused"})
Expand All @@ -41,6 +43,9 @@ public class ProtocolAdapterEntity {
@XmlElement(name = "protocolId", required = true)
private @NotNull String protocolId;

@XmlElement(name = "configVersion")
private @Nullable Integer configVersion;

@XmlElement(name = "config")
@XmlJavaTypeAdapter(ArbitraryValuesMapAdapter.class)
private @NotNull Map<String, Object> config = new HashMap<>();
Expand All @@ -65,12 +70,15 @@ public ProtocolAdapterEntity() {
public ProtocolAdapterEntity(
final @NotNull String adapterId,
final @NotNull String protocolId,
final @Nullable Integer configVersion,
final @NotNull Map<String, Object> config,
final @NotNull List<NorthboundMappingEntity> northboundMappingEntities,
final @NotNull List<SouthboundMappingEntity> southboundMappingEntities,
final @NotNull List<TagEntity> tags) {
this.adapterId = adapterId;
this.config = config;
// if no config version is present, we assume it is the oldest possible version
this.configVersion = configVersion;
this.northboundMappingEntities = northboundMappingEntities;
this.protocolId = protocolId;
this.tags = tags;
Expand Down Expand Up @@ -101,6 +109,10 @@ public ProtocolAdapterEntity(
return adapterId;
}

public @NotNull Integer getConfigVersion() {
return Objects.requireNonNullElse(configVersion, 1);
}

public void validate(final @NotNull List<ValidationEvent> validationEvents) {
if (adapterId == null || adapterId.isEmpty()) {
validationEvents.add(new ValidationEventImpl(ValidationEvent.FATAL_ERROR, "adapterId is missing", null));
Expand Down Expand Up @@ -130,16 +142,17 @@ public void validate(final @NotNull List<ValidationEvent> validationEvents) {
.collect(Collectors.toList());

final List<TagEntity> tagEntities = protocolAdapterConfig.getTags()
.stream().map(tag -> TagEntity.fromAdapterTag(tag, objectMapper))
.stream()
.map(tag -> TagEntity.fromAdapterTag(tag, objectMapper))
.collect(Collectors.toList());

final Map<String, Object> configAsMaps =
objectMapper.convertValue(protocolAdapterConfig.getAdapterConfig(), new TypeReference<>() {
});

return new ProtocolAdapterEntity(
protocolAdapterConfig.getAdapterId(),
return new ProtocolAdapterEntity(protocolAdapterConfig.getAdapterId(),
protocolAdapterConfig.getProtocolId(),
protocolAdapterConfig.getConfigVersion(),
configAsMaps,
northboundMappings,
southboundMappings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public ConfigurationMigrator(

@VisibleForTesting
public ConfigurationMigrator(
final @NotNull ConfigurationFile configurationFile, final @NotNull ModuleLoader moduleLoader) {
final @NotNull ConfigurationFile configurationFile,
final @NotNull ModuleLoader moduleLoader) {
this.moduleLoader = moduleLoader;
moduleLoader.loadModules();
this.configurationFile = configurationFile;
Expand Down Expand Up @@ -188,6 +189,7 @@ public Optional<HiveMQConfigEntity> migrateIfNeeded(final Map<String, ProtocolAd

return List.of(new ProtocolAdapterEntity(configTagsTuple.getAdapterId(),
protocolId,
protocolAdapterFactory.getInformation().getCurrentConfigVersion(),
objectMapper.convertValue(configTagsTuple.getConfig(), new TypeReference<>() {
}),
northboundMappingEntities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.List;
import java.util.Map;

import static com.hivemq.configuration.entity.HiveMQConfigEntity.CURRENT_CONFIG_VERSION;

/**
* @author Dominik Obermaier
* @author Lukas brandl
Expand Down Expand Up @@ -165,7 +167,8 @@ public class LegacyHiveMQConfigEntity {

final @NotNull HiveMQConfigEntity to(
final @NotNull List<ProtocolAdapterEntity> adapterEntities) {
return new HiveMQConfigEntity(this.getApiConfig(),
return new HiveMQConfigEntity(CURRENT_CONFIG_VERSION,
this.getApiConfig(),
this.getGatewayConfig(),
this.getModuleConfigs(),
this.getMqttConfig(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@

public class SimulationProtocolAdapterInformation implements ProtocolAdapterInformation {

public static final ProtocolAdapterInformation INSTANCE = new SimulationProtocolAdapterInformation();

private static final Logger log = LoggerFactory.getLogger(SimulationProtocolAdapterInformation.class);

public static final ProtocolAdapterInformation INSTANCE = new SimulationProtocolAdapterInformation();
public static final int CURRENT_CONFIG_VERSION = 1;

private SimulationProtocolAdapterInformation() {
}
Expand Down Expand Up @@ -114,6 +116,11 @@ public ProtocolAdapterCategory getCategory() {
}
}

@Override
public int getCurrentConfigVersion() {
return CURRENT_CONFIG_VERSION;
}


@Override
public @NotNull Class<? extends Tag> tagConfigurationClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ public class ProtocolAdapterConfig {
private final @NotNull List<? extends Tag> tags;
private final @NotNull String adapterId;
private final @NotNull String protocolId;
private final int configVersion;
private final @NotNull List<SouthboundMapping> southboundMappings;
private final @NotNull List<NorthboundMapping> northboundMappings;

public ProtocolAdapterConfig(
final @NotNull String adapterId,
final @NotNull String protocolId,
final int configVersion,
final @NotNull ProtocolSpecificAdapterConfig protocolSpecificConfig,
final @NotNull List<SouthboundMapping> southboundMappings,
final @NotNull List<NorthboundMapping> northboundMappings,
final @NotNull List<? extends Tag> tags) {
this.adapterId = adapterId;
this.protocolId = protocolId;
this.configVersion = configVersion;
this.southboundMappings = southboundMappings;
this.northboundMappings = northboundMappings;
this.adapterConfig = protocolSpecificConfig;
Expand Down Expand Up @@ -90,4 +93,8 @@ public ProtocolAdapterConfig(
public @NotNull List<SouthboundMapping> getToEdgeMappings() {
return southboundMappings;
}

public int getConfigVersion() {
return configVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public ProtocolAdapterConfigConverter(

return new ProtocolAdapterConfig(protocolAdapterEntity.getAdapterId(),
protocolAdapterEntity.getProtocolId(),
protocolSpecificAdapterConfig, southboundMappingList,
protocolAdapterEntity.getConfigVersion(),
protocolSpecificAdapterConfig,
southboundMappingList,
northboundMappingList,
tags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ protected void handleStartupError(
Preconditions.checkNotNull(adapterId);
Preconditions.checkNotNull(config);

if (getAdapterTypeById(adapterType).isEmpty()) {
final Optional<ProtocolAdapterInformation> adapterInformationOptional = getAdapterTypeById(adapterType);
if (adapterInformationOptional.isEmpty()) {
throw new IllegalArgumentException("invalid adapter type '" + adapterType + "'");
}
if (getAdapterById(adapterId).isPresent()) {
Expand All @@ -436,6 +437,7 @@ protected void handleStartupError(
final ProtocolAdapterConfig protocolAdapterConfig = new ProtocolAdapterConfig(
adapterId,
adapterType,
adapterInformationOptional.get().getCurrentConfigVersion(),
protocolSpecificAdapterConfig,
List.of(),
List.of(),
Expand Down Expand Up @@ -498,6 +500,7 @@ public boolean updateAdapterConfig(

final ProtocolAdapterConfig protocolAdapterConfig = new ProtocolAdapterConfig(adapterId,
adapterType,
oldInstance.getAdapterInformation().getCurrentConfigVersion(),
protocolSpecificAdapterConfig,
oldInstance.getToEdgeMappings(),
oldInstance.getFromEdgeMappings(),
Expand All @@ -516,6 +519,7 @@ public boolean updateAdapterTags(final @NotNull String adapterId, final @NotNull
final String protocolId = oldInstance.getAdapterInformation().getProtocolId();
final ProtocolAdapterConfig protocolAdapterConfig = new ProtocolAdapterConfig(oldInstance.getId(),
protocolId,
oldInstance.getAdapterInformation().getCurrentConfigVersion(),
oldInstance.getConfigObject(),
oldInstance.getToEdgeMappings(),
oldInstance.getFromEdgeMappings(),
Expand All @@ -533,6 +537,7 @@ public boolean updateAdapterFromMappings(final @NotNull String adapterId, final
final String protocolId = oldInstance.getAdapterInformation().getProtocolId();
final ProtocolAdapterConfig protocolAdapterConfig = new ProtocolAdapterConfig(oldInstance.getId(),
protocolId,
oldInstance.getAdapterInformation().getCurrentConfigVersion(),
oldInstance.getConfigObject(),
oldInstance.getToEdgeMappings(), northboundMappings,
oldInstance.getTags());
Expand All @@ -556,6 +561,7 @@ public boolean updateAdapterToMappings(final @NotNull String adapterId, final @N
final String protocolId = oldInstance.getAdapterInformation().getProtocolId();
final ProtocolAdapterConfig protocolAdapterConfig = new ProtocolAdapterConfig(oldInstance.getId(),
protocolId,
oldInstance.getAdapterInformation().getCurrentConfigVersion(),
oldInstance.getConfigObject(), southboundMappings,
oldInstance.getFromEdgeMappings(),
oldInstance.getTags());
Expand Down
3 changes: 2 additions & 1 deletion hivemq-edge/src/main/resources/config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<xs:complexType name="hiveMQConfigEntity">
<xs:all>
<xs:element name="config-version" type="xs:integer" minOccurs="0"/>
<xs:element name="mqtt-listeners" type="mqttListenersEntity" minOccurs="0"/>
<xs:element name="mqtt-sn-listeners" type="mqttsnListenersEntity" minOccurs="0"/>
<xs:element name="mqtt" type="mqttConfigEntity" minOccurs="0"/>
Expand Down Expand Up @@ -861,7 +862,7 @@
<xs:complexType>
<xs:choice>
<xs:element name="mqtt-topic-filter"
type="nonEmptyString" minOccurs="1"
type="nonEmptyString"
maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ static class TestWritingProtocolAdapterInformation implements ProtocolAdapterInf
public @org.jetbrains.annotations.NotNull Class<? extends ProtocolSpecificAdapterConfig> configurationClassNorthAndSouthbound() {
return null;
}

@Override
public int getCurrentConfigVersion() {
return 1;
}
}

static class TestWritingAdapter implements WritingProtocolAdapter {
Expand Down
1 change: 1 addition & 0 deletions hivemq-edge/src/test/resources/test-config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<hivemq xsi:schemaLocation="config.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<config-version>1</config-version>
<mqtt-listeners>
<tcp-listener>
<port>1883</port>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class EipProtocolAdapterInformation implements ProtocolAdapterInformation
public static final @NotNull ProtocolAdapterInformation INSTANCE = new EipProtocolAdapterInformation();
private static final @NotNull Logger log = LoggerFactory.getLogger(EipProtocolAdapterInformation.class);
public static final String PROTOCOL_ID = "eip";
private static final int CURRENT_CONFIG_VERSION = 1;

private EipProtocolAdapterInformation() {
}
Expand Down Expand Up @@ -137,4 +138,9 @@ public List<ProtocolAdapterTag> getTags() {
public @NotNull Class<? extends ProtocolSpecificAdapterConfig> configurationClassNorthAndSouthbound() {
return EipSpecificAdapterConfig.class;
}

@Override
public int getCurrentConfigVersion() {
return CURRENT_CONFIG_VERSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
public class FileProtocolAdapterInformation implements ProtocolAdapterInformation {

public static final @NotNull ProtocolAdapterInformation INSTANCE = new FileProtocolAdapterInformation();
private static final @NotNull Logger LOG = LoggerFactory.getLogger(FileProtocolAdapterInformation.class);
public static final String PROTOCOL_ID = "file";

private static final @NotNull Logger LOG = LoggerFactory.getLogger(FileProtocolAdapterInformation.class);
private static final int CURRENT_CONFIG_VERSION = 1;


protected FileProtocolAdapterInformation() {
}

Expand Down Expand Up @@ -135,4 +138,9 @@ public List<ProtocolAdapterTag> getTags() {
public @NotNull Class<? extends ProtocolSpecificAdapterConfig> configurationClassNorthAndSouthbound() {
return FileSpecificAdapterConfig.class;
}

@Override
public int getCurrentConfigVersion() {
return CURRENT_CONFIG_VERSION;
}
}
Loading
Loading