diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherService.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherService.java index c806536ec..193bbff16 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherService.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherService.java @@ -44,6 +44,7 @@ import org.eclipse.hawkbit.dmf.json.model.DmfSoftwareModule; import org.eclipse.hawkbit.dmf.json.model.DmfTarget; import org.eclipse.hawkbit.repository.DeploymentManagement; import org.eclipse.hawkbit.repository.DistributionSetManagement; +import org.eclipse.hawkbit.repository.RepositoryProperties; import org.eclipse.hawkbit.repository.SoftwareModuleManagement; import org.eclipse.hawkbit.repository.SystemManagement; import org.eclipse.hawkbit.repository.TargetManagement; @@ -102,6 +103,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService { private final DistributionSetManagement distributionSetManagement; private final DeploymentManagement deploymentManagement; private final TenantConfigurationManagement tenantConfigurationManagement; + private final RepositoryProperties repositoryProperties; @SuppressWarnings("java:S107") protected AmqpMessageDispatcherService( @@ -112,7 +114,8 @@ public class AmqpMessageDispatcherService extends BaseAmqpService { final SoftwareModuleManagement softwareModuleManagement, final DistributionSetManagement distributionSetManagement, final DeploymentManagement deploymentManagement, - final TenantConfigurationManagement tenantConfigurationManagement) { + final TenantConfigurationManagement tenantConfigurationManagement, + final RepositoryProperties repositoryProperties) { super(rabbitTemplate); this.artifactUrlHandler = artifactUrlHandler; this.amqpSenderService = amqpSenderService; @@ -123,6 +126,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService { this.distributionSetManagement = distributionSetManagement; this.deploymentManagement = deploymentManagement; this.tenantConfigurationManagement = tenantConfigurationManagement; + this.repositoryProperties = repositoryProperties; } public boolean isBatchAssignmentsEnabled() { @@ -281,7 +285,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService { final Map softwareModuleMetadata = getSoftwareModuleMetaData.apply(module); return softwareModuleMetadata == null ? Collections.emptyMap() : softwareModuleMetadata; }))); - final int weight = deploymentManagement.getWeightConsideringDefault(action); + final int weight = getWeightConsideringDefault(action); return new DmfMultiActionRequest.DmfMultiActionElement(getEventTypeForAction(action), actionRequest, weight); }) .toList()); @@ -292,6 +296,10 @@ public class AmqpMessageDispatcherService extends BaseAmqpService { amqpSenderService.sendMessage(message, targetAddress); } + private int getWeightConsideringDefault(final Action action) { + return action.getWeight().orElse(repositoryProperties.getActionWeightIfAbsent()); + } + /** * Method to get the type of event depending on whether the action is a DOWNLOAD_ONLY action or if it has a valid maintenance window * available or not based on defined maintenance schedule. In case of no maintenance schedule or if there is a valid window available, diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DmfApiConfiguration.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DmfApiConfiguration.java index aeac74da2..8025ecf33 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DmfApiConfiguration.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/DmfApiConfiguration.java @@ -18,6 +18,7 @@ import java.util.regex.Pattern; import lombok.ToString; import lombok.extern.slf4j.Slf4j; +import org.eclipse.hawkbit.repository.RepositoryProperties; import org.eclipse.hawkbit.repository.artifact.urlhandler.ArtifactUrlHandler; import org.eclipse.hawkbit.dmf.amqp.api.AmqpSettings; import org.eclipse.hawkbit.repository.ConfirmationManagement; @@ -268,10 +269,10 @@ public class DmfApiConfiguration { final SystemSecurityContext systemSecurityContext, final SystemManagement systemManagement, final TargetManagement targetManagement, final DistributionSetManagement distributionSetManagement, final SoftwareModuleManagement softwareModuleManagement, final DeploymentManagement deploymentManagement, - final TenantConfigurationManagement tenantConfigurationManagement) { + final TenantConfigurationManagement tenantConfigurationManagement, final RepositoryProperties repositoryProperties) { return new AmqpMessageDispatcherService(rabbitTemplate, amqpSenderService, artifactUrlHandler, systemSecurityContext, systemManagement, targetManagement, softwareModuleManagement, distributionSetManagement, - deploymentManagement, tenantConfigurationManagement); + deploymentManagement, tenantConfigurationManagement, repositoryProperties); } private static Map getTTLMaxArgsAuthenticationQueue() { diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java index 8c9fd7d7a..e7bd50957 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageDispatcherServiceTest.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Optional; import java.util.UUID; +import org.eclipse.hawkbit.repository.RepositoryProperties; import org.eclipse.hawkbit.repository.TargetManagement; import org.eclipse.hawkbit.repository.TargetManagement.Create; import org.eclipse.hawkbit.repository.artifact.ArtifactFilesystem; @@ -64,6 +65,7 @@ import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.support.converter.AbstractJavaTypeMapper; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestPropertySource; @@ -79,6 +81,9 @@ import org.springframework.test.context.TestPropertySource; "spring.main.allow-bean-definition-overriding=true" }) class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest { + @Autowired + protected RepositoryProperties repositoryProperties; + private static final String TENANT = "DEFAULT"; private static final Long TENANT_ID = 4711L; @@ -115,7 +120,7 @@ class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest { amqpMessageDispatcherService = new AmqpMessageDispatcherService(rabbitTemplate, senderService, artifactUrlHandlerMock, systemSecurityContext, systemManagement, targetManagement, - softwareModuleManagement, distributionSetManagement, deploymentManagement, tenantConfigurationManagement); + softwareModuleManagement, distributionSetManagement, deploymentManagement, tenantConfigurationManagement, repositoryProperties); } diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageDispatcherServiceIntegrationTest.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageDispatcherServiceIntegrationTest.java index c9a01574d..e3f25990b 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageDispatcherServiceIntegrationTest.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageDispatcherServiceIntegrationTest.java @@ -40,7 +40,6 @@ import org.eclipse.hawkbit.dmf.json.model.DmfMultiActionRequest; import org.eclipse.hawkbit.dmf.json.model.DmfMultiActionRequest.DmfMultiActionElement; import org.eclipse.hawkbit.dmf.json.model.DmfSoftwareModule; import org.eclipse.hawkbit.dmf.json.model.DmfTarget; -import org.eclipse.hawkbit.repository.DeploymentManagement; import org.eclipse.hawkbit.repository.event.remote.CancelTargetAssignmentEvent; import org.eclipse.hawkbit.repository.event.remote.MultiActionAssignEvent; import org.eclipse.hawkbit.repository.event.remote.MultiActionCancelEvent; @@ -64,6 +63,7 @@ import org.eclipse.hawkbit.repository.event.remote.entity.TenantConfigurationCre import org.eclipse.hawkbit.repository.exception.TenantConfigurationValueChangeNotAllowedException; import org.eclipse.hawkbit.repository.jpa.model.JpaTarget; import org.eclipse.hawkbit.repository.model.Action.ActionType; +import org.eclipse.hawkbit.repository.model.DeploymentRequest; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetAssignmentResult; import org.eclipse.hawkbit.repository.model.RepositoryModelConstants; @@ -258,13 +258,13 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt final DistributionSet ds = testdataFactory.createDistributionSet(); testdataFactory.addSoftwareModuleMetadata(ds); - final Long installActionId = makeAssignment(DeploymentManagement.deploymentRequest(controllerId, ds.getId()) - .setActionType(ActionType.FORCED).build()).getAssignedEntity().get(0).getId(); + final Long installActionId = makeAssignment(DeploymentRequest.builder(controllerId, ds.getId()) + .actionType(ActionType.FORCED).build()).getAssignedEntity().get(0).getId(); enableMultiAssignments(); - final Long downloadActionId = makeAssignment(DeploymentManagement.deploymentRequest(controllerId, ds.getId()) - .setActionType(ActionType.DOWNLOAD_ONLY).setWeight(weight1).build()).getAssignedEntity().get(0).getId(); + final Long downloadActionId = makeAssignment(DeploymentRequest.builder(controllerId, ds.getId()) + .actionType(ActionType.DOWNLOAD_ONLY).weight(weight1).build()).getAssignedEntity().get(0).getId(); final Long cancelActionId = makeAssignment( - DeploymentManagement.deploymentRequest(controllerId, ds.getId()).setWeight(DEFAULT_TEST_WEIGHT).build()) + DeploymentRequest.builder(controllerId, ds.getId()).weight(DEFAULT_TEST_WEIGHT).build()) .getAssignedEntity().get(0).getId(); // make sure the latest message in the queue is the one triggered by the // cancellation diff --git a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java index bfab8a7b9..6004ab33f 100644 --- a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java +++ b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetResource.java @@ -255,7 +255,7 @@ public class MgmtDistributionSetResource implements MgmtDistributionSetRestApi { ? tenantConfigHelper.isConfirmationFlowEnabled() : dsAssignment.getConfirmationRequired(); return MgmtDeploymentRequestMapper.createAssignmentRequestBuilder(dsAssignment, distributionSetId) - .setConfirmationRequired(isConfirmationRequired).build(); + .confirmationRequired(isConfirmationRequired).build(); }).toList(); final List assignmentResults = deployManagement.assignDistributionSets(deploymentRequests); diff --git a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java index 3c1eb6d11..09d061103 100644 --- a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java +++ b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java @@ -382,7 +382,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi { ? tenantConfigHelper.isConfirmationFlowEnabled() : dsAssignment.getConfirmationRequired(); return MgmtDeploymentRequestMapper.createAssignmentRequestBuilder(dsAssignment, targetId) - .setConfirmationRequired(isConfirmationRequired).build(); + .confirmationRequired(isConfirmationRequired).build(); }).toList(); final List assignmentResults = deploymentManagement diff --git a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/mapper/MgmtDeploymentRequestMapper.java b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/mapper/MgmtDeploymentRequestMapper.java index 2e0db2460..05bc17e7d 100644 --- a/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/mapper/MgmtDeploymentRequestMapper.java +++ b/hawkbit-mgmt/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/mapper/MgmtDeploymentRequestMapper.java @@ -15,9 +15,9 @@ import org.eclipse.hawkbit.mgmt.json.model.MgmtMaintenanceWindowRequestBody; import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtActionType; import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtTargetAssignmentRequestBody; import org.eclipse.hawkbit.mgmt.json.model.target.MgmtDistributionSetAssignment; -import org.eclipse.hawkbit.repository.DeploymentManagement; import org.eclipse.hawkbit.repository.MaintenanceScheduleHelper; -import org.eclipse.hawkbit.repository.model.DeploymentRequestBuilder; +import org.eclipse.hawkbit.repository.model.DeploymentRequest; +import org.eclipse.hawkbit.repository.model.DeploymentRequest.DeploymentRequestBuilder; /** * A mapper for assignment requests @@ -54,14 +54,14 @@ public final class MgmtDeploymentRequestMapper { private static DeploymentRequestBuilder createAssignmentRequestBuilder(final String targetId, final Long dsId, final MgmtActionType type, final long forcetime, final Integer weight, final MgmtMaintenanceWindowRequestBody maintenanceWindow) { - final DeploymentRequestBuilder request = DeploymentManagement.deploymentRequest(targetId, dsId) - .setActionType(MgmtRestModelMapper.convertActionType(type)).setForceTime(forcetime).setWeight(weight); + final DeploymentRequestBuilder request = DeploymentRequest.builder(targetId, dsId) + .actionType(MgmtRestModelMapper.convertActionType(type)).forceTime(forcetime).weight(weight); if (maintenanceWindow != null) { final String cronSchedule = maintenanceWindow.getSchedule(); final String duration = maintenanceWindow.getDuration(); final String timezone = maintenanceWindow.getTimezone(); MaintenanceScheduleHelper.validateMaintenanceSchedule(cronSchedule, duration, timezone); - request.setMaintenance(cronSchedule, duration, timezone); + request.maintenance(cronSchedule, duration, timezone); } return request; } diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java index 8ca2f9ca4..2185065dd 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DeploymentManagement.java @@ -34,7 +34,6 @@ import org.eclipse.hawkbit.repository.model.Action.Status; import org.eclipse.hawkbit.repository.model.ActionCancellationType; import org.eclipse.hawkbit.repository.model.ActionStatus; import org.eclipse.hawkbit.repository.model.DeploymentRequest; -import org.eclipse.hawkbit.repository.model.DeploymentRequestBuilder; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetAssignmentResult; import org.eclipse.hawkbit.repository.model.DistributionSetType; @@ -59,17 +58,6 @@ public interface DeploymentManagement extends PermissionSupport { return SpPermission.TARGET; } - /** - * build a {@link DeploymentRequest} for a target distribution set assignment - * - * @param controllerId ID of target - * @param distributionSetId ID of distribution set - * @return the builder - */ - static DeploymentRequestBuilder deploymentRequest(final String controllerId, final long distributionSetId) { - return new DeploymentRequestBuilder(controllerId, distributionSetId); - } - /** * Assigns {@link DistributionSet}s to {@link Target}s according to the {@link DeploymentRequest}. * @@ -316,14 +304,6 @@ public interface DeploymentManagement extends PermissionSupport { @PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY) List findActiveActionsWithHighestWeight(@NotEmpty String controllerId, int maxActionCount); - /** - * Get weight of an Action. Returns the default value if the weight is null according to the properties. - * - * @param action to extract the weight from - * @return weight of the action - */ - int getWeightConsideringDefault(final Action action); - /** * Force cancels given {@link Action} for given {@link Target}. Force canceling means that the action is marked as canceled on the SP server * and a cancel request is sent to the target. But however it's not tracked, if the targets handles the cancel request or not. diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/RolloutManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/RolloutManagement.java index 3d433013d..f60b62a29 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/RolloutManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/RolloutManagement.java @@ -266,6 +266,7 @@ public interface RolloutManagement extends PermissionSupport { * @param rolloutId rollout id * @return true if rollout exists */ + @PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY) boolean exists(long rolloutId); /** diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetFilterQueryManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetFilterQueryManagement.java index 4553f35b2..2db2f4417 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetFilterQueryManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetFilterQueryManagement.java @@ -85,7 +85,7 @@ public interface TargetFilterQueryManagement * @return the page with the found {@link TargetFilterQuery}s * @throws EntityNotFoundException if DS with given ID does not exist */ - @PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY) + @PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY + " and " + "hasAuthority('READ_" + SpPermission.DISTRIBUTION_SET + "')") Page findByAutoAssignDSAndRsql(long setId, String rsql, @NotNull Pageable pageable); /** diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequest.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequest.java index fcaaae798..14be2a1fe 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequest.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequest.java @@ -12,6 +12,8 @@ package org.eclipse.hawkbit.repository.model; import jakarta.validation.Valid; import lombok.Data; +import lombok.Setter; +import lombok.experimental.Accessors; import org.eclipse.hawkbit.repository.exception.InvalidMaintenanceScheduleException; import org.eclipse.hawkbit.repository.model.Action.ActionType; @@ -69,4 +71,72 @@ public class DeploymentRequest { public String getControllerId() { return targetWithActionType.getControllerId(); } -} + + /** + * Build a {@link DeploymentRequest} for a target distribution set assignment + * + * @param controllerId ID of target + * @param distributionSetId ID of distribution set + * @return the builder + */ + public static DeploymentRequestBuilder builder(final String controllerId, final long distributionSetId) { + return new DeploymentRequestBuilder(controllerId, distributionSetId); + } + + @Accessors(fluent = true) + public static class DeploymentRequestBuilder { + + private final String controllerId; + private final Long distributionSetId; + @Setter + private Integer weight; + @Setter + private long forceTime = RepositoryModelConstants.NO_FORCE_TIME; + @Setter + private ActionType actionType = ActionType.FORCED; + private String maintenanceSchedule; + private String maintenanceWindowDuration; + private String maintenanceWindowTimeZone; + @Setter + private boolean confirmationRequired; + + /** + * Create a builder for a target distribution set assignment with the + * mandatory fields + * + * @param controllerId ID of the target + * @param distributionSetId ID of the distribution set + */ + private DeploymentRequestBuilder(final String controllerId, final Long distributionSetId) { + this.controllerId = controllerId; + this.distributionSetId = distributionSetId; + } + + /** + * Set a maintenanceWindow + * + * @param maintenanceSchedule is the cron expression to be used for scheduling maintenance + * windows. Expression has 6 mandatory fields and 1 last optional + * field: "second minute hour dayofmonth month weekday year" + * @param maintenanceWindowDuration in HH:mm:ss format specifying the duration of a maintenance + * window, for example 00:30:00 for 30 minutes + * @param maintenanceWindowTimeZone is the time zone specified as +/-hh:mm offset from UTC, for + * example +02:00 for CET summer time and +00:00 for UTC. The + * start time of a maintenance window calculated based on the + * cron expression is relative to this time zone. + * @return builder + */ + public DeploymentRequestBuilder maintenance( + final String maintenanceSchedule, final String maintenanceWindowDuration, final String maintenanceWindowTimeZone) { + this.maintenanceSchedule = maintenanceSchedule; + this.maintenanceWindowDuration = maintenanceWindowDuration; + this.maintenanceWindowTimeZone = maintenanceWindowTimeZone; + return this; + } + + public DeploymentRequest build() { + return new DeploymentRequest(controllerId, distributionSetId, actionType, forceTime, weight, + maintenanceSchedule, maintenanceWindowDuration, maintenanceWindowTimeZone, confirmationRequired); + } + } +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequestBuilder.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequestBuilder.java deleted file mode 100644 index d4c49247b..000000000 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/model/DeploymentRequestBuilder.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright (c) 2019 Bosch Software Innovations GmbH and others - * - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.eclipse.hawkbit.repository.model; - -import org.eclipse.hawkbit.repository.model.Action.ActionType; - -/** - * Builder for {@link DeploymentRequest} - */ -public class DeploymentRequestBuilder { - - private final String controllerId; - private final Long distributionSetId; - private Integer weight; - private long forceTime = RepositoryModelConstants.NO_FORCE_TIME; - private ActionType actionType = ActionType.FORCED; - private String maintenanceSchedule; - private String maintenanceWindowDuration; - private String maintenanceWindowTimeZone; - private boolean confirmationRequired; - - /** - * Create a builder for a target distribution set assignment with the - * mandatory fields - * - * @param controllerId ID of the target - * @param distributionSetId ID of the distribution set - */ - public DeploymentRequestBuilder(final String controllerId, final Long distributionSetId) { - this.controllerId = controllerId; - this.distributionSetId = distributionSetId; - } - - /** - * Set an other {@link ActionType} than {@link ActionType#FORCED} - * - * @param actionType type to used - * @return builder - */ - public DeploymentRequestBuilder setActionType(final ActionType actionType) { - this.actionType = actionType; - return this; - } - - /** - * Set a forceTime other than the default one. - * - * @param forceTime at what time the type soft turns into forced. - * @return builder - */ - public DeploymentRequestBuilder setForceTime(final long forceTime) { - this.forceTime = forceTime; - return this; - } - - /** - * Set the weight of the action. - * - * @param weight the priority given to the action. - * @return builder - */ - public DeploymentRequestBuilder setWeight(final Integer weight) { - this.weight = weight; - return this; - } - - /** - * Set a maintenanceWindow - * - * @param maintenanceSchedule is the cron expression to be used for scheduling maintenance - * windows. Expression has 6 mandatory fields and 1 last optional - * field: "second minute hour dayofmonth month weekday year" - * @param maintenanceWindowDuration in HH:mm:ss format specifying the duration of a maintenance - * window, for example 00:30:00 for 30 minutes - * @param maintenanceWindowTimeZone is the time zone specified as +/-hh:mm offset from UTC, for - * example +02:00 for CET summer time and +00:00 for UTC. The - * start time of a maintenance window calculated based on the - * cron expression is relative to this time zone. - * @return builder - */ - public DeploymentRequestBuilder setMaintenance(final String maintenanceSchedule, - final String maintenanceWindowDuration, final String maintenanceWindowTimeZone) { - this.maintenanceSchedule = maintenanceSchedule; - this.maintenanceWindowDuration = maintenanceWindowDuration; - this.maintenanceWindowTimeZone = maintenanceWindowTimeZone; - return this; - } - - /** - * Set if a confirmation is required. - * - * @param confirmationRequired if a confirmation is required for the {@link Action} - * @return builder - */ - public DeploymentRequestBuilder setConfirmationRequired(final boolean confirmationRequired) { - this.confirmationRequired = confirmationRequired; - return this; - } - - /** - * build the request - * - * @return the request object - */ - public DeploymentRequest build() { - return new DeploymentRequest(controllerId, distributionSetId, actionType, forceTime, weight, - maintenanceSchedule, maintenanceWindowDuration, maintenanceWindowTimeZone, confirmationRequired); - } -} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/autoassign/AbstractAutoAssignExecutor.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/autoassign/AbstractAutoAssignExecutor.java index 0601dd306..d17dac6c3 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/autoassign/AbstractAutoAssignExecutor.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/autoassign/AbstractAutoAssignExecutor.java @@ -169,10 +169,10 @@ public abstract class AbstractAutoAssignExecutor implements AutoAssignExecutor { : filterQuery.getAutoAssignActionType(); return controllerIds.stream() - .map(controllerId -> DeploymentManagement - .deploymentRequest(controllerId, filterQuery.getAutoAssignDistributionSet().getId()) - .setActionType(autoAssignActionType).setWeight(filterQuery.getAutoAssignWeight().orElse(null)) - .setConfirmationRequired(filterQuery.isConfirmationRequired()).build()) + .map(controllerId -> DeploymentRequest + .builder(controllerId, filterQuery.getAutoAssignDistributionSet().getId()) + .actionType(autoAssignActionType).weight(filterQuery.getAutoAssignWeight().orElse(null)) + .confirmationRequired(filterQuery.isConfirmationRequired()).build()) .toList(); } } \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaActionManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaActionManagement.java index 52774ecd8..e04be2053 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaActionManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaActionManagement.java @@ -57,7 +57,7 @@ public class JpaActionManagement { this.repositoryProperties = repositoryProperties; } - public int getWeightConsideringDefault(final Action action) { + protected int getWeightConsideringDefault(final Action action) { return action.getWeight().orElse(repositoryProperties.getActionWeightIfAbsent()); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java index 8e26cbe4e..6a23e18c7 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java @@ -198,7 +198,7 @@ public class JpaDeploymentManagement extends JpaActionManagement implements Depl enforceMaxAssignmentsPerRequest(distinctAssignments.size()); final List deploymentRequests = distinctAssignments.stream() - .map(entry -> DeploymentManagement.deploymentRequest(entry.getKey(), entry.getValue()).build()).toList(); + .map(entry -> DeploymentRequest.builder(entry.getKey(), entry.getValue()).build()).toList(); return assignDistributionSets( auditorAware.getCurrentAuditor().orElse(tenantAware.getCurrentUsername()), deploymentRequests, null, diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/ConfirmationManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/ConfirmationManagementTest.java index fa131ccc2..0254367e9 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/ConfirmationManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/ConfirmationManagementTest.java @@ -24,7 +24,6 @@ import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest; import org.eclipse.hawkbit.repository.model.Action; import org.eclipse.hawkbit.repository.model.Action.Status; import org.eclipse.hawkbit.repository.model.DeploymentRequest; -import org.eclipse.hawkbit.repository.model.DeploymentRequestBuilder; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.Target; import org.junit.jupiter.api.Test; @@ -326,7 +325,7 @@ class ConfirmationManagementTest extends AbstractJpaIntegrationTest { } private static DeploymentRequest toDeploymentRequest(final String controllerId, final Long distributionSetId) { - return new DeploymentRequestBuilder(controllerId, distributionSetId).setConfirmationRequired(true).build(); + return DeploymentRequest.builder(controllerId, distributionSetId).confirmationRequired(true).build(); } private void verifyAutoConfirmationIsDisabled(final String controllerId) { diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DeploymentManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DeploymentManagementTest.java index 5ec1333ac..cc54298e8 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DeploymentManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/DeploymentManagementTest.java @@ -32,7 +32,6 @@ import jakarta.validation.ConstraintViolationException; import lombok.Getter; import org.assertj.core.api.Assertions; import org.eclipse.hawkbit.repository.ActionStatusFields; -import org.eclipse.hawkbit.repository.DeploymentManagement; import org.eclipse.hawkbit.repository.DistributionSetManagement; import org.eclipse.hawkbit.repository.DistributionSetTagManagement; import org.eclipse.hawkbit.repository.event.remote.CancelTargetAssignmentEvent; @@ -65,7 +64,6 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet_; import org.eclipse.hawkbit.repository.jpa.model.JpaTarget; import org.eclipse.hawkbit.repository.jpa.model.JpaTarget_; import org.eclipse.hawkbit.repository.jpa.repository.TargetRepository; -import org.eclipse.hawkbit.repository.jpa.specifications.ActionSpecifications; import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetSpecification; import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder; import org.eclipse.hawkbit.repository.model.Action; @@ -74,7 +72,6 @@ import org.eclipse.hawkbit.repository.model.Action.ActionType; import org.eclipse.hawkbit.repository.model.Action.Status; import org.eclipse.hawkbit.repository.model.ActionStatus; import org.eclipse.hawkbit.repository.model.DeploymentRequest; -import org.eclipse.hawkbit.repository.model.DeploymentRequestBuilder; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetAssignmentResult; import org.eclipse.hawkbit.repository.model.DistributionSetType; @@ -747,11 +744,11 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final Target target = testdataFactory.createTarget(); final List distributionSets = testdataFactory.createDistributionSets(2); - final DeploymentRequest targetToDS0 = DeploymentManagement - .deploymentRequest(target.getControllerId(), distributionSets.get(0).getId()).setWeight(78).build(); + final DeploymentRequest targetToDS0 = DeploymentRequest + .builder(target.getControllerId(), distributionSets.get(0).getId()).weight(78).build(); - final DeploymentRequest targetToDS1 = DeploymentManagement - .deploymentRequest(target.getControllerId(), distributionSets.get(1).getId()).setWeight(565).build(); + final DeploymentRequest targetToDS1 = DeploymentRequest + .builder(target.getControllerId(), distributionSets.get(1).getId()).weight(565).build(); final List deploymentRequests = List.of(targetToDS0, targetToDS1); Assertions.assertThatExceptionOfType(MultiAssignmentIsNotEnabledException.class) @@ -835,8 +832,8 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final DistributionSet distributionSet = testdataFactory.createDistributionSet(); assignDistributionSets(Collections - .singletonList(new DeploymentRequestBuilder(target.getControllerId(), distributionSet.getId()) - .setConfirmationRequired(confirmationRequired).build())); + .singletonList(DeploymentRequest.builder(target.getControllerId(), distributionSet.getId()) + .confirmationRequired(confirmationRequired).build())); assertThat(deploymentManagement.findActionsByTarget(target.getControllerId(), PAGE).getContent()).hasSize(1) .allSatisfy(action -> { @@ -952,14 +949,14 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final String targetId = testdataFactory.createTarget().getControllerId(); final Long dsId = testdataFactory.createDistributionSet().getId(); final List twoEqualAssignments = Collections.nCopies(2, - DeploymentManagement.deploymentRequest(targetId, dsId).build()); + DeploymentRequest.builder(targetId, dsId).build()); assertThat(getResultingActionCount(deploymentManagement.assignDistributionSets(twoEqualAssignments))) .isEqualTo(1); enableMultiAssignments(); final List twoEqualAssignmentsWithWeight = Collections.nCopies(2, - DeploymentManagement.deploymentRequest(targetId, dsId).setWeight(555).build()); + DeploymentRequest.builder(targetId, dsId).weight(555).build()); assertThat(getResultingActionCount(deploymentManagement.assignDistributionSets(twoEqualAssignmentsWithWeight))) .isEqualTo(1); @@ -987,7 +984,7 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { for (int i = 0; i < size; i++) { final Long dsId = testdataFactory.createDistributionSet().getId(); deploymentRequests.add( - DeploymentManagement.deploymentRequest(controllerId, dsId).setWeight(24).build()); + DeploymentRequest.builder(controllerId, dsId).weight(24).build()); } enableMultiAssignments(); @@ -1004,8 +1001,8 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final String targetId = testdataFactory.createTarget().getControllerId(); final Long dsId = testdataFactory.createDistributionSet().getId(); - final DeploymentRequest assignWithoutWeight = DeploymentManagement.deploymentRequest(targetId, dsId).build(); - final DeploymentRequest assignWithWeight = DeploymentManagement.deploymentRequest(targetId, dsId).setWeight(567).build(); + final DeploymentRequest assignWithoutWeight = DeploymentRequest.builder(targetId, dsId).build(); + final DeploymentRequest assignWithWeight = DeploymentRequest.builder(targetId, dsId).weight(567).build(); enableMultiAssignments(); assertThat(deploymentManagement.assignDistributionSets(List.of(assignWithoutWeight, assignWithWeight))).isNotNull(); @@ -1019,7 +1016,7 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final String targetId = testdataFactory.createTarget().getControllerId(); final Long dsId = testdataFactory.createDistributionSet().getId(); - final DeploymentRequest assignWithoutWeight = DeploymentManagement.deploymentRequest(targetId, dsId).setWeight(456).build(); + final DeploymentRequest assignWithoutWeight = DeploymentRequest.builder(targetId, dsId).weight(456).build(); assertThat(deploymentManagement.assignDistributionSets(Collections.singletonList(assignWithoutWeight))).isNotNull().size().isEqualTo(1); } @@ -1040,14 +1037,10 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { void weightValidatedAndSaved() { final String targetId = testdataFactory.createTarget().getControllerId(); final Long dsId = testdataFactory.createDistributionSet().getId(); - final DeploymentRequest valideRequest1 = DeploymentManagement.deploymentRequest(targetId, dsId) - .setWeight(Action.WEIGHT_MAX).build(); - final DeploymentRequest valideRequest2 = DeploymentManagement.deploymentRequest(targetId, dsId) - .setWeight(Action.WEIGHT_MIN).build(); - final DeploymentRequest weightTooLow = DeploymentManagement.deploymentRequest(targetId, dsId) - .setWeight(Action.WEIGHT_MIN - 1).build(); - final DeploymentRequest weightTooHigh = DeploymentManagement.deploymentRequest(targetId, dsId) - .setWeight(Action.WEIGHT_MAX + 1).build(); + final DeploymentRequest valideRequest1 = DeploymentRequest.builder(targetId, dsId).weight(Action.WEIGHT_MAX).build(); + final DeploymentRequest valideRequest2 = DeploymentRequest.builder(targetId, dsId).weight(Action.WEIGHT_MIN).build(); + final DeploymentRequest weightTooLow = DeploymentRequest.builder(targetId, dsId).weight(Action.WEIGHT_MIN - 1).build(); + final DeploymentRequest weightTooHigh = DeploymentRequest.builder(targetId, dsId).weight(Action.WEIGHT_MAX + 1).build(); enableMultiAssignments(); final List deploymentRequestsTooLow = Collections.singletonList(weightTooLow); Assertions.assertThatExceptionOfType(ConstraintViolationException.class) @@ -1613,8 +1606,8 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final List deploymentRequests = new ArrayList<>(); for (int i = 0; i < quotaManagement.getMaxTargetDistributionSetAssignmentsPerManualAssignment(); i++) { final Target target = testdataFactory.createTarget("test-target-" + i, "test-target-" + i, targetType); - final DeploymentRequest deployment = DeploymentManagement - .deploymentRequest(target.getControllerId(), ds.getId()).build(); + final DeploymentRequest deployment = DeploymentRequest + .builder(target.getControllerId(), ds.getId()).build(); deploymentRequests.add(deployment); } @@ -1637,8 +1630,8 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final Target target1 = testdataFactory.createTarget("test-target1", "test-target1", targetType1); final Target target2 = testdataFactory.createTarget("test-target2", "test-target2", targetType2); - final DeploymentRequest deployment1 = DeploymentManagement.deploymentRequest(target1.getControllerId(), ds.getId()).build(); - final DeploymentRequest deployment2 = DeploymentManagement.deploymentRequest(target2.getControllerId(), ds.getId()).build(); + final DeploymentRequest deployment1 = DeploymentRequest.builder(target1.getControllerId(), ds.getId()).build(); + final DeploymentRequest deployment2 = DeploymentRequest.builder(target2.getControllerId(), ds.getId()).build(); final List deploymentRequests = Arrays.asList(deployment1, deployment2); deploymentManagement.assignDistributionSets(deploymentRequests); @@ -1663,7 +1656,7 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final TargetType targetType = testdataFactory.createTargetType("target-type", Set.of(dsType)); final Target target = testdataFactory.createTarget("test-target", "test-target", targetType); - final DeploymentRequest deploymentRequest = DeploymentManagement.deploymentRequest(target.getControllerId(), ds.getId()).build(); + final DeploymentRequest deploymentRequest = DeploymentRequest.builder(target.getControllerId(), ds.getId()).build(); final List deploymentRequests = List.of(deploymentRequest); assertThatExceptionOfType(IncompatibleTargetTypeException.class) @@ -1679,8 +1672,8 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final TargetType emptyTargetType = testdataFactory.createTargetType("target-type", Set.of()); final Target targetWithEmptyType = testdataFactory.createTarget("test-target", "test-target", emptyTargetType); - final DeploymentRequest deploymentRequestWithEmptyType = DeploymentManagement - .deploymentRequest(targetWithEmptyType.getControllerId(), ds.getId()).build(); + final DeploymentRequest deploymentRequestWithEmptyType = DeploymentRequest + .builder(targetWithEmptyType.getControllerId(), ds.getId()).build(); final List deploymentRequests = Collections.singletonList(deploymentRequestWithEmptyType); assertThatExceptionOfType(IncompatibleTargetTypeException.class) .isThrownBy(() -> deploymentManagement.assignDistributionSets(deploymentRequests)); @@ -1695,8 +1688,8 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest { final Collection targets, final int weight, final boolean confirmationRequired) { final List deploymentRequests = new ArrayList<>(); distributionSets.forEach(ds -> targets.forEach(target -> deploymentRequests - .add(DeploymentManagement.deploymentRequest(target.getControllerId(), ds.getId()).setWeight(weight) - .setConfirmationRequired(confirmationRequired).build()))); + .add(DeploymentRequest.builder(target.getControllerId(), ds.getId()) + .weight(weight).confirmationRequired(confirmationRequired).build()))); return deploymentRequests; } diff --git a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java index 2d97d8444..2a3f977bf 100644 --- a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java +++ b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/repository/test/util/AbstractIntegrationTest.java @@ -340,8 +340,8 @@ public abstract class AbstractIntegrationTest { final boolean confirmationFlowActive = isConfirmationFlowActive(); final List deploymentRequests = controllerIds.stream() - .map(id -> DeploymentManagement.deploymentRequest(id, dsId) - .setActionType(actionType).setForceTime(forcedTime).setWeight(weight).setConfirmationRequired(confirmationFlowActive) + .map(id -> DeploymentRequest.builder(id, dsId) + .actionType(actionType).forceTime(forcedTime).weight(weight).confirmationRequired(confirmationFlowActive) .build()) .toList(); final List results = deploymentManagement.assignDistributionSets(deploymentRequests); @@ -393,9 +393,9 @@ public abstract class AbstractIntegrationTest { final String controllerId, final String maintenanceWindowSchedule, final String maintenanceWindowDuration, final String maintenanceWindowTimeZone) { - return makeAssignment(DeploymentManagement.deploymentRequest(controllerId, dsID) - .setMaintenance(maintenanceWindowSchedule, maintenanceWindowDuration, maintenanceWindowTimeZone) - .setConfirmationRequired(true).build()); + return makeAssignment(DeploymentRequest.builder(controllerId, dsID) + .maintenance(maintenanceWindowSchedule, maintenanceWindowDuration, maintenanceWindowTimeZone) + .confirmationRequired(true).build()); } protected DistributionSetAssignmentResult assignDistributionSet(final DistributionSet pset, final Target target) {