2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.tasks;
23 import java.time.Duration;
25 import org.oransc.policyagent.clients.A1ClientFactory;
26 import org.oransc.policyagent.repository.Lock;
27 import org.oransc.policyagent.repository.Lock.LockType;
28 import org.oransc.policyagent.repository.Policies;
29 import org.oransc.policyagent.repository.Policy;
30 import org.oransc.policyagent.repository.Service;
31 import org.oransc.policyagent.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;
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
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
50 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
51 public class ServiceSupervision {
52 private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
53 private final Services services;
54 private final Policies policies;
55 private A1ClientFactory a1ClientFactory;
56 private final Duration checkInterval;
59 public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
60 this(services, policies, a1ClientFactory, Duration.ofMinutes(1));
63 public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory,
64 Duration checkInterval) {
65 this.services = services;
66 this.policies = policies;
67 this.a1ClientFactory = a1ClientFactory;
68 this.checkInterval = checkInterval;
72 private void start() {
73 logger.debug("Checking services starting");
74 createTask().subscribe(null, null, () -> logger.error("Checking services unexpectedly terminated"));
77 private Flux<?> createTask() {
78 return Flux.interval(this.checkInterval) //
79 .flatMap(notUsed -> checkAllServices());
82 Flux<Policy> checkAllServices() {
83 return Flux.fromIterable(services.getAll()) //
84 .filter(Service::isExpired) //
85 .doOnNext(service -> logger.info("Service is expired: {}", service.getName())) //
86 .doOnNext(service -> services.remove(service.getName())) //
87 .flatMap(this::getAllPoliciesForService) //
88 .flatMap(this::deletePolicy);
91 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
92 private Flux<Policy> deletePolicy(Policy policy) {
93 Lock lock = policy.ric().getLock();
94 return lock.lock(LockType.SHARED) //
95 .doOnNext(notUsed -> policies.remove(policy)) //
96 .flatMap(notUsed -> deletePolicyInRic(policy))
97 .doOnNext(notUsed -> logger.debug("Policy deleted due to service inactivity: {}, service: {}", policy.id(),
98 policy.ownerServiceName())) //
99 .doOnNext(notUsed -> lock.unlockBlocking()) //
100 .doOnError(throwable -> lock.unlockBlocking()) //
101 .doOnError(throwable -> logger.debug("Failed to delete inactive policy: {}, reason: {}", policy.id(),
102 throwable.getMessage())) //
103 .flatMapMany(notUsed -> Flux.just(policy)) //
104 .onErrorResume(throwable -> Flux.empty());
107 private Flux<Policy> getAllPoliciesForService(Service service) {
108 return Flux.fromIterable(policies.getForService(service.getName()));
111 private Mono<Policy> deletePolicyInRic(Policy policy) {
112 return a1ClientFactory.createA1Client(policy.ric()) //
113 .flatMap(client -> client.deletePolicy(policy) //
114 .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
115 .map(nothing -> policy));
118 private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
119 logger.warn("Could not delete policy: {} from ric: {}. Cause: {}", policy.id(), policy.ric().name(),