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.repository.Policies;
24 import org.oransc.policyagent.repository.Policy;
25 import org.oransc.policyagent.repository.Service;
26 import org.oransc.policyagent.repository.Services;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.scheduling.annotation.EnableScheduling;
31 import org.springframework.scheduling.annotation.Scheduled;
32 import org.springframework.stereotype.Component;
33 import reactor.core.publisher.Flux;
37 public class ServiceSupervision {
38 private static final Logger logger = LoggerFactory.getLogger(ServiceSupervision.class);
39 private final Services services;
40 private final Policies policies;
43 public ServiceSupervision(Services services, Policies policies) {
44 this.services = services;
45 this.policies = policies;
48 @Scheduled(fixedRate = 1000 * 60)
49 public void checkAllServices() {
50 logger.debug("Checking services starting");
51 createTask().subscribe(this::onPolicyDeleted, this::onError, this::onComplete);
54 private void onPolicyDeleted(Policy policy) {
55 logger.info("Policy deleted due to inactivity: " + policy.id() + ", service: " + policy.ownerServiceName());
58 private void onError(Throwable t) {
59 logger.error("Service supervision failed", t);
62 private void onComplete() {
63 logger.debug("Checking services completed");
66 Flux<Policy> createTask() {
67 return Flux.fromIterable(services.getAll()) //
68 .filter(service -> service.isExpired()) //
69 .doOnNext(service -> logger.info("Service is expired:" + service.getName()))
70 .flatMap(service -> getAllPolicies(service)) //
71 .flatMap(policy -> deletePolicy(policy));
74 Flux<Policy> getAllPolicies(Service service) {
75 return Flux.fromIterable(policies.getForService(service.getName()));
78 Flux<Policy> deletePolicy(Policy policy) {
79 this.policies.remove(policy);
80 return Flux.just(policy);