Introduce error handling in StartupService
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / tasks / StartupService.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 org.oransc.policyagent.clients.A1Client;
24 import org.oransc.policyagent.configuration.ApplicationConfig;
25 import org.oransc.policyagent.repository.ImmutablePolicyType;
26 import org.oransc.policyagent.repository.PolicyType;
27 import org.oransc.policyagent.repository.PolicyTypes;
28 import org.oransc.policyagent.repository.Ric;
29 import org.oransc.policyagent.repository.Ric.RicState;
30 import org.oransc.policyagent.repository.Rics;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
37
38 /**
39  * Loads information about RealTime-RICs at startup.
40  */
41 @Service("startupService")
42 public class StartupService {
43
44     private static final Logger logger = LoggerFactory.getLogger(StartupService.class);
45
46     @Autowired
47     ApplicationConfig applicationConfig;
48
49     @Autowired
50     private Rics rics;
51
52     @Autowired
53     PolicyTypes policyTypes;
54
55     @Autowired
56     private A1Client a1Client;
57
58     StartupService(ApplicationConfig appConfig, Rics rics, PolicyTypes policyTypes, A1Client a1Client) {
59         this.applicationConfig = appConfig;
60         this.rics = rics;
61         this.policyTypes = policyTypes;
62         this.a1Client = a1Client;
63     }
64
65     /**
66      * Reads the configured Rics and performs the service discovery. The result is put into the repository.
67      */
68     public void startup() {
69         applicationConfig.initialize();
70         Flux.fromIterable(applicationConfig.getRicConfigs()) //
71             .map(ricConfig -> new Ric(ricConfig)) //
72             .doOnNext(ric -> logger.debug("Handling ric: {}", ric.getConfig().name())) //
73             .flatMap(this::addPolicyTypesForRic) //
74             .flatMap(this::deletePoliciesForRic) //
75             .flatMap(this::addRicToRepo) //
76             .subscribe();
77     }
78
79     private Mono<Ric> addPolicyTypesForRic(Ric ric) {
80         a1Client.getPolicyTypeIdentities(ric.getConfig().baseUrl()) //
81             .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId))
82             .flatMap(this::addTypeToRepo) //
83             .flatMap(type -> addTypeToRic(ric, type)) //
84             .subscribe(null, cause -> setRicToNotReachable(ric, cause), () -> setRicToActive(ric));
85         return Mono.just(ric);
86     }
87
88     private Mono<PolicyType> addTypeToRepo(String policyTypeId) {
89         ImmutablePolicyType type = ImmutablePolicyType.builder().name(policyTypeId).build();
90         if (!policyTypes.contains(policyTypeId)) {
91             policyTypes.put(type);
92         }
93         return Mono.just(type);
94     }
95
96     private Mono<Void> addTypeToRic(Ric ric, PolicyType policyType) {
97         ric.addSupportedPolicyType(policyType);
98         return Mono.empty();
99     }
100
101     private Mono<Ric> deletePoliciesForRic(Ric ric) {
102         if (!Ric.RicState.NOT_REACHABLE.equals(ric.state())) {
103             a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
104                 .doOnNext(
105                     policyId -> logger.debug("Deleting policy: {}, for ric: {}", policyId, ric.getConfig().name()))
106                 .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
107                 .subscribe(null, cause -> setRicToNotReachable(ric, cause), null);
108         }
109
110         return Mono.just(ric);
111     }
112
113     private void setRicToNotReachable(Ric ric, Throwable t) {
114         ric.setState(Ric.RicState.NOT_REACHABLE);
115         logger.info("Unable to connect to ric {}. Cause: {}", ric.name(), t.getMessage());
116     }
117
118     private void setRicToActive(Ric ric) {
119         ric.setState(RicState.ACTIVE);
120     }
121
122     private Mono<Void> addRicToRepo(Ric ric) {
123         rics.put(ric);
124
125         return Mono.empty();
126     }
127 }