2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.tasks;
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;
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
40 * Loads information about RealTime-RICs at startup.
42 @Service("startupService")
43 public class StartupService {
45 private static final Logger logger = LoggerFactory.getLogger(StartupService.class);
48 ApplicationConfig applicationConfig;
54 PolicyTypes policyTypes;
57 private A1Client a1Client;
59 StartupService(ApplicationConfig appConfig, Rics rics, PolicyTypes policyTypes, A1Client a1Client) {
60 this.applicationConfig = appConfig;
62 this.policyTypes = policyTypes;
63 this.a1Client = a1Client;
67 * Reads the configured Rics and performs the service discovery. The result is put into the repository.
69 public void startup() {
70 applicationConfig.initialize();
71 Flux.fromIterable(applicationConfig.getRicConfigs()) //
72 .map(ricConfig -> new Ric(ricConfig)) //
73 .doOnNext(ric -> logger.debug("Handling ric: {}", ric.getConfig().name())) //
74 .flatMap(this::addPolicyTypesForRic) //
75 .flatMap(this::deletePoliciesForRic) //
76 .flatMap(this::addRicToRepo) //
80 private Mono<Ric> addPolicyTypesForRic(Ric ric) {
81 a1Client.getPolicyTypeIdentities(ric.getConfig().baseUrl()) //
82 .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId))
83 .flatMap(this::addTypeToRepo) //
84 .flatMap(type -> addTypeToRic(ric, type)) //
85 .subscribe(null, cause -> setRicToNotReachable(ric, cause), () -> setRicToActive(ric));
86 return Mono.just(ric);
89 private Mono<PolicyType> addTypeToRepo(String policyTypeId) {
90 ImmutablePolicyType type = ImmutablePolicyType.builder().name(policyTypeId).build();
91 if (!policyTypes.contains(policyTypeId)) {
92 policyTypes.put(type);
94 return Mono.just(type);
97 private Mono<Void> addTypeToRic(Ric ric, PolicyType policyType) {
98 ric.addSupportedPolicyType(policyType);
102 private Mono<Ric> deletePoliciesForRic(Ric ric) {
103 if (!Ric.RicState.NOT_REACHABLE.equals(ric.state())) {
104 a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
106 policyId -> logger.debug("Deleting policy: {}, for ric: {}", policyId, ric.getConfig().name()))
107 .flatMap(policyId -> a1Client.deletePolicy(ric.getConfig().baseUrl(), policyId)) //
108 .subscribe(null, cause -> setRicToNotReachable(ric, cause), null);
111 return Mono.just(ric);
114 private void setRicToNotReachable(Ric ric, Throwable t) {
115 ric.setState(Ric.RicState.NOT_REACHABLE);
116 logger.info("Unable to connect to ric {}. Cause: {}", ric.name(), t.getMessage());
119 private void setRicToActive(Ric ric) {
120 ric.setState(RicState.ACTIVE);
123 private Mono<Void> addRicToRepo(Ric ric) {