Merge "Recovery handling"
[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) 2019 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.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;
36
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
39
40 /**
41  * Regularly checks the exisiting rics towards the local repository to keep it consistent.
42  */
43 @Component
44 @EnableScheduling
45 public class RepositorySupervision {
46     private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
47
48     private final Rics rics;
49     private final Policies policies;
50     private final PolicyTypes policyTypes;
51     private final A1Client a1Client;
52
53     @Autowired
54     public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client, PolicyTypes policyTypes) {
55         this.rics = rics;
56         this.policies = policies;
57         this.a1Client = a1Client;
58         this.policyTypes = policyTypes;
59     }
60
61     /**
62      * Regularly contacts all Rics to check if they are alive.
63      */
64     @Scheduled(fixedRate = 1000 * 60)
65     public void checkAllRics() {
66         logger.debug("Checking Rics starting");
67         createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
68
69     }
70
71     private Flux<Ric> createTask() {
72         return Flux.fromIterable(rics.getRics()) //
73             .flatMap(ric -> checkInstances(ric)) //
74             .flatMap(ric -> checkTypes(ric));
75     }
76
77     private Mono<Ric> checkInstances(Ric ric) {
78
79         return a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
80             .onErrorResume(t -> Mono.empty()) //
81             .flatMap(ricP -> validateInstances(ricP, ric));
82     }
83
84     private Flux<Ric> junk() {
85         return Flux.empty();
86     }
87
88     private Mono<Ric> validateInstances(Collection<String> ricPolicies, Ric ric) {
89         if (ricPolicies.size() != policies.getForRic(ric.name()).size()) {
90             return startRecovery(ric);
91         }
92         for (String policyId : ricPolicies) {
93             if (!policies.containsPolicy(policyId)) {
94                 return startRecovery(ric);
95             }
96         }
97         return Mono.just(ric);
98     }
99
100     private Mono<Ric> checkTypes(Ric ric) {
101         return a1Client.getPolicyTypeIdentities(ric.getConfig().baseUrl()) //
102             .onErrorResume(t -> {
103                 return Mono.empty();
104             }) //
105             .flatMap(ricTypes -> validateTypes(ricTypes, ric));
106     }
107
108     private Mono<Ric> validateTypes(Collection<String> ricTypes, Ric ric) {
109         if (ricTypes.size() != ric.getSupportedPolicyTypes().size()) {
110             return startRecovery(ric);
111         }
112         for (String typeName : ricTypes) {
113             if (!ric.isSupportingType(typeName)) {
114                 return startRecovery(ric);
115             }
116         }
117         return Mono.just(ric);
118     }
119
120     private Mono<Ric> startRecovery(Ric ric) {
121         RicRecoveryTask recovery = new RicRecoveryTask(a1Client, policyTypes, policies);
122         recovery.run(ric);
123         return Mono.empty();
124     }
125
126     private void onRicChecked(Ric ric) {
127         logger.info("Ric: " + ric.name() + " checked");
128     }
129
130     private void onError(Throwable t) {
131         logger.error("Rics supervision failed", t);
132     }
133
134     private void onComplete() {
135         logger.debug("Checking Rics completed");
136     }
137
138 }