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 RicSupervision {
51 private static final Logger logger = LoggerFactory.getLogger(RicSupervision.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 RicSupervision(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(ric -> logger.debug("Ric: {} checked", ric.ric.name()), //
77 () -> logger.debug("Checking Rics completed"));
80 private Flux<RicData> createTask() {
81 synchronized (this.rics) {
82 return Flux.fromIterable(rics.getRics()) //
83 .flatMap(this::createRicData) //
84 .flatMap(this::checkOneRic) //
85 .onErrorResume(throwable -> Mono.empty());
89 private Mono<RicData> checkOneRic(RicData ricData) {
90 return checkRicState(ricData) //
91 .flatMap(x -> ricData.ric.getLock().lock(LockType.EXCLUSIVE)) //
92 .flatMap(x -> checkRicPolicies(ricData)) //
93 .flatMap(x -> ricData.ric.getLock().unlock()) //
94 .doOnError(throwable -> ricData.ric.getLock().unlockBlocking()) //
95 .flatMap(x -> checkRicPolicyTypes(ricData)); //
98 private static class RicData {
99 RicData(Ric ric, A1Client a1Client) {
101 this.a1Client = a1Client;
105 final A1Client a1Client;
108 private Mono<RicData> createRicData(Ric ric) {
109 return Mono.just(ric) //
110 .flatMap(aRic -> this.a1ClientFactory.createA1Client(ric)) //
111 .flatMap(a1Client -> Mono.just(new RicData(ric, a1Client)));
114 private Mono<RicData> checkRicState(RicData ric) {
115 if (ric.ric.getState() == RicState.UNDEFINED) {
116 return startSynchronization(ric) //
117 .onErrorResume(t -> Mono.empty());
118 } else if (ric.ric.getState() == RicState.SYNCHRONIZING) {
121 return Mono.just(ric);
125 private Mono<RicData> checkRicPolicies(RicData ric) {
126 return ric.a1Client.getPolicyIdentities() //
127 .flatMap(ricP -> validateInstances(ricP, ric));
130 private Mono<RicData> validateInstances(Collection<String> ricPolicies, RicData ric) {
131 synchronized (this.policies) {
132 if (ricPolicies.size() != policies.getForRic(ric.ric.name()).size()) {
133 return startSynchronization(ric);
136 for (String policyId : ricPolicies) {
137 if (!policies.containsPolicy(policyId)) {
138 return startSynchronization(ric);
141 return Mono.just(ric);
145 private Mono<RicData> checkRicPolicyTypes(RicData ric) {
146 return ric.a1Client.getPolicyTypeIdentities() //
147 .flatMap(ricTypes -> validateTypes(ricTypes, ric));
150 private Mono<RicData> validateTypes(Collection<String> ricTypes, RicData ric) {
151 if (ricTypes.size() != ric.ric.getSupportedPolicyTypes().size()) {
152 return startSynchronization(ric);
154 for (String typeName : ricTypes) {
155 if (!ric.ric.isSupportingType(typeName)) {
156 return startSynchronization(ric);
159 return Mono.just(ric);
162 private Mono<RicData> startSynchronization(RicData ric) {
163 RicSynchronizationTask synchronizationTask = createSynchronizationTask();
164 synchronizationTask.run(ric.ric);
165 return Mono.error(new Exception("Syncronization started"));
168 RicSynchronizationTask createSynchronizationTask() {
169 return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services);