Merge "Kafka now works in kube for calls outside its namespace"
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / repository / InfoJobs.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.r1producer.ProducerCallbacks;
43 import org.oransc.enrichment.exceptions.ServiceException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.util.FileSystemUtils;
48
49 /**
50  * Dynamic representation of all existing Information Jobs.
51  */
52 public class InfoJobs {
53     private Map<String, InfoJob> allEiJobs = new HashMap<>();
54
55     private MultiMap<InfoJob> jobsByType = new MultiMap<>();
56     private MultiMap<InfoJob> jobsByOwner = new MultiMap<>();
57     private final Gson gson;
58
59     private final ApplicationConfig config;
60     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62     private final ProducerCallbacks producerCallbacks;
63
64     public InfoJobs(ApplicationConfig config, ProducerCallbacks producerCallbacks) {
65         this.config = config;
66         GsonBuilder gsonBuilder = new GsonBuilder();
67         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
68         this.gson = gsonBuilder.create();
69         this.producerCallbacks = producerCallbacks;
70     }
71
72     public synchronized void restoreJobsFromDatabase() throws IOException {
73         Files.createDirectories(Paths.get(getDatabaseDirectory()));
74         File dbDir = new File(getDatabaseDirectory());
75
76         for (File file : dbDir.listFiles()) {
77             String json = Files.readString(file.toPath());
78             InfoJob job = gson.fromJson(json, InfoJob.class);
79             this.doPut(job);
80         }
81     }
82
83     public synchronized void put(InfoJob job) {
84         this.doPut(job);
85         storeJobInFile(job);
86     }
87
88     public synchronized Collection<InfoJob> getJobs() {
89         return new Vector<>(allEiJobs.values());
90     }
91
92     public synchronized InfoJob getJob(String id) throws ServiceException {
93         InfoJob ric = allEiJobs.get(id);
94         if (ric == null) {
95             throw new ServiceException("Could not find Information job: " + id, HttpStatus.NOT_FOUND);
96         }
97         return ric;
98     }
99
100     public synchronized Collection<InfoJob> getJobsForType(String typeId) {
101         return jobsByType.get(typeId);
102     }
103
104     public synchronized Collection<InfoJob> getJobsForType(InfoType type) {
105         return jobsByType.get(type.getId());
106     }
107
108     public synchronized Collection<InfoJob> getJobsForOwner(String owner) {
109         return jobsByOwner.get(owner);
110     }
111
112     public synchronized InfoJob get(String id) {
113         return allEiJobs.get(id);
114     }
115
116     public synchronized InfoJob remove(String id, InfoProducers infoProducers) {
117         InfoJob job = allEiJobs.get(id);
118         if (job != null) {
119             remove(job, infoProducers);
120         }
121         return job;
122     }
123
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());
128
129         try {
130             Files.delete(getPath(job));
131         } catch (IOException e) {
132             logger.warn("Could not remove file: {}", e.getMessage());
133         }
134         this.producerCallbacks.stopInfoJob(job, infoProducers);
135     }
136
137     public synchronized int size() {
138         return allEiJobs.size();
139     }
140
141     public synchronized void clear() {
142         this.allEiJobs.clear();
143         this.jobsByType.clear();
144         jobsByOwner.clear();
145         clearDatabase();
146     }
147
148     private void clearDatabase() {
149         try {
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());
154         }
155     }
156
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);
161     }
162
163     private void storeJobInFile(InfoJob job) {
164         try {
165             try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
166                 out.print(gson.toJson(job));
167             }
168         } catch (Exception e) {
169             logger.warn("Could not store job: {} {}", job.getId(), e.getMessage());
170         }
171     }
172
173     private File getFile(InfoJob job) {
174         return getPath(job).toFile();
175     }
176
177     private Path getPath(InfoJob job) {
178         return Path.of(getDatabaseDirectory(), job.getId());
179     }
180
181     private String getDatabaseDirectory() {
182         return config.getVardataDirectory() + "/database/eijobs";
183     }
184
185 }