Added callback to R-APPS invoked after RIC recovery
[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.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;
37
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40
41 /**
42  * Regularly checks the exisiting rics towards the local repository to keep it consistent.
43  */
44 @Component
45 @EnableScheduling
46 public class RepositorySupervision {
47     private static final Logger logger = LoggerFactory.getLogger(RepositorySupervision.class);
48
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;
54
55     @Autowired
56     public RepositorySupervision(Rics rics, Policies policies, A1Client a1Client, PolicyTypes policyTypes,
57         Services services) {
58         this.rics = rics;
59         this.policies = policies;
60         this.a1Client = a1Client;
61         this.policyTypes = policyTypes;
62         this.services = services;
63     }
64
65     /**
66      * Regularly contacts all Rics to check if they are alive.
67      */
68     @Scheduled(fixedRate = 1000 * 60)
69     public void checkAllRics() {
70         logger.debug("Checking Rics starting");
71         createTask().subscribe(this::onRicChecked, this::onError, this::onComplete);
72
73     }
74
75     private Flux<Ric> createTask() {
76         return Flux.fromIterable(rics.getRics()) //
77             .flatMap(ric -> checkInstances(ric)) //
78             .flatMap(ric -> checkTypes(ric));
79     }
80
81     private Mono<Ric> checkInstances(Ric ric) {
82
83         return a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
84             .onErrorResume(t -> Mono.empty()) //
85             .flatMap(ricP -> validateInstances(ricP, ric));
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, services);
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 }