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.controllers.r1producer.ProducerCallbacks;
43 import org.oransc.enrichment.exceptions.ServiceException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.util.FileSystemUtils;
49 * Dynamic representation of all existing Information Jobs.
51 public class InfoJobs {
52 private Map<String, InfoJob> allEiJobs = new HashMap<>();
54 private MultiMap<InfoJob> jobsByType = new MultiMap<>();
55 private MultiMap<InfoJob> jobsByOwner = new MultiMap<>();
56 private final Gson gson;
58 private final ApplicationConfig config;
59 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61 private final ProducerCallbacks producerCallbacks;
63 public InfoJobs(ApplicationConfig config, ProducerCallbacks producerCallbacks) {
65 GsonBuilder gsonBuilder = new GsonBuilder();
66 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
67 this.gson = gsonBuilder.create();
68 this.producerCallbacks = producerCallbacks;
71 public synchronized void restoreJobsFromDatabase() throws IOException {
72 Files.createDirectories(Paths.get(getDatabaseDirectory()));
73 File dbDir = new File(getDatabaseDirectory());
75 for (File file : dbDir.listFiles()) {
76 String json = Files.readString(file.toPath());
77 InfoJob job = gson.fromJson(json, InfoJob.class);
82 public synchronized void put(InfoJob job) {
87 public synchronized Collection<InfoJob> getJobs() {
88 return new Vector<>(allEiJobs.values());
91 public synchronized InfoJob getJob(String id) throws ServiceException {
92 InfoJob ric = allEiJobs.get(id);
94 throw new ServiceException("Could not find Information job: " + id);
99 public synchronized Collection<InfoJob> getJobsForType(String typeId) {
100 return jobsByType.get(typeId);
103 public synchronized Collection<InfoJob> getJobsForType(InfoType type) {
104 return jobsByType.get(type.getId());
107 public synchronized Collection<InfoJob> getJobsForOwner(String owner) {
108 return jobsByOwner.get(owner);
111 public synchronized InfoJob get(String id) {
112 return allEiJobs.get(id);
115 public synchronized InfoJob remove(String id, InfoProducers infoProducers) {
116 InfoJob job = allEiJobs.get(id);
118 remove(job, infoProducers);
123 public synchronized void remove(InfoJob job, InfoProducers infoProducers) {
124 this.allEiJobs.remove(job.getId());
125 jobsByType.remove(job.getTypeId(), job.getId());
126 jobsByOwner.remove(job.getOwner(), job.getId());
129 Files.delete(getPath(job));
130 } catch (IOException e) {
131 logger.warn("Could not remove file: {}", e.getMessage());
133 this.producerCallbacks.stopInfoJob(job, infoProducers);
136 public synchronized int size() {
137 return allEiJobs.size();
140 public synchronized void clear() {
141 this.allEiJobs.clear();
142 this.jobsByType.clear();
147 private void clearDatabase() {
149 FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
150 Files.createDirectories(Paths.get(getDatabaseDirectory()));
151 } catch (IOException e) {
152 logger.warn("Could not delete database : {}", e.getMessage());
156 private void doPut(InfoJob job) {
157 allEiJobs.put(job.getId(), job);
158 jobsByType.put(job.getTypeId(), job.getId(), job);
159 jobsByOwner.put(job.getOwner(), job.getId(), job);
162 private void storeJobInFile(InfoJob job) {
164 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
165 out.print(gson.toJson(job));
167 } catch (Exception e) {
168 logger.warn("Could not store job: {} {}", job.getId(), e.getMessage());
172 private File getFile(InfoJob job) {
173 return getPath(job).toFile();
176 private Path getPath(InfoJob job) {
177 return Path.of(getDatabaseDirectory(), job.getId());
180 private String getDatabaseDirectory() {
181 return config.getVardataDirectory() + "/database/eijobs";