Skip to content

Commit

Permalink
Refactoring/Improving source: repository 4 (slf4j) (#1604)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm authored Feb 3, 2024
1 parent 5821c25 commit 990d1a7
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

import jakarta.transaction.TransactionManager;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.eclipse.hawkbit.exception.GenericSpServerException;
import org.eclipse.hawkbit.repository.exception.ConcurrentModificationException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.InsufficientPermissionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
Expand All @@ -36,11 +35,10 @@
* specific exceptions Additionally it checks and prevents access to certain
* packages. Logging aspect which logs the call stack
*/
@Slf4j
@Aspect
public class ExceptionMappingAspectHandler implements Ordered {

private static final Logger LOG = LoggerFactory.getLogger(ExceptionMappingAspectHandler.class);

private static final Map<String, String> EXCEPTION_MAPPING = new HashMap<>(4);

/**
Expand Down Expand Up @@ -98,7 +96,7 @@ public void catchAndWrapJpaExceptionsService(final Exception ex) throws Throwabl
.getConstructor(Throwable.class).newInstance(ex);
}

LOG.error("there is no mapping configured for exception class {}", mappedEx.getName());
log.error("there is no mapping configured for exception class {}", mappedEx.getName());
throw new GenericSpServerException(ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import jakarta.persistence.PersistenceException;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.ContextAware;
import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.repository.DeploymentManagement;
Expand All @@ -22,8 +23,6 @@
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
Expand All @@ -36,10 +35,9 @@
* retrieved. All targets get listed per target filter query, that match the TFQ
* and that don't have the auto assign DS in their action history.
*/
@Slf4j
public class AutoAssignChecker extends AbstractAutoAssignExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignChecker.class);

private final TargetManagement targetManagement;

/**
Expand All @@ -66,17 +64,17 @@ public AutoAssignChecker(final TargetFilterQueryManagement targetFilterQueryMana
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void checkAllTargets() {
LOGGER.debug("Auto assign check call for tenant {} started", getContextAware().getCurrentTenant());
log.debug("Auto assign check call for tenant {} started", getContextAware().getCurrentTenant());
forEachFilterWithAutoAssignDS(this::checkByTargetFilterQueryAndAssignDS);
LOGGER.debug("Auto assign check call for tenant {} finished", getContextAware().getCurrentTenant());
log.debug("Auto assign check call for tenant {} finished", getContextAware().getCurrentTenant());
}

@Override
public void checkSingleTarget(String controllerId) {
LOGGER.debug("Auto assign check call for tenant {} and device {} started", getContextAware().getCurrentTenant(),
log.debug("Auto assign check call for tenant {} and device {} started", getContextAware().getCurrentTenant(),
controllerId);
forEachFilterWithAutoAssignDS(filter -> checkForDevice(controllerId, filter));
LOGGER.debug("Auto assign check call for tenant {} and device {} finished", getContextAware().getCurrentTenant(),
log.debug("Auto assign check call for tenant {} and device {} finished", getContextAware().getCurrentTenant(),
controllerId);
}

Expand All @@ -89,7 +87,7 @@ public void checkSingleTarget(String controllerId) {
* the target filter query
*/
private void checkByTargetFilterQueryAndAssignDS(final TargetFilterQuery targetFilterQuery) {
LOGGER.debug("Auto assign check call for tenant {} and target filter query id {} started",
log.debug("Auto assign check call for tenant {} and target filter query id {} started",
getContextAware().getCurrentTenant(), targetFilterQuery.getId());
try {
int count;
Expand All @@ -99,24 +97,24 @@ private void checkByTargetFilterQueryAndAssignDS(final TargetFilterQuery targetF
PageRequest.of(0, Constants.MAX_ENTRIES_IN_STATEMENT),
targetFilterQuery.getAutoAssignDistributionSet().getId(), targetFilterQuery.getQuery())
.getContent().stream().map(Target::getControllerId).toList();
LOGGER.debug(
log.debug(
"Retrieved {} auto assign targets for tenant {} and target filter query id {}, starting with assignment",
controllerIds.size(), getContextAware().getCurrentTenant(), targetFilterQuery.getId());

count = runTransactionalAssignment(targetFilterQuery, controllerIds);
LOGGER.debug(
log.debug(
"Assignment for {} auto assign targets for tenant {} and target filter query id {} finished",
controllerIds.size(), getContextAware().getCurrentTenant(), targetFilterQuery.getId());
} while (count == Constants.MAX_ENTRIES_IN_STATEMENT);
} catch (final PersistenceException | AbstractServerRtException e) {
LOGGER.error("Error during auto assign check of target filter query id {}", targetFilterQuery.getId(), e);
log.error("Error during auto assign check of target filter query id {}", targetFilterQuery.getId(), e);
}
LOGGER.debug("Auto assign check call for tenant {} and target filter query id {} finished",
log.debug("Auto assign check call for tenant {} and target filter query id {} finished",
getContextAware().getCurrentTenant(), targetFilterQuery.getId());
}

private void checkForDevice(final String controllerId, final TargetFilterQuery targetFilterQuery) {
LOGGER.debug("Auto assign check call for tenant {} and target filter query id {} for device {} started",
log.debug("Auto assign check call for tenant {} and target filter query id {} for device {} started",
getContextAware().getCurrentTenant(), targetFilterQuery.getId(), controllerId);
try {
final boolean controllerIdMatches = targetManagement.isTargetMatchingQueryAndDSNotAssignedAndCompatibleAndUpdatable(
Expand All @@ -128,9 +126,9 @@ private void checkForDevice(final String controllerId, final TargetFilterQuery t
}

} catch (final PersistenceException | AbstractServerRtException e) {
LOGGER.error("Error during auto assign check of target filter query id {}", targetFilterQuery.getId(), e);
log.error("Error during auto assign check of target filter query id {}", targetFilterQuery.getId(), e);
}
LOGGER.debug("Auto assign check call for tenant {} and target filter query id {} finished",
log.debug("Auto assign check call for tenant {} and target filter query id {} finished",
getContextAware().getCurrentTenant(), targetFilterQuery.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@
import java.util.List;
import java.util.concurrent.locks.Lock;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.scheduling.annotation.Scheduled;

/**
* A scheduler to invoke a set of cleanup handlers periodically.
*/
@Slf4j
public class AutoCleanupScheduler {

private static final Logger LOGGER = LoggerFactory.getLogger(AutoCleanupScheduler.class);

private static final String AUTO_CLEANUP = "auto-cleanup";
private static final String SEP = ".";
private static final String PROP_AUTO_CLEANUP_INTERVAL = "${hawkbit.autocleanup.scheduler.fixedDelay:86400000}";
Expand Down Expand Up @@ -62,7 +60,7 @@ public AutoCleanupScheduler(final SystemManagement systemManagement,
*/
@Scheduled(initialDelayString = PROP_AUTO_CLEANUP_INTERVAL, fixedDelayString = PROP_AUTO_CLEANUP_INTERVAL)
public void run() {
LOGGER.debug("Auto cleanup scheduler has been triggered.");
log.debug("Auto cleanup scheduler has been triggered.");
// run this code in system code privileged to have the necessary
// permission to query and create entities
if (!cleanupTasks.isEmpty()) {
Expand All @@ -83,7 +81,7 @@ private Void executeAutoCleanup() {
try {
task.run();
} catch (final RuntimeException e) {
LOGGER.error("Cleanup task failed.", e);
log.error("Cleanup task failed.", e);
} finally {
lock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RepositoryConstants;
import org.eclipse.hawkbit.repository.RepositoryProperties;
Expand Down Expand Up @@ -44,8 +45,6 @@
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetWithActionType;
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

Expand All @@ -54,12 +53,10 @@
/**
* {@link DistributionSet} to {@link Target} assignment strategy as utility for
* {@link JpaDeploymentManagement}.
*
*/
@Slf4j
public abstract class AbstractDsAssignmentStrategy {

private static final Logger LOG = LoggerFactory.getLogger(AbstractDsAssignmentStrategy.class);

protected final TargetRepository targetRepository;
protected final AfterTransactionCommitExecutor afterCommit;
protected final EventPublisherHolder eventPublisherHolder;
Expand Down Expand Up @@ -267,7 +264,7 @@ public JpaAction createTargetAction(final String initiatedBy, final TargetWithAc
actionForTarget.setInitiatedBy(initiatedBy);
return actionForTarget;
}).orElseGet(() -> {
LOG.warn("Cannot find target for targetWithActionType '{}'.", targetWithActionType.getControllerId());
log.warn("Cannot find target for targetWithActionType '{}'.", targetWithActionType.getControllerId());
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RepositoryProperties;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
Expand All @@ -28,8 +29,6 @@
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.ActionStatus;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

Expand All @@ -39,10 +38,9 @@
/**
* Implements utility methods for managing {@link Action}s
*/
@Slf4j
public class JpaActionManagement {

private static final Logger LOG = LoggerFactory.getLogger(JpaActionManagement.class);

protected final ActionRepository actionRepository;
protected final ActionStatusRepository actionStatusRepository;
protected final QuotaManagement quotaManagement;
Expand Down Expand Up @@ -99,7 +97,7 @@ protected Action addActionStatus(final JpaActionStatusCreate statusCreate) {
return handleAddUpdateActionStatus(actionStatus, action);
}

LOG.debug("Update of actionStatus {} for action {} not possible since action not active anymore.",
log.debug("Update of actionStatus {} for action {} not possible since action not active anymore.",
actionStatus.getStatus(), action.getId());
return action;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.ConfirmationManagement;
import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.QuotaManagement;
Expand All @@ -39,8 +40,6 @@
import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.model.AutoConfirmationStatus;
import org.eclipse.hawkbit.repository.model.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
Expand All @@ -51,16 +50,14 @@

/**
* JPA implementation for {@link ConfirmationManagement}.
*
*/
@Slf4j
@Transactional(readOnly = true)
@Validated
public class JpaConfirmationManagement extends JpaActionManagement implements ConfirmationManagement {

public static final String CONFIRMATION_CODE_MSG_PREFIX = "Confirmation status code: %d";

private static final Logger LOG = LoggerFactory.getLogger(JpaConfirmationManagement.class);

private final EntityFactory entityFactory;
private final TargetRepository targetRepository;

Expand All @@ -85,11 +82,11 @@ public List<Action> findActiveActionsWaitingConfirmation(final String controller
@Transactional
public AutoConfirmationStatus activateAutoConfirmation(final String controllerId, final String initiator,
final String remark) {
LOG.trace("'activateAutoConfirmation' was called with values: controllerId={}; initiator={}; remark={}",
log.trace("'activateAutoConfirmation' was called with values: controllerId={}; initiator={}; remark={}",
controllerId, initiator, remark);
final JpaTarget target = getTargetByControllerIdAndThrowIfNotFound(controllerId);
if (target.getAutoConfirmationStatus() != null) {
LOG.debug("'activateAutoConfirmation' was called for an controller id {} with active auto confirmation.",
log.debug("'activateAutoConfirmation' was called for an controller id {} with active auto confirmation.",
controllerId);
throw new AutoConfirmationAlreadyActiveException(controllerId);
}
Expand All @@ -101,7 +98,7 @@ public AutoConfirmationStatus activateAutoConfirmation(final String controllerId
final String message = String.format("Persisted auto confirmation status is null. "
+ "Cannot proceed with giving confirmations for active actions for device %s with initiator %s.",
controllerId, initiator);
LOG.error("message");
log.error("message");
throw new IllegalStateException(message);
}
giveConfirmationForActiveActions(autoConfStatus);
Expand Down Expand Up @@ -129,7 +126,7 @@ public List<Action> autoConfirmActiveActions(final String controllerId) {
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public Action confirmAction(final long actionId, final Integer code, final Collection<String> deviceMessages) {
LOG.trace("Action with id {} confirm request is triggered.", actionId);
log.trace("Action with id {} confirm request is triggered.", actionId);
final Action action = getActionAndThrowExceptionIfNotFound(actionId);
assertActionCanAcceptFeedback(action);
final List<String> messages = new ArrayList<>();
Expand All @@ -148,7 +145,7 @@ public Action confirmAction(final long actionId, final Integer code, final Colle
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public Action denyAction(final long actionId, final Integer code, final Collection<String> deviceMessages) {
LOG.trace("Action with id {} deny request is triggered.", actionId);
log.trace("Action with id {} deny request is triggered.", actionId);
final Action action = getActionAndThrowExceptionIfNotFound(actionId);
assertActionCanAcceptFeedback(action);
final List<String> messages = new ArrayList<>();
Expand Down Expand Up @@ -179,13 +176,13 @@ private static void assertActionCanAcceptFeedback(final Action action) {
if (!action.isActive()) {
final String msg = String.format(
"Confirming action %s is not possible since the action is not active anymore.", action.getId());
LOG.warn(msg);
log.warn(msg);
throw new InvalidConfirmationFeedbackException(InvalidConfirmationFeedbackException.Reason.ACTION_CLOSED,
msg);
} else if (!action.isWaitingConfirmation()) {
LOG.debug("Action is not waiting for confirmation, deny request.");
log.debug("Action is not waiting for confirmation, deny request.");
final String msg = String.format("Action %s is not waiting for confirmation.", action.getId());
LOG.warn(msg);
log.warn(msg);
throw new InvalidConfirmationFeedbackException(
InvalidConfirmationFeedbackException.Reason.NOT_AWAITING_CONFIRMATION, msg);
}
Expand All @@ -199,14 +196,14 @@ private List<Action> giveConfirmationForActiveActions(final AutoConfirmationStat

private Action autoConfirmAction(final JpaAction action, final AutoConfirmationStatus autoConfirmationStatus) {
if (!action.isWaitingConfirmation()) {
LOG.debug("Auto-confirming action is not necessary, since action {} is in RUNNING state already.",
log.debug("Auto-confirming action is not necessary, since action {} is in RUNNING state already.",
action.getId());
return action;
}
final JpaActionStatus actionStatus = (JpaActionStatus) entityFactory.actionStatus().create(action.getId())
.status(Status.RUNNING)
.messages(Collections.singletonList(autoConfirmationStatus.constructActionMessage())).build();
LOG.debug(
log.debug(
"Automatically confirm actionId '{}' due to active auto-confirmation initiated by '{}' and rollouts system user '{}'",
action.getId(), autoConfirmationStatus.getInitiator(), autoConfirmationStatus.getCreatedBy());

Expand All @@ -224,7 +221,7 @@ private Action autoConfirmAction(final JpaAction action, final AutoConfirmationS
@Override
@Transactional
public void deactivateAutoConfirmation(String controllerId) {
LOG.debug("Deactivate auto confirmation for controllerId '{}'", controllerId);
log.debug("Deactivate auto confirmation for controllerId '{}'", controllerId);
final JpaTarget target = getTargetByControllerIdAndThrowIfNotFound(controllerId);
target.setAutoConfirmationStatus(null);
targetRepository.save(target);
Expand Down
Loading

0 comments on commit 990d1a7

Please sign in to comment.