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.enrichment.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.enrichment.configuration.ApplicationConfig;
42 import org.oransc.enrichment.exceptions.ServiceException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.util.FileSystemUtils;
49 * Dynamic representation of all Information Types in the system.
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;
58 public InfoTypes(ApplicationConfig config) {
60 GsonBuilder gsonBuilder = new GsonBuilder();
61 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
62 this.gson = gsonBuilder.create();
65 public synchronized void restoreTypesFromDatabase() throws IOException {
66 Files.createDirectories(Paths.get(getDatabaseDirectory()));
67 File dbDir = new File(getDatabaseDirectory());
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);
76 public synchronized void put(InfoType type) {
77 allEiTypes.put(type.getId(), type);
81 public synchronized Collection<InfoType> getAllInfoTypes() {
82 return new Vector<>(allEiTypes.values());
85 public synchronized InfoType getType(String id) throws ServiceException {
86 InfoType type = allEiTypes.get(id);
88 throw new ServiceException("Information type not found: " + id, HttpStatus.NOT_FOUND);
93 public synchronized InfoType get(String id) {
94 return allEiTypes.get(id);
97 public synchronized void remove(InfoType type) {
98 allEiTypes.remove(type.getId());
100 Files.delete(getPath(type));
101 } catch (IOException e) {
102 logger.warn("Could not remove file: {} {}", type.getId(), e.getMessage());
106 public synchronized int size() {
107 return allEiTypes.size();
110 public synchronized void clear() {
111 this.allEiTypes.clear();
115 private void clearDatabase() {
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());
124 private void storeInFile(InfoType type) {
126 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(type)))) {
127 out.print(gson.toJson(type));
129 } catch (Exception e) {
130 logger.warn("Could not save type: {} {}", type.getId(), e.getMessage());
134 private File getFile(InfoType type) {
135 return getPath(type).toFile();
138 private Path getPath(InfoType type) {
139 return getPath(type.getId());
142 private Path getPath(String typeId) {
143 return Path.of(getDatabaseDirectory(), typeId);
146 private String getDatabaseDirectory() {
147 return config.getVardataDirectory() + "/database/eitypes";