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 Files.createDirectories(Paths.get(getDatabaseDirectory()));
69 File dbDir = new File(getDatabaseDirectory());
71 for (File file : dbDir.listFiles()) {
72 String json = Files.readString(file.toPath());
73 EiJob job = gson.fromJson(json, EiJob.class);
79 public synchronized void put(EiJob job) {
83 public synchronized Collection<EiJob> getJobs() {
84 return new Vector<>(allEiJobs.values());
87 public synchronized EiJob getJob(String id) throws ServiceException {
88 EiJob ric = allEiJobs.get(id);
90 throw new ServiceException("Could not find EI job: " + id);
95 public synchronized Collection<EiJob> getJobsForType(String typeId) {
96 return jobsByType.get(typeId);
99 public synchronized Collection<EiJob> getJobsForType(EiType type) {
100 return jobsByType.get(type.getId());
103 public synchronized Collection<EiJob> getJobsForOwner(String owner) {
104 return jobsByOwner.get(owner);
107 public synchronized EiJob get(String id) {
108 return allEiJobs.get(id);
111 public synchronized EiJob remove(String id) {
112 EiJob job = allEiJobs.get(id);
119 public synchronized void remove(EiJob job) {
120 this.allEiJobs.remove(job.getId());
121 jobsByType.remove(job.getTypeId(), job.getId());
122 jobsByOwner.remove(job.getOwner(), job.getId());
125 Files.delete(getPath(job));
126 } catch (IOException e) {
127 logger.warn("Could not remove file: {}", e.getMessage());
132 public synchronized int size() {
133 return allEiJobs.size();
136 public synchronized void clear() {
137 this.allEiJobs.clear();
138 this.jobsByType.clear();
141 FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
142 Files.createDirectories(Paths.get(getDatabaseDirectory()));
143 } catch (IOException e) {
144 logger.warn("Could not delete database : {}", e.getMessage());
148 private void put(EiJob job, boolean storePersistently) {
149 allEiJobs.put(job.getId(), job);
150 jobsByType.put(job.getTypeId(), job.getId(), job);
151 jobsByOwner.put(job.getOwner(), job.getId(), job);
152 if (storePersistently) {
157 private void storeJobInFile(EiJob job) {
159 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
160 out.print(gson.toJson(job));
162 } catch (Exception e) {
163 logger.warn("Could not save job: {} {}", job.getId(), e.getMessage());
167 private File getFile(EiJob job) {
168 return getPath(job).toFile();
171 private Path getPath(EiJob job) {
172 return Path.of(getDatabaseDirectory(), job.getId());
175 private String getDatabaseDirectory() {
176 return config.getVardataDirectory() + "/database";