f5224f25b8ecb45eecdb70a9067e8e81b08166b8
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / repository / EiJobs.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.enrichment.repository;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.TypeAdapterFactory;
26
27 import java.io.File;
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;
37 import java.util.Map;
38 import java.util.ServiceLoader;
39 import java.util.Vector;
40
41 import org.oransc.enrichment.configuration.ApplicationConfig;
42 import org.oransc.enrichment.controllers.producer.ProducerCallbacks;
43 import org.oransc.enrichment.exceptions.ServiceException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.util.FileSystemUtils;
47
48 /**
49  * Dynamic representation of all existing EI jobs.
50  */
51 public class EiJobs {
52     private Map<String, EiJob> allEiJobs = new HashMap<>();
53
54     private MultiMap<EiJob> jobsByType = new MultiMap<>();
55     private MultiMap<EiJob> jobsByOwner = new MultiMap<>();
56     private final Gson gson;
57
58     private final ApplicationConfig config;
59     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60
61     private final ProducerCallbacks producerCallbacks;
62
63     public EiJobs(ApplicationConfig config, ProducerCallbacks producerCallbacks) {
64         this.config = config;
65         GsonBuilder gsonBuilder = new GsonBuilder();
66         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
67         this.gson = gsonBuilder.create();
68         this.producerCallbacks = producerCallbacks;
69     }
70
71     public synchronized void restoreJobsFromDatabase() throws IOException {
72         Files.createDirectories(Paths.get(getDatabaseDirectory()));
73         File dbDir = new File(getDatabaseDirectory());
74
75         for (File file : dbDir.listFiles()) {
76             String json = Files.readString(file.toPath());
77             EiJob job = gson.fromJson(json, EiJob.class);
78             this.doPut(job);
79         }
80     }
81
82     public synchronized void put(EiJob job) {
83         this.doPut(job);
84         storeJobInFile(job);
85     }
86
87     public synchronized Collection<EiJob> getJobs() {
88         return new Vector<>(allEiJobs.values());
89     }
90
91     public synchronized EiJob getJob(String id) throws ServiceException {
92         EiJob ric = allEiJobs.get(id);
93         if (ric == null) {
94             throw new ServiceException("Could not find EI job: " + id);
95         }
96         return ric;
97     }
98
99     public synchronized Collection<EiJob> getJobsForType(String typeId) {
100         return jobsByType.get(typeId);
101     }
102
103     public synchronized Collection<EiJob> getJobsForType(EiType type) {
104         return jobsByType.get(type.getId());
105     }
106
107     public synchronized Collection<EiJob> getJobsForOwner(String owner) {
108         return jobsByOwner.get(owner);
109     }
110
111     public synchronized EiJob get(String id) {
112         return allEiJobs.get(id);
113     }
114
115     public synchronized EiJob remove(String id, EiProducers eiProducers) {
116         EiJob job = allEiJobs.get(id);
117         if (job != null) {
118             remove(job, eiProducers);
119         }
120         return job;
121     }
122
123     public synchronized void remove(EiJob job, EiProducers eiProducers) {
124         this.allEiJobs.remove(job.getId());
125         jobsByType.remove(job.getTypeId(), job.getId());
126         jobsByOwner.remove(job.getOwner(), job.getId());
127
128         try {
129             Files.delete(getPath(job));
130         } catch (IOException e) {
131             logger.warn("Could not remove file: {}", e.getMessage());
132         }
133         this.producerCallbacks.stopEiJob(job, eiProducers);
134     }
135
136     public synchronized int size() {
137         return allEiJobs.size();
138     }
139
140     public synchronized void clear() {
141         this.allEiJobs.clear();
142         this.jobsByType.clear();
143         jobsByOwner.clear();
144         clearDatabase();
145     }
146
147     private void clearDatabase() {
148         try {
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());
153         }
154     }
155
156     private void doPut(EiJob job) {
157         allEiJobs.put(job.getId(), job);
158         jobsByType.put(job.getTypeId(), job.getId(), job);
159         jobsByOwner.put(job.getOwner(), job.getId(), job);
160     }
161
162     private void storeJobInFile(EiJob job) {
163         try {
164             try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
165                 out.print(gson.toJson(job));
166             }
167         } catch (Exception e) {
168             logger.warn("Could not save job: {} {}", job.getId(), e.getMessage());
169         }
170     }
171
172     private File getFile(EiJob job) {
173         return getPath(job).toFile();
174     }
175
176     private Path getPath(EiJob job) {
177         return Path.of(getDatabaseDirectory(), job.getId());
178     }
179
180     private String getDatabaseDirectory() {
181         return config.getVardataDirectory() + "/eijobs";
182     }
183
184 }