4c063e2d5c463c63f604a7b7561ac34a7ddb82d5
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 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.tasks;
22
23 import java.time.Duration;
24
25 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
26 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
27 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
28 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.scheduling.annotation.EnableScheduling;
36 import org.springframework.stereotype.Component;
37
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40
41 /**
42  * Periodically checks that services with a keepAliveInterval set are alive. If
43  * a service is deemed not alive, all the service's policies are deleted, both
44  * in the repository and in the affected Rics, and the service is removed from
45  * the repository. This means that the service needs to register again after
46  * this.
47  */
48 @Component
49 @EnableScheduling
50 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
51 public class ServiceSupervision {
52     private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
53     static final int CONCURRENCY_RIC = 1; // How may paralell requests that is sent
54     private final Services services;
55     private final Policies policies;
56     private A1ClientFactory a1ClientFactory;
57     private final Duration checkInterval;
58
59     @Autowired
60     public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
61         this(services, policies, a1ClientFactory, Duration.ofMinutes(1));
62     }
63
64     public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory,
65             Duration checkInterval) {
66         this.services = services;
67         this.policies = policies;
68         this.a1ClientFactory = a1ClientFactory;
69         this.checkInterval = checkInterval;
70         start();
71     }
72
73     private void start() {
74         logger.debug("Checking services starting");
75         createTask().subscribe(null, null, () -> logger.error("Checking services unexpectedly terminated"));
76     }
77
78     private Flux<?> createTask() {
79         return Flux.interval(this.checkInterval) //
80                 .flatMap(notUsed -> checkAllServices());
81     }
82
83     Flux<Policy> checkAllServices() {
84         return Flux.fromIterable(services.getAll()) //
85                 .filter(Service::isExpired) //
86                 .doOnNext(service -> logger.info("Service is expired: {}", service.getName())) //
87                 .doOnNext(service -> services.remove(service.getName())) //
88                 .flatMap(this::getAllPoliciesForService) //
89                 .flatMap(this::deletePolicy, CONCURRENCY_RIC);
90     }
91
92     @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
93     private Flux<Policy> deletePolicy(Policy policy) {
94         Lock lock = policy.ric().getLock();
95         return lock.lock(LockType.SHARED) //
96                 .doOnNext(notUsed -> policies.remove(policy)) //
97                 .flatMap(notUsed -> deletePolicyInRic(policy))
98                 .doOnNext(notUsed -> logger.debug("Policy deleted due to service inactivity: {}, service: {}",
99                         policy.id(), policy.ownerServiceId())) //
100                 .doOnNext(notUsed -> lock.unlockBlocking()) //
101                 .doOnError(throwable -> lock.unlockBlocking()) //
102                 .doOnError(throwable -> logger.debug("Failed to delete inactive policy: {}, reason: {}", policy.id(),
103                         throwable.getMessage())) //
104                 .flatMapMany(notUsed -> Flux.just(policy)) //
105                 .onErrorResume(throwable -> Flux.empty());
106     }
107
108     private Flux<Policy> getAllPoliciesForService(Service service) {
109         return Flux.fromIterable(policies.getForService(service.getName()));
110     }
111
112     private Mono<Policy> deletePolicyInRic(Policy policy) {
113         return a1ClientFactory.createA1Client(policy.ric()) //
114                 .flatMap(client -> client.deletePolicy(policy) //
115                         .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
116                         .map(nothing -> policy));
117     }
118
119     private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
120         logger.warn("Could not delete policy: {} from ric: {}. Cause: {}", policy.id(), policy.ric().id(),
121                 e.getMessage());
122         return Mono.empty();
123     }
124 }