aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java
diff options
context:
space:
mode:
Diffstat (limited to 'a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java')
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java82
1 files changed, 81 insertions, 1 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java
index 63633f67..2d2f6364 100644
--- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java
@@ -20,19 +20,37 @@
package org.onap.ccsdk.oran.a1policymanagementservice.repository;
+import com.google.gson.Gson;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
+import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.FileSystemUtils;
public class Services {
private static final Logger logger = LoggerFactory.getLogger(Services.class);
+ private static Gson gson = Service.createGson();
+ private final ApplicationConfig appConfig;
private Map<String, Service> registeredServices = new HashMap<>();
+ public Services(@Autowired ApplicationConfig appConfig) {
+ this.appConfig = appConfig;
+ restoreFromDatabase();
+ }
+
public synchronized Service getService(String name) throws ServiceException {
Service s = registeredServices.get(name);
if (s == null) {
@@ -49,6 +67,7 @@ public class Services {
logger.debug("Put service: {}", service.getName());
service.keepAlive();
registeredServices.put(service.getName(), service);
+ store(service);
}
public synchronized Iterable<Service> getAll() {
@@ -56,7 +75,14 @@ public class Services {
}
public synchronized void remove(String name) {
- registeredServices.remove(name);
+ Service service = registeredServices.remove(name);
+ if (service != null) {
+ try {
+ Files.delete(getPath(service));
+ } catch (Exception e) {
+
+ }
+ }
}
public synchronized int size() {
@@ -65,5 +91,59 @@ public class Services {
public synchronized void clear() {
registeredServices.clear();
+ try {
+ FileSystemUtils.deleteRecursively(getDatabasePath());
+ } catch (Exception e) {
+ logger.warn("Could not delete services database : {}", e.getMessage());
+ }
+ }
+
+ public void store(Service service) {
+ try {
+ Files.createDirectories(getDatabasePath());
+ try (PrintStream out = new PrintStream(new FileOutputStream(getFile(service)))) {
+ String str = gson.toJson(service);
+ out.print(str);
+ }
+ } catch (ServiceException e) {
+ logger.debug("Could not store service: {} {}", service.getName(), e.getMessage());
+ } catch (IOException e) {
+ logger.warn("Could not store pservice: {} {}", service.getName(), e.getMessage());
+ }
+ }
+
+ private File getFile(Service service) throws ServiceException {
+ return getPath(service).toFile();
+ }
+
+ private Path getPath(Service service) throws ServiceException {
+ return Path.of(getDatabaseDirectory(), service.getName() + ".json");
+ }
+
+ void restoreFromDatabase() {
+ try {
+ Files.createDirectories(getDatabasePath());
+ for (File file : getDatabasePath().toFile().listFiles()) {
+ String json = Files.readString(file.toPath());
+ Service service = gson.fromJson(json, Service.class);
+ this.registeredServices.put(service.getName(), service);
+ }
+ logger.debug("Restored type database,no of services: {}", this.registeredServices.size());
+ } catch (ServiceException e) {
+ logger.debug("Could not restore services database : {}", e.getMessage());
+ } catch (Exception e) {
+ logger.warn("Could not restore services database : {}", e.getMessage());
+ }
+ }
+
+ private String getDatabaseDirectory() throws ServiceException {
+ if (appConfig.getVardataDirectory() == null) {
+ throw new ServiceException("No storage provided");
+ }
+ return appConfig.getVardataDirectory() + "/database/services";
+ }
+
+ private Path getDatabasePath() throws ServiceException {
+ return Path.of(getDatabaseDirectory());
}
}