X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=enrichment-coordinator-service%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fenrichment%2Frepository%2FEiJobs.java;h=1532c535a54a2c44afc4b7cd83ffac88897ca6b2;hb=768cb9522bcb458171eddd5cc6213fb8e8797be3;hp=7a0dcfd172edba5dd0b3225d8239767d0da334fe;hpb=b5cb68ea0e77d0a1421b4f17cc58b981628c29f7;p=nonrtric.git diff --git a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/repository/EiJobs.java b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/repository/EiJobs.java index 7a0dcfd1..1532c535 100644 --- a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/repository/EiJobs.java +++ b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/repository/EiJobs.java @@ -20,26 +20,61 @@ package org.oransc.enrichment.repository; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapterFactory; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.invoke.MethodHandles; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.ServiceLoader; import java.util.Vector; +import org.oransc.enrichment.configuration.ApplicationConfig; import org.oransc.enrichment.exceptions.ServiceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.FileSystemUtils; /** - * Dynamic representation of all EI Jobs in the system. + * Dynamic representation of all existing EI jobs. */ public class EiJobs { private Map allEiJobs = new HashMap<>(); private MultiMap jobsByType = new MultiMap<>(); private MultiMap jobsByOwner = new MultiMap<>(); + private final Gson gson; + + private final ApplicationConfig config; + private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public EiJobs(ApplicationConfig config) { + this.config = config; + GsonBuilder gsonBuilder = new GsonBuilder(); + ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory); + this.gson = gsonBuilder.create(); + } + + public synchronized void restoreJobsFromDatabase() throws IOException { + File dbDir = new File(getDatabaseDirectory()); + for (File file : dbDir.listFiles()) { + String json = Files.readString(file.toPath()); + EiJob job = gson.fromJson(json, EiJob.class); + this.put(job, false); + } + } public synchronized void put(EiJob job) { - allEiJobs.put(job.id(), job); - jobsByType.put(job.type().getId(), job.id(), job); - jobsByOwner.put(job.owner(), job.id(), job); + this.put(job, true); } public synchronized Collection getJobs() { @@ -80,8 +115,15 @@ public class EiJobs { public synchronized void remove(EiJob job) { this.allEiJobs.remove(job.id()); - jobsByType.remove(job.type().getId(), job.id()); + jobsByType.remove(job.typeId(), job.id()); jobsByOwner.remove(job.owner(), job.id()); + + try { + Files.delete(getPath(job)); + } catch (IOException e) { + logger.warn("Could not remove file: {}", e.getMessage()); + } + } public synchronized int size() { @@ -92,6 +134,43 @@ public class EiJobs { this.allEiJobs.clear(); this.jobsByType.clear(); jobsByOwner.clear(); + try { + FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory())); + } catch (IOException e) { + logger.warn("Could not delete database : {}", e.getMessage()); + } + } + + private void put(EiJob job, boolean storePersistently) { + allEiJobs.put(job.id(), job); + jobsByType.put(job.typeId(), job.id(), job); + jobsByOwner.put(job.owner(), job.id(), job); + if (storePersistently) { + storeJobInFile(job); + } + } + + private void storeJobInFile(EiJob job) { + try { + Files.createDirectories(Paths.get(getDatabaseDirectory())); + try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) { + out.print(gson.toJson(job)); + } + } catch (Exception e) { + logger.warn("Could not save job: {} {}", job.id(), e.getMessage()); + } + } + + private File getFile(EiJob job) { + return getPath(job).toFile(); + } + + private Path getPath(EiJob job) { + return Path.of(getDatabaseDirectory(), job.id()); + } + + private String getDatabaseDirectory() { + return config.getVardataDirectory() + "/database"; } }