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=bff5be2c3a27ee705c9400a88ad72d6916d10161;hb=3ac4de0524650cea3d17f9ad5ff7e9cf5dffbe83;hp=bb2e40fd8348be78526b8f50e4349a20076130b4;hpb=6f86ab364ac739951556bf2d5bf70429b518de47;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 bb2e40fd..bff5be2c 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,24 +20,64 @@ 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.Collections; 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 Map> jobsByType = 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 { + Files.createDirectories(Paths.get(getDatabaseDirectory())); + 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); - multiMapPut(this.jobsByType, job.typeId(), job); + this.put(job, true); } public synchronized Collection getJobs() { @@ -47,13 +87,21 @@ public class EiJobs { public synchronized EiJob getJob(String id) throws ServiceException { EiJob ric = allEiJobs.get(id); if (ric == null) { - throw new ServiceException("Could not find EI Job: " + id); + throw new ServiceException("Could not find EI job: " + id); } return ric; } public synchronized Collection getJobsForType(String typeId) { - return multiMapGet(this.jobsByType, typeId); + return jobsByType.get(typeId); + } + + public synchronized Collection getJobsForType(EiType type) { + return jobsByType.get(type.getId()); + } + + public synchronized Collection getJobsForOwner(String owner) { + return jobsByOwner.get(owner); } public synchronized EiJob get(String id) { @@ -69,8 +117,16 @@ public class EiJobs { } public synchronized void remove(EiJob job) { - this.allEiJobs.remove(job.id()); - multiMapRemove(this.jobsByType, job.typeId(), job); + this.allEiJobs.remove(job.getId()); + jobsByType.remove(job.getTypeId(), job.getId()); + jobsByOwner.remove(job.getOwner(), job.getId()); + + try { + Files.delete(getPath(job)); + } catch (IOException e) { + logger.warn("Could not remove file: {}", e.getMessage()); + } + } public synchronized int size() { @@ -79,28 +135,45 @@ public class EiJobs { public synchronized void clear() { this.allEiJobs.clear(); + this.jobsByType.clear(); + jobsByOwner.clear(); + try { + FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory())); + Files.createDirectories(Paths.get(getDatabaseDirectory())); + } catch (IOException e) { + logger.warn("Could not delete database : {}", e.getMessage()); + } } - private void multiMapPut(Map> multiMap, String key, EiJob value) { - multiMap.computeIfAbsent(key, k -> new HashMap<>()).put(value.id(), value); + private void put(EiJob job, boolean storePersistently) { + allEiJobs.put(job.getId(), job); + jobsByType.put(job.getTypeId(), job.getId(), job); + jobsByOwner.put(job.getOwner(), job.getId(), job); + if (storePersistently) { + storeJobInFile(job); + } } - private void multiMapRemove(Map> multiMap, String key, EiJob value) { - Map map = multiMap.get(key); - if (map != null) { - map.remove(value.id()); - if (map.isEmpty()) { - multiMap.remove(key); + private void storeJobInFile(EiJob job) { + try { + try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) { + out.print(gson.toJson(job)); } + } catch (Exception e) { + logger.warn("Could not save job: {} {}", job.getId(), e.getMessage()); } } - private Collection multiMapGet(Map> multiMap, String key) { - Map map = multiMap.get(key); - if (map == null) { - return Collections.emptyList(); - } - return new Vector<>(map.values()); + private File getFile(EiJob job) { + return getPath(job).toFile(); + } + + private Path getPath(EiJob job) { + return Path.of(getDatabaseDirectory(), job.getId()); + } + + private String getDatabaseDirectory() { + return config.getVardataDirectory() + "/database"; } }