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.Ric;
26 import org.oransc.policyagent.repository.Ric.RicState;
27 import org.oransc.policyagent.repository.Rics;
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 * Regularly checks the exisiting rics towards the local repository to keep it consistent.
43 public class RepositorySupervision {
44 private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
46 private final Rics rics;
47 private final Policies policies;
48 private final A1Client a1Client;
51 public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client) {
53 this.policies = policies;
54 this.a1Client = a1Client;
58 * Regularly contacts all Rics to check if they are alive.
60 @Scheduled(fixedRate = 1000 * 60)
61 public void checkAllRics() {
62 logger.debug("Checking Rics starting");
63 createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
67 private Flux<Ric> createTask() {
68 return Flux.fromIterable(rics.getRics()) //
69 .groupBy(ric -> ric.state()) //
70 .flatMap(fluxGroup -> handleGroup(fluxGroup.key(), fluxGroup));
73 private Flux<Ric> handleGroup(Ric.RicState key, Flux<Ric> fluxGroup) {
74 logger.debug("Handling group {}", key);
77 return fluxGroup.flatMap(this::checkActive);
80 return fluxGroup.flatMap(this::checkNotReachable);
83 // If not initiated, leave it to the StartupService.
88 private Mono<Ric> checkActive(Ric ric) {
89 logger.debug("Handling active ric {}", ric);
90 a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
91 .filter(policyId -> !policies.containsPolicy(policyId)) //
92 .doOnNext(policyId -> logger.debug("Deleting policy {}", policyId))
93 .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
95 return Mono.just(ric);
98 private Mono<Ric> checkNotReachable(Ric ric) {
99 logger.debug("Handling not reachable ric {}", ric);
100 a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
101 .filter(policyId -> !policies.containsPolicy(policyId)) //
102 .doOnNext(policyId -> logger.debug("Deleting policy {}", policyId))
103 .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
104 .subscribe(null, null, () -> ric.setState(RicState.ACTIVE));
105 return Mono.just(ric);
108 private void onRicChecked(Ric ric) {
109 logger.info("Ric: " + ric.name() + " checked");
112 private void onError(Throwable t) {
113 logger.error("Rics supervision failed", t);
116 private void onComplete() {
117 logger.debug("Checking Rics completed");