d8a902e9614dde76ad77c9be5536c115d8cf3742
[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.lang.invoke.MethodHandles;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Vector;
29
30 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
31 import org.onap.ccsdk.oran.a1policymanagementservice.datastore.DataStore;
32 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
39
40 public class Services {
41     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
42     private static Gson gson = Service.createGson();
43     private final DataStore dataStore;
44
45     private Map<String, Service> registeredServices = new HashMap<>();
46
47     public Services(@Autowired ApplicationConfig appConfig) {
48         this.dataStore = DataStore.create(appConfig, "services");
49     }
50
51     public synchronized Service getService(String name) throws ServiceException {
52         Service service = registeredServices.get(name);
53         if (service == null) {
54             throw new ServiceException("Could not find service: " + name);
55         }
56         return service;
57     }
58
59     public synchronized Service get(String name) {
60         return registeredServices.get(name);
61     }
62
63     public synchronized void put(Service service) {
64         logger.debug("Put service: {}", service.getName());
65         service.keepAlive();
66         registeredServices.put(service.getName(), service);
67         store(service);
68     }
69
70     public synchronized Iterable<Service> getAll() {
71         return new Vector<>(registeredServices.values());
72     }
73
74     public synchronized void remove(String name) {
75         Service service = registeredServices.remove(name);
76         if (service != null) {
77             dataStore.deleteObject(getPath(service)).subscribe();
78         }
79     }
80
81     public synchronized int size() {
82         return registeredServices.size();
83     }
84
85     public synchronized void clear() {
86         registeredServices.clear();
87         dataStore.deleteAllObjects().onErrorResume(t -> Mono.empty()).subscribe();
88     }
89
90     public void store(Service service) {
91         byte[] bytes = gson.toJson(service).getBytes();
92         dataStore.writeObject(getPath(service), bytes) //
93                 .doOnError(t -> logger.warn("Could not service: {} {}", service.getName(), t.getMessage())).subscribe();
94     }
95
96     public Flux<Service> restoreFromDatabase() {
97         return dataStore.createDataStore().flatMapMany(ds -> dataStore.listObjects("")) //
98                 .flatMap(dataStore::readObject, 1) //
99                 .map(String::new) //
100                 .map(json -> gson.fromJson(json, Service.class))
101                 .doOnNext(service -> this.registeredServices.put(service.getName(), service))
102                 .doOnError(t -> logger.warn("Could not restore services database : {}", t.getMessage()))
103                 .doFinally(sig -> logger.debug("Restored type database,no of services: {}",
104                         this.registeredServices.size())) //
105                 .onErrorResume(t -> Flux.empty()); //
106     }
107
108     private String getPath(Service service) {
109         return service.getName() + ".json";
110     }
111
112 }