d4b32e022808e7226b8fbbff1204fcfe919972f3
[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.A1ClientFactory;
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 A1ClientFactory a1ClientFactory;
45
46     @Autowired
47     public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
48         this.services = services;
49         this.policies = policies;
50         this.a1ClientFactory = a1ClientFactory;
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         synchronized (services) {
73             return Flux.fromIterable(services.getAll()) //
74                 .filter(service -> service.isExpired()) //
75                 .doOnNext(service -> logger.info("Service is expired:" + service.getName())) //
76                 .flatMap(service -> getAllPolicies(service)) //
77                 .doOnNext(policy -> this.policies.remove(policy)) //
78                 .flatMap(policy -> deletePolicyInRic(policy));
79         }
80     }
81
82     private Flux<Policy> getAllPolicies(Service service) {
83         synchronized (policies) {
84             return Flux.fromIterable(policies.getForService(service.getName()));
85         }
86     }
87
88     private Mono<Policy> deletePolicyInRic(Policy policy) {
89         return a1ClientFactory.createA1Client(policy.ric()) //
90             .flatMap(client -> client.deletePolicy(policy) //
91                 .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
92                 .map((nothing) -> policy));
93     }
94
95     private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
96         logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);
97         return Mono.empty();
98     }
99 }