Make StartupService use asynchronous client
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / tasks / ServiceSupervision.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.tasks;
22
23 import org.oransc.policyagent.clients.A1Client;
24 import org.oransc.policyagent.repository.Policies;
25 import org.oransc.policyagent.repository.Policy;
26 import org.oransc.policyagent.repository.Service;
27 import org.oransc.policyagent.repository.Services;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.scheduling.annotation.EnableScheduling;
32 import org.springframework.scheduling.annotation.Scheduled;
33 import org.springframework.stereotype.Component;
34
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
37
38 @Component
39 @EnableScheduling
40 public class ServiceSupervision {
41     private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
42     private final Services services;
43     private final Policies policies;
44     private A1Client a1Client;
45
46     @Autowired
47     public ServiceSupervision(Services services, Policies policies, A1Client a1Client) {
48         this.services = services;
49         this.policies = policies;
50         this.a1Client = a1Client;
51     }
52
53     @Scheduled(fixedRate = 1000 * 60)
54     public void checkAllServices() {
55         logger.debug("Checking services starting");
56         createTask().subscribe(this::onPolicyDeleted, this::onError, this::onComplete);
57     }
58
59     private void onPolicyDeleted(Policy policy) {
60         logger.info("Policy deleted due to inactivity: " + policy.id() + ", service: " + policy.ownerServiceName());
61     }
62
63     private void onError(Throwable t) {
64         logger.error("Service supervision failed", t);
65     }
66
67     private void onComplete() {
68         logger.debug("Checking services completed");
69     }
70
71     private Flux<Policy> createTask() {
72         return Flux.fromIterable(services.getAll()) //
73             .filter(service -> service.isExpired()) //
74             .doOnNext(service -> logger.info("Service is expired:" + service.getName()))
75             .flatMap(service -> getAllPolicies(service)) //
76             .doOnNext(policy -> this.policies.remove(policy)) //
77             .flatMap(policy -> deletePolicyInRic(policy));
78     }
79
80     private Flux<Policy> getAllPolicies(Service service) {
81         return Flux.fromIterable(policies.getForService(service.getName()));
82     }
83
84     private Mono<Policy> deletePolicyInRic(Policy policy) {
85         return a1Client.deletePolicy(policy.ric().getConfig().baseUrl(), policy.id()) //
86             .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
87             .map((nothing) -> policy);
88     }
89
90     private Mono<Void> handleDeleteFromRicFailure(Policy policy, Throwable e) {
91         logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);
92         return Mono.empty();
93     }
94 }