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;
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
39 * Loads information about RealTime-RICs at startup.
41 @Service("startupService")
42 public class StartupService {
44 private static final Logger logger = LoggerFactory.getLogger(StartupService.class);
47 ApplicationConfig applicationConfig;
53 PolicyTypes policyTypes;
56 private A1Client a1Client;
58 StartupService(ApplicationConfig appConfig, Rics rics, PolicyTypes policyTypes, A1Client a1Client) {
59 this.applicationConfig = appConfig;
61 this.policyTypes = policyTypes;
62 this.a1Client = a1Client;
66 * Reads the configured Rics and performs the service discovery. The result is put into the repository.
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) //
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);
88 private Mono<PolicyType> addTypeToRepo(String policyTypeId) {
89 ImmutablePolicyType type = ImmutablePolicyType.builder().name(policyTypeId).build();
90 if (!policyTypes.contains(policyTypeId)) {
91 policyTypes.put(type);
93 return Mono.just(type);
96 private Mono<Void> addTypeToRic(Ric ric, PolicyType policyType) {
97 ric.addSupportedPolicyType(policyType);
101 private Mono<Ric> deletePoliciesForRic(Ric ric) {
102 if (!Ric.RicState.NOT_REACHABLE.equals(ric.state())) {
103 a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //
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);
110 return Mono.just(ric);
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());
118 private void setRicToActive(Ric ric) {
119 ric.setState(RicState.ACTIVE);
122 private Mono<Void> addRicToRepo(Ric ric) {