introduced open actionIds in MgmtTargetAssignmentResponseBody (#864)

* introduced open actionIds in MgmtTargetAssignmentResponseBody

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed SonarQube issues

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* removed unused method parameter

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* updated documentation tests

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* added limit to the alreadyAssignedActions in the AssignmentResult

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* moved alreadyAssignedActions limitation to MgmtDistributionSetMapper

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed review findings

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* removed alreadyAssignedActions

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed sonarQube issues

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed compilation error

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed PR review findings

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed PR review findings

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* Renamed AssignmentResult to AbstractAssignmentResult

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed review findings

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* fixed formatting

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>

* reverted method visibility

Signed-off-by: Ahmed Sayed <ahmed.sayed@bosch-si.com>
This commit is contained in:
Ahmed Sayed
2019-08-20 11:31:20 +02:00
committed by Dominic Schabel
parent 973f1952c7
commit d40b11d2ab
43 changed files with 580 additions and 402 deletions

View File

@@ -76,7 +76,7 @@ public abstract class AbstractDsAssignmentStrategy {
abstract List<JpaTarget> findTargetsForAssignment(final List<String> controllerIDs, final long distributionSetId);
/**
*
*
* @param set
* @param targets
*/

View File

@@ -242,7 +242,7 @@ public interface ActionRepository extends BaseEntityRepository<JpaAction, Long>,
/**
* Retrieves all {@link Action}s that matches the queried externalRefs.
*
*
* @param externalRefs
* for which the actions need to be found
* @param active
@@ -518,7 +518,7 @@ public interface ActionRepository extends BaseEntityRepository<JpaAction, Long>,
/**
* Updates the externalRef of an action by its actionId.
*
*
* @param actionId
* for which the externalRef is being updated.
* @param externalRef

View File

@@ -37,7 +37,6 @@ import org.eclipse.hawkbit.repository.ActionFields;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RepositoryConstants;
import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.event.remote.TargetAssignDistributionSetEvent;
import org.eclipse.hawkbit.repository.exception.CancelActionNotAllowedException;
@@ -122,7 +121,6 @@ public class JpaDeploymentManagement implements DeploymentManagement {
private final DistributionSetRepository distributionSetRepository;
private final TargetRepository targetRepository;
private final ActionStatusRepository actionStatusRepository;
private final TargetManagement targetManagement;
private final AuditorAware<String> auditorProvider;
private final VirtualPropertyReplacer virtualPropertyReplacer;
private final PlatformTransactionManager txManager;
@@ -136,10 +134,9 @@ public class JpaDeploymentManagement implements DeploymentManagement {
protected JpaDeploymentManagement(final EntityManager entityManager, final ActionRepository actionRepository,
final DistributionSetRepository distributionSetRepository, final TargetRepository targetRepository,
final ActionStatusRepository actionStatusRepository, final TargetManagement targetManagement,
final AuditorAware<String> auditorProvider, final EventPublisherHolder eventPublisherHolder,
final AfterTransactionCommitExecutor afterCommit, final VirtualPropertyReplacer virtualPropertyReplacer,
final PlatformTransactionManager txManager,
final ActionStatusRepository actionStatusRepository, final AuditorAware<String> auditorProvider,
final EventPublisherHolder eventPublisherHolder, final AfterTransactionCommitExecutor afterCommit,
final VirtualPropertyReplacer virtualPropertyReplacer, final PlatformTransactionManager txManager,
final TenantConfigurationManagement tenantConfigurationManagement, final QuotaManagement quotaManagement,
final SystemSecurityContext systemSecurityContext, final TenantAware tenantAware, final Database database) {
this.entityManager = entityManager;
@@ -147,7 +144,6 @@ public class JpaDeploymentManagement implements DeploymentManagement {
this.distributionSetRepository = distributionSetRepository;
this.targetRepository = targetRepository;
this.actionStatusRepository = actionStatusRepository;
this.targetManagement = targetManagement;
this.auditorProvider = auditorProvider;
this.virtualPropertyReplacer = virtualPropertyReplacer;
this.txManager = txManager;
@@ -263,50 +259,70 @@ public class JpaDeploymentManagement implements DeploymentManagement {
final AbstractDsAssignmentStrategy assignmentStrategy) {
final JpaDistributionSet distributionSetEntity = getAndValidateDsById(dsID);
final List<String> controllerIDs = getControllerIdsForAssignmentAndCheckQuota(targetsWithActionType,
distributionSetEntity);
final List<JpaTarget> targetEntities = assignmentStrategy.findTargetsForAssignment(controllerIDs,
checkQuotaForAssignment(targetsWithActionType, distributionSetEntity);
final List<JpaTarget> targetEntities = assignmentStrategy.findTargetsForAssignment(
targetsWithActionType.stream().map(TargetWithActionType::getControllerId).collect(Collectors.toList()),
distributionSetEntity.getId());
if (targetEntities.isEmpty()) {
// detaching as it is not necessary to persist the set itself
entityManager.detach(distributionSetEntity);
// return with nothing as all targets had the DS already assigned
return new DistributionSetAssignmentResult(distributionSetEntity, Collections.emptyList(), 0,
targetsWithActionType.size(), Collections.emptyList(), targetManagement);
return allTargetsAlreadyAssignedResult(distributionSetEntity, targetsWithActionType.size());
}
// split tIDs length into max entries in-statement because many database
// have constraint of max entries in in-statements e.g. Oracle with
// maximum 1000 elements, so we need to split the entries here and
// execute multiple statements
final List<List<Long>> targetEntitiesIdsChunks = Lists.partition(
targetEntities.stream().map(Target::getId).collect(Collectors.toList()),
Constants.MAX_ENTRIES_IN_STATEMENT);
final List<JpaAction> assignedActions = doAssignDistributionSetToTargets(targetsWithActionType,
actionMessage, assignmentStrategy, distributionSetEntity, targetEntities);
return buildAssignmentResult(distributionSetEntity, assignedActions, targetsWithActionType.size());
}
private DistributionSetAssignmentResult allTargetsAlreadyAssignedResult(
final JpaDistributionSet distributionSetEntity, final int alreadyAssignedCount) {
// detaching as it is not necessary to persist the set itself
entityManager.detach(distributionSetEntity);
// return with nothing as all targets had the DS already assigned
return new DistributionSetAssignmentResult(distributionSetEntity, alreadyAssignedCount,
Collections.emptyList());
}
private List<JpaAction> doAssignDistributionSetToTargets(
final Collection<TargetWithActionType> targetsWithActionType, final String actionMessage,
final AbstractDsAssignmentStrategy assignmentStrategy, final JpaDistributionSet distributionSetEntity,
final List<JpaTarget> targetEntities) {
final List<List<Long>> targetEntitiesIdsChunks = getTargetEntitiesAsChunks(targetEntities);
closeOrCancelActiveActions(assignmentStrategy, targetEntitiesIdsChunks);
// cancel all scheduled actions which are in-active, these actions were
// not active before and the manual assignment which has been done
// cancels them
targetEntitiesIdsChunks.forEach(this::cancelInactiveScheduledActionsForTargets);
setAssignedDistributionSetAndTargetUpdateStatus(assignmentStrategy, distributionSetEntity,
targetEntitiesIdsChunks);
final Map<String, JpaAction> controllerIdsToActions = createActions(targetsWithActionType, targetEntities,
assignmentStrategy, distributionSetEntity);
final List<JpaAction> assignedActions = createActions(targetsWithActionType, targetEntities, assignmentStrategy,
distributionSetEntity);
// create initial action status when action is created so we remember
// the initial running status because we will change the status
// of the action itself and with this action status we have a nicer
// action history.
createActionsStatus(controllerIdsToActions.values(), assignmentStrategy, actionMessage);
createActionsStatus(assignedActions, assignmentStrategy, actionMessage);
detachEntitiesAndSendTargetUpdatedEvents(distributionSetEntity, targetEntities, assignmentStrategy);
return assignedActions;
}
return new DistributionSetAssignmentResult(distributionSetEntity,
targetEntities.stream().map(Target::getControllerId).collect(Collectors.toList()),
targetEntities.size(), controllerIDs.size() - targetEntities.size(),
Lists.newArrayList(controllerIdsToActions.values()), targetManagement);
/**
* split tIDs length into max entries in-statement because many database
* have constraint of max entries in in-statements e.g. Oracle with maximum
* 1000 elements, so we need to split the entries here and execute multiple
* statements
*/
private static List<List<Long>> getTargetEntitiesAsChunks(final List<JpaTarget> targetEntities) {
return Lists.partition(targetEntities.stream().map(Target::getId).collect(Collectors.toList()),
Constants.MAX_ENTRIES_IN_STATEMENT);
}
private static DistributionSetAssignmentResult buildAssignmentResult(final JpaDistributionSet distributionSet,
final List<JpaAction> assignedActions, final int totalTargetsForAssignment) {
int alreadyAssignedTargetsCount = totalTargetsForAssignment - assignedActions.size();
return new DistributionSetAssignmentResult(distributionSet, alreadyAssignedTargetsCount, assignedActions);
}
private JpaDistributionSet getAndValidateDsById(final Long dsID) {
@@ -321,17 +337,12 @@ public class JpaDeploymentManagement implements DeploymentManagement {
return distributionSet;
}
private List<String> getControllerIdsForAssignmentAndCheckQuota(
final Collection<TargetWithActionType> targetsWithActionType, final JpaDistributionSet distributionSet) {
final List<String> controllerIDs = targetsWithActionType.stream().map(TargetWithActionType::getControllerId)
.collect(Collectors.toList());
private void checkQuotaForAssignment(final Collection<TargetWithActionType> targetsWithActionType,
final JpaDistributionSet distributionSet) {
// enforce the 'max targets per manual assignment' quota
if (!controllerIDs.isEmpty()) {
assertMaxTargetsPerManualAssignmentQuota(distributionSet.getId(), controllerIDs.size());
if (!targetsWithActionType.isEmpty()) {
assertMaxTargetsPerManualAssignmentQuota(distributionSet.getId(), targetsWithActionType.size());
}
return controllerIDs;
}
/**
@@ -382,15 +393,14 @@ public class JpaDeploymentManagement implements DeploymentManagement {
assignmentStrategy.setAssignedDistributionSetAndTargetStatus(set, targetIdsChunks, currentUser);
}
private Map<String, JpaAction> createActions(final Collection<TargetWithActionType> targetsWithActionType,
private List<JpaAction> createActions(final Collection<TargetWithActionType> targetsWithActionType,
final List<JpaTarget> targets, final AbstractDsAssignmentStrategy assignmentStrategy,
final JpaDistributionSet set) {
final Map<String, TargetWithActionType> targetsWithActionMap = targetsWithActionType.stream()
.collect(Collectors.toMap(TargetWithActionType::getControllerId, Function.identity()));
return targets.stream().map(trg -> assignmentStrategy.createTargetAction(targetsWithActionMap, trg, set))
.filter(Objects::nonNull).map(actionRepository::save)
.collect(Collectors.toMap(action -> action.getTarget().getControllerId(), Function.identity()));
.filter(Objects::nonNull).map(actionRepository::save).collect(Collectors.toList());
}
private void createActionsStatus(final Collection<JpaAction> actions,
@@ -590,7 +600,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Optional<Action> findAction(final long actionId) {
return actionRepository.findById(actionId).map(a -> (Action) a);
return actionRepository.findById(actionId).map(a -> a);
}
@Override
@@ -703,8 +713,8 @@ public class JpaDeploymentManagement implements DeploymentManagement {
final CriteriaQuery<String> selMsgQuery = msgQuery.select(join);
selMsgQuery.where(cb.equal(as.get(JpaActionStatus_.id), actionStatusId));
final List<String> result = entityManager.createQuery(selMsgQuery).setFirstResult((int) pageable.getOffset())
.setMaxResults(pageable.getPageSize()).getResultList().stream().collect(Collectors.toList());
final List<String> result = new ArrayList<>(entityManager.createQuery(selMsgQuery)
.setFirstResult((int) pageable.getOffset()).setMaxResults(pageable.getPageSize()).getResultList());
return new PageImpl<>(result, pageable, totalCount);
}
@@ -810,4 +820,4 @@ public class JpaDeploymentManagement implements DeploymentManagement {
.runAsSystem(() -> tenantConfigurationManagement.getConfigurationValue(key, valueType).getValue());
}
}
}

View File

@@ -188,14 +188,13 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
toBeChangedDSs.add(set);
}
}
result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(), 0,
toBeChangedDSs.size(), Collections.emptyList(),
result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(),
Collections.emptyList(),
Collections.unmodifiableList(
toBeChangedDSs.stream().map(distributionSetRepository::save).collect(Collectors.toList())),
myTag);
} else {
result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(), toBeChangedDSs.size(),
0,
result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(),
Collections.unmodifiableList(
toBeChangedDSs.stream().map(distributionSetRepository::save).collect(Collectors.toList())),
Collections.emptyList(), myTag);
@@ -386,7 +385,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
specList = Arrays.asList(DistributionSetSpecification.isDeleted(false),
DistributionSetSpecification.isCompleted(complete));
} else {
specList = Arrays.asList(DistributionSetSpecification.isDeleted(false));
specList = Collections.singletonList(DistributionSetSpecification.isDeleted(false));
}
return convertDsPage(findByCriteriaAPI(pageReq, specList), pageReq);
@@ -754,7 +753,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
public void delete(final long setId) {
throwExceptionIfDistributionSetDoesNotExist(setId);
delete(Arrays.asList(setId));
delete(Collections.singletonList(setId));
}
private void throwExceptionIfDistributionSetDoesNotExist(final Long setId) {
@@ -811,7 +810,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Override
public Optional<DistributionSet> get(final long id) {
return distributionSetRepository.findById(id).map(d -> (DistributionSet) d);
return distributionSetRepository.findById(id).map(d -> d);
}
@Override

View File

@@ -294,7 +294,7 @@ public class JpaTargetManagement implements TargetManagement {
final Long targetId = getByControllerIdAndThrowIfNotFound(controllerId).getId();
return targetMetadataRepository.findById(new TargetMetadataCompositeKey(targetId, key))
.map(t -> (TargetMetadata) t);
.map(t -> t);
}
@Override
@@ -470,14 +470,14 @@ public class JpaTargetManagement implements TargetManagement {
specList.add(TargetSpecifications
.likeIdOrNameOrDescriptionOrAttributeValue(filterParams.getFilterBySearchText()));
}
if (isHasTagsFilterActive(filterParams)) {
if (hasTagsFilterActive(filterParams)) {
specList.add(TargetSpecifications.hasTags(filterParams.getFilterByTagNames(),
filterParams.getSelectTargetWithNoTag()));
}
return specList;
}
private static boolean isHasTagsFilterActive(final FilterParams filterParams) {
private static boolean hasTagsFilterActive(final FilterParams filterParams) {
return ((filterParams.getSelectTargetWithNoTag() != null) && filterParams.getSelectTargetWithNoTag())
|| ((filterParams.getFilterByTagNames() != null) && (filterParams.getFilterByTagNames().length > 0));
}
@@ -520,7 +520,7 @@ public class JpaTargetManagement implements TargetManagement {
// all are already assigned -> unassign
if (alreadyAssignedTargets.size() == allTargets.size()) {
alreadyAssignedTargets.forEach(target -> target.removeTag(tag));
return new TargetTagAssignmentResult(0, 0, alreadyAssignedTargets.size(), Collections.emptyList(),
return new TargetTagAssignmentResult(0, Collections.emptyList(),
Collections.unmodifiableList(alreadyAssignedTargets), tag);
}
@@ -528,7 +528,6 @@ public class JpaTargetManagement implements TargetManagement {
// some or none are assigned -> assign
allTargets.forEach(target -> target.addTag(tag));
final TargetTagAssignmentResult result = new TargetTagAssignmentResult(alreadyAssignedTargets.size(),
allTargets.size(), 0,
Collections
.unmodifiableList(allTargets.stream().map(targetRepository::save).collect(Collectors.toList())),
Collections.emptyList(), tag);
@@ -776,7 +775,7 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Optional<Target> get(final long id) {
return targetRepository.findById(id).map(t -> (Target) t);
return targetRepository.findById(id).map(t -> t);
}
@Override

View File

@@ -59,7 +59,7 @@ public class OnlineDsAssignmentStrategy extends AbstractDsAssignmentStrategy {
@Override
void sendTargetUpdatedEvents(final DistributionSet set, final List<JpaTarget> targets) {
targets.stream().forEach(target -> {
targets.forEach(target -> {
target.setUpdateStatus(TargetUpdateStatus.PENDING);
sendTargetUpdatedEvent(target);
});
@@ -77,7 +77,7 @@ public class OnlineDsAssignmentStrategy extends AbstractDsAssignmentStrategy {
@Override
void sendDeploymentEvents(final List<DistributionSetAssignmentResult> assignmentResults) {
if (isMultiAssignmentsEnabled()) {
sendDeploymentEvent(assignmentResults.stream().flatMap(result -> result.getActions().stream())
sendDeploymentEvent(assignmentResults.stream().flatMap(result -> result.getAssignedEntity().stream())
.collect(Collectors.toList()));
} else {
assignmentResults.forEach(this::sendDistributionSetAssignedEvent);
@@ -170,7 +170,7 @@ public class OnlineDsAssignmentStrategy extends AbstractDsAssignmentStrategy {
private DistributionSetAssignmentResult sendDistributionSetAssignedEvent(
final DistributionSetAssignmentResult assignmentResult) {
final List<Action> filteredActions = filterCancellations(assignmentResult.getActions())
final List<Action> filteredActions = filterCancellations(assignmentResult.getAssignedEntity())
.filter(action -> !hasPendingCancellations(action.getTarget())).collect(Collectors.toList());
final DistributionSet set = assignmentResult.getDistributionSet();
sendTargetAssignDistributionSetEvent(set.getTenant(), set.getId(), filteredActions);

View File

@@ -666,16 +666,16 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
DeploymentManagement deploymentManagement(final EntityManager entityManager,
final ActionRepository actionRepository, final DistributionSetRepository distributionSetRepository,
final TargetRepository targetRepository, final ActionStatusRepository actionStatusRepository,
final TargetManagement targetManagement, final AuditorAware<String> auditorProvider,
final EventPublisherHolder eventPublisherHolder, final AfterTransactionCommitExecutor afterCommit,
final VirtualPropertyReplacer virtualPropertyReplacer, final PlatformTransactionManager txManager,
final AuditorAware<String> auditorProvider, final EventPublisherHolder eventPublisherHolder,
final AfterTransactionCommitExecutor afterCommit, final VirtualPropertyReplacer virtualPropertyReplacer,
final PlatformTransactionManager txManager,
final TenantConfigurationManagement tenantConfigurationManagement, final QuotaManagement quotaManagement,
final SystemSecurityContext systemSecurityContext, final TenantAware tenantAware,
final JpaProperties properties) {
return new JpaDeploymentManagement(entityManager, actionRepository, distributionSetRepository, targetRepository,
actionStatusRepository, targetManagement, auditorProvider, eventPublisherHolder, afterCommit,
virtualPropertyReplacer, txManager, tenantConfigurationManagement, quotaManagement,
systemSecurityContext, tenantAware, properties.getDatabase());
actionStatusRepository, auditorProvider, eventPublisherHolder, afterCommit, virtualPropertyReplacer,
txManager, tenantConfigurationManagement, quotaManagement, systemSecurityContext, tenantAware,
properties.getDatabase());
}
/**