ad3270cbe05b908d99cb757d7fed504e1a5d87e9
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.repository;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.io.File;
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;
35 import java.util.Map;
36 import java.util.Vector;
37
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;
46
47 @Configuration
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();
53
54     public PolicyTypes(@Autowired ApplicationConfig appConfig) {
55         this.appConfig = appConfig;
56         restoreFromDatabase();
57     }
58
59     public synchronized PolicyType getType(String name) throws EntityNotFoundException {
60         PolicyType t = types.get(name);
61         if (t == null) {
62             throw new EntityNotFoundException("Could not find type: " + name);
63         }
64         return t;
65     }
66
67     public synchronized PolicyType get(String name) {
68         return types.get(name);
69     }
70
71     public synchronized void put(PolicyType type) {
72         types.put(type.getId(), type);
73         store(type);
74     }
75
76     public synchronized boolean contains(String policyType) {
77         return types.containsKey(policyType);
78     }
79
80     public synchronized Collection<PolicyType> getAll() {
81         return new Vector<>(types.values());
82     }
83
84     public synchronized int size() {
85         return types.size();
86     }
87
88     public synchronized void clear() {
89         this.types.clear();
90         try {
91             FileSystemUtils.deleteRecursively(getDatabasePath());
92         } catch (IOException | ServiceException e) {
93             logger.warn("Could not delete policy type database : {}", e.getMessage());
94         }
95     }
96
97     public void store(PolicyType type) {
98         try {
99             Files.createDirectories(getDatabasePath());
100             try (PrintStream out = new PrintStream(new FileOutputStream(getFile(type)))) {
101                 out.print(gson.toJson(type));
102             }
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());
107         }
108     }
109
110     private File getFile(PolicyType type) throws ServiceException {
111         return Path.of(getDatabaseDirectory(), type.getId() + ".json").toFile();
112     }
113
114     void restoreFromDatabase() {
115         try {
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);
121             }
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());
127         }
128     }
129
130     private String getDatabaseDirectory() throws ServiceException {
131         if (appConfig.getVardataDirectory() == null) {
132             throw new ServiceException("No policy type storage provided");
133         }
134         return appConfig.getVardataDirectory() + "/database/policyTypes";
135     }
136
137     private Path getDatabasePath() throws ServiceException {
138         return Path.of(getDatabaseDirectory());
139     }
140 }