68a8d7ddc181b2f99513a95c9364fc58d56f8466
[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
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Vector;
34
35 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
36 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.util.FileSystemUtils;
41
42 public class Services {
43     private static final Logger logger = LoggerFactory.getLogger(Services.class);
44     private static Gson gson = Service.createGson();
45     private final ApplicationConfig appConfig;
46
47     private Map<String, Service> registeredServices = new HashMap<>();
48
49     public Services(@Autowired ApplicationConfig appConfig) {
50         this.appConfig = appConfig;
51         restoreFromDatabase();
52     }
53
54     public synchronized Service getService(String name) throws ServiceException {
55         Service service = registeredServices.get(name);
56         if (service == null) {
57             throw new ServiceException("Could not find service: " + name);
58         }
59         return service;
60     }
61
62     public synchronized Service get(String name) {
63         return registeredServices.get(name);
64     }
65
66     public synchronized void put(Service service) {
67         logger.debug("Put service: {}", service.getName());
68         service.keepAlive();
69         registeredServices.put(service.getName(), service);
70         store(service);
71     }
72
73     public synchronized Iterable<Service> getAll() {
74         return new Vector<>(registeredServices.values());
75     }
76
77     public synchronized void remove(String name) {
78         Service service = registeredServices.remove(name);
79         if (service != null) {
80             try {
81                 Files.delete(getPath(service));
82             } catch (Exception e) {
83                 // Doesn't matter.
84             }
85         }
86     }
87
88     public synchronized int size() {
89         return registeredServices.size();
90     }
91
92     public synchronized void clear() {
93         registeredServices.clear();
94         try {
95             FileSystemUtils.deleteRecursively(getDatabasePath());
96         } catch (Exception e) {
97             logger.warn("Could not delete services database : {}", e.getMessage());
98         }
99     }
100
101     public void store(Service service) {
102         try {
103             Files.createDirectories(getDatabasePath());
104             try (PrintStream out = new PrintStream(new FileOutputStream(getFile(service)))) {
105                 String str = gson.toJson(service);
106                 out.print(str);
107             }
108         } catch (ServiceException e) {
109             logger.debug("Could not store service: {} {}", service.getName(), e.getMessage());
110         } catch (IOException e) {
111             logger.warn("Could not store pservice: {} {}", service.getName(), e.getMessage());
112         }
113     }
114
115     private File getFile(Service service) throws ServiceException {
116         return getPath(service).toFile();
117     }
118
119     private Path getPath(Service service) throws ServiceException {
120         return Path.of(getDatabaseDirectory(), service.getName() + ".json");
121     }
122
123     void restoreFromDatabase() {
124         try {
125             Files.createDirectories(getDatabasePath());
126             for (File file : getDatabasePath().toFile().listFiles()) {
127                 String json = Files.readString(file.toPath());
128                 Service service = gson.fromJson(json, Service.class);
129                 this.registeredServices.put(service.getName(), service);
130             }
131             logger.debug("Restored type database,no of services: {}", this.registeredServices.size());
132         } catch (ServiceException e) {
133             logger.debug("Could not restore services database : {}", e.getMessage());
134         } catch (Exception e) {
135             logger.warn("Could not restore services database : {}", e.getMessage());
136         }
137     }
138
139     private String getDatabaseDirectory() throws ServiceException {
140         if (appConfig.getVardataDirectory() == null) {
141             throw new ServiceException("No storage provided");
142         }
143         return appConfig.getVardataDirectory() + "/database/services";
144     }
145
146     private Path getDatabasePath() throws ServiceException {
147         return Path.of(getDatabaseDirectory());
148     }
149 }