diff options
Diffstat (limited to 'a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java')
-rw-r--r-- | a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java index 7bf03782..76f0e216 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java @@ -20,15 +20,41 @@ package org.onap.ccsdk.oran.a1policymanagementservice.repository; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.invoke.MethodHandles; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; 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.EntityNotFoundException; +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.context.annotation.Configuration; +import org.springframework.util.FileSystemUtils; +@Configuration public class PolicyTypes { + private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private Map<String, PolicyType> types = new HashMap<>(); + private final ApplicationConfig appConfig; + private static Gson gson = new GsonBuilder().create(); + + public PolicyTypes(@Autowired ApplicationConfig appConfig) { + this.appConfig = appConfig; + restoreFromDatabase(); + } public synchronized PolicyType getType(String name) throws EntityNotFoundException { PolicyType t = types.get(name); @@ -43,7 +69,8 @@ public class PolicyTypes { } public synchronized void put(PolicyType type) { - types.put(type.id(), type); + types.put(type.getId(), type); + store(type); } public synchronized boolean contains(String policyType) { @@ -60,5 +87,54 @@ public class PolicyTypes { public synchronized void clear() { this.types.clear(); + try { + FileSystemUtils.deleteRecursively(getDatabasePath()); + } catch (IOException | ServiceException e) { + logger.warn("Could not delete policy type database : {}", e.getMessage()); + } + } + + public void store(PolicyType type) { + try { + Files.createDirectories(getDatabasePath()); + try (PrintStream out = new PrintStream(new FileOutputStream(getFile(type)))) { + out.print(gson.toJson(type)); + } + } catch (ServiceException e) { + logger.debug("Could not store policy type: {} {}", type.getId(), e.getMessage()); + } catch (IOException e) { + logger.warn("Could not store policy type: {} {}", type.getId(), e.getMessage()); + } + } + + private File getFile(PolicyType type) throws ServiceException { + return Path.of(getDatabaseDirectory(), type.getId() + ".json").toFile(); + } + + void restoreFromDatabase() { + try { + Files.createDirectories(getDatabasePath()); + for (File file : getDatabasePath().toFile().listFiles()) { + String json = Files.readString(file.toPath()); + PolicyType type = gson.fromJson(json, PolicyType.class); + this.types.put(type.getId(), type); + } + + } catch (IOException e) { + logger.warn("Could not restore policy type database : {}", e.getMessage()); + } catch (ServiceException e) { + logger.debug("Could not restore policy type database : {}", e.getMessage()); + } + } + + private String getDatabaseDirectory() throws ServiceException { + if (appConfig.getVardataDirectory() == null) { + throw new ServiceException("No policy type storage provided"); + } + return appConfig.getVardataDirectory() + "/database/policyTypes"; + } + + private Path getDatabasePath() throws ServiceException { + return Path.of(getDatabaseDirectory()); } } |