1532c535a54a2c44afc4b7cd83ffac88897ca6b2
[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.exceptions.ServiceException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.util.FileSystemUtils;
46
47 /**
48  * Dynamic representation of all existing EI jobs.
49  */
50 public class EiJobs {
51     private Map<String, EiJob> allEiJobs = new HashMap<>();
52
53     private MultiMap<EiJob> jobsByType = new MultiMap<>();
54     private MultiMap<EiJob> jobsByOwner = new MultiMap<>();
55     private final Gson gson;
56
57     private final ApplicationConfig config;
58     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     public EiJobs(ApplicationConfig config) {
61         this.config = config;
62         GsonBuilder gsonBuilder = new GsonBuilder();
63         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
64         this.gson = gsonBuilder.create();
65     }
66
67     public synchronized void restoreJobsFromDatabase() throws IOException {
68         File dbDir = new File(getDatabaseDirectory());
69         for (File file : dbDir.listFiles()) {
70             String json = Files.readString(file.toPath());
71             EiJob job = gson.fromJson(json, EiJob.class);
72             this.put(job, false);
73         }
74     }
75
76     public synchronized void put(EiJob job) {
77         this.put(job, true);
78     }
79
80     public synchronized Collection<EiJob> getJobs() {
81         return new Vector<>(allEiJobs.values());
82     }
83
84     public synchronized EiJob getJob(String id) throws ServiceException {
85         EiJob ric = allEiJobs.get(id);
86         if (ric == null) {
87             throw new ServiceException("Could not find EI job: " + id);
88         }
89         return ric;
90     }
91
92     public synchronized Collection<EiJob> getJobsForType(String typeId) {
93         return jobsByType.get(typeId);
94     }
95
96     public synchronized Collection<EiJob> getJobsForType(EiType type) {
97         return jobsByType.get(type.getId());
98     }
99
100     public synchronized Collection<EiJob> getJobsForOwner(String owner) {
101         return jobsByOwner.get(owner);
102     }
103
104     public synchronized EiJob get(String id) {
105         return allEiJobs.get(id);
106     }
107
108     public synchronized EiJob remove(String id) {
109         EiJob job = allEiJobs.get(id);
110         if (job != null) {
111             remove(job);
112         }
113         return job;
114     }
115
116     public synchronized void remove(EiJob job) {
117         this.allEiJobs.remove(job.id());
118         jobsByType.remove(job.typeId(), job.id());
119         jobsByOwner.remove(job.owner(), job.id());
120
121         try {
122             Files.delete(getPath(job));
123         } catch (IOException e) {
124             logger.warn("Could not remove file: {}", e.getMessage());
125         }
126
127     }
128
129     public synchronized int size() {
130         return allEiJobs.size();
131     }
132
133     public synchronized void clear() {
134         this.allEiJobs.clear();
135         this.jobsByType.clear();
136         jobsByOwner.clear();
137         try {
138             FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
139         } catch (IOException e) {
140             logger.warn("Could not delete database : {}", e.getMessage());
141         }
142     }
143
144     private void put(EiJob job, boolean storePersistently) {
145         allEiJobs.put(job.id(), job);
146         jobsByType.put(job.typeId(), job.id(), job);
147         jobsByOwner.put(job.owner(), job.id(), job);
148         if (storePersistently) {
149             storeJobInFile(job);
150         }
151     }
152
153     private void storeJobInFile(EiJob job) {
154         try {
155             Files.createDirectories(Paths.get(getDatabaseDirectory()));
156             try (PrintStream out = new PrintStream(new FileOutputStream(getFile(job)))) {
157                 out.print(gson.toJson(job));
158             }
159         } catch (Exception e) {
160             logger.warn("Could not save job: {} {}", job.id(), e.getMessage());
161         }
162     }
163
164     private File getFile(EiJob job) {
165         return getPath(job).toFile();
166     }
167
168     private Path getPath(EiJob job) {
169         return Path.of(getDatabaseDirectory(), job.id());
170     }
171
172     private String getDatabaseDirectory() {
173         return config.getVardataDirectory() + "/database";
174     }
175
176 }