Remove DMF API dependency from security integration (#604)
* Dmf security token out of API. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Allow to override dispatching routines. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * TargetAssign event is bulk ready. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Completed Javadoc. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * readibility and fix serialization bug. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix sonar issue. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Simplify artifact management usage. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -31,6 +31,7 @@ import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetWithActionType;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* {@link DistributionSet} to {@link Target} assignment strategy as utility for
|
||||
@@ -109,6 +110,8 @@ public abstract class AbstractDsAssignmentStrategy {
|
||||
/**
|
||||
* Handles event sending related to the assignment.
|
||||
*
|
||||
* @param set
|
||||
* that has been assigned
|
||||
* @param targets
|
||||
* to send events for
|
||||
* @param targetIdsCancelList
|
||||
@@ -117,12 +120,17 @@ public abstract class AbstractDsAssignmentStrategy {
|
||||
* mapping of {@link Target#getControllerId()} to new
|
||||
* {@link Action} that was created as part of the assignment.
|
||||
*/
|
||||
abstract void sendAssignmentEvents(final List<JpaTarget> targets, final Set<Long> targetIdsCancelList,
|
||||
final Map<String, JpaAction> controllerIdsToActions);
|
||||
abstract void sendAssignmentEvents(DistributionSet set, final List<JpaTarget> targets,
|
||||
final Set<Long> targetIdsCancelList, final Map<String, JpaAction> controllerIdsToActions);
|
||||
|
||||
protected void sendTargetAssignDistributionSetEvent(final Action action) {
|
||||
afterCommit.afterCommit(() -> eventPublisher
|
||||
.publishEvent(new TargetAssignDistributionSetEvent(action, applicationContext.getId())));
|
||||
protected void sendTargetAssignDistributionSetEvent(final String tenant, final long distributionSetId,
|
||||
final List<Action> actions) {
|
||||
if (CollectionUtils.isEmpty(actions)) {
|
||||
return;
|
||||
}
|
||||
|
||||
afterCommit.afterCommit(() -> eventPublisher.publishEvent(
|
||||
new TargetAssignDistributionSetEvent(tenant, distributionSetId, actions, applicationContext.getId())));
|
||||
}
|
||||
|
||||
protected void sendTargetUpdatedEvent(final JpaTarget target) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
@@ -24,7 +25,6 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.ListJoin;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ActionFields;
|
||||
import org.eclipse.hawkbit.repository.DeploymentManagement;
|
||||
@@ -80,6 +80,7 @@ import org.springframework.transaction.annotation.Isolation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -306,7 +307,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
// detaching as the entity has been updated by the JPQL query above
|
||||
targets.forEach(entityManager::detach);
|
||||
|
||||
assignmentStrategy.sendAssignmentEvents(targets, targetIdsCancellList, targetIdsToActions);
|
||||
assignmentStrategy.sendAssignmentEvents(set, targets, targetIdsCancellList, targetIdsToActions);
|
||||
|
||||
return new DistributionSetAssignmentResult(
|
||||
targets.stream().map(Target::getControllerId).collect(Collectors.toList()), targets.size(),
|
||||
@@ -373,13 +374,13 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long startScheduledActionsByRolloutGroupParent(@NotNull final Long rolloutId,
|
||||
public long startScheduledActionsByRolloutGroupParent(final Long rolloutId, final Long distributionSetId,
|
||||
final Long rolloutGroupParentId) {
|
||||
long totalActionsCount = 0L;
|
||||
long lastStartedActionsCount;
|
||||
do {
|
||||
lastStartedActionsCount = startScheduledActionsByRolloutGroupParentInNewTransaction(rolloutId,
|
||||
rolloutGroupParentId, ACTION_PAGE_LIMIT);
|
||||
distributionSetId, rolloutGroupParentId, ACTION_PAGE_LIMIT);
|
||||
totalActionsCount += lastStartedActionsCount;
|
||||
} while (lastStartedActionsCount > 0);
|
||||
|
||||
@@ -387,7 +388,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
}
|
||||
|
||||
private long startScheduledActionsByRolloutGroupParentInNewTransaction(final Long rolloutId,
|
||||
final Long rolloutGroupParentId, final int limit) {
|
||||
final Long distributionSetId, final Long rolloutGroupParentId, final int limit) {
|
||||
final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
def.setName("startScheduledActions-" + rolloutId);
|
||||
def.setReadOnly(false);
|
||||
@@ -396,7 +397,21 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
final Page<Action> rolloutGroupActions = findActionsByRolloutAndRolloutGroupParent(rolloutId,
|
||||
rolloutGroupParentId, limit);
|
||||
|
||||
rolloutGroupActions.map(action -> (JpaAction) action).forEach(this::startScheduledAction);
|
||||
if (rolloutGroupActions.getContent().isEmpty()) {
|
||||
return 0L;
|
||||
}
|
||||
|
||||
final String tenant = rolloutGroupActions.getContent().get(0).getTenant();
|
||||
|
||||
final List<Action> targetAssignments = rolloutGroupActions.getContent().stream()
|
||||
.map(action -> (JpaAction) action).map(this::closeActionIfSetWasAlreadyAssigned)
|
||||
.filter(Objects::nonNull).map(this::startScheduledActionIfNoCancelationHasToBeHandledFirst)
|
||||
.filter(Objects::nonNull).collect(Collectors.toList());
|
||||
|
||||
if (!CollectionUtils.isEmpty(targetAssignments)) {
|
||||
afterCommit.afterCommit(() -> eventPublisher.publishEvent(new TargetAssignDistributionSetEvent(tenant,
|
||||
distributionSetId, targetAssignments, applicationContext.getId())));
|
||||
}
|
||||
|
||||
return rolloutGroupActions.getTotalElements();
|
||||
});
|
||||
@@ -415,9 +430,8 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
}
|
||||
}
|
||||
|
||||
private void startScheduledAction(final JpaAction action) {
|
||||
JpaTarget target = (JpaTarget) action.getTarget();
|
||||
|
||||
private JpaAction closeActionIfSetWasAlreadyAssigned(final JpaAction action) {
|
||||
final JpaTarget target = (JpaTarget) action.getTarget();
|
||||
if (target.getAssignedDistributionSet() != null
|
||||
&& action.getDistributionSet().getId().equals(target.getAssignedDistributionSet().getId())) {
|
||||
// the target has already the distribution set assigned, we don't
|
||||
@@ -426,9 +440,13 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
action.setActive(false);
|
||||
setSkipActionStatus(action);
|
||||
actionRepository.save(action);
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
return action;
|
||||
}
|
||||
|
||||
private JpaAction startScheduledActionIfNoCancelationHasToBeHandledFirst(final JpaAction action) {
|
||||
// check if we need to override running update actions
|
||||
final List<Long> overrideObsoleteUpdateActions = onlineDsAssignmentStrategy
|
||||
.overrideObsoleteUpdateActions(Collections.singletonList(action.getTarget().getId()));
|
||||
@@ -439,7 +457,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
|
||||
actionStatusRepository.save(onlineDsAssignmentStrategy.createActionStatus(savedAction, null));
|
||||
|
||||
target = (JpaTarget) entityManager.merge(savedAction.getTarget());
|
||||
final JpaTarget target = (JpaTarget) entityManager.merge(savedAction.getTarget());
|
||||
|
||||
target.setAssignedDistributionSet(savedAction.getDistributionSet());
|
||||
target.setUpdateStatus(TargetUpdateStatus.PENDING);
|
||||
@@ -447,10 +465,11 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
|
||||
// in case we canceled an action before for this target, then don't fire
|
||||
// assignment event
|
||||
if (!overrideObsoleteUpdateActions.contains(savedAction.getId())) {
|
||||
afterCommit.afterCommit(() -> eventPublisher
|
||||
.publishEvent(new TargetAssignDistributionSetEvent(savedAction, applicationContext.getId())));
|
||||
if (overrideObsoleteUpdateActions.contains(savedAction.getId())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return savedAction;
|
||||
}
|
||||
|
||||
private void setSkipActionStatus(final JpaAction action) {
|
||||
|
||||
@@ -367,8 +367,7 @@ public class JpaRolloutManagement extends AbstractRolloutManagement {
|
||||
RolloutGroupStatus.READY, group);
|
||||
|
||||
final long targetsInGroupFilter = runInNewTransaction("countAllTargetsByTargetFilterQueryAndNotInRolloutGroups",
|
||||
count -> targetManagement.countByRsqlAndNotInRolloutGroups(readyGroups,
|
||||
groupTargetFilter));
|
||||
count -> targetManagement.countByRsqlAndNotInRolloutGroups(readyGroups, groupTargetFilter));
|
||||
final long expectedInGroup = Math.round(group.getTargetPercentage() / 100 * (double) targetsInGroupFilter);
|
||||
final long currentlyInGroup = runInNewTransaction("countRolloutTargetGroupByRolloutGroup",
|
||||
count -> rolloutTargetGroupRepository.countByRolloutGroup(group));
|
||||
@@ -408,8 +407,8 @@ public class JpaRolloutManagement extends AbstractRolloutManagement {
|
||||
final PageRequest pageRequest = new PageRequest(0, Math.toIntExact(limit));
|
||||
final List<Long> readyGroups = RolloutHelper.getGroupsByStatusIncludingGroup(rollout.getRolloutGroups(),
|
||||
RolloutGroupStatus.READY, group);
|
||||
final Page<Target> targets = targetManagement
|
||||
.findByTargetFilterQueryAndNotInRolloutGroups(pageRequest, readyGroups, targetFilter);
|
||||
final Page<Target> targets = targetManagement.findByTargetFilterQueryAndNotInRolloutGroups(pageRequest,
|
||||
readyGroups, targetFilter);
|
||||
|
||||
createAssignmentOfTargetsToGroup(targets, group);
|
||||
|
||||
@@ -461,7 +460,8 @@ public class JpaRolloutManagement extends AbstractRolloutManagement {
|
||||
throw new RolloutIllegalStateException("First Group is not the first group.");
|
||||
}
|
||||
|
||||
deploymentManagement.startScheduledActionsByRolloutGroupParent(rollout.getId(), null);
|
||||
deploymentManagement.startScheduledActionsByRolloutGroupParent(rollout.getId(),
|
||||
rollout.getDistributionSet().getId(), null);
|
||||
|
||||
rolloutGroup.setStatus(RolloutGroupStatus.RUNNING);
|
||||
rolloutGroupRepository.save(rolloutGroup);
|
||||
@@ -530,8 +530,7 @@ public class JpaRolloutManagement extends AbstractRolloutManagement {
|
||||
final ActionType actionType = rollout.getActionType();
|
||||
final long forceTime = rollout.getForcedTime();
|
||||
|
||||
final Page<Target> targets = targetManagement.findByInRolloutGroupWithoutAction(pageRequest,
|
||||
groupId);
|
||||
final Page<Target> targets = targetManagement.findByInRolloutGroupWithoutAction(pageRequest, groupId);
|
||||
if (targets.getTotalElements() > 0) {
|
||||
createScheduledAction(targets.getContent(), distributionSet, actionType, forceTime, rollout, group);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
|
||||
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
|
||||
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.eclipse.hawkbit.repository.model.TargetWithActionType;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -48,8 +49,8 @@ public class OfflineDsAssignmentStrategy extends AbstractDsAssignmentStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
void sendAssignmentEvents(final List<JpaTarget> targets, final Set<Long> targetIdsCancellList,
|
||||
final Map<String, JpaAction> targetIdsToActions) {
|
||||
void sendAssignmentEvents(final DistributionSet set, final List<JpaTarget> targets,
|
||||
final Set<Long> targetIdsCancellList, final Map<String, JpaAction> targetIdsToActions) {
|
||||
|
||||
targets.forEach(target -> {
|
||||
target.setUpdateStatus(TargetUpdateStatus.IN_SYNC);
|
||||
|
||||
@@ -21,7 +21,10 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
|
||||
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
|
||||
import org.eclipse.hawkbit.repository.model.Action;
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.eclipse.hawkbit.repository.model.TargetWithActionType;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -44,18 +47,19 @@ public class OnlineDsAssignmentStrategy extends AbstractDsAssignmentStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
void sendAssignmentEvents(final List<JpaTarget> targets, final Set<Long> targetIdsCancellList,
|
||||
final Map<String, JpaAction> targetIdsToActions) {
|
||||
void sendAssignmentEvents(final DistributionSet set, final List<JpaTarget> targets,
|
||||
final Set<Long> targetIdsCancellList, final Map<String, JpaAction> targetIdsToActions) {
|
||||
|
||||
targets.forEach(target -> {
|
||||
final List<Action> actions = targets.stream().map(target -> {
|
||||
target.setUpdateStatus(TargetUpdateStatus.PENDING);
|
||||
sendTargetUpdatedEvent(target);
|
||||
if (targetIdsCancellList.contains(target.getId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendTargetAssignDistributionSetEvent(targetIdsToActions.get(target.getControllerId()));
|
||||
});
|
||||
return target;
|
||||
}).filter(target -> !targetIdsCancellList.contains(target.getId())).map(Target::getControllerId)
|
||||
.map(targetIdsToActions::get).collect(Collectors.toList());
|
||||
|
||||
sendTargetAssignDistributionSetEvent(set.getTenant(), set.getId(), actions);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,8 +56,8 @@ public class StartNextGroupRolloutGroupSuccessAction implements RolloutGroupActi
|
||||
// retrieve all actions according to the parent group of the finished
|
||||
// rolloutGroup, so retrieve all child-group actions which need to be
|
||||
// started.
|
||||
final long countOfStartedActions = deploymentManagement
|
||||
.startScheduledActionsByRolloutGroupParent(rollout.getId(), rolloutGroup.getId());
|
||||
final long countOfStartedActions = deploymentManagement.startScheduledActionsByRolloutGroupParent(
|
||||
rollout.getId(), rollout.getDistributionSet().getId(), rolloutGroup.getId());
|
||||
logger.debug("{} Next actions started for rollout {} and parent group {}", countOfStartedActions, rollout,
|
||||
rolloutGroup);
|
||||
if (countOfStartedActions > 0) {
|
||||
|
||||
Reference in New Issue
Block a user