2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 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.clients.A1ClientFactory;
27 import org.oransc.policyagent.repository.Lock.LockType;
28 import org.oransc.policyagent.repository.Policies;
29 import org.oransc.policyagent.repository.PolicyTypes;
30 import org.oransc.policyagent.repository.Ric;
31 import org.oransc.policyagent.repository.Ric.RicState;
32 import org.oransc.policyagent.repository.Rics;
33 import org.oransc.policyagent.repository.Services;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.scheduling.annotation.EnableScheduling;
38 import org.springframework.scheduling.annotation.Scheduled;
39 import org.springframework.stereotype.Component;
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
45 * Regularly checks the existing rics towards the local repository to keep it
50 public class RepositorySupervision {
51 private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
53 private final Rics rics;
54 private final Policies policies;
55 private final PolicyTypes policyTypes;
56 private final A1ClientFactory a1ClientFactory;
57 private final Services services;
60 public RepositorySupervision(Rics rics, Policies policies, A1ClientFactory a1ClientFactory, PolicyTypes policyTypes,
63 this.policies = policies;
64 this.a1ClientFactory = a1ClientFactory;
65 this.policyTypes = policyTypes;
66 this.services = services;
70 * Regularly contacts all Rics to check if they are alive and synchronized.
72 @Scheduled(fixedRate = 1000 * 60)
73 public void checkAllRics() {
74 logger.debug("Checking Rics starting");
75 createTask().subscribe(this::onRicChecked, null, this::onComplete);
78 private Flux<RicData> createTask() {
79 synchronized (this.rics) {
80 return Flux.fromIterable(rics.getRics()) //
81 .flatMap(this::createRicData) //
82 .flatMap(this::checkRicState) //
83 .doOnNext(ricData -> ricData.ric.getLock().lockBlocking(LockType.EXCLUSIVE)) //
84 .flatMap(this::checkRicPolicies) //
85 .doOnNext(ricData -> ricData.ric.getLock().unlock()) //
86 .flatMap(this::checkRicPolicyTypes); //
90 private static class RicData {
91 RicData(Ric ric, A1Client a1Client) {
93 this.a1Client = a1Client;
97 final A1Client a1Client;
100 private Mono<RicData> createRicData(Ric ric) {
101 return Mono.just(ric) //
102 .flatMap(aRic -> this.a1ClientFactory.createA1Client(ric)) //
103 .flatMap(a1Client -> Mono.just(new RicData(ric, a1Client)));
106 private Mono<RicData> checkRicState(RicData ric) {
107 if (ric.ric.getState() == RicState.UNDEFINED) {
108 return startSynchronization(ric);
109 } else if (ric.ric.getState() == RicState.SYNCHRONIZING) {
112 return Mono.just(ric);
116 private Mono<RicData> checkRicPolicies(RicData ric) {
117 return ric.a1Client.getPolicyIdentities() //
118 .onErrorResume(t -> {
119 ric.ric.getLock().unlock();
122 .flatMap(ricP -> validateInstances(ricP, ric));
125 private Mono<RicData> validateInstances(Collection<String> ricPolicies, RicData ric) {
126 synchronized (this.policies) {
127 if (ricPolicies.size() != policies.getForRic(ric.ric.name()).size()) {
128 ric.ric.getLock().unlock();
129 return startSynchronization(ric);
132 for (String policyId : ricPolicies) {
133 if (!policies.containsPolicy(policyId)) {
134 ric.ric.getLock().unlock();
135 return startSynchronization(ric);
138 return Mono.just(ric);
142 private Mono<RicData> checkRicPolicyTypes(RicData ric) {
143 return ric.a1Client.getPolicyTypeIdentities() //
144 .onErrorResume(notUsed -> Mono.empty()) //
145 .flatMap(ricTypes -> validateTypes(ricTypes, ric));
148 private Mono<RicData> validateTypes(Collection<String> ricTypes, RicData ric) {
149 if (ricTypes.size() != ric.ric.getSupportedPolicyTypes().size()) {
150 return startSynchronization(ric);
152 for (String typeName : ricTypes) {
153 if (!ric.ric.isSupportingType(typeName)) {
154 return startSynchronization(ric);
157 return Mono.just(ric);
160 private Mono<RicData> startSynchronization(RicData ric) {
161 RicSynchronizationTask recovery = createSynchronizationTask();
162 recovery.run(ric.ric);
166 @SuppressWarnings("squid:S2629")
167 private void onRicChecked(RicData ric) {
168 logger.debug("Ric: {} checked", ric.ric.name());
171 private void onComplete() {
172 logger.debug("Checking Rics completed");
175 RicSynchronizationTask createSynchronizationTask() {
176 return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services);