Merge "Remove code smell and bug"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / tasks / RicSupervision.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 RicSupervision {
51     private static final Logger logger = LoggerFactory.getLogger(RicSupervision.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 RicSupervision(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(ric -> logger.debug("Ric: {} checked", ric.ric.name()), //
76             null, //
77             () -> logger.debug("Checking Rics completed"));
78     }
79
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());
86         }
87     }
88
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)); //
96     }
97
98     private static class RicData {
99         RicData(Ric ric, A1Client a1Client) {
100             this.ric = ric;
101             this.a1Client = a1Client;
102         }
103
104         final Ric ric;
105         final A1Client a1Client;
106     }
107
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)));
112     }
113
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) {
119             return Mono.empty();
120         } else {
121             return Mono.just(ric);
122         }
123     }
124
125     private Mono<RicData> checkRicPolicies(RicData ric) {
126         return ric.a1Client.getPolicyIdentities() //
127             .flatMap(ricP -> validateInstances(ricP, ric));
128     }
129
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);
134             }
135
136             for (String policyId : ricPolicies) {
137                 if (!policies.containsPolicy(policyId)) {
138                     return startSynchronization(ric);
139                 }
140             }
141             return Mono.just(ric);
142         }
143     }
144
145     private Mono<RicData> checkRicPolicyTypes(RicData ric) {
146         return ric.a1Client.getPolicyTypeIdentities() //
147             .flatMap(ricTypes -> validateTypes(ricTypes, ric));
148     }
149
150     private Mono<RicData> validateTypes(Collection<String> ricTypes, RicData ric) {
151         if (ricTypes.size() != ric.ric.getSupportedPolicyTypes().size()) {
152             return startSynchronization(ric);
153         }
154         for (String typeName : ricTypes) {
155             if (!ric.ric.isSupportingType(typeName)) {
156                 return startSynchronization(ric);
157             }
158         }
159         return Mono.just(ric);
160     }
161
162     private Mono<RicData> startSynchronization(RicData ric) {
163         RicSynchronizationTask synchronizationTask = createSynchronizationTask();
164         synchronizationTask.run(ric.ric);
165         return Mono.error(new Exception("Syncronization started"));
166     }
167
168     RicSynchronizationTask createSynchronizationTask() {
169         return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services);
170     }
171 }