Merge "Remove A1 Policy Management Service"
[nonrtric.git] / information-coordinator-service / src / main / java / org / oransc / ics / repository / InfoTypes.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.ics.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.ics.configuration.ApplicationConfig;
42 import org.oransc.ics.exceptions.ServiceException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.util.FileSystemUtils;
47
48 /**
49  * Dynamic representation of all Information Types in the system.
50  */
51 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
52 public class InfoTypes {
53     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54     private final Map<String, InfoType> allEiTypes = new HashMap<>();
55     private final ApplicationConfig config;
56     private final Gson gson;
57
58     public InfoTypes(ApplicationConfig config) {
59         this.config = config;
60         GsonBuilder gsonBuilder = new GsonBuilder();
61         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
62         this.gson = gsonBuilder.create();
63     }
64
65     public synchronized void restoreTypesFromDatabase() throws IOException {
66         Files.createDirectories(Paths.get(getDatabaseDirectory()));
67         File dbDir = new File(getDatabaseDirectory());
68
69         for (File file : dbDir.listFiles()) {
70             String json = Files.readString(file.toPath());
71             InfoType type = gson.fromJson(json, InfoType.class);
72             allEiTypes.put(type.getId(), type);
73         }
74     }
75
76     public synchronized void put(InfoType type) {
77         allEiTypes.put(type.getId(), type);
78         storeInFile(type);
79     }
80
81     public synchronized Collection<InfoType> getAllInfoTypes() {
82         return new Vector<>(allEiTypes.values());
83     }
84
85     public synchronized InfoType getType(String id) throws ServiceException {
86         InfoType type = allEiTypes.get(id);
87         if (type == null) {
88             throw new ServiceException("Information type not found: " + id, HttpStatus.NOT_FOUND);
89         }
90         return type;
91     }
92
93     public synchronized InfoType get(String id) {
94         return allEiTypes.get(id);
95     }
96
97     public synchronized void remove(InfoType type) {
98         allEiTypes.remove(type.getId());
99         try {
100             Files.delete(getPath(type));
101         } catch (IOException e) {
102             logger.warn("Could not remove file: {} {}", type.getId(), e.getMessage());
103         }
104     }
105
106     public synchronized int size() {
107         return allEiTypes.size();
108     }
109
110     public synchronized void clear() {
111         this.allEiTypes.clear();
112         clearDatabase();
113     }
114
115     private void clearDatabase() {
116         try {
117             FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
118             Files.createDirectories(Paths.get(getDatabaseDirectory()));
119         } catch (IOException e) {
120             logger.warn("Could not delete database : {}", e.getMessage());
121         }
122     }
123
124     private void storeInFile(InfoType type) {
125         try {
126             try (PrintStream out = new PrintStream(new FileOutputStream(getFile(type)))) {
127                 out.print(gson.toJson(type));
128             }
129         } catch (Exception e) {
130             logger.warn("Could not save type: {} {}", type.getId(), e.getMessage());
131         }
132     }
133
134     private File getFile(InfoType type) {
135         return getPath(type).toFile();
136     }
137
138     private Path getPath(InfoType type) {
139         return getPath(type.getId());
140     }
141
142     private Path getPath(String typeId) {
143         return Path.of(getDatabaseDirectory(), typeId);
144     }
145
146     private String getDatabaseDirectory() {
147         return config.getVardataDirectory() + "/database/eitypes";
148     }
149 }