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 java.util.Vector;
25 import org.oransc.policyagent.clients.A1Client;
26 import org.oransc.policyagent.clients.A1ClientFactory;
27 import org.oransc.policyagent.clients.AsyncRestClient;
28 import org.oransc.policyagent.exceptions.ServiceException;
29 import org.oransc.policyagent.repository.ImmutablePolicyType;
30 import org.oransc.policyagent.repository.Policies;
31 import org.oransc.policyagent.repository.Policy;
32 import org.oransc.policyagent.repository.PolicyType;
33 import org.oransc.policyagent.repository.PolicyTypes;
34 import org.oransc.policyagent.repository.Ric;
35 import org.oransc.policyagent.repository.Service;
36 import org.oransc.policyagent.repository.Services;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import reactor.core.publisher.Flux;
41 import reactor.core.publisher.Mono;
44 * Recovery handling of RIC.
46 * - load all policy types
47 * - send all policy instances to the RIC
48 * --- if that fails remove all policy instances
49 * - Notify subscribing services
51 public class RicRecoveryTask {
53 private static final Logger logger = LoggerFactory.getLogger(RicRecoveryTask.class);
55 private final A1ClientFactory a1ClientFactory;
56 private final PolicyTypes policyTypes;
57 private final Policies policies;
58 private final Services services;
60 public RicRecoveryTask(A1ClientFactory a1ClientFactory, PolicyTypes policyTypes, Policies policies,
62 this.a1ClientFactory = a1ClientFactory;
63 this.policyTypes = policyTypes;
64 this.policies = policies;
65 this.services = services;
68 public void run(Ric ric) {
69 logger.debug("Handling ric: {}", ric.getConfig().name());
72 if (ric.getState() == Ric.RicState.RECOVERING) {
73 return; // Already running
75 ric.setState(Ric.RicState.RECOVERING);
77 this.a1ClientFactory.createA1Client(ric)//
78 .flatMapMany(client -> startRecover(ric, client)) //
79 .subscribe(x -> logger.debug("Recover: " + x), //
80 throwable -> onRecoveryError(ric, throwable), //
81 () -> onRecoveryComplete(ric));
84 private Flux<Object> startRecover(Ric ric, A1Client a1Client) {
85 Flux<PolicyType> recoverTypes = recoverPolicyTypes(ric, a1Client);
86 Flux<?> deletePoliciesInRic = a1Client.deleteAllPolicies();
87 Flux<?> recreatePoliciesInRic = recreateAllPoliciesInRic(ric, a1Client);
89 return Flux.concat(recoverTypes, deletePoliciesInRic, recreatePoliciesInRic);
92 private void onRecoveryComplete(Ric ric) {
93 logger.debug("Recovery completed for:" + ric.name());
94 ric.setState(Ric.RicState.IDLE);
95 notifyAllServices("Recovery completed for:" + ric.name());
98 private void notifyAllServices(String body) {
99 synchronized (services) {
100 for (Service service : services.getAll()) {
101 String url = service.getCallbackUrl();
102 if (service.getCallbackUrl().length() > 0) {
105 .subscribe(rsp -> logger.debug("Service called"),
106 throwable -> logger.warn("Service called failed", throwable),
107 () -> logger.debug("Service called complete"));
113 private void onRecoveryError(Ric ric, Throwable t) {
114 logger.warn("Recovery failed for: {}, reason: {}", ric.name(), t.getMessage());
115 // If recovery fails, try to remove all instances
116 deleteAllPolicies(ric);
117 Flux<PolicyType> recoverTypes = this.a1ClientFactory.createA1Client(ric) //
118 .flatMapMany(a1Client -> recoverPolicyTypes(ric, a1Client));
119 Flux<?> deletePoliciesInRic = this.a1ClientFactory.createA1Client(ric) //
120 .flatMapMany(a1Client -> a1Client.deleteAllPolicies());
122 Flux.merge(recoverTypes, deletePoliciesInRic) //
123 .subscribe(x -> logger.debug("Brute recover: " + x), //
124 throwable -> onRemoveAllError(ric, throwable), //
125 () -> onRecoveryComplete(ric));
128 private void onRemoveAllError(Ric ric, Throwable t) {
129 logger.warn("Remove all failed for: {}, reason: {}", ric.name(), t.getMessage());
130 ric.setState(Ric.RicState.UNDEFINED);
133 private AsyncRestClient createClient(final String url) {
134 return new AsyncRestClient(url);
137 private Flux<PolicyType> recoverPolicyTypes(Ric ric, A1Client a1Client) {
138 ric.clearSupportedPolicyTypes();
139 return a1Client.getPolicyTypeIdentities() //
140 .flatMapMany(types -> Flux.fromIterable(types)) //
141 .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId)) //
142 .flatMap((policyTypeId) -> getPolicyType(ric, policyTypeId, a1Client)) //
143 .doOnNext(policyType -> ric.addSupportedPolicyType(policyType)); //
146 private Mono<PolicyType> getPolicyType(Ric ric, String policyTypeId, A1Client a1Client) {
147 if (policyTypes.contains(policyTypeId)) {
149 return Mono.just(policyTypes.getType(policyTypeId));
150 } catch (ServiceException e) {
151 return Mono.error(e);
154 return a1Client.getPolicyTypeSchema(policyTypeId) //
155 .flatMap(schema -> createPolicyType(policyTypeId, schema));
158 private Mono<PolicyType> createPolicyType(String policyTypeId, String schema) {
159 PolicyType pt = ImmutablePolicyType.builder().name(policyTypeId).schema(schema).build();
161 return Mono.just(pt);
164 private void deleteAllPolicies(Ric ric) {
165 synchronized (policies) {
166 for (Policy policy : policies.getForRic(ric.name())) {
167 this.policies.remove(policy);
172 private Flux<String> recreateAllPoliciesInRic(Ric ric, A1Client a1Client) {
173 synchronized (policies) {
174 return Flux.fromIterable(new Vector<>(policies.getForRic(ric.name()))) //
176 policy -> logger.debug("Recreating policy: {}, for ric: {}", policy.id(), ric.getConfig().name()))
177 .flatMap(policy -> a1Client.putPolicy(policy));