+
+ final boolean isCreate = this.policies.get(policy.id()) == null;
+
+ 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().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)));
+ }
+
+ return ric == null || type == null ? Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND))
+ : Mono.just(new ResponseEntity<>(HttpStatus.LOCKED)); // Recovering
+ }
+
+ private Mono<Object> validateModifiedPolicy(Policy policy) {
+ // Check that ric is not updated
+ Policy current = this.policies.get(policy.id());
+ 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");
+ }
+
+ @GetMapping("/policies")
+ @ApiOperation(value = "Query policies")
+ @ApiResponses(
+ value = {
+ @ApiResponse(code = 200, message = "Policies", response = PolicyInfo.class, responseContainer = "List"),
+ @ApiResponse(code = 404, message = "RIC or type not found", response = String.class)})
+ public ResponseEntity<String> getPolicies( //
+ @RequestParam(name = "type", required = false) String type, //
+ @RequestParam(name = "ric", required = false) String ric, //
+ @RequestParam(name = "service", required = false) String service) //
+ {
+ if ((type != null && this.policyTypes.get(type) == null)) {
+ return new ResponseEntity<>("Policy type not found", HttpStatus.NOT_FOUND);
+ }
+ if ((ric != null && this.rics.get(ric) == null)) {
+ return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND);
+ }
+ synchronized (policies) {
+ Collection<Policy> result = null;
+
+ if (type != null) {
+ result = policies.getForType(type);
+ result = filter(result, null, ric, service);
+ } else if (service != null) {
+ result = policies.getForService(service);
+ result = filter(result, type, ric, null);
+ } else if (ric != null) {
+ result = filter(policies.getForRic(ric), type, null, service);
+ } else {
+ result = policies.getAll();
+ }
+
+ String policiesJson;
+ try {
+ policiesJson = policiesToJson(result);
+ } catch (ServiceException e) {
+ return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ return new ResponseEntity<>(policiesJson, HttpStatus.OK);
+ }
+ }
+
+ @GetMapping("/policy_status")
+ @ApiOperation(value = "Returns a policy status") //
+ @ApiResponses(
+ value = { //
+ @ApiResponse(code = 200, message = "Policy status", response = Object.class), //
+ @ApiResponse(code = 404, message = "Policy is not found", response = String.class)} //
+ )
+ public Mono<ResponseEntity<String>> getPolicyStatus( //
+ @RequestParam(name = "instance", required = true) String instance) {
+ try {
+ Policy policy = policies.getPolicy(instance);
+
+ return a1ClientFactory.createA1Client(policy.ric()) //
+ .flatMap(client -> client.getPolicyStatus(policy)) //
+ .flatMap(status -> Mono.just(new ResponseEntity<>(status, HttpStatus.OK)));