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.ics.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.ics.configuration.ApplicationConfig;
42 import org.oransc.ics.controllers.r1producer.ProducerCallbacks;
43 import org.oransc.ics.exceptions.ServiceException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.util.FileSystemUtils;
50 * Dynamic representation of all existing Information Jobs.
52 public class InfoJobs {
53 private Map<String, InfoJob> allEiJobs = new HashMap<>();
55 private MultiMap<InfoJob> jobsByType = new MultiMap<>();
56 private MultiMap<InfoJob> jobsByOwner = new MultiMap<>();
57 private final Gson gson;
59 private final ApplicationConfig config;
60 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62 private final ProducerCallbacks producerCallbacks;
64 public InfoJobs(ApplicationConfig config, ProducerCallbacks producerCallbacks) {
66 GsonBuilder gsonBuilder = new GsonBuilder();
67 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
68 this.gson = gsonBuilder.create();
69 this.producerCallbacks = producerCallbacks;
72 public synchronized void restoreJobsFromDatabase() throws IOException {
73 Files.createDirectories(Paths.get(getDatabaseDirectory()));
74 File dbDir = new File(getDatabaseDirectory());
76 for (File file : dbDir.listFiles()) {
77 String json = Files.readString(file.toPath());
78 InfoJob job = gson.fromJson(json, InfoJob.class);
83 public synchronized void put(InfoJob job) {
88 public synchronized Collection<InfoJob> getJobs() {
89 return new Vector<>(allEiJobs.values());
92 public synchronized InfoJob getJob(String id) throws ServiceException {
93 InfoJob ric = allEiJobs.get(id);
95 throw new ServiceException("Could not find Information job: " + id, HttpStatus.NOT_FOUND);
100 public synchronized Collection<InfoJob> getJobsForType(String typeId) {
101 return jobsByType.get(typeId);
104 public synchronized Collection<InfoJob> getJobsForType(InfoType type) {
105 return jobsByType.get(type.getId());
108 public synchronized Collection<InfoJob> getJobsForOwner(String owner) {
109 return jobsByOwner.get(owner);
112 public synchronized InfoJob get(String id) {
113 return allEiJobs.get(id);
116 public synchronized InfoJob remove(String id, InfoProducers infoProducers) {
117 InfoJob job = allEiJobs.get(id);
119 remove(job, infoProducers);
124 public synchronized void remove(InfoJob job, InfoProducers infoProducers) {
125 this.allEiJobs.remove(job.getId());
126 jobsByType.remove(job.getTypeId(), job.getId());
127 jobsByOwner.remove(job.getOwner(), job.getId());
130 Files.delete(getPath(job));
131 } catch (IOException e) {
132 logger.warn("Could not remove file: {}", e.getMessage());
134 this.producerCallbacks.stopInfoJob(job, infoProducers);
137 public synchronized int size() {
138 return allEiJobs.size();
141 public synchronized void clear() {
142 this.allEiJobs.clear();
143 this.jobsByType.clear();
148 private void clearDatabase() {
150 FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
151 Files.createDirectories(Paths.get(getDatabaseDirectory()));
152 } catch (IOException e) {
153 logger.warn("Could not delete database : {}", e.getMessage());
157 private void doPut(InfoJob job) {
158 allEiJobs.put(job.getId(), job);
159 jobsByType.put(job.getTypeId(), job.getId(), job);
160 jobsByOwner.put(job.getOwner(), job.getId(), job);
163 private void storeJobInFile(InfoJob job) {
165 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
166 out.print(gson.toJson(job));
168 } catch (Exception e) {
169 logger.warn("Could not store job: {} {}", job.getId(), e.getMessage());
173 private File getFile(InfoJob job) {
174 return getPath(job).toFile();
177 private Path getPath(InfoJob job) {
178 return Path.of(getDatabaseDirectory(), job.getId());
181 private String getDatabaseDirectory() {
182 return config.getVardataDirectory() + "/database/eijobs";