8d7062b185f9d166b1331d2668f1d44483e7dadc
[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 /**
39  * Periodically checks that services with a keepAliveInterval set are alive. If a service is deemed not alive,
40  * all the service's policies are deleted, both in the repository and in the affected Rics, and the service is
41  * removed from the repository. This means that the service needs to register again after this.
42  */
43 @Component
44 @EnableScheduling
45 public class ServiceSupervision {
46     private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
47     private final Services services;
48     private final Policies policies;
49     private A1ClientFactory a1ClientFactory;
50
51     @Autowired
52     public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
53         this.services = services;
54         this.policies = policies;
55         this.a1ClientFactory = a1ClientFactory;
56     }
57
58     @Scheduled(fixedRate = 1000 * 60)
59     public void checkAllServices() {
60         logger.debug("Checking services starting");
61         createTask().subscribe(this::onPolicyDeleted, null, this::onComplete);
62     }
63
64     @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
65     private void onPolicyDeleted(Policy policy) {
66         logger.debug("Policy deleted due to inactivity: {}, service: {}", policy.id(), policy.ownerServiceName());
67     }
68
69     private void onComplete() {
70         logger.debug("Checking services completed");
71     }
72
73     private Flux<Policy> createTask() {
74         synchronized (services) {
75             return Flux.fromIterable(services.getAll()) //
76                 .filter(Service::isExpired) //
77                 .doOnNext(service -> logger.info("Service is expired: {}", service.getName())) //
78                 .doOnNext(service -> services.remove(service.getName())) //
79                 .flatMap(this::getAllPoliciesForService) //
80                 .doOnNext(policies::remove) //
81                 .flatMap(this::deletePolicyInRic);
82         }
83     }
84
85     private Flux<Policy> getAllPoliciesForService(Service service) {
86         synchronized (policies) {
87             return Flux.fromIterable(policies.getForService(service.getName()));
88         }
89     }
90
91     private Mono<Policy> deletePolicyInRic(Policy policy) {
92         return a1ClientFactory.createA1Client(policy.ric()) //
93             .flatMap(client -> client.deletePolicy(policy) //
94                 .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
95                 .map(nothing -> policy));
96     }
97
98     @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
99     private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
100         logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);
101         return Mono.empty();
102     }
103 }