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;
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 A1ClientFactory a1ClientFactory;
47 public ServiceSupervision(Services services, Policies policies, A1ClientFactory a1ClientFactory) {
48 this.services = services;
49 this.policies = policies;
50 this.a1ClientFactory = a1ClientFactory;
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 synchronized (services) {
73 return Flux.fromIterable(services.getAll()) //
74 .filter(service -> service.isExpired()) //
75 .doOnNext(service -> logger.info("Service is expired:" + service.name())) //
76 .flatMap(service -> getAllPolicies(service)) //
77 .doOnNext(policy -> this.policies.remove(policy)) //
78 .flatMap(policy -> deletePolicyInRic(policy));
82 private Flux<Policy> getAllPolicies(Service service) {
83 synchronized (policies) {
84 return Flux.fromIterable(policies.getForService(service.name()));
88 private Mono<Policy> deletePolicyInRic(Policy policy) {
89 return a1ClientFactory.createA1Client(policy.ric()) //
90 .flatMap(client -> client.deletePolicy(policy) //
91 .onErrorResume(exception -> handleDeleteFromRicFailure(policy, exception)) //
92 .map((nothing) -> policy));
95 private Mono<String> handleDeleteFromRicFailure(Policy policy, Throwable e) {
96 logger.warn("Could not delete policy: {} from ric: {}", policy.id(), policy.ric().name(), e);