9c2846a905a1a896e69c43d5e21e85d212037874
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
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.util.HashMap;
26 import java.util.Map;
27 import java.util.Vector;
28
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;
35
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38
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;
43
44     private Map<String, Service> registeredServices = new HashMap<>();
45
46     public Services(@Autowired ApplicationConfig appConfig) {
47         this.dataStore = DataStore.create(appConfig, "services");
48     }
49
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);
54         }
55         return service;
56     }
57
58     public synchronized Service get(String name) {
59         return registeredServices.get(name);
60     }
61
62     public synchronized void put(Service service) {
63         logger.debug("Put service: {}", service.getName());
64         service.keepAlive();
65         registeredServices.put(service.getName(), service);
66         store(service);
67     }
68
69     public synchronized Iterable<Service> getAll() {
70         return new Vector<>(registeredServices.values());
71     }
72
73     public synchronized void remove(String name) {
74         Service service = registeredServices.remove(name);
75         if (service != null) {
76             dataStore.deleteObject(getPath(service)).subscribe();
77         }
78     }
79
80     public synchronized int size() {
81         return registeredServices.size();
82     }
83
84     public synchronized void clear() {
85         registeredServices.clear();
86         dataStore.deleteAllObjects().onErrorResume(t -> Mono.empty()).subscribe();
87     }
88
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();
93     }
94
95     public Flux<Service> restoreFromDatabase() {
96         return dataStore.createDataStore().flatMapMany(ds -> dataStore.listObjects("")) //
97                 .flatMap(dataStore::readObject, 1) //
98                 .map(String::new) //
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()); //
105     }
106
107     private String getPath(Service service) {
108         return service.getName() + ".json";
109     }
110
111 }