Refactor action repository (#2118)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-12-05 11:41:41 +02:00
committed by GitHub
parent 1d52d3b102
commit 39861e7790
21 changed files with 318 additions and 393 deletions

View File

@@ -41,23 +41,19 @@ import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
/**
* Service layer for all operations of the DDI API (with access permissions only
* for the controller).
* Service layer for all operations of the DDI API (with access permissions only for the controller).
*/
public interface ControllerManagement {
/**
* Adds an {@link ActionStatus} for a cancel {@link Action} including
* potential state changes for the target and the {@link Action} itself.
* Adds an {@link ActionStatus} for a cancel {@link Action} including potential state changes for the target and the {@link Action} itself.
*
* @param create to be added
* @return the updated {@link Action}
* @throws EntityAlreadyExistsException if a given entity already exists
* @throws AssignmentQuotaExceededException if more than the allowed number of status entries or messages
* per entry are inserted
* @throws AssignmentQuotaExceededException if more than the allowed number of status entries or messages per entry are inserted
* @throws EntityNotFoundException if given action does not exist
* @throws ConstraintViolationException if fields are not filled as specified. Check
* {@link ActionStatusCreate} for field constraints.
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link ActionStatusCreate} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
Action addCancelActionStatus(@NotNull @Valid ActionStatusCreate create);
@@ -72,54 +68,45 @@ public interface ControllerManagement {
Optional<SoftwareModule> getSoftwareModule(long moduleId);
/**
* Retrieves {@link SoftwareModuleMetadata} where
* {@link SoftwareModuleMetadata#isTargetVisible()}.
* Retrieves {@link SoftwareModuleMetadata} where {@link SoftwareModuleMetadata#isTargetVisible()}.
*
* @param moduleId of the {@link SoftwareModule}
* @return list of {@link SoftwareModuleMetadata} with maximum size of
* @return the map of software module id to {@link SoftwareModuleMetadata} with maximum size of
* {@link RepositoryConstants#MAX_META_DATA_COUNT}
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
Map<Long, List<SoftwareModuleMetadata>> findTargetVisibleMetaDataBySoftwareModuleId(
@NotNull Collection<Long> moduleId);
Map<Long, List<SoftwareModuleMetadata>> findTargetVisibleMetaDataBySoftwareModuleId(@NotNull Collection<Long> moduleId);
/**
* Simple addition of a new {@link ActionStatus} entry to the
* {@link Action}. No state changes.
* Simple addition of a new {@link ActionStatus} entry to the {@link Action}. No state changes.
*
* @param create to add to the action
* @return created {@link ActionStatus} entity
* @throws AssignmentQuotaExceededException if more than the allowed number of status entries or messages
* per entry are inserted
* @throws AssignmentQuotaExceededException if more than the allowed number of status entries or messages per entry are inserted
* @throws EntityNotFoundException if given action does not exist
* @throws ConstraintViolationException if fields are not filled as specified. Check
* {@link ActionStatusCreate} for field constraints.
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link ActionStatusCreate} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
ActionStatus addInformationalActionStatus(@NotNull @Valid ActionStatusCreate create);
/**
* Adds an {@link ActionStatus} entry for an update {@link Action} including
* potential state changes for the target and the {@link Action} itself.
* Adds an {@link ActionStatus} entry for an update {@link Action} including potential state changes for the target and the {@link Action}
* itself.
*
* @param create to be added
* @return the updated {@link Action}
* @throws EntityAlreadyExistsException if a given entity already exists
* @throws AssignmentQuotaExceededException if more than the allowed number of status entries or messages
* per entry are inserted
* @throws AssignmentQuotaExceededException if more than the allowed number of status entries or messages per entry are inserted
* @throws EntityNotFoundException if action status not exist
* @throws ConstraintViolationException if fields are not filled as specified. Check
* {@link ActionStatusCreate} for field constraints.
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link ActionStatusCreate} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
Action addUpdateActionStatus(@NotNull @Valid ActionStatusCreate create);
/**
* Retrieves active {@link Action} with highest priority that is assigned to
* a {@link Target}.
* Retrieves active {@link Action} with the highest priority that is assigned to a {@link Target}.
*
* For performance reasons this method does not throw
* {@link EntityNotFoundException} in case target with given controllerId
* For performance reasons this method does not throw {@link EntityNotFoundException} in case target with given controllerId
* does not exist but will return an {@link Optional#empty()} instead.
*
* @param controllerId identifies the target to retrieve the action from
@@ -140,17 +127,7 @@ public interface ControllerManagement {
List<Action> 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);
/**
* Get the {@link Action} entity for given actionId with all lazy
* attributes.
* Get the {@link Action} entity for given actionId with all lazy attributes.
*
* @param actionId to be id of the action
* @return the corresponding {@link Action}
@@ -159,8 +136,7 @@ public interface ControllerManagement {
Optional<Action> findActionWithDetails(long actionId);
/**
* Retrieves all the {@link ActionStatus} entries of the given
* {@link Action}.
* Retrieves all the {@link ActionStatus} entries of the given {@link Action}.
*
* @param pageReq pagination parameter
* @param actionId to be filtered on
@@ -171,11 +147,8 @@ public interface ControllerManagement {
Page<ActionStatus> findActionStatusByAction(@NotNull Pageable pageReq, long actionId);
/**
* Register new target in the repository (plug-and-play) and in case it
* already exists updates {@link Target#getAddress()} and
* {@link Target#getLastTargetQuery()} and switches if
* {@link TargetUpdateStatus#UNKNOWN} to
* {@link TargetUpdateStatus#REGISTERED}.
* Register new target in the repository (plug-and-play) and in case it already exists updates {@link Target#getAddress()} and
* {@link Target#getLastTargetQuery()} and switches if {@link TargetUpdateStatus#UNKNOWN} to {@link TargetUpdateStatus#REGISTERED}.
*
* @param controllerId reference
* @param address the client IP address of the target, might be {@code null}
@@ -199,16 +172,13 @@ public interface ControllerManagement {
* @return target reference
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
Target findOrRegisterTargetIfItDoesNotExist(@NotEmpty String controllerId, @NotNull URI address, String name,
String type);
Target findOrRegisterTargetIfItDoesNotExist(@NotEmpty String controllerId, @NotNull URI address, String name, String type);
/**
* Retrieves last {@link Action} for a download of an artifact of given
* module and target if exists and is not canceled.
* Retrieves last {@link Action} for a download of an artifact of given module and target if exists and is not canceled.
*
* @param controllerId to look for
* @param moduleId of the the {@link SoftwareModule} that should be assigned to
* the target
* @param moduleId of the {@link SoftwareModule} that should be assigned to the target
* @return last {@link Action} for given combination
* @throws EntityNotFoundException if target with given ID does not exist
*/
@@ -216,8 +186,7 @@ public interface ControllerManagement {
Optional<Action> getActionForDownloadByTargetAndSoftwareModule(@NotEmpty String controllerId, long moduleId);
/**
* Returns configured polling interval at which the controller polls hawkBit
* server.
* Returns configured polling interval at which the controller polls hawkBit server.
*
* @return current {@link TenantConfigurationKey#POLLING_TIME_INTERVAL}.
*/
@@ -233,44 +202,33 @@ public interface ControllerManagement {
String getMinPollingTime();
/**
* Returns the count to be used for reducing polling interval while calling
* {@link ControllerManagement#getPollingTimeForAction(long)}.
* Returns the count to be used for reducing polling interval while calling {@link ControllerManagement#getPollingTimeForAction(long)}.
*
* @return configured value of
* {@link TenantConfigurationKey#MAINTENANCE_WINDOW_POLL_COUNT}.
* @return configured value of {@link TenantConfigurationKey#MAINTENANCE_WINDOW_POLL_COUNT}.
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
int getMaintenanceWindowPollCount();
/**
* Returns polling time based on the maintenance window for an action.
* Server will reduce the polling interval as the start time for maintenance
* window approaches, so that at least these many attempts are made between
* current polling until start of maintenance window. Poll time keeps
* reducing with MinPollingTime as lower limit
* {@link TenantConfigurationKey#MIN_POLLING_TIME_INTERVAL}. After the start
* of maintenance window, it resets to default
* {@link TenantConfigurationKey#POLLING_TIME_INTERVAL}.
* Returns polling time based on the maintenance window for an action. Server will reduce the polling interval as the start time for
* maintenance window approaches, so that at least these many attempts are made between current polling until start of maintenance window.
* Poll time keeps reducing with MinPollingTime as lower limit {@link TenantConfigurationKey#MIN_POLLING_TIME_INTERVAL}. After the start
* of maintenance window, it resets to default {@link TenantConfigurationKey#POLLING_TIME_INTERVAL}.
*
* @param actionId id the {@link Action} for which polling time is calculated
* based on it having maintenance window or not
* @param actionId id the {@link Action} for which polling time is calculated based on it having maintenance window or not
* @return current {@link TenantConfigurationKey#POLLING_TIME_INTERVAL}.
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
String getPollingTimeForAction(long actionId);
/**
* Checks if a given target has currently or has even been assigned to the
* given artifact through the action history list. This can e.g. indicate if
* a target is allowed to download a given artifact because it has currently
* assigned or had ever been assigned to the target and so it's visible to a
* specific target e.g. for downloading.
* Checks if a given target has currently or has even been assigned to the given artifact through the action history list. This can e.g.
* indicate if a target is allowed to download a given artifact because it has currently assigned or had ever been assigned to the target
* and so it's visible to a specific target e.g. for downloading.
*
* @param controllerId the ID of the target to check
* @param sha1Hash of the artifact to verify if the given target had even been
* assigned to
* @return {@code true} if the given target has currently or had ever a
* relation to the given artifact through the action history,
* @param sha1Hash of the artifact to verify if the given target had even been assigned to
* @return {@code true} if the given target has currently or had ever a relation to the given artifact through the action history,
* otherwise {@code false}
* @throws EntityNotFoundException if target with given ID does not exist
*/
@@ -278,17 +236,13 @@ public interface ControllerManagement {
boolean hasTargetArtifactAssigned(@NotEmpty String controllerId, @NotEmpty String sha1Hash);
/**
* Checks if a given target has currently or has even been assigned to the
* given artifact through the action history list. This can e.g. indicate if
* a target is allowed to download a given artifact because it has currently
* assigned or had ever been assigned to the target and so it's visible to a
* specific target e.g. for downloading.
* Checks if a given target has currently or has even been assigned to the given artifact through the action history list. This can e.g.
* indicate if a target is allowed to download a given artifact because it has currently assigned or had ever been assigned to the target
* and so it's visible to a specific target e.g. for downloading.
*
* @param targetId the ID of the target to check
* @param sha1Hash of the artifact to verify if the given target had even been
* assigned to
* @return {@code true} if the given target has currently or had ever a
* relation to the given artifact through the action history,
* @param sha1Hash of the artifact to verify if the given target had even been assigned to
* @return {@code true} if the given target has currently or had ever a relation to the given artifact through the action history,
* otherwise {@code false}
* @throws EntityNotFoundException if target with given ID does not exist
*/
@@ -296,21 +250,18 @@ public interface ControllerManagement {
boolean hasTargetArtifactAssigned(long targetId, @NotEmpty String sha1Hash);
/**
* Registers retrieved status for given {@link Target} and {@link Action} if
* it does not exist yet.
* Registers retrieved status for given {@link Target} and {@link Action} if it does not exist yet.
*
* @param actionId to the handle status for
* @param message for the status
* @return the update action in case the status has been changed to
* {@link Status#RETRIEVED}
* @return the update action in case the status has been changed to {@link Status#RETRIEVED}
* @throws EntityNotFoundException if action with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
Action registerRetrieved(long actionId, String message);
/**
* Updates attributes of the controller according to the given
* {@link UpdateMode}.
* Updates attributes of the controller according to the given {@link UpdateMode}.
*
* @param controllerId to update
* @param attributes to insert
@@ -324,47 +275,37 @@ public interface ControllerManagement {
Target updateControllerAttributes(@NotEmpty String controllerId, @NotNull Map<String, String> attributes, UpdateMode mode);
/**
* Finds {@link Target} based on given controller ID returns found Target
* without details, i.e. NO {@link Target#getTags()} and
* {@link Target#getActions()} possible.
* Finds {@link Target} based on given controller ID returns found Target without details, i.e.
* NO {@link Target#getTags()} and {@link Target#getActions()} possible.
*
* @param controllerId to look for.
* @return {@link Target} or {@code null} if it does not exist
* @see Target#getControllerId()
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER + SpringEvalExpressions.HAS_AUTH_OR
+ SpringEvalExpressions.IS_SYSTEM_CODE)
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER + SpringEvalExpressions.HAS_AUTH_OR + SpringEvalExpressions.IS_SYSTEM_CODE)
Optional<Target> getByControllerId(@NotEmpty String controllerId);
/**
* Finds {@link Target} based on given ID returns found Target without
* details, i.e. NO {@link Target#getTags()} and {@link Target#getActions()}
* Finds {@link Target} based on given ID returns found Target without details, i.e.
* NO {@link Target#getTags()} and {@link Target#getActions()}
* possible.
*
* @param targetId to look for.
* @return {@link Target} or {@code null} if it does not exist
* @see Target#getId()
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER + SpringEvalExpressions.HAS_AUTH_OR
+ SpringEvalExpressions.IS_SYSTEM_CODE)
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER + SpringEvalExpressions.HAS_AUTH_OR + SpringEvalExpressions.IS_SYSTEM_CODE)
Optional<Target> get(long targetId);
/**
* Retrieves the specified number of messages from action history of the
* given {@link Action} based on messageCount. Regardless of the value of
* messageCount, in order to restrict resource utilisation by controllers,
* maximum number of messages that are retrieved from database is limited by
* {@link RepositoryConstants#MAX_ACTION_HISTORY_MSG_COUNT}. messageCount
* less then zero, retrieves the maximum allowed number of action status
* messages from history; messageCount equal zero, does not retrieve any
* message; and messageCount larger then zero, retrieves the specified
* number of messages, limited by maximum allowed number. A controller sends
* the feedback for an {@link ActionStatus} as a list of messages; while
* returning the messages, even though the messages from multiple
* {@link ActionStatus} are retrieved in descending order by the reported
* time ({@link ActionStatus#getOccurredAt()}), i.e. latest ActionStatus
* first, the sub-ordering of messages from within single
* {@link ActionStatus} is unspecified.
* Retrieves the specified number of messages from action history of the given {@link Action} based on messageCount. Regardless of the
* value of messageCount, in order to restrict resource utilisation by controllers, maximum number of messages that are retrieved from
* database is limited by {@link RepositoryConstants#MAX_ACTION_HISTORY_MSG_COUNT}. messageCount less than zero, retrieves the maximum
* allowed number of action status messages from history; messageCount equal zero, does not retrieve any message; and messageCount larger
* than zero, retrieves the specified number of messages, limited by maximum allowed number. A controller sends the feedback for an
* {@link ActionStatus} as a list of messages; while returning the messages, even though the messages from multiple {@link ActionStatus}
* are retrieved in descending order by the reported time ({@link ActionStatus#getOccurredAt()}), i.e. latest ActionStatus first, the
* sub-ordering of messages from within single {@link ActionStatus} is unspecified.
*
* @param actionId to be filtered on
* @param messageCount is the number of messages to return from history
@@ -374,10 +315,8 @@ public interface ControllerManagement {
List<String> getActionHistoryMessages(long actionId, int messageCount);
/**
* Cancels given {@link Action} for this {@link Target}. However, it might
* be possible that the controller will continue to work on the
* cancellation. The controller needs to acknowledge or reject the
* cancellation using {@link DdiRootController#postCancelActionFeedback}.
* Cancels given {@link Action} for this {@link Target}. However, it might be possible that the controller will continue to work on the
* cancellation. The controller needs to acknowledge or reject the cancellation using {@link DdiRootController#postCancelActionFeedback}.
*
* @param actionId to be canceled
* @return canceled {@link Action}
@@ -425,8 +364,7 @@ public interface ControllerManagement {
* Activate auto confirmation for a given controllerId
*
* @param controllerId to activate auto-confirmation on
* @param initiator can be set optionally (fallback is the current acting security
* user)
* @param initiator can be set optionally (fallback is the current acting security user)
* @param remark (optional) remark
* @return the persisted {@link AutoConfirmationStatus}
*/
@@ -452,4 +390,4 @@ public interface ControllerManagement {
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER)
boolean updateOfflineAssignedVersion(@NotEmpty String controllerId, String distributionName, String version);
}
}

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import jakarta.validation.Valid;
@@ -173,8 +174,7 @@ public interface SoftwareModuleManagement
* @throws EntityNotFoundException if software module with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
Page<SoftwareModuleMetadata> findMetaDataBySoftwareModuleIdAndTargetVisible(@NotNull Pageable pageable,
long id);
Page<SoftwareModuleMetadata> findMetaDataBySoftwareModuleIdAndTargetVisible(@NotNull Pageable pageable, long id);
/**
* Finds all meta data by the given software module id.
@@ -190,8 +190,7 @@ public interface SoftwareModuleManagement
* @throws EntityNotFoundException if software module with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
Page<SoftwareModuleMetadata> findMetaDataByRsql(@NotNull Pageable pageable, long id,
@NotNull String rsqlParam);
Page<SoftwareModuleMetadata> findMetaDataByRsql(@NotNull Pageable pageable, long id, @NotNull String rsqlParam);
/**
* Retrieves the {@link SoftwareModule}s by their {@link SoftwareModuleType}
@@ -235,4 +234,7 @@ public interface SoftwareModuleManagement
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_REPOSITORY)
SoftwareModuleMetadata updateMetaData(@NotNull @Valid SoftwareModuleMetadataUpdate update);
}
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
Map<Long, List<SoftwareModuleMetadata>> findMetaDataBySoftwareModuleIdsAndTargetVisible(Collection<Long> moduleIds);
}

View File

@@ -29,8 +29,7 @@ public interface BaseEntity extends Serializable, Identifiable<Long> {
String getCreatedBy();
/**
* @return time in {@link TimeUnit#MILLISECONDS} when the {@link BaseEntity}
* was created.
* @return time in {@link TimeUnit#MILLISECONDS} when the {@link BaseEntity} was created.
*/
long getCreatedAt();