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;
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;
33 import java.util.Vector;
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;
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;
47 private Map<String, Service> registeredServices = new HashMap<>();
49 public Services(@Autowired ApplicationConfig appConfig) {
50 this.appConfig = appConfig;
51 restoreFromDatabase();
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);
62 public synchronized Service get(String name) {
63 return registeredServices.get(name);
66 public synchronized void put(Service service) {
67 logger.debug("Put service: {}", service.getName());
69 registeredServices.put(service.getName(), service);
73 public synchronized Iterable<Service> getAll() {
74 return new Vector<>(registeredServices.values());
77 public synchronized void remove(String name) {
78 Service service = registeredServices.remove(name);
79 if (service != null) {
81 Files.delete(getPath(service));
82 } catch (Exception e) {
88 public synchronized int size() {
89 return registeredServices.size();
92 public synchronized void clear() {
93 registeredServices.clear();
95 FileSystemUtils.deleteRecursively(getDatabasePath());
96 } catch (Exception e) {
97 logger.warn("Could not delete services database : {}", e.getMessage());
101 public void store(Service service) {
103 Files.createDirectories(getDatabasePath());
104 try (PrintStream out = new PrintStream(new FileOutputStream(getFile(service)))) {
105 String str = gson.toJson(service);
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());
115 private File getFile(Service service) throws ServiceException {
116 return getPath(service).toFile();
119 private Path getPath(Service service) throws ServiceException {
120 return Path.of(getDatabaseDirectory(), service.getName() + ".json");
123 void restoreFromDatabase() {
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);
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());
139 private String getDatabaseDirectory() throws ServiceException {
140 if (appConfig.getVardataDirectory() == null) {
141 throw new ServiceException("No storage provided");
143 return appConfig.getVardataDirectory() + "/database/services";
146 private Path getDatabasePath() throws ServiceException {
147 return Path.of(getDatabaseDirectory());