Fixed concurrency problems
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / tasks / RepositorySupervision.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.tasks;
22
23 import java.util.Collection;
24
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;
40
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
43
44 /**
45  * Regularly checks the existing rics towards the local repository to keep it
46  * consistent.
47  */
48 @Component
49 @EnableScheduling
50 public class RepositorySupervision {
51     private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
52
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;
58
59     @Autowired
60     public RepositorySupervision(Rics rics, Policies policies, A1ClientFactory a1ClientFactory, PolicyTypes policyTypes,
61         Services services) {
62         this.rics = rics;
63         this.policies = policies;
64         this.a1ClientFactory = a1ClientFactory;
65         this.policyTypes = policyTypes;
66         this.services = services;
67     }
68
69     /**
70      * Regularly contacts all Rics to check if they are alive and synchronized.
71      */
72     @Scheduled(fixedRate = 1000 * 60)
73     public void checkAllRics() {
74         logger.debug("Checking Rics starting");
75         createTask().subscribe(this::onRicChecked, null, this::onComplete);
76     }
77
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); //
87         }
88     }
89
90     private static class RicData {
91         RicData(Ric ric, A1Client a1Client) {
92             this.ric = ric;
93             this.a1Client = a1Client;
94         }
95
96         final Ric ric;
97         final A1Client a1Client;
98     }
99
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)));
104     }
105
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) {
110             return Mono.empty();
111         } else {
112             return Mono.just(ric);
113         }
114     }
115
116     private Mono<RicData> checkRicPolicies(RicData ric) {
117         return ric.a1Client.getPolicyIdentities() //
118             .onErrorResume(t -> {
119                 ric.ric.getLock().unlock();
120                 return Mono.empty();
121             }) //
122             .flatMap(ricP -> validateInstances(ricP, ric));
123     }
124
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);
130             }
131
132             for (String policyId : ricPolicies) {
133                 if (!policies.containsPolicy(policyId)) {
134                     ric.ric.getLock().unlock();
135                     return startSynchronization(ric);
136                 }
137             }
138             return Mono.just(ric);
139         }
140     }
141
142     private Mono<RicData> checkRicPolicyTypes(RicData ric) {
143         return ric.a1Client.getPolicyTypeIdentities() //
144             .onErrorResume(notUsed -> Mono.empty()) //
145             .flatMap(ricTypes -> validateTypes(ricTypes, ric));
146     }
147
148     private Mono<RicData> validateTypes(Collection<String> ricTypes, RicData ric) {
149         if (ricTypes.size() != ric.ric.getSupportedPolicyTypes().size()) {
150             return startSynchronization(ric);
151         }
152         for (String typeName : ricTypes) {
153             if (!ric.ric.isSupportingType(typeName)) {
154                 return startSynchronization(ric);
155             }
156         }
157         return Mono.just(ric);
158     }
159
160     private Mono<RicData> startSynchronization(RicData ric) {
161         RicSynchronizationTask recovery = createSynchronizationTask();
162         recovery.run(ric.ric);
163         return Mono.empty();
164     }
165
166     @SuppressWarnings("squid:S2629")
167     private void onRicChecked(RicData ric) {
168         logger.debug("Ric: {} checked", ric.ric.name());
169     }
170
171     private void onComplete() {
172         logger.debug("Checking Rics completed");
173     }
174
175     RicSynchronizationTask createSynchronizationTask() {
176         return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services);
177     }
178 }