/*-
* ========================LICENSE_START=================================
* O-RAN-SC
* %%
* Copyright (C) 2019 Nordix Foundation
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================LICENSE_END===================================
*/
package org.oransc.policyagent.tasks;
import static org.oransc.policyagent.repository.Ric.RicState;
import org.oransc.policyagent.clients.A1Client;
import org.oransc.policyagent.clients.A1ClientFactory;
import org.oransc.policyagent.clients.AsyncRestClient;
import org.oransc.policyagent.repository.ImmutablePolicyType;
import org.oransc.policyagent.repository.Lock.LockType;
import org.oransc.policyagent.repository.Policies;
import org.oransc.policyagent.repository.Policy;
import org.oransc.policyagent.repository.PolicyType;
import org.oransc.policyagent.repository.PolicyTypes;
import org.oransc.policyagent.repository.Ric;
import org.oransc.policyagent.repository.Service;
import org.oransc.policyagent.repository.Services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.BaseSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;
/**
* Synchronizes the content of a RIC with the content in the repository. This
* means:
*
* load all policy types
*
* send all policy instances to the RIC
*
* if that fails remove all policy instances
*
* Notify subscribing services
*/
@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
public class RicSynchronizationTask {
private static final Logger logger = LoggerFactory.getLogger(RicSynchronizationTask.class);
private final A1ClientFactory a1ClientFactory;
private final PolicyTypes policyTypes;
private final Policies policies;
private final Services services;
public RicSynchronizationTask(A1ClientFactory a1ClientFactory, PolicyTypes policyTypes, Policies policies,
Services services) {
this.a1ClientFactory = a1ClientFactory;
this.policyTypes = policyTypes;
this.policies = policies;
this.services = services;
}
public void run(Ric ric) {
logger.debug("Handling ric: {}", ric.getConfig().name());
if (ric.getState() == RicState.SYNCHRONIZING) {
logger.debug("Ric: {} is already being synchronized", ric.getConfig().name());
return;
}
ric.getLock().lock(LockType.EXCLUSIVE) //
.flatMap(notUsed -> setRicState(ric)) //
.flatMap(lock -> this.a1ClientFactory.createA1Client(ric)) //
.flatMapMany(client -> runSynchronization(ric, client)) //
.onErrorResume(throwable -> deleteAllPolicyInstances(ric, throwable))
.subscribe(new BaseSubscriber