2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2022 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;
25 import java.util.HashMap;
27 import java.util.Vector;
29 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
30 import org.onap.ccsdk.oran.a1policymanagementservice.datastore.DataStore;
31 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
39 public class Services {
40 private static final Logger logger = LoggerFactory.getLogger(Services.class);
41 private static Gson gson = Service.createGson();
42 private final DataStore dataStore;
44 private Map<String, Service> registeredServices = new HashMap<>();
46 public Services(@Autowired ApplicationConfig appConfig) {
47 this.dataStore = DataStore.create(appConfig, "services");
50 public synchronized Service getService(String name) throws ServiceException {
51 Service service = registeredServices.get(name);
52 if (service == null) {
53 throw new ServiceException("Could not find service: " + name);
58 public synchronized Service get(String name) {
59 return registeredServices.get(name);
62 public synchronized void put(Service service) {
63 logger.debug("Put service: {}", service.getName());
65 registeredServices.put(service.getName(), service);
69 public synchronized Iterable<Service> getAll() {
70 return new Vector<>(registeredServices.values());
73 public synchronized void remove(String name) {
74 Service service = registeredServices.remove(name);
75 if (service != null) {
76 dataStore.deleteObject(getPath(service)).subscribe();
80 public synchronized int size() {
81 return registeredServices.size();
84 public synchronized void clear() {
85 registeredServices.clear();
86 dataStore.deleteAllObjects().onErrorResume(t -> Mono.empty()).subscribe();
89 public void store(Service service) {
90 byte[] bytes = gson.toJson(service).getBytes();
91 dataStore.writeObject(getPath(service), bytes) //
92 .doOnError(t -> logger.warn("Could not service: {} {}", service.getName(), t.getMessage())).subscribe();
95 public Flux<Service> restoreFromDatabase() {
96 return dataStore.createDataStore().flatMapMany(ds -> dataStore.listObjects("")) //
97 .flatMap(dataStore::readObject, 1) //
99 .map(json -> gson.fromJson(json, Service.class))
100 .doOnNext(service -> this.registeredServices.put(service.getName(), service))
101 .doOnError(t -> logger.warn("Could not restore services database : {}", t.getMessage()))
102 .doFinally(sig -> logger.debug("Restored type database,no of services: {}",
103 this.registeredServices.size())) //
104 .onErrorResume(t -> Flux.empty()); //
107 private String getPath(Service service) {
108 return service.getName() + ".json";