Skip to content

Commit

Permalink
fix(core): 🐛 fix diagnostic execution
Browse files Browse the repository at this point in the history
  • Loading branch information
SCadilhac committed Sep 23, 2024
1 parent 5cd8829 commit 425fc68
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/main/java/onl/netfishers/netshot/device/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public void addDiagnosticResult(DiagnosticResult result) {
while (existingIterator.hasNext()) {
DiagnosticResult existingResult = existingIterator.next();
if (existingResult.getDiagnostic().equals(result.getDiagnostic())) {
if (result.equals(existingResult)) {
if (result.equals(existingResult) && result.valueEquals(existingResult)) {
existingResult.setLastCheckDate(result.getLastCheckDate());
doAdd = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ConfigAttribute(Config config, String name) {
@Transient
public abstract Object getData();

public abstract boolean deepEquals(ConfigAttribute other);
public abstract boolean valueEquals(ConfigAttribute other);

@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Object getData() {
}

@Override
public boolean deepEquals(ConfigAttribute obj) {
public boolean valueEquals(ConfigAttribute obj) {
if (this == obj)
return true;
if (!(obj instanceof ConfigBinaryAttribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Object getData() {
}

@Override
public boolean deepEquals(ConfigAttribute obj) {
public boolean valueEquals(ConfigAttribute obj) {
if (this == obj)
return true;
if (!(obj instanceof ConfigBinaryFileAttribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Object getData() {
}

@Override
public boolean deepEquals(ConfigAttribute obj) {
public boolean valueEquals(ConfigAttribute obj) {
if (this == obj)
return true;
if (!(obj instanceof ConfigLongTextAttribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Object getData() {
}

@Override
public boolean deepEquals(ConfigAttribute obj) {
public boolean valueEquals(ConfigAttribute obj) {
if (this == obj)
return true;
if (!(obj instanceof ConfigNumericAttribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Object getData() {
}

@Override
public boolean deepEquals(ConfigAttribute obj) {
public boolean valueEquals(ConfigAttribute obj) {
if (this == obj)
return true;
if (!(obj instanceof ConfigTextAttribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected void run(Session session, Device device, Cli cli, Snmp snmp, DriverPro
ConfigAttribute oldAttribute = oldAttributes.get(definition.getName());
ConfigAttribute newAttribute = newAttributes.get(definition.getName());
if (oldAttribute != null) {
if (!oldAttribute.deepEquals(newAttribute)) {
if (!oldAttribute.valueEquals(newAttribute)) {
different = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import jakarta.persistence.Transient;
import jakarta.xml.bind.annotation.XmlElement;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonView;

import lombok.Getter;
Expand Down Expand Up @@ -73,27 +75,13 @@ public Object getData() {
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((assumption == null) ? 0 : assumption.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
public boolean valueEquals(DiagnosticResult obj) {
if (this == obj)
return true;
if (!(obj instanceof DiagnosticBinaryResult))
return false;
DiagnosticBinaryResult other = (DiagnosticBinaryResult) obj;
if (assumption == null) {
if (other.assumption != null)
return false;
}
else if (!assumption.equals(other.assumption))
return false;
return true;
return Objects.equals(this.assumption, other.assumption);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Transient;

import java.util.Objects;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

Expand All @@ -37,7 +38,7 @@
public class DiagnosticLongTextResult extends DiagnosticResult {

@Getter(onMethod=@__({
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY),
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true),
@OnDelete(action = OnDeleteAction.CASCADE)
}))
@Setter
Expand Down Expand Up @@ -70,27 +71,13 @@ public Object getData() {
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((longText == null) ? 0 : longText.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
public boolean valueEquals(DiagnosticResult obj) {
if (this == obj)
return true;
if (!(obj instanceof DiagnosticLongTextResult))
return false;
DiagnosticLongTextResult other = (DiagnosticLongTextResult) obj;
if (longText == null) {
if (other.longText != null)
return false;
}
else if (!longText.equals(other.longText))
return false;
return true;
return Objects.equals(this.longText, other.longText);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import jakarta.persistence.Transient;
import jakarta.xml.bind.annotation.XmlElement;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonView;

import lombok.Getter;
Expand Down Expand Up @@ -68,27 +70,13 @@ public Object getData() {
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((number == null) ? 0 : number.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
public boolean valueEquals(DiagnosticResult obj) {
if (this == obj)
return true;
if (!(obj instanceof DiagnosticNumericResult))
return false;
DiagnosticNumericResult other = (DiagnosticNumericResult) obj;
if (number == null) {
if (other.number != null)
return false;
}
else if (!number.equals(other.number))
return false;
return true;
return Objects.equals(this.number, other.number);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package onl.netfishers.netshot.diagnostic;

import java.util.Date;
import java.util.Objects;

import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
Expand Down Expand Up @@ -145,4 +146,20 @@ public String getDiagnosticName() {
return this.getDiagnostic().getName();
}

public abstract boolean valueEquals(DiagnosticResult obj);

@Override
public int hashCode() {
return Objects.hash(diagnostic, device);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof DiagnosticResult)) return false;
DiagnosticResult other = (DiagnosticResult) obj;
return Objects.equals(diagnostic, other.diagnostic)
&& Objects.equals(device, other.device);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import jakarta.persistence.Transient;
import jakarta.xml.bind.annotation.XmlElement;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonView;

import lombok.Getter;
Expand Down Expand Up @@ -65,27 +67,13 @@ public Object getData() {
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((text == null) ? 0 : text.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
public boolean valueEquals(DiagnosticResult obj) {
if (this == obj)
return true;
if (!(obj instanceof DiagnosticTextResult))
return false;
DiagnosticTextResult other = (DiagnosticTextResult) obj;
if (text == null) {
if (other.text != null)
return false;
}
else if (!text.equals(other.text))
return false;
return true;
return Objects.equals(this.text, other.text);
}

}
4 changes: 3 additions & 1 deletion src/main/java/onl/netfishers/netshot/rest/RestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9039,7 +9039,9 @@ public List<DiagnosticResult> getDeviceDiagnosticResults(@BeanParam PaginationPa
Session session = Database.getSession(true);
try {
Query<DiagnosticResult> query = session
.createQuery("from DiagnosticResult dr join fetch dr.diagnostic where dr.device.id = :id", DiagnosticResult.class)
.createQuery(
"from DiagnosticResult dr join fetch dr.diagnostic where dr.device.id = :id",
DiagnosticResult.class)
.setParameter("id", id);
paginationParams.apply(query);
return query.list();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/onl/netfishers/netshot/work/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import onl.netfishers.netshot.work.tasks.TakeGroupSnapshotTask;
import onl.netfishers.netshot.work.tasks.TakeSnapshotTask;

import org.hibernate.Session;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.quartz.JobKey;
Expand Down Expand Up @@ -492,8 +493,8 @@ public void onSchedule() {
/**
* Prepare.
*/
public void prepare() {

public void prepare(Session session) {
// Override to actually do something
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/onl/netfishers/netshot/work/TaskJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,27 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
task.setRunning();
log.trace("The task runner ID for {} is {}", task.getId(), task.getRunnerId());
session.merge(task);
session.getTransaction().commit();
log.trace("Got the task.");
task.prepare();
task.prepare(session);
log.trace("The task has prepared its fields.");
session.getTransaction().commit();
}
catch (Exception e) {
log.error("Error while retrieving and updating the task.", e);
log.error("Error while retrieving, updating or preparing the task.", e);
try {
session.getTransaction().rollback();
session.beginTransaction();
Task eTask = (Task) session.get(Task.class, id);
eTask.setRunning();
eTask.setFailed();
eTask.setLog(e.getMessage());
session.merge(eTask);
session.getTransaction().commit();
}
catch (Exception e1) {

}
throw new JobExecutionException("Unable to access the task.");
throw new JobExecutionException("Unable to access or prepare the task.");
}
finally {
session.close();
Expand All @@ -103,7 +110,7 @@ public void execute(JobExecutionContext context) throws JobExecutionException {

if (task.getStatus() == Status.RUNNING) {
log.error("The task {} exited with a status of RUNNING.", id);
task.setStatus(Status.FAILURE);
task.setFailed();
}

log.trace("Updating the task with the result.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public CheckComplianceTask(Device device, String comments, String author) {
* @see onl.netfishers.netshot.work.Task#prepare()
*/
@Override
public void prepare() {
public void prepare(Session session) {
Hibernate.initialize(device);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String getTaskDescription() {
* @see onl.netfishers.netshot.work.Task#prepare()
*/
@Override
public void prepare() {
public void prepare(Session session) {
Hibernate.initialize(this.getDeviceGroup());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String getTaskDescription() {
* @see onl.netfishers.netshot.work.Task#prepare()
*/
@Override
public void prepare() {
public void prepare(Session session) {
Hibernate.initialize(this.getDeviceGroup());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private boolean snmpv3Discover(DeviceSnmpv3Community cred) {
* @see onl.netfishers.netshot.work.Task#prepare()
*/
@Override
public void prepare() {
public void prepare(Session session) {
Hibernate.initialize(this.getCredentialSets());
this.getDomain().getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import onl.netfishers.netshot.work.Task;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
Expand Down Expand Up @@ -108,7 +109,7 @@ public String getTaskDescription() {
* @see onl.netfishers.netshot.work.Task#prepare()
*/
@Override
public void prepare() {
public void prepare(Session session) {
Hibernate.initialize(this.getDeviceGroup());
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
}
Expand Down
Loading

0 comments on commit 425fc68

Please sign in to comment.