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 public class ServiceSupervision {
51 private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
52 private final Services services;
53 private final Policies policies;
54 private A1ClientFactory a1ClientFactory;
55 private final Duration checkInterval;
58 public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
59 this(services, policies, a1ClientFactory, Duration.ofMinutes(1));
62 public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory,
63 Duration checkInterval) {
64 this.services = services;
65 this.policies = policies;
66 this.a1ClientFactory = a1ClientFactory;
67 this.checkInterval = checkInterval;
71 private void start() {
72 logger.debug("Checking services starting");
73 createTask().subscribe(null, null, () -> logger.error("Checking services unexpectedly terminated"));
76 private Flux<?> createTask() {
77 return Flux.interval(this.checkInterval) //
78 .flatMap(notUsed -> checkAllServices());
81 Flux<Policy> checkAllServices() {
82 return Flux.fromIterable(services.getAll()) //
83 .filter(Service::isExpired) //
84 .doOnNext(service -> logger.info("Service is expired: {}", service.getName())) //
85 .doOnNext(service -> services.remove(service.getName())) //
86 .flatMap(this::getAllPoliciesForService) //
87 .flatMap(this::deletePolicy);
90 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
91 private Flux<Policy> deletePolicy(Policy policy) {
92 Lock lock = policy.ric().getLock();
93 return lock.lock(LockType.SHARED) //
94 .doOnNext(notUsed -> policies.remove(policy)) //
95 .flatMap(notUsed -> deletePolicyInRic(policy))
96 .doOnNext(notUsed -> logger.debug("Policy deleted due to service inactivity: {}, service: {}", policy.id(),
97 policy.ownerServiceName())) //
98 .doOnNext(notUsed -> lock.unlockBlocking()) //
99 .doOnError(throwable -> lock.unlockBlocking()) //
100 .doOnError(throwable -> logger.debug("Failed to delete inactive policy: {}, reason: {}", policy.id(),
101 throwable.getMessage())) //
102 .flatMapMany(notUsed -> Flux.just(policy)) //
103 .onErrorResume(throwable -> Flux.empty());
106 private Flux<Policy> getAllPoliciesForService(Service service) {
107 synchronized (policies) {
108 return Flux.fromIterable(policies.getForService(service.getName()));
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));
119 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
120 private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
121 logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);