2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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
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.onap.ccsdk.oran.a1policymanagementservice.repository;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.lang.invoke.MethodHandles;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.util.Collection;
34 import java.util.HashMap;
36 import java.util.Vector;
38 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
39 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException;
40 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.context.annotation.Configuration;
45 import org.springframework.util.FileSystemUtils;
48 public class PolicyTypes {
49 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50 private Map<String, PolicyType> types = new HashMap<>();
51 private final ApplicationConfig appConfig;
52 private static Gson gson = new GsonBuilder().create();
54 public PolicyTypes(@Autowired ApplicationConfig appConfig) {
55 this.appConfig = appConfig;
56 restoreFromDatabase();
59 public synchronized PolicyType getType(String name) throws EntityNotFoundException {
60 PolicyType t = types.get(name);
62 throw new EntityNotFoundException("Could not find type: " + name);
67 public synchronized PolicyType get(String name) {
68 return types.get(name);
71 public synchronized void put(PolicyType type) {
72 types.put(type.getId(), type);
76 public synchronized boolean contains(String policyType) {
77 return types.containsKey(policyType);
80 public synchronized Collection<PolicyType> getAll() {
81 return new Vector<>(types.values());
84 public synchronized int size() {
88 public synchronized void clear() {
91 FileSystemUtils.deleteRecursively(getDatabasePath());
92 } catch (IOException | ServiceException e) {
93 logger.warn("Could not delete policy type database : {}", e.getMessage());
97 public void store(PolicyType type) {
99 Files.createDirectories(getDatabasePath());
100 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(type)))) {
101 out.print(gson.toJson(type));
103 } catch (ServiceException e) {
104 logger.debug("Could not store policy type: {} {}", type.getId(), e.getMessage());
105 } catch (IOException e) {
106 logger.warn("Could not store policy type: {} {}", type.getId(), e.getMessage());
110 private File getFile(PolicyType type) throws ServiceException {
111 return Path.of(getDatabaseDirectory(), type.getId() + ".json").toFile();
114 void restoreFromDatabase() {
116 Files.createDirectories(getDatabasePath());
117 for (File file : getDatabasePath().toFile().listFiles()) {
118 String json = Files.readString(file.toPath());
119 PolicyType type = gson.fromJson(json, PolicyType.class);
120 this.types.put(type.getId(), type);
122 logger.debug("Restored type database,no of types: {}", this.types.size());
123 } catch (IOException e) {
124 logger.warn("Could not restore policy type database : {}", e.getMessage());
125 } catch (ServiceException e) {
126 logger.debug("Could not restore policy type database : {}", e.getMessage());
130 private String getDatabaseDirectory() throws ServiceException {
131 if (appConfig.getVardataDirectory() == null) {
132 throw new ServiceException("No policy type storage provided");
134 return appConfig.getVardataDirectory() + "/database/policyTypes";
137 private Path getDatabasePath() throws ServiceException {
138 return Path.of(getDatabaseDirectory());