Fix sonar findings: JpaRolloutGroup (#1988)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-11-09 18:48:35 +02:00
committed by GitHub
parent 4299377769
commit a9714337e8
3 changed files with 59 additions and 168 deletions

View File

@@ -232,7 +232,7 @@ public class JpaRolloutExecutor implements RolloutExecutor {
if (readyGroups == rolloutGroups.size()) {
if (rollout.isDynamic() && !rolloutGroups.get(rolloutGroups.size() - 1).isDynamic()) {
// add first dynamic group one by using the last as a parent and as a pattern
createDynamicGroup(rollout, rolloutGroups.get(rolloutGroups.size() - 1), rolloutGroups.size(), RolloutGroupStatus.READY);
createDynamicGroup(rollout, (JpaRolloutGroup) rolloutGroups.get(rolloutGroups.size() - 1), rolloutGroups.size(), RolloutGroupStatus.READY);
}
if (!rolloutApprovalStrategy.isApprovalNeeded(rollout)) {
@@ -583,7 +583,7 @@ public class JpaRolloutExecutor implements RolloutExecutor {
return scheduledGroups == groupsToBeScheduled.size();
}
private RolloutGroup fillRolloutGroupWithTargets(final JpaRollout rollout, final JpaRolloutGroup group,
private JpaRolloutGroup fillRolloutGroupWithTargets(final JpaRollout rollout, final JpaRolloutGroup group,
final List<RolloutGroup> rolloutGroups) {
RolloutHelper.verifyRolloutInStatus(rollout, RolloutStatus.CREATING);
@@ -735,7 +735,7 @@ public class JpaRolloutExecutor implements RolloutExecutor {
return false;
}
private void createDynamicGroup(final JpaRollout rollout, final RolloutGroup lastGroup, final int groupCount,
private void createDynamicGroup(final JpaRollout rollout, final JpaRolloutGroup lastGroup, final int groupCount,
final RolloutGroupStatus status) {
try {
RolloutHelper.verifyRolloutGroupAmount(groupCount + 1, quotaManagement);

View File

@@ -664,7 +664,7 @@ public class JpaRolloutManagement implements RolloutManagement {
}
// create and persist the groups (w/o filling them with targets)
RolloutGroup lastSavedGroup = null;
JpaRolloutGroup lastSavedGroup = null;
for (final RolloutGroup srcGroup : groups) {
final JpaRolloutGroup group = new JpaRolloutGroup();
group.setName(srcGroup.getName());

View File

@@ -9,6 +9,7 @@
*/
package org.eclipse.hawkbit.repository.jpa.model;
import java.io.Serial;
import java.util.Collections;
import java.util.List;
@@ -27,6 +28,8 @@ import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.hawkbit.repository.event.remote.RolloutGroupDeletedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.RolloutGroupUpdatedEvent;
import org.eclipse.hawkbit.repository.model.Rollout;
@@ -43,19 +46,21 @@ import org.eclipse.persistence.descriptors.DescriptorEvent;
* JPA entity definition of persisting a group of an rollout.
*/
@Entity
@Table(name = "sp_rolloutgroup", uniqueConstraints = @UniqueConstraint(columnNames = { "name", "rollout",
"tenant" }, name = "uk_rolloutgroup"))
// exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for
// sub entities
@Table(name = "sp_rolloutgroup", uniqueConstraints = @UniqueConstraint(columnNames = { "name", "rollout", "tenant" }, name = "uk_rolloutgroup"))
// exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for sub entities
@SuppressWarnings("squid:S2160")
public class JpaRolloutGroup extends AbstractJpaNamedEntity implements RolloutGroup, EventAwareEntity {
@Serial
private static final long serialVersionUID = 1L;
@Getter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "rollout", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_rolloutgroup_rollout"))
private JpaRollout rollout;
@Setter
@Getter
@Column(name = "status", nullable = false)
@ObjectTypeConverter(name = "rolloutgroupstatus", objectType = RolloutGroup.RolloutGroupStatus.class, dataType = Integer.class, conversionValues = {
@ConversionValue(objectValue = "READY", dataValue = "0"),
@@ -68,227 +73,107 @@ public class JpaRolloutGroup extends AbstractJpaNamedEntity implements RolloutGr
private RolloutGroupStatus status = RolloutGroupStatus.CREATING;
@CascadeOnDelete
@OneToMany(mappedBy = "rolloutGroup", fetch = FetchType.LAZY, cascade = {
CascadeType.PERSIST }, targetEntity = RolloutTargetGroup.class)
@OneToMany(mappedBy = "rolloutGroup", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST }, targetEntity = RolloutTargetGroup.class)
private List<RolloutTargetGroup> rolloutTargetGroup;
// No foreign key to avoid to many nested cascades on delete which some DBs cannot handle
@Setter
@Getter
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST })
@JoinColumn(name = "parent_id")
private JpaRolloutGroup parent;
@Setter @Getter
@Column(name = "is_dynamic") // dynamic is reserved keyword in some databases
private boolean dynamic;
@Setter
@Getter
@Column(name = "success_condition", nullable = false)
@NotNull
private RolloutGroupSuccessCondition successCondition = RolloutGroupSuccessCondition.THRESHOLD;
@Setter
@Getter
@Column(name = "success_condition_exp", length = 512, nullable = false)
@Size(max = 512)
@NotNull
private String successConditionExp;
@Setter
@Getter
@Column(name = "success_action", nullable = false)
@NotNull
private RolloutGroupSuccessAction successAction = RolloutGroupSuccessAction.NEXTGROUP;
@Setter
@Getter
@Column(name = "success_action_exp", length = 512)
@Size(max = 512)
private String successActionExp;
@Setter
@Getter
@Column(name = "error_condition")
private RolloutGroupErrorCondition errorCondition;
@Setter
@Getter
@Column(name = "error_condition_exp", length = 512)
@Size(max = 512)
private String errorConditionExp;
@Setter
@Getter
@Column(name = "error_action")
private RolloutGroupErrorAction errorAction;
@Setter
@Getter
@Column(name = "error_action_exp", length = 512)
@Size(max = 512)
private String errorActionExp;
@Setter
@Getter
@Column(name = "total_targets")
private int totalTargets;
@Setter
@Getter
@Column(name = "target_filter", length = 1024)
@Size(max = 1024)
private String targetFilterQuery = "";
@Setter
@Getter
@Column(name = "target_percentage")
private float targetPercentage = 100;
@Setter
@Getter
@Column(name = "confirmation_required")
private boolean confirmationRequired;
@Setter
@Transient
private transient TotalTargetCountStatus totalTargetCountStatus;
@Override
public Rollout getRollout() {
return rollout;
}
public void setRollout(final Rollout rollout) {
this.rollout = (JpaRollout) rollout;
}
@Override
public RolloutGroupStatus getStatus() {
return status;
}
public void setStatus(final RolloutGroupStatus status) {
this.status = status;
}
@Override
public RolloutGroup getParent() {
return parent;
}
@Override
public boolean isDynamic() {
return dynamic;
}
public void setDynamic(final boolean dynamic) {
this.dynamic = dynamic;
}
@Override
public RolloutGroupSuccessCondition getSuccessCondition() {
return successCondition;
}
public void setSuccessCondition(final RolloutGroupSuccessCondition finishCondition) {
successCondition = finishCondition;
}
@Override
public String getSuccessConditionExp() {
return successConditionExp;
}
public void setSuccessConditionExp(final String finishExp) {
successConditionExp = finishExp;
}
@Override
public RolloutGroupErrorCondition getErrorCondition() {
return errorCondition;
}
public void setErrorCondition(final RolloutGroupErrorCondition errorCondition) {
this.errorCondition = errorCondition;
}
@Override
public String getErrorConditionExp() {
return errorConditionExp;
}
public void setErrorConditionExp(final String errorExp) {
errorConditionExp = errorExp;
}
@Override
public RolloutGroupErrorAction getErrorAction() {
return errorAction;
}
public void setErrorAction(final RolloutGroupErrorAction errorAction) {
this.errorAction = errorAction;
}
@Override
public String getErrorActionExp() {
return errorActionExp;
}
public void setErrorActionExp(final String errorActionExp) {
this.errorActionExp = errorActionExp;
}
@Override
public RolloutGroupSuccessAction getSuccessAction() {
return successAction;
}
@Override
public String getSuccessActionExp() {
return successActionExp;
}
@Override
public int getTotalTargets() {
return totalTargets;
}
public void setTotalTargets(final int totalTargets) {
this.totalTargets = totalTargets;
}
/**
* @return the totalTargetCountStatus
*/
@Override
public TotalTargetCountStatus getTotalTargetCountStatus() {
if (totalTargetCountStatus == null) {
totalTargetCountStatus = new TotalTargetCountStatus(Long.valueOf(totalTargets), rollout.getActionType());
totalTargetCountStatus = new TotalTargetCountStatus((long) totalTargets, rollout.getActionType());
}
return totalTargetCountStatus;
}
@Override
public String getTargetFilterQuery() {
return targetFilterQuery;
}
public void setTargetFilterQuery(final String targetFilterQuery) {
this.targetFilterQuery = targetFilterQuery;
}
@Override
public float getTargetPercentage() {
return targetPercentage;
}
@Override
public boolean isConfirmationRequired() {
return confirmationRequired;
}
public void setConfirmationRequired(final boolean confirmationRequired) {
this.confirmationRequired = confirmationRequired;
}
public void setTargetPercentage(final float targetPercentage) {
this.targetPercentage = targetPercentage;
}
/**
* @param totalTargetCountStatus the totalTargetCountStatus to set
*/
public void setTotalTargetCountStatus(final TotalTargetCountStatus totalTargetCountStatus) {
this.totalTargetCountStatus = totalTargetCountStatus;
}
public void setSuccessActionExp(final String successActionExp) {
this.successActionExp = successActionExp;
}
public void setSuccessAction(final RolloutGroupSuccessAction successAction) {
this.successAction = successAction;
}
public void setParent(final RolloutGroup parent) {
this.parent = (JpaRolloutGroup) parent;
}
public List<RolloutTargetGroup> getRolloutTargetGroup() {
if (rolloutTargetGroup == null) {
return Collections.emptyList();
@@ -299,11 +184,17 @@ public class JpaRolloutGroup extends AbstractJpaNamedEntity implements RolloutGr
@Override
public String toString() {
return "RolloutGroup [rollout=" + (rollout != null ? rollout.getId() : "") + ", status=" + status
+ ", rolloutTargetGroup=" + rolloutTargetGroup + ", parent=" + parent + ", finishCondition="
+ successCondition + ", finishExp=" + successConditionExp + ", errorCondition=" + errorCondition
+ ", errorExp=" + errorConditionExp + ", getName()=" + getName() + ", getId()=" + getId()
+ ", isConfirmationRequired()=" + isConfirmationRequired() + "]";
return "RolloutGroup [" +
"rollout=" + (rollout != null ? rollout.getId() : "") +
", name=" + getName() +
", status=" + status +
", parent=" + parent +
", finishCondition=" + successCondition + ", finishExp=" + successConditionExp +
", errorCondition=" + errorCondition + ", errorExp=" + errorConditionExp +
", isConfirmationRequired()=" + isConfirmationRequired() +
", rolloutTargetGroup=" + rolloutTargetGroup +
", id()=" + getId() +
"]";
}
@Override
@@ -313,13 +204,13 @@ public class JpaRolloutGroup extends AbstractJpaNamedEntity implements RolloutGr
@Override
public void fireUpdateEvent(final DescriptorEvent descriptorEvent) {
EventPublisherHolder.getInstance().getEventPublisher().publishEvent(new RolloutGroupUpdatedEvent(this,
this.getRollout().getId(), EventPublisherHolder.getInstance().getApplicationId()));
EventPublisherHolder.getInstance().getEventPublisher().publishEvent(
new RolloutGroupUpdatedEvent(this, getRollout().getId(), EventPublisherHolder.getInstance().getApplicationId()));
}
@Override
public void fireDeleteEvent(final DescriptorEvent descriptorEvent) {
EventPublisherHolder.getInstance().getEventPublisher().publishEvent(new RolloutGroupDeletedEvent(getTenant(),
getId(), getClass(), EventPublisherHolder.getInstance().getApplicationId()));
EventPublisherHolder.getInstance().getEventPublisher().publishEvent(
new RolloutGroupDeletedEvent(getTenant(), getId(), getClass(), EventPublisherHolder.getInstance().getApplicationId()));
}
}
}