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 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;
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
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.
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;
52 public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
53 this.services = services;
54 this.policies = policies;
55 this.a1ClientFactory = a1ClientFactory;
58 @Scheduled(fixedRate = 1000 * 60)
59 public void checkAllServices() {
60 logger.debug("Checking services starting");
61 createTask().subscribe(this::onPolicyDeleted, null, this::onComplete);
64 @SuppressWarnings("squid:S2629")
65 private void onPolicyDeleted(Policy policy) {
66 logger.debug("Policy deleted due to inactivity: {}, service: {}", policy.id(), policy.ownerServiceName());
69 private void onComplete() {
70 logger.debug("Checking services completed");
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);
85 private Flux<Policy> getAllPoliciesForService(Service service) {
86 synchronized (policies) {
87 return Flux.fromIterable(policies.getForService(service.getName()));
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));
98 @SuppressWarnings("squid:S2629")
99 private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
100 logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);