+ @ApiOperation(value = "Delete a policy", response = Object.class)
+ @ApiResponses(
+ value = { //
+ @ApiResponse(code = 204, message = "Policy deleted", response = Object.class),
+ @ApiResponse(code = 404, message = "Policy is not found", response = String.class),
+ @ApiResponse(code = 423, message = "RIC is not operational", response = String.class)})
+ public Mono<ResponseEntity<Object>> deletePolicy( //
+ @RequestParam(name = "id", required = true) String id) {
+ try {
+ Policy policy = policies.getPolicy(id);
+ keepServiceAlive(policy.ownerServiceName());
+ Ric ric = policy.ric();
+ return ric.getLock().lock(LockType.SHARED) //
+ .flatMap(notUsed -> assertRicStateIdle(ric)) //
+ .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.ric())) //
+ .doOnNext(notUsed -> policies.remove(policy)) //
+ .flatMap(client -> client.deletePolicy(policy)) //
+ .doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
+ .doOnError(notUsed -> ric.getLock().unlockBlocking()) //
+ .flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT)))
+ .onErrorResume(this::handleException);
+ } catch (ServiceException e) {
+ return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
+ }
+ }
+
+ @PutMapping(path = "/policy")
+ @ApiOperation(value = "Put a policy", response = String.class)
+ @ApiResponses(
+ value = { //
+ @ApiResponse(code = 201, message = "Policy created", response = Object.class), //
+ @ApiResponse(code = 200, message = "Policy updated", response = Object.class), //
+ @ApiResponse(code = 423, message = "RIC is not operational", response = String.class), //
+ @ApiResponse(code = 404, message = "RIC or policy type is not found", response = String.class) //
+ })
+ public Mono<ResponseEntity<Object>> putPolicy( //
+ @RequestParam(name = "type", required = false, defaultValue = "") String typeName, //
+ @RequestParam(name = "id", required = true) String instanceId, //
+ @RequestParam(name = "ric", required = true) String ricName, //
+ @RequestParam(name = "service", required = true) String service, //
+ @RequestParam(name = "transient", required = false, defaultValue = "false") boolean isTransient, //
+ @RequestBody Object jsonBody) {
+
+ String jsonString = gson.toJson(jsonBody);
+ Ric ric = rics.get(ricName);
+ PolicyType type = policyTypes.get(typeName);
+ keepServiceAlive(service);
+ if (ric == null || type == null) {
+ return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
+ }
+ Policy policy = ImmutablePolicy.builder() //
+ .id(instanceId) //
+ .json(jsonString) //
+ .type(type) //
+ .ric(ric) //
+ .ownerServiceName(service) //
+ .lastModified(getTimeStampUtc()) //
+ .isTransient(isTransient) //
+ .build();
+
+ final boolean isCreate = this.policies.get(policy.id()) == null;
+
+ return ric.getLock().lock(LockType.SHARED) //
+ .flatMap(notUsed -> assertRicStateIdle(ric)) //
+ .flatMap(notUsed -> checkSupportedType(ric, type)) //
+ .flatMap(notUsed -> validateModifiedPolicy(policy)) //
+ .flatMap(notUsed -> a1ClientFactory.createA1Client(ric)) //
+ .flatMap(client -> client.putPolicy(policy)) //
+ .doOnNext(notUsed -> policies.put(policy)) //
+ .doOnNext(notUsed -> ric.getLock().unlockBlocking()) //
+ .doOnError(trowable -> ric.getLock().unlockBlocking()) //
+ .flatMap(notUsed -> Mono.just(new ResponseEntity<>(isCreate ? HttpStatus.CREATED : HttpStatus.OK))) //
+ .onErrorResume(this::handleException);
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private <T> Mono<ResponseEntity<T>> createResponseEntity(String message, HttpStatus status) {
+ ResponseEntity<T> re = new ResponseEntity<>((T) message, status);
+ return Mono.just(re);
+ }
+
+ private <T> Mono<ResponseEntity<T>> handleException(Throwable throwable) {
+ if (throwable instanceof WebClientResponseException) {
+ WebClientResponseException e = (WebClientResponseException) throwable;
+ return createResponseEntity(e.getResponseBodyAsString(), e.getStatusCode());
+ } else if (throwable instanceof RejectionException) {
+ RejectionException e = (RejectionException) throwable;
+ return createResponseEntity(e.getMessage(), e.getStatus());
+ } else {
+ return createResponseEntity(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ 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())) {
+ RejectionException e = new RejectionException("Policy cannot change RIC, policyId: " + current.id() + //
+ ", RIC name: " + current.ric().name() + //
+ ", new name: " + policy.ric().name(), HttpStatus.CONFLICT);
+ return Mono.error(e);
+ }
+ return Mono.just("OK");
+ }