import org.oransc.policyagent.configuration.ApplicationConfig;
import org.oransc.policyagent.exceptions.ServiceException;
import org.oransc.policyagent.repository.ImmutablePolicy;
+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;
Policy policy = policies.get(id);
if (policy != null && policy.ric().getState() == Ric.RicState.IDLE) {
Ric ric = policy.ric();
- return a1ClientFactory.createA1Client(policy.ric()) //
- .doOnNext(notUsed -> ric.getLock().lockBlocking()) //
+ return ric.getLock().lock(LockType.SHARED) // //
+ .flatMap(lock -> a1ClientFactory.createA1Client(policy.ric())) //
.doOnNext(notUsed -> policies.remove(policy)) //
.flatMap(client -> client.deletePolicy(policy)) //
- .doOnNext(notUsed -> ric.getLock().unlock()) //
- .doOnError(notUsed -> ric.getLock().unlock()) //
+ .doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
+ .doOnError(notUsed -> ric.getLock().unlockBlocking()) //
.flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT)));
} else if (policy != null) {
return Mono.just(new ResponseEntity<>("Busy, recovering", HttpStatus.LOCKED));
final boolean isCreate = this.policies.get(policy.id()) == null;
- return Mono.just(policy) //
- .doOnNext(notUsed -> ric.getLock().lockBlocking()) //
+ return ric.getLock().lock(LockType.SHARED) //
.flatMap(p -> validateModifiedPolicy(policy)) //
.flatMap(notUsed -> a1ClientFactory.createA1Client(ric)) //
.flatMap(client -> client.putPolicy(policy)) //
.doOnNext(notUsed -> policies.put(policy)) //
- .doOnNext(notUsed -> ric.getLock().unlock()) //
- .doOnError(t -> ric.getLock().unlock()) //
+ .doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
+ .doOnError(t -> ric.getLock().unlockBlocking()) //
.flatMap(notUsed -> Mono.just(new ResponseEntity<>(isCreate ? HttpStatus.CREATED : HttpStatus.OK))) //
.onErrorResume(t -> Mono.just(new ResponseEntity<>(t.getMessage(), HttpStatus.METHOD_NOT_ALLOWED)));
}
private Mono<Object> validateModifiedPolicy(Policy policy) {
// Check that ric is not updated
Policy current = this.policies.get(policy.id());
- if (current != null) {
- if (!current.ric().name().equals(policy.ric().name())) {
- return Mono.error(new Exception("Policy cannot change RIC, policyId: " + current.id() + //
- ", RIC name: " + current.ric().name() + //
- ", new name: " + policy.ric().name()));
- }
+ if (current != null && !current.ric().name().equals(policy.ric().name())) {
+ return Mono.error(new Exception("Policy cannot change RIC, policyId: " + current.id() + //
+ ", RIC name: " + current.ric().name() + //
+ ", new name: " + policy.ric().name()));
}
return Mono.just("OK");
}