Send DMF batch message on thing_created event (#1284)

* send dmf batch message on thing created event
* fix tests

Signed-off-by: Stefan Klotz <stefan.klotz@bosch.io>
This commit is contained in:
Stefan Klotz
2022-10-05 18:39:32 +02:00
committed by GitHub
parent c5afc6b17e
commit 06e8ef4c15

View File

@@ -24,7 +24,6 @@ import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import com.google.common.collect.Iterables;
import org.eclipse.hawkbit.api.ApiType; import org.eclipse.hawkbit.api.ApiType;
import org.eclipse.hawkbit.api.ArtifactUrl; import org.eclipse.hawkbit.api.ArtifactUrl;
import org.eclipse.hawkbit.api.ArtifactUrlHandler; import org.eclipse.hawkbit.api.ArtifactUrlHandler;
@@ -49,11 +48,11 @@ import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.SystemManagement; import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.TargetManagement; import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement; import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.event.remote.CancelTargetAssignmentEvent;
import org.eclipse.hawkbit.repository.event.remote.MultiActionEvent; import org.eclipse.hawkbit.repository.event.remote.MultiActionEvent;
import org.eclipse.hawkbit.repository.event.remote.TargetAssignDistributionSetEvent; import org.eclipse.hawkbit.repository.event.remote.TargetAssignDistributionSetEvent;
import org.eclipse.hawkbit.repository.event.remote.TargetAttributesRequestedEvent; import org.eclipse.hawkbit.repository.event.remote.TargetAttributesRequestedEvent;
import org.eclipse.hawkbit.repository.event.remote.TargetDeletedEvent; import org.eclipse.hawkbit.repository.event.remote.TargetDeletedEvent;
import org.eclipse.hawkbit.repository.event.remote.CancelTargetAssignmentEvent;
import org.eclipse.hawkbit.repository.model.Action; import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.ActionProperties; import org.eclipse.hawkbit.repository.model.ActionProperties;
import org.eclipse.hawkbit.repository.model.Artifact; import org.eclipse.hawkbit.repository.model.Artifact;
@@ -77,6 +76,8 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import com.google.common.collect.Iterables;
/** /**
* {@link AmqpMessageDispatcherService} create all outgoing AMQP messages and * {@link AmqpMessageDispatcherService} create all outgoing AMQP messages and
* delegate the messages to a {@link AmqpMessageSenderService}. * delegate the messages to a {@link AmqpMessageSenderService}.
@@ -164,7 +165,8 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
if (!filteredTargetList.isEmpty()) { if (!filteredTargetList.isEmpty()) {
LOG.debug("targetAssignDistributionSet retrieved. I will forward it to DMF broker."); LOG.debug("targetAssignDistributionSet retrieved. I will forward it to DMF broker.");
sendUpdateMessageToTarget(assignedEvent, filteredTargetList); sendUpdateMessageToTargets(assignedEvent.getDistributionSetId(), assignedEvent.getActions(),
filteredTargetList);
} }
} }
@@ -196,20 +198,34 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
}); });
} }
private void sendUpdateMessageToTarget(final TargetAssignDistributionSetEvent assignedEvent, private void sendUpdateMessageToTargets(final Long dsId, final Map<String, ActionProperties> actionsPropsByTargetId,
final List<Target> targets) { final List<Target> targets) {
distributionSetManagement.get(assignedEvent.getDistributionSetId()).ifPresent(ds -> { distributionSetManagement.get(dsId).ifPresent(ds -> {
final Map<SoftwareModule, List<SoftwareModuleMetadata>> softwareModules = getSoftwareModulesWithMetadata( final Map<SoftwareModule, List<SoftwareModuleMetadata>> softwareModules = getSoftwareModulesWithMetadata(
ds); ds);
sendUpdateMessageToTargets(actionsPropsByTargetId, targets, softwareModules);
});
}
protected void sendUpdateMessageToTarget(final ActionProperties actionsProps, final Target target,
final Map<SoftwareModule, List<SoftwareModuleMetadata>> softwareModules) {
final Map<String, ActionProperties> actionProp = new HashMap<>();
actionProp.put(target.getControllerId(), actionsProps);
sendUpdateMessageToTargets(actionProp, Collections.singletonList(target), softwareModules);
}
private void sendUpdateMessageToTargets(final Map<String, ActionProperties> actionsPropsByTargetId,
final List<Target> targets, final Map<SoftwareModule, List<SoftwareModuleMetadata>> softwareModules) {
if (!targets.isEmpty() && isBatchAssignmentsEnabled()) { if (!targets.isEmpty() && isBatchAssignmentsEnabled()) {
sendUpdateMessageToTargets(assignedEvent.getActions(), targets, softwareModules); sendBatchUpdateMessage(actionsPropsByTargetId, targets, softwareModules);
} else { } else {
targets.forEach(target -> sendUpdateMessageToTarget( targets.forEach(target -> {
assignedEvent.getActions().get(target.getControllerId()), target, softwareModules)); final ActionProperties actionProp = actionsPropsByTargetId.get(target.getControllerId());
} sendSingleUpdateMessage(actionProp, target, softwareModules);
}); });
} }
}
private void sendMultiActionRequestMessages(final String tenant, final List<String> controllerIds) { private void sendMultiActionRequestMessages(final String tenant, final List<String> controllerIds) {
@@ -389,7 +405,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
updateAttributesEvent.getTargetAddress()); updateAttributesEvent.getTargetAddress());
} }
protected void sendUpdateMessageToTarget(final ActionProperties action, final Target target, private void sendSingleUpdateMessage(final ActionProperties action, final Target target,
final Map<SoftwareModule, List<SoftwareModuleMetadata>> modules) { final Map<SoftwareModule, List<SoftwareModuleMetadata>> modules) {
final String tenant = action.getTenant(); final String tenant = action.getTenant();
@@ -551,25 +567,27 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
PageRequest.of(0, RepositoryConstants.MAX_META_DATA_COUNT), module.getId()).getContent(); PageRequest.of(0, RepositoryConstants.MAX_META_DATA_COUNT), module.getId()).getContent();
} }
private void sendUpdateMessageToTargets(final Map<String, ActionProperties> actions, final List<Target> targets, private void sendBatchUpdateMessage(final Map<String, ActionProperties> actions, final List<Target> targets,
final Map<SoftwareModule, List<SoftwareModuleMetadata>> modules) { final Map<SoftwareModule, List<SoftwareModuleMetadata>> modules) {
List<DmfTarget> dmfTargets = targets.stream().filter(target -> IpUtil.isAmqpUri(target.getAddress())) final List<DmfTarget> dmfTargets = targets.stream().filter(target -> IpUtil.isAmqpUri(target.getAddress()))
.map(t -> convertToDmfTarget(t, actions.get(t.getControllerId()).getId())).collect(Collectors.toList()); .map(t -> convertToDmfTarget(t, actions.get(t.getControllerId()).getId())).collect(Collectors.toList());
final DmfBatchDownloadAndUpdateRequest batchRequest = new DmfBatchDownloadAndUpdateRequest(); final DmfBatchDownloadAndUpdateRequest batchRequest = new DmfBatchDownloadAndUpdateRequest();
batchRequest.setTimestamp(System.currentTimeMillis()); batchRequest.setTimestamp(System.currentTimeMillis());
batchRequest.addTargets(dmfTargets); batchRequest.addTargets(dmfTargets);
//due to the fact that all targets in a batch use the same set of software modules we don't generate // due to the fact that all targets in a batch use the same set of
// software modules we don't generate
// target-specific urls // target-specific urls
Target firstTarget = targets.get(0); final Target firstTarget = targets.get(0);
if (modules != null) { if (modules != null) {
modules.entrySet().forEach(entry -> modules.entrySet()
batchRequest.addSoftwareModule(convertToAmqpSoftwareModule(firstTarget, entry))); .forEach(entry -> batchRequest.addSoftwareModule(convertToAmqpSoftwareModule(firstTarget, entry)));
} }
// we use only the first action when constructing message as Tenant and action type are the same // we use only the first action when constructing message as Tenant and
// action type are the same
// since all actions have the same trigger // since all actions have the same trigger
final ActionProperties firstAction = actions.values().iterator().next(); final ActionProperties firstAction = actions.values().iterator().next();
final Message message = getMessageConverter().toMessage(batchRequest, final Message message = getMessageConverter().toMessage(batchRequest,
@@ -578,7 +596,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
} }
protected DmfTarget convertToDmfTarget(final Target target, final Long actionId) { protected DmfTarget convertToDmfTarget(final Target target, final Long actionId) {
DmfTarget dmfTarget = new DmfTarget(); final DmfTarget dmfTarget = new DmfTarget();
dmfTarget.setActionId(actionId); dmfTarget.setActionId(actionId);
dmfTarget.setControllerId(target.getControllerId()); dmfTarget.setControllerId(target.getControllerId());
dmfTarget.setTargetSecurityToken(systemSecurityContext.runAsSystem(target::getSecurityToken)); dmfTarget.setTargetSecurityToken(systemSecurityContext.runAsSystem(target::getSecurityToken));