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 java.util.Collection;
25 import org.oransc.policyagent.clients.A1Client;
26 import org.oransc.policyagent.repository.Policies;
27 import org.oransc.policyagent.repository.PolicyTypes;
28 import org.oransc.policyagent.repository.Ric;
29 import org.oransc.policyagent.repository.Rics;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.scheduling.annotation.EnableScheduling;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Component;
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
41 * Regularly checks the exisiting rics towards the local repository to keep it consistent.
45 public class RepositorySupervision {
46 private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
48 private final Rics rics;
49 private final Policies policies;
50 private final PolicyTypes policyTypes;
51 private final A1Client a1Client;
54 public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client, PolicyTypes policyTypes) {
56 this.policies = policies;
57 this.a1Client = a1Client;
58 this.policyTypes = policyTypes;
62 * Regularly contacts all Rics to check if they are alive.
64 @Scheduled(fixedRate = 1000 * 60)
65 public void checkAllRics() {
66 logger.debug("Checking Rics starting");
67 createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
71 private Flux<Ric> createTask() {
72 return Flux.fromIterable(rics.getRics()) //
73 .flatMap(ric -> checkInstances(ric)) //
74 .flatMap(ric -> checkTypes(ric));
77 private Mono<Ric> checkInstances(Ric ric) {
79 return a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
80 .onErrorResume(t -> Mono.empty()) //
81 .flatMap(ricP -> validateInstances(ricP, ric));
84 private Flux<Ric> junk() {
88 private Mono<Ric> validateInstances(Collection<String> ricPolicies, Ric ric) {
89 if (ricPolicies.size() != policies.getForRic(ric.name()).size()) {
90 return startRecovery(ric);
92 for (String policyId : ricPolicies) {
93 if (!policies.containsPolicy(policyId)) {
94 return startRecovery(ric);
97 return Mono.just(ric);
100 private Mono<Ric> checkTypes(Ric ric) {
101 return a1Client.getPolicyTypeIdentities(ric.getConfig().baseUrl()) //
102 .onErrorResume(t -> {
105 .flatMap(ricTypes -> validateTypes(ricTypes, ric));
108 private Mono<Ric> validateTypes(Collection<String> ricTypes, Ric ric) {
109 if (ricTypes.size() != ric.getSupportedPolicyTypes().size()) {
110 return startRecovery(ric);
112 for (String typeName : ricTypes) {
113 if (!ric.isSupportingType(typeName)) {
114 return startRecovery(ric);
117 return Mono.just(ric);
120 private Mono<Ric> startRecovery(Ric ric) {
121 RicRecoveryTask recovery = new RicRecoveryTask(a1Client, policyTypes, policies);
126 private void onRicChecked(Ric ric) {
127 logger.info("Ric: " + ric.name() + " checked");
130 private void onError(Throwable t) {
131 logger.error("Rics supervision failed", t);
134 private void onComplete() {
135 logger.debug("Checking Rics completed");