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.A1Client;
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;
40 public class ServiceSupervision {
41 private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
42 private final Services services;
43 private final Policies policies;
44 private A1Client a1Client;
47 public ServiceSupervision(Services services, Policies policies, A1Client a1Client) {
48 this.services = services;
49 this.policies = policies;
50 this.a1Client = a1Client;
53 @Scheduled(fixedRate = 1000 * 60)
54 public void checkAllServices() {
55 logger.debug("Checking services starting");
56 createTask().subscribe(this::onPolicyDeleted, this::onError, this::onComplete);
59 private void onPolicyDeleted(Policy policy) {
60 logger.info("Policy deleted due to inactivity: " + policy.id() + ", service: " + policy.ownerServiceName());
63 private void onError(Throwable t) {
64 logger.error("Service supervision failed", t);
67 private void onComplete() {
68 logger.debug("Checking services completed");
71 private Flux<Policy> createTask() {
72 return Flux.fromIterable(services.getAll()) //
73 .filter(service -> service.isExpired()) //
74 .doOnNext(service -> logger.info("Service is expired:" + service.getName()))
75 .flatMap(service -> getAllPolicies(service)) //
76 .doOnNext(policy -> this.policies.remove(policy)) //
77 .flatMap(policy -> deletePolicyInRic(policy));
80 private Flux<Policy> getAllPolicies(Service service) {
81 return Flux.fromIterable(policies.getForService(service.getName()));
84 private Mono<Policy> deletePolicyInRic(Policy policy) {
85 return a1Client.deletePolicy(policy.ric().getConfig().baseUrl(), policy.id()) //
86 .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
87 .flatMap((nothing) -> Mono.just(policy));
90 private Mono<Void> handleDeleteFromRicFailure(Policy policy, Throwable e) {
91 logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);