Skip to content

Commit 01ad559

Browse files
committed
Add adapterId to getDomainTags(), getNorthboundMappings(), getSouthboundMappings()
1 parent 8a6ec92 commit 01ad559

21 files changed

+648
-140
lines changed

hivemq-edge/src/main/java/com/hivemq/api/resources/impl/ProtocolAdaptersResourceImpl.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
import com.hivemq.api.model.mappings.northbound.NorthboundMappingModel;
4848
import com.hivemq.api.utils.ApiErrorUtils;
4949
import com.hivemq.configuration.entity.adapter.NorthboundMappingEntity;
50+
import com.hivemq.configuration.entity.adapter.NorthboundMappingEntityConverter;
5051
import com.hivemq.configuration.entity.adapter.ProtocolAdapterEntity;
5152
import com.hivemq.configuration.entity.adapter.SouthboundMappingEntity;
53+
import com.hivemq.configuration.entity.adapter.SouthboundMappingEntityConverter;
5254
import com.hivemq.configuration.entity.adapter.TagEntity;
5355
import com.hivemq.configuration.info.SystemInformation;
5456
import com.hivemq.configuration.reader.ProtocolAdapterExtractor;
@@ -62,8 +64,13 @@
6264
import com.hivemq.edge.api.model.AdaptersList;
6365
import com.hivemq.edge.api.model.DomainTag;
6466
import com.hivemq.edge.api.model.DomainTagList;
67+
import com.hivemq.edge.api.model.DomainTagOwnerList;
6568
import com.hivemq.edge.api.model.NorthboundMappingList;
69+
import com.hivemq.edge.api.model.NorthboundMappingOwnerList;
70+
import com.hivemq.edge.api.model.NorthboundMappingOwnerListItemsInner;
6671
import com.hivemq.edge.api.model.SouthboundMappingList;
72+
import com.hivemq.edge.api.model.SouthboundMappingOwnerList;
73+
import com.hivemq.edge.api.model.SouthboundMappingOwnerListItemsInner;
6774
import com.hivemq.edge.api.model.Status;
6875
import com.hivemq.edge.api.model.StatusList;
6976
import com.hivemq.edge.api.model.StatusTransitionCommand;
@@ -389,9 +396,8 @@ public int getDepth() {
389396
log.trace("Adapter '{}' was stopped successfully.", adapterId);
390397
}
391398
});
392-
case RESTART -> protocolAdapterManager
393-
.stopAsync(adapterId, false)
394-
.thenCompose(ignore -> protocolAdapterManager.startAsync(adapterId))
399+
case RESTART -> protocolAdapterManager.stopAsync(adapterId, false)
400+
.thenCompose(ignore -> protocolAdapterManager.startAsync(adapterId))
395401
.whenComplete((result, throwable) -> {
396402
if (throwable != null) {
397403
log.error("Failed to restart adapter '{}'.", adapterId, throwable);
@@ -615,10 +621,12 @@ public int getDepth() {
615621
@Override
616622
public @NotNull Response getDomainTags() {
617623
final List<com.hivemq.persistence.domain.DomainTag> domainTags = protocolAdapterManager.getDomainTags();
624+
final DomainTagOwnerList domainTagOwnerList = new DomainTagOwnerList();
625+
domainTags.stream()
626+
.map(com.hivemq.persistence.domain.DomainTag::toDomainTagOwner)
627+
.forEach(domainTagOwnerList::addItemsItem);
618628
// empty list is also 200 as discussed.
619-
return Response.ok(new DomainTagList().items(domainTags.stream()
620-
.map(com.hivemq.persistence.domain.DomainTag::toModel)
621-
.toList())).build();
629+
return Response.ok(domainTagOwnerList).build();
622630
}
623631

624632
@Override
@@ -746,26 +754,26 @@ public int getDepth() {
746754

747755
@Override
748756
public @NotNull Response getNorthboundMappings() {
749-
return Response.status(200)
750-
.entity(new NorthboundMappingListModel(configExtractor.getAllConfigs()
757+
final NorthboundMappingOwnerList northboundMappingOwnerList = new NorthboundMappingOwnerList();
758+
configExtractor.getAllConfigs()
759+
.forEach(adapter -> adapter.getNorthboundMappings()
751760
.stream()
752-
.flatMap(adapter -> adapter.getNorthboundMappings()
753-
.stream()
754-
.map(NorthboundMappingModel::fromEntity))
755-
.toList()))
756-
.build();
761+
.map(entity -> new NorthboundMappingOwnerListItemsInner().adapterId(adapter.getAdapterId())
762+
.mapping(NorthboundMappingEntityConverter.INSTANCE.toRestEntity(entity)))
763+
.forEach(northboundMappingOwnerList::addItemsItem));
764+
return Response.status(200).entity(northboundMappingOwnerList).build();
757765
}
758766

759767
@Override
760768
public @NotNull Response getSouthboundMappings() {
761-
return Response.status(200)
762-
.entity(new SouthboundMappingList().items(configExtractor.getAllConfigs()
769+
final SouthboundMappingOwnerList southboundMappingOwnerList = new SouthboundMappingOwnerList();
770+
configExtractor.getAllConfigs()
771+
.forEach(adapter -> adapter.getSouthboundMappings()
763772
.stream()
764-
.flatMap(adapter -> adapter.getSouthboundMappings()
765-
.stream()
766-
.map(SouthboundMappingEntity::toAPi))
767-
.toList()))
768-
.build();
773+
.map(entity -> new SouthboundMappingOwnerListItemsInner().adapterId(adapter.getAdapterId())
774+
.mapping(SouthboundMappingEntityConverter.INSTANCE.toRestEntity(entity)))
775+
.forEach(southboundMappingOwnerList::addItemsItem));
776+
return Response.status(200).entity(southboundMappingOwnerList).build();
769777
}
770778

771779
@Override
@@ -938,14 +946,16 @@ public int getDepth() {
938946

939947
final String errorMessage = String.format(
940948
"Cannot delete or rename tag '%s' for adapter '%s' because it is referenced in %d mapping(s): [%s]. " +
941-
"Please remove or update these mappings before deleting or renaming the tag.",
949+
"Please remove or update these mappings before deleting or renaming the tag.",
942950
tagName,
943951
adapter.getAdapterId(),
944952
allUsages.size(),
945953
String.join(", ", allUsages));
946954

947955
log.warn("Tag deletion/rename blocked for adapter '{}': tag '{}' is used in {} mapping(s)",
948-
adapter.getAdapterId(), tagName, allUsages.size());
956+
adapter.getAdapterId(),
957+
tagName,
958+
allUsages.size());
949959

950960
return Optional.of(errorResponse(new com.hivemq.api.errors.adapters.DomainTagInUseError(errorMessage)));
951961
}

hivemq-edge/src/main/java/com/hivemq/pulse/converters/PulseApiEntityConverter.java renamed to hivemq-edge/src/main/java/com/hivemq/configuration/entity/EntityConverter.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.hivemq.pulse.converters;
17+
package com.hivemq.configuration.entity;
1818

1919
import org.jetbrains.annotations.NotNull;
2020

21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
2125
/**
2226
* A generic interface for converting between REST entities and internal entities.
2327
* This interface defines methods for converting data models used in REST APIs
@@ -26,7 +30,7 @@
2630
* @param <RestEntity> the type representing the REST entity.
2731
* @param <InternalEntity> the type representing the internal entity.
2832
*/
29-
public interface PulseApiEntityConverter<RestEntity, InternalEntity> {
33+
public interface EntityConverter<RestEntity, InternalEntity> {
3034

3135
/**
3236
* Converts a REST entity to an internal entity.
@@ -43,4 +47,12 @@ public interface PulseApiEntityConverter<RestEntity, InternalEntity> {
4347
* @return the converted REST entity. Will not be null.
4448
*/
4549
@NotNull RestEntity toRestEntity(final @NotNull InternalEntity internalEntity);
50+
51+
default @NotNull List<InternalEntity> toInternalEntities(final @NotNull List<RestEntity> restEntities) {
52+
return restEntities.stream().map(this::toInternalEntity).collect(Collectors.toCollection(ArrayList::new));
53+
}
54+
55+
default @NotNull List<RestEntity> toRestEntities(final @NotNull List<InternalEntity> internalEntities) {
56+
return internalEntities.stream().map(this::toRestEntity).collect(Collectors.toCollection(ArrayList::new));
57+
}
4658
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hivemq.configuration.entity.adapter;
18+
19+
import com.hivemq.configuration.entity.EntityConverter;
20+
import com.hivemq.edge.api.model.MqttUserProperty;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
public final class MqttUserPropertyEntityConverter
24+
implements EntityConverter<MqttUserProperty, MqttUserPropertyEntity> {
25+
public static final MqttUserPropertyEntityConverter INSTANCE = new MqttUserPropertyEntityConverter();
26+
27+
private MqttUserPropertyEntityConverter() {
28+
}
29+
30+
@Override
31+
public @NotNull MqttUserPropertyEntity toInternalEntity(final @NotNull MqttUserProperty mqttUserProperty) {
32+
return new MqttUserPropertyEntity(mqttUserProperty.getName(), mqttUserProperty.getValue());
33+
}
34+
35+
@Override
36+
public @NotNull MqttUserProperty toRestEntity(final @NotNull MqttUserPropertyEntity mqttUserPropertyEntity) {
37+
return MqttUserProperty.builder()
38+
.name(mqttUserPropertyEntity.getName())
39+
.value(mqttUserPropertyEntity.getValue())
40+
.build();
41+
}
42+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hivemq.configuration.entity.adapter;
18+
19+
import com.hivemq.adapter.sdk.api.config.MessageHandlingOptions;
20+
import com.hivemq.configuration.entity.EntityConverter;
21+
import com.hivemq.edge.api.model.NorthboundMapping;
22+
import org.jetbrains.annotations.NotNull;
23+
24+
public final class NorthboundMappingEntityConverter
25+
implements EntityConverter<NorthboundMapping, NorthboundMappingEntity> {
26+
public static final NorthboundMappingEntityConverter INSTANCE = new NorthboundMappingEntityConverter();
27+
28+
private NorthboundMappingEntityConverter() {
29+
}
30+
31+
@Override
32+
public @NotNull NorthboundMappingEntity toInternalEntity(final @NotNull NorthboundMapping entity) {
33+
return new NorthboundMappingEntity(entity.getTagName(),
34+
entity.getTopic(),
35+
QoSConverter.INSTANCE.toInternalEntity(entity.getMaxQoS()).getQosNumber(),
36+
MessageHandlingOptions.MQTTMessagePerTag,
37+
entity.getIncludeTagNames(),
38+
entity.getIncludeTimestamp(),
39+
MqttUserPropertyEntityConverter.INSTANCE.toInternalEntities(entity.getUserProperties()),
40+
entity.getMessageExpiryInterval());
41+
}
42+
43+
@Override
44+
public @NotNull NorthboundMapping toRestEntity(final @NotNull NorthboundMappingEntity entity) {
45+
return new NorthboundMapping().tagName(entity.getTagName())
46+
.topic(entity.getTopic())
47+
.includeTagNames(entity.isIncludeTagNames())
48+
.includeTimestamp(entity.isIncludeTimestamp())
49+
.maxQoS(QoSConverter.INSTANCE.toRestEntity(entity.getMaxQoS()))
50+
.messageExpiryInterval(entity.getMessageExpiryInterval())
51+
.userProperties(MqttUserPropertyEntityConverter.INSTANCE.toRestEntities(entity.getUserProperties()));
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hivemq.configuration.entity.adapter;
18+
19+
import com.hivemq.configuration.entity.EntityConverter;
20+
import com.hivemq.mqtt.message.QoS;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
public final class QoSConverter implements EntityConverter<com.hivemq.edge.api.model.QoS, QoS> {
24+
public static final QoSConverter INSTANCE = new QoSConverter();
25+
26+
private QoSConverter() {
27+
}
28+
29+
@Override
30+
public @NotNull QoS toInternalEntity(final @NotNull com.hivemq.edge.api.model.QoS qoS) {
31+
return switch (qoS) {
32+
case AT_MOST_ONCE -> QoS.AT_MOST_ONCE;
33+
case AT_LEAST_ONCE -> QoS.AT_LEAST_ONCE;
34+
case EXACTLY_ONCE -> QoS.EXACTLY_ONCE;
35+
};
36+
}
37+
38+
@Override
39+
public @NotNull com.hivemq.edge.api.model.QoS toRestEntity(final @NotNull QoS qoS) {
40+
return switch (qoS) {
41+
case AT_MOST_ONCE -> com.hivemq.edge.api.model.QoS.AT_MOST_ONCE;
42+
case AT_LEAST_ONCE -> com.hivemq.edge.api.model.QoS.AT_LEAST_ONCE;
43+
case EXACTLY_ONCE -> com.hivemq.edge.api.model.QoS.EXACTLY_ONCE;
44+
};
45+
}
46+
47+
public @NotNull com.hivemq.edge.api.model.QoS toRestEntity(final int intQoS) {
48+
final QoS qoS = QoS.valueOf(intQoS);
49+
return toRestEntity(qoS == null ? QoS.AT_MOST_ONCE : qoS);
50+
}
51+
}

hivemq-edge/src/main/java/com/hivemq/configuration/entity/adapter/SouthboundMappingEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public SouthboundMappingEntity(
7272
return topicFilter;
7373
}
7474

75+
public @Nullable FieldMappingEntity getFieldMapping() {
76+
return fieldMapping;
77+
}
78+
7579
@Override
7680
public void validate(final @NotNull List<ValidationEvent> validationEvents) {
7781
EntityValidatable.notEmpty(validationEvents, topicFilter, "topicFilter");
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hivemq.configuration.entity.adapter;
18+
19+
import com.hivemq.configuration.entity.EntityConverter;
20+
import com.hivemq.configuration.entity.adapter.fieldmapping.FieldMappingEntityConverter;
21+
import com.hivemq.edge.api.model.SouthboundMapping;
22+
import org.jetbrains.annotations.NotNull;
23+
24+
public final class SouthboundMappingEntityConverter
25+
implements EntityConverter<SouthboundMapping, SouthboundMappingEntity> {
26+
public static final SouthboundMappingEntityConverter INSTANCE = new SouthboundMappingEntityConverter();
27+
28+
private SouthboundMappingEntityConverter() {
29+
}
30+
31+
@Override
32+
public @NotNull SouthboundMappingEntity toInternalEntity(final @NotNull SouthboundMapping entity) {
33+
return null;
34+
}
35+
36+
@Override
37+
public @NotNull SouthboundMapping toRestEntity(final @NotNull SouthboundMappingEntity entity) {
38+
return SouthboundMapping.builder()
39+
.tagName(entity.getTagName())
40+
.topicFilter(entity.getTopicFilter())
41+
.fieldMapping(entity.getFieldMapping() == null ?
42+
null :
43+
FieldMappingEntityConverter.INSTANCE_WITHOUT_SOURCE_REF.toRestEntity(entity.getFieldMapping()))
44+
.build();
45+
}
46+
}

0 commit comments

Comments
 (0)