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.util.FileSystemUtils;
48 * Dynamic representation of all EI types in the system.
50 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
51 public class EiTypes {
52 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53 private final Map<String, EiType> allEiTypes = new HashMap<>();
54 private final ApplicationConfig config;
55 private final Gson gson;
57 public EiTypes(ApplicationConfig config) {
59 GsonBuilder gsonBuilder = new GsonBuilder();
60 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
61 this.gson = gsonBuilder.create();
64 public synchronized void restoreTypesFromDatabase() throws IOException {
65 Files.createDirectories(Paths.get(getDatabaseDirectory()));
66 File dbDir = new File(getDatabaseDirectory());
68 for (File file : dbDir.listFiles()) {
69 String json = Files.readString(file.toPath());
70 EiType type = gson.fromJson(json, EiType.class);
71 allEiTypes.put(type.getId(), type);
75 public synchronized void put(EiType type) {
76 allEiTypes.put(type.getId(), type);
80 public synchronized Collection<EiType> getAllInfoTypes() {
81 return new Vector<>(allEiTypes.values());
84 public synchronized EiType getType(String id) throws ServiceException {
85 EiType type = allEiTypes.get(id);
87 throw new ServiceException("Information type not found: " + id);
92 public synchronized EiType get(String id) {
93 return allEiTypes.get(id);
96 public synchronized void remove(EiType type) {
97 allEiTypes.remove(type.getId());
99 Files.delete(getPath(type));
100 } catch (IOException e) {
101 logger.warn("Could not remove file: {} {}", type.getId(), e.getMessage());
105 public synchronized int size() {
106 return allEiTypes.size();
109 public synchronized void clear() {
110 this.allEiTypes.clear();
114 private void clearDatabase() {
116 FileSystemUtils.deleteRecursively(Path.of(getDatabaseDirectory()));
117 Files.createDirectories(Paths.get(getDatabaseDirectory()));
118 } catch (IOException e) {
119 logger.warn("Could not delete database : {}", e.getMessage());
123 private void storeInFile(EiType type) {
125 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(type)))) {
126 out.print(gson.toJson(type));
128 } catch (Exception e) {
129 logger.warn("Could not save job: {} {}", type.getId(), e.getMessage());
133 private File getFile(EiType type) {
134 return getPath(type).toFile();
137 private Path getPath(EiType type) {
138 return getPath(type.getId());
141 private Path getPath(String typeId) {
142 return Path.of(getDatabaseDirectory(), typeId);
145 private String getDatabaseDirectory() {
146 return config.getVardataDirectory() + "/database/eitypes";