Skip to content

Commit

Permalink
Fix DMF context (as system)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm committed Nov 22, 2024
1 parent 49d27b1 commit 457134b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@ protected void targetAssignDistributionSet(final TargetAssignDistributionSetEven
return;
}

final List<Target> filteredTargetList = getTargetsWithoutPendingCancellations(assignedEvent.getActions().keySet());
systemSecurityContext.runAsSystemAsTenant(() -> {
final List<Target> filteredTargetList = getTargetsWithoutPendingCancellations(
assignedEvent.getTenant(), assignedEvent.getActions().keySet());

if (!filteredTargetList.isEmpty()) {
log.debug("targetAssignDistributionSet retrieved. I will forward it to DMF broker.");
sendUpdateMessageToTargets(assignedEvent.getDistributionSetId(), assignedEvent.getActions(), filteredTargetList);
}
if (!filteredTargetList.isEmpty()) {
log.debug("targetAssignDistributionSet retrieved. I will forward it to DMF broker.");
sendUpdateMessageToTargets(assignedEvent.getDistributionSetId(), assignedEvent.getActions(), filteredTargetList);
}

return null;
}, assignedEvent.getTenant());
}

/**
Expand All @@ -171,8 +176,12 @@ protected void onMultiAction(final MultiActionEvent multiActionEvent) {
if (!shouldBeProcessed(multiActionEvent)) {
return;
}
log.debug("MultiActionEvent received for {}", multiActionEvent.getControllerIds());
sendMultiActionRequestMessages(multiActionEvent.getTenant(), multiActionEvent.getControllerIds());

systemSecurityContext.runAsSystemAsTenant(() -> {
log.debug("MultiActionEvent received for {}", multiActionEvent.getControllerIds());
sendMultiActionRequestMessages(multiActionEvent.getTenant(), multiActionEvent.getControllerIds());
return null;
}, multiActionEvent.getTenant());
}

protected void sendUpdateMessageToTarget(
Expand Down Expand Up @@ -229,16 +238,20 @@ protected void targetCancelAssignmentToDistributionSet(final CancelTargetAssignm
return;
}

final List<Target> eventTargets = partitionedParallelExecution(
cancelEvent.getActions().keySet(), targetManagement::getByControllerID);
systemSecurityContext.runAsSystemAsTenant(() -> {
final List<Target> eventTargets = partitionedParallelExecution(
cancelEvent.getActions().keySet(), targetManagement::getByControllerID);

eventTargets.forEach(target ->
cancelEvent.getActionPropertiesForController(target.getControllerId())
.map(ActionProperties::getId)
.ifPresent(actionId ->
sendCancelMessageToTarget(cancelEvent.getTenant(), target.getControllerId(), actionId, target.getAddress())
)
);
eventTargets.forEach(target ->
cancelEvent.getActionPropertiesForController(target.getControllerId())
.map(ActionProperties::getId)
.ifPresent(actionId ->
sendCancelMessageToTarget(cancelEvent.getTenant(), target.getControllerId(), actionId, target.getAddress())
)
);

return null;
}, cancelEvent.getTenant());
}

/**
Expand All @@ -251,6 +264,7 @@ protected void targetDelete(final TargetDeletedEvent deleteEvent) {
if (!shouldBeProcessed(deleteEvent)) {
return;
}

sendDeleteMessage(deleteEvent.getTenant(), deleteEvent.getControllerId(), deleteEvent.getTargetAddress());
}

Expand Down Expand Up @@ -427,12 +441,13 @@ private static EventTopic getBatchEventTopicForAction(final ActionProperties act
: EventTopic.BATCH_DOWNLOAD_AND_INSTALL;
}

private List<Target> getTargetsWithoutPendingCancellations(final Set<String> controllerIds) {
private List<Target> getTargetsWithoutPendingCancellations(final String tenant, final Set<String> controllerIds) {
return partitionedParallelExecution(controllerIds, partition ->
targetManagement.getByControllerID(partition).stream()
.filter(target -> {
if (hasPendingCancellations(target.getId())) {
log.debug("Target {} has pending cancellations. Will not send update message to it.", target.getControllerId());
log.debug("Target {} has pending cancellations. Will not send update message to it.",
target.getControllerId());
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,9 @@ private static boolean isCorrelationIdNotEmpty(final Message message) {
return StringUtils.hasLength(message.getMessageProperties().getCorrelationId());
}

// Exception squid:MethodCyclomaticComplexity - false positive, is a simple
// mapping
// Exception squid:MethodCyclomaticComplexity - false positive, is a simple mapping
@SuppressWarnings("squid:MethodCyclomaticComplexity")
private static Status mapStatus(final Message message, final DmfActionUpdateStatus actionUpdateStatus,
final Action action) {
private static Status mapStatus(final Message message, final DmfActionUpdateStatus actionUpdateStatus, final Action action) {
Status status = null;
switch (actionUpdateStatus.getActionStatus()) {
case DOWNLOAD: {
Expand Down

0 comments on commit 457134b

Please sign in to comment.