diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java index 1317ea8c85..c6779c3039 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java @@ -22,7 +22,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -191,11 +190,14 @@ protected void onActionStatusUpdate(final JpaActionStatus newActionStatus, final break; } case FINISHED: { - handleFinishedAndStoreInTargetStatus(occurredAt, action).ifPresent(this::requestControllerAttributes); + requestControllerAttributes(handleFinishedAndStoreInTargetStatus(occurredAt, action)); break; } case DOWNLOADED: { - handleDownloadedActionStatus(action).ifPresent(this::requestControllerAttributes); + handleDownloadedActionStatus(action).ifPresent(controllerId -> + requestControllerAttributes(getByControllerId(controllerId) + .map(JpaTarget.class::cast) + .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)))); break; } default: { @@ -282,8 +284,6 @@ public Action addUpdateActionStatus(final ActionStatusCreate statusCreate) { return addActionStatus((JpaActionStatusCreate) statusCreate); } - private static final Pageable PAGEABLE_1 = PageRequest.of(0, 1); - @Override public Optional findActiveActionWithHighestWeight(final String controllerId) { return Stream.concat( @@ -328,11 +328,8 @@ public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, fi @Override @Transactional(isolation = Isolation.READ_COMMITTED) @Retryable(retryFor = ConcurrencyFailureException.class, exclude = EntityAlreadyExistsException.class, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY)) - public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, final URI address, - final String name, final String type) { - final Specification spec = - (targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTarget_.controllerId), controllerId); - + public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, final URI address, final String name, final String type) { + final Specification spec = (targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTarget_.controllerId), controllerId); return targetRepository.findOne(spec).map(target -> updateTarget(target, address, name, type)) .orElseGet(() -> createTarget(controllerId, address, name, type)); } @@ -845,10 +842,7 @@ private Optional handleDownloadedActionStatus(final JpaAction action) { return Optional.of(target.getControllerId()); } - private void requestControllerAttributes(final String controllerId) { - final JpaTarget target = (JpaTarget) getByControllerId(controllerId) - .orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId)); - + private void requestControllerAttributes(final JpaTarget target) { target.setRequestControllerAttributes(true); eventPublisherHolder.getEventPublisher() @@ -866,15 +860,13 @@ private void handleErrorOnAction(final JpaAction mergedAction, final JpaTarget m } /** - * Handles the case where the {@link Action.Status#FINISHED} status is - * reported by the device. In case the update is finished, a controllerId - * will be returned to trigger a request for attributes. + * Handles the case where the {@link Action.Status#FINISHED} status is reported by the device. In case the update is finished, + * a controllerId will be returned to trigger a request for attributes. * * @param action updated action - * @return a present controllerId in case the attributes needs to be - * requested. + * @return a present controllerId in case the attributes needs to be requested. */ - private Optional handleFinishedAndStoreInTargetStatus(final long occurredAt, final JpaAction action) { + private JpaTarget handleFinishedAndStoreInTargetStatus(final long occurredAt, final JpaAction action) { final JpaTarget target = (JpaTarget) action.getTarget(); action.setActive(false); action.setStatus(Status.FINISHED); @@ -884,10 +876,8 @@ private Optional handleFinishedAndStoreInTargetStatus(final long occurre target.setInstalledDistributionSet(ds); target.setInstallationDate(occurredAt); - // Target reported an installation of a DOWNLOAD_ONLY assignment, the - // assigned DS has to be adapted - // because the currently assigned DS can be unequal to the currently - // installed DS (the downloadOnly DS) + // Target reported an installation of a DOWNLOAD_ONLY assignment, the assigned DS has to be adapted + // because the currently assigned DS can be unequal to the currently installed DS (the downloadOnly DS) if (isDownloadOnly(action)) { target.setAssignedDistributionSet((JpaDistributionSet) action.getDistributionSet()); } @@ -899,11 +889,10 @@ private Optional handleFinishedAndStoreInTargetStatus(final long occurre target.setUpdateStatus(TargetUpdateStatus.IN_SYNC); } - targetRepository.save(target); entityManager.detach(ds); } - return Optional.of(target.getControllerId()); + return target; } private void assertTargetAttributesQuota(final JpaTarget target) { diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetManagementTest.java index 11d786d2f3..3dfea5da92 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetManagementTest.java @@ -343,7 +343,7 @@ void findTargetByControllerIDWithDetails() { assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(testDs2.getId())) .as("For newly created distributions sets the assigned target count should be zero").isFalse(); - Target target = createTargetWithAttributes("4711"); + createTargetWithAttributes("4711"); final long current = System.currentTimeMillis(); controllerManagement.findOrRegisterTargetIfItDoesNotExist("4711", LOCALHOST); @@ -356,7 +356,7 @@ void findTargetByControllerIDWithDetails() { assignDistributionSet(testDs2.getId(), "4711"); implicitLock(testDs2); - target = targetManagement.getByControllerID("4711").orElseThrow(IllegalStateException::new); + Target target = targetManagement.getByControllerID("4711").orElseThrow(IllegalStateException::new); // read data assertThat(targetManagement.countByAssignedDistributionSet(testDs1.getId())).as("Target count is wrong") @@ -1362,8 +1362,7 @@ private Target createTargetWithAttributes(final String controllerId) { targetManagement.create(entityFactory.target().create().controllerId(controllerId)); final Target target = controllerManagement.updateControllerAttributes(controllerId, testData, null); - assertThat(targetManagement.getControllerAttributes(controllerId)).as("Controller Attributes are wrong") - .isEqualTo(testData); + assertThat(targetManagement.getControllerAttributes(controllerId)).as("Controller Attributes are wrong").isEqualTo(testData); return target; }