X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fcontrollers%2FPolicyController.java;h=09916057c6ab1805be0f97f0a00ac557e55b99f9;hb=3d0aa333829644f0590b5064d68866da212ef717;hp=0f3b391f2a372777d87e47951a4eb5528a51b649;hpb=e2ac0dcf30eaf828a7c7bbd722730410f4110030;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java index 0f3b391f..09916057 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java @@ -95,17 +95,15 @@ public class PolicyController { @ApiResponse(code = 200, message = "Policy schemas", response = Object.class, responseContainer = "List"), // @ApiResponse(code = 404, message = "RIC is not found", response = String.class)}) public ResponseEntity getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) { - synchronized (this.policyTypes) { - if (ricName == null) { - Collection types = this.policyTypes.getAll(); + if (ricName == null) { + Collection types = this.policyTypes.getAll(); + return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); + } else { + try { + Collection types = rics.getRic(ricName).getSupportedPolicyTypes(); return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); - } else { - try { - Collection types = rics.getRic(ricName).getSupportedPolicyTypes(); - return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); - } catch (ServiceException e) { - return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); - } + } catch (ServiceException e) { + return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); } } } @@ -136,17 +134,15 @@ public class PolicyController { responseContainer = "List"), @ApiResponse(code = 404, message = "RIC is not found", response = String.class)}) public ResponseEntity getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) { - synchronized (this.policyTypes) { - if (ricName == null) { - Collection types = this.policyTypes.getAll(); + if (ricName == null) { + Collection types = this.policyTypes.getAll(); + return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); + } else { + try { + Collection types = rics.getRic(ricName).getSupportedPolicyTypes(); return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); - } else { - try { - Collection types = rics.getRic(ricName).getSupportedPolicyTypes(); - return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); - } catch (ServiceException e) { - return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); - } + } catch (ServiceException e) { + return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); } } } @@ -159,9 +155,9 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found")} // ) public ResponseEntity getPolicy( // - @RequestParam(name = "instance", required = true) String instance) { + @RequestParam(name = "id", required = true) String id) { try { - Policy p = policies.getPolicy(instance); + Policy p = policies.getPolicy(id); return new ResponseEntity<>(p.json(), HttpStatus.OK); } catch (ServiceException e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); @@ -174,19 +170,16 @@ public class PolicyController { 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 locked", response = String.class)}) + @ApiResponse(code = 423, message = "RIC is not operational", response = String.class)}) public Mono> deletePolicy( // - @RequestParam(name = "instance", required = true) String id) { - Policy policy; + @RequestParam(name = "id", required = true) String id) { try { - policy = policies.getPolicy(id); + Policy policy = policies.getPolicy(id); keepServiceAlive(policy.ownerServiceName()); - if (policy.ric().getState() != Ric.RicState.IDLE) { - return Mono.just(new ResponseEntity<>("Busy, synchronizing", HttpStatus.LOCKED)); - } Ric ric = policy.ric(); - return ric.getLock().lock(LockType.SHARED) // // - .flatMap(lock -> a1ClientFactory.createA1Client(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()) // @@ -204,12 +197,12 @@ public class PolicyController { 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 locked", response = String.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> putPolicy( // @RequestParam(name = "type", required = false, defaultValue = "") String typeName, // - @RequestParam(name = "instance", required = true) String instanceId, // + @RequestParam(name = "id", required = true) String instanceId, // @RequestParam(name = "ric", required = true) String ricName, // @RequestParam(name = "service", required = true) String service, // @RequestBody Object jsonBody) { @@ -218,31 +211,30 @@ public class PolicyController { Ric ric = rics.get(ricName); PolicyType type = policyTypes.get(typeName); keepServiceAlive(service); - if (ric != null && type != null && ric.getState() == Ric.RicState.IDLE) { - Policy policy = ImmutablePolicy.builder() // - .id(instanceId) // - .json(jsonString) // - .type(type) // - .ric(ric) // - .ownerServiceName(service) // - .lastModified(getTimeStampUtc()) // - .build(); - - 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(this::handleException); + if (ric == null || type == null) { + return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)); } - - return ric == null || type == null ? Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)) - : Mono.just(new ResponseEntity<>(HttpStatus.LOCKED)); // Synchronizing + Policy policy = ImmutablePolicy.builder() // + .id(instanceId) // + .json(jsonString) // + .type(type) // + .ric(ric) // + .ownerServiceName(service) // + .lastModified(getTimeStampUtc()) // + .build(); + + final boolean isCreate = this.policies.get(policy.id()) == null; + + return ric.getLock().lock(LockType.SHARED) // + .flatMap(p -> assertRicStateIdle(ric)) // + .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(this::handleException); } @SuppressWarnings({"unchecked"}) @@ -275,6 +267,16 @@ public class PolicyController { return Mono.just("OK"); } + private Mono assertRicStateIdle(Ric ric) { + if (ric.getState() == Ric.RicState.IDLE) { + return Mono.just("OK"); + } else { + RejectionException e = new RejectionException( + "Ric is not operational, RIC name: " + ric.name() + ", state: " + ric.getState(), HttpStatus.LOCKED); + return Mono.error(e); + } + } + @GetMapping("/policies") @ApiOperation(value = "Query policies") @ApiResponses( @@ -292,10 +294,9 @@ public class PolicyController { if ((ric != null && this.rics.get(ric) == null)) { return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND); } - synchronized (policies) { - String filteredPolicies = policiesToJson(filter(type, ric, service)); - return new ResponseEntity<>(filteredPolicies, HttpStatus.OK); - } + + String filteredPolicies = policiesToJson(filter(type, ric, service)); + return new ResponseEntity<>(filteredPolicies, HttpStatus.OK); } @GetMapping("/policy_ids") @@ -314,10 +315,9 @@ public class PolicyController { if ((ric != null && this.rics.get(ric) == null)) { return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND); } - synchronized (policies) { - String policyIdsJson = toPolicyIdsJson(filter(type, ric, service)); - return new ResponseEntity<>(policyIdsJson, HttpStatus.OK); - } + + String policyIdsJson = toPolicyIdsJson(filter(type, ric, service)); + return new ResponseEntity<>(policyIdsJson, HttpStatus.OK); } @GetMapping("/policy_status") @@ -328,9 +328,9 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found", response = String.class)} // ) public Mono> getPolicyStatus( // - @RequestParam(name = "instance", required = true) String instance) { + @RequestParam(name = "id", required = true) String id) { try { - Policy policy = policies.getPolicy(instance); + Policy policy = policies.getPolicy(id); return a1ClientFactory.createA1Client(policy.ric()) // .flatMap(client -> client.getPolicyStatus(policy)) // @@ -367,16 +367,14 @@ public class PolicyController { } private Collection filter(String type, String ric, String service) { - synchronized (policies) { - if (type != null) { - return filter(policies.getForType(type), null, ric, service); - } else if (service != null) { - return filter(policies.getForService(service), type, ric, null); - } else if (ric != null) { - return filter(policies.getForRic(ric), type, null, service); - } else { - return policies.getAll(); - } + if (type != null) { + return filter(policies.getForType(type), null, ric, service); + } else if (service != null) { + return filter(policies.getForService(service), type, ric, null); + } else if (ric != null) { + return filter(policies.getForRic(ric), type, null, service); + } else { + return policies.getAll(); } }