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.Rics;
27 import org.oransc.policyagent.repository.Ric.RicState;
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;
34 import reactor.core.publisher.Flux;
35 import reactor.core.publisher.Mono;
38 * Regularly checks the exisiting rics towards the local repository to keep it consistent.
42 public class RepositorySupervision {
43 private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
45 private final Rics rics;
46 private final Policies policies;
47 private final A1Client a1Client;
50 public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client) {
52 this.policies = policies;
53 this.a1Client = a1Client;
57 * Regularly contacts all Rics to check if they are alive.
59 @Scheduled(fixedRate = 1000 * 60)
60 public void checkAllRics() {
61 logger.debug("Checking Rics starting");
62 createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
66 private Flux<Ric> createTask() {
67 return Flux.fromIterable(rics.getRics()) //
68 .groupBy(ric -> ric.state()) //
69 .flatMap(fluxGroup -> handleGroup(fluxGroup.key(), fluxGroup));
72 private Flux<Ric> handleGroup(Ric.RicState key, Flux<Ric> fluxGroup) {
73 logger.debug("Handling group {}", key);
76 return fluxGroup.flatMap(this::checkActive);
79 return fluxGroup.flatMap(this::checkNotReachable);
82 // If not initiated, leave it to the StartupService.
87 private Mono<Ric> checkActive(Ric ric) {
88 logger.debug("Handling active ric {}", ric);
89 a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
90 .filter(policyId -> !policies.containsPolicy(policyId)) //
91 .doOnNext(policyId -> logger.debug("Deleting policy {}", policyId))
92 .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
94 return Mono.just(ric);
97 private Mono<Ric> checkNotReachable(Ric ric) {
98 logger.debug("Handling not reachable ric {}", ric);
99 a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
100 .filter(policyId -> !policies.containsPolicy(policyId)) //
101 .doOnNext(policyId -> logger.debug("Deleting policy {}", policyId))
102 .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
103 .subscribe(null, null, () -> ric.setState(RicState.ACTIVE));
104 return Mono.just(ric);
107 private void onRicChecked(Ric ric) {
108 logger.info("Ric: " + ric.name() + " checked");
111 private void onError(Throwable t) {
112 logger.error("Rics supervision failed", t);
115 private void onComplete() {
116 logger.debug("Checking Rics completed");