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.oransc.policyagent.repository.Services;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.scheduling.annotation.EnableScheduling;
35 import org.springframework.scheduling.annotation.Scheduled;
36 import org.springframework.stereotype.Component;
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
42 * Regularly checks the exisiting rics towards the local repository to keep it consistent.
46 public class RepositorySupervision {
47 private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
49 private final Rics rics;
50 private final Policies policies;
51 private final PolicyTypes policyTypes;
52 private final A1Client a1Client;
53 private final Services services;
56 public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client, PolicyTypes policyTypes,
59 this.policies = policies;
60 this.a1Client = a1Client;
61 this.policyTypes = policyTypes;
62 this.services = services;
66 * Regularly contacts all Rics to check if they are alive.
68 @Scheduled(fixedRate = 1000 * 60)
69 public void checkAllRics() {
70 logger.debug("Checking Rics starting");
71 createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
75 private Flux<Ric> createTask() {
76 return Flux.fromIterable(rics.getRics()) //
77 .flatMap(ric -> checkInstances(ric)) //
78 .flatMap(ric -> checkTypes(ric));
81 private Mono<Ric> checkInstances(Ric ric) {
83 return a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
84 .onErrorResume(t -> Mono.empty()) //
85 .flatMap(ricP -> validateInstances(ricP, ric));
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, services);
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");