2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.enrichment.repository;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.TypeAdapterFactory;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.lang.invoke.MethodHandles;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.Collection;
36 import java.util.HashMap;
38 import java.util.ServiceLoader;
39 import java.util.Vector;
41 import org.oransc.enrichment.configuration.ApplicationConfig;
42 import org.oransc.enrichment.exceptions.ServiceException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.util.FileSystemUtils;
48 * Dynamic representation of all existing EI jobs.
51 private Map<String, EiJob> allEiJobs = new HashMap<>();
53 private MultiMap<EiJob> jobsByType = new MultiMap<>();
54 private MultiMap<EiJob> jobsByOwner = new MultiMap<>();
55 private final Gson gson;
57 private final ApplicationConfig config;
58 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 public EiJobs(ApplicationConfig config) {
62 GsonBuilder gsonBuilder = new GsonBuilder();
63 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
64 this.gson = gsonBuilder.create();
67 public synchronized void restoreJobsFromDatabase() throws IOException {
68 File dbDir = new File(getDatabaseDirectory());
69 for (File file : dbDir.listFiles()) {
70 String json = Files.readString(file.toPath());
71 EiJob job = gson.fromJson(json, EiJob.class);
76 public synchronized void put(EiJob job) {
80 public synchronized Collection<EiJob> getJobs() {
81 return new Vector<>(allEiJobs.values());
84 public synchronized EiJob getJob(String id) throws ServiceException {
85 EiJob ric = allEiJobs.get(id);
87 throw new ServiceException("Could not find EI job: " + id);
92 public synchronized Collection<EiJob> getJobsForType(String typeId) {
93 return jobsByType.get(typeId);
96 public synchronized Collection<EiJob> getJobsForType(EiType type) {
97 return jobsByType.get(type.getId());
100 public synchronized Collection<EiJob> getJobsForOwner(String owner) {
101 return jobsByOwner.get(owner);
104 public synchronized EiJob get(String id) {
105 return allEiJobs.get(id);
108 public synchronized EiJob remove(String id) {
109 EiJob job = allEiJobs.get(id);
116 public synchronized void remove(EiJob job) {
117 this.allEiJobs.remove(job.id());
118 jobsByType.remove(job.typeId(), job.id());
119 jobsByOwner.remove(job.owner(), job.id());
122 Files.delete(getPath(job));
123 } catch (IOException e) {
124 logger.warn("Could not remove file: {}", e.getMessage());
129 public synchronized int size() {
130 return allEiJobs.size();
133 public synchronized void clear() {
134 this.allEiJobs.clear();
135 this.jobsByType.clear();
138 FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
139 } catch (IOException e) {
140 logger.warn("Could not delete database : {}", e.getMessage());
144 private void put(EiJob job, boolean storePersistently) {
145 allEiJobs.put(job.id(), job);
146 jobsByType.put(job.typeId(), job.id(), job);
147 jobsByOwner.put(job.owner(), job.id(), job);
148 if (storePersistently) {
153 private void storeJobInFile(EiJob job) {
155 Files.createDirectories(Paths.get(getDatabaseDirectory()));
156 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
157 out.print(gson.toJson(job));
159 } catch (Exception e) {
160 logger.warn("Could not save job: {} {}", job.id(), e.getMessage());
164 private File getFile(EiJob job) {
165 return getPath(job).toFile();
168 private Path getPath(EiJob job) {
169 return Path.of(getDatabaseDirectory(), job.id());
172 private String getDatabaseDirectory() {
173 return config.getVardataDirectory() + "/database";