Storing EiJobs persistently
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / repository / EiJobs.java
index 606ee41..1532c53 100644 (file)
 
 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<String, EiJob> allEiJobs = new HashMap<>();
 
     private MultiMap<EiJob> jobsByType = new MultiMap<>();
+    private MultiMap<EiJob> 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);
+        this.put(job, true);
     }
 
     public synchronized Collection<EiJob> getJobs() {
@@ -60,6 +97,10 @@ public class EiJobs {
         return jobsByType.get(type.getId());
     }
 
+    public synchronized Collection<EiJob> getJobsForOwner(String owner) {
+        return jobsByOwner.get(owner);
+    }
+
     public synchronized EiJob get(String id) {
         return allEiJobs.get(id);
     }
@@ -74,7 +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() {
@@ -84,6 +133,44 @@ public class EiJobs {
     public synchronized void clear() {
         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";
     }
 
 }