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=246fdd45009328b27b95eba2e53013c469c0d6a0;hb=4891734864ae374cf6ff357f41cc840c4f685300;hp=21521b77b4a31f76c66a451861405bc10bf6ab74;hpb=3ed724aaef96d30347f9a60ca111627b28133721;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 21521b77..246fdd45 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 @@ -17,83 +17,261 @@ * limitations under the License. * ========================LICENSE_END=================================== */ + package org.oransc.policyagent.controllers; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + +import java.util.Collection; +import java.util.Vector; + +import org.oransc.policyagent.clients.A1Client; import org.oransc.policyagent.configuration.ApplicationConfig; import org.oransc.policyagent.exceptions.ServiceException; import org.oransc.policyagent.repository.ImmutablePolicy; 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.Rics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; @RestController +@Api(value = "Policy Management API") public class PolicyController { - private final ApplicationConfig appConfig; - private final PolicyTypes types; + private final Rics rics; + private final PolicyTypes policyTypes; private final Policies policies; + private final A1Client a1Client; + + private static Gson gson = new GsonBuilder() // + .serializeNulls() // + .create(); // @Autowired - PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) { - this.appConfig = config; - this.types = types; + PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, A1Client a1Client) { + this.policyTypes = types; this.policies = policies; + this.rics = rics; + this.a1Client = a1Client; + } + + @GetMapping("/policy_schemas") + @ApiOperation(value = "Returns policy type schema definitions") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")}) + public ResponseEntity getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) { + synchronized (this.policyTypes) { + 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); + } catch (ServiceException e) { + return new ResponseEntity(e.toString(), HttpStatus.NOT_FOUND); + } + } + } + } + + @GetMapping("/policy_schema") + @ApiOperation(value = "Returns one policy type schema definition") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Type found")}) + public ResponseEntity getPolicySchema(@RequestParam(name = "id", required = true) String id) { + try { + PolicyType type = policyTypes.getType(id); + return new ResponseEntity(type.schema(), HttpStatus.OK); + } catch (ServiceException e) { + return new ResponseEntity(e.toString(), HttpStatus.NOT_FOUND); + } + } + + @GetMapping("/policy_types") + @ApiOperation(value = "Returns policy types") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")}) + public ResponseEntity getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) { + synchronized (this.policyTypes) { + 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); + } catch (ServiceException e) { + return new ResponseEntity(e.toString(), HttpStatus.NOT_FOUND); + } + } + } } - // http://localhost:8080/policy?type=type3&instance=xxx @GetMapping("/policy") + @ApiOperation(value = "Returns the policy") + @ApiResponses( + value = {@ApiResponse(code = 200, message = "Policy found"), + @ApiResponse(code = 204, message = "Policy is not found")}) public ResponseEntity getPolicy( // - @RequestParam(name = "type", required = false, defaultValue = "type1") String typeName, // - @RequestParam(name = "instance", required = false, defaultValue = "new") String instanceId) { + @RequestParam(name = "instance", required = true) String instance) { + try { + Policy p = policies.getPolicy(instance); + return new ResponseEntity(p.json(), HttpStatus.OK); + } catch (ServiceException e) { + return new ResponseEntity(e.getMessage(), HttpStatus.NO_CONTENT); + } + } - return new ResponseEntity("policy" + typeName + instanceId, HttpStatus.OK); + @DeleteMapping("/policy") + @ApiOperation(value = "Deletes the policy") + @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted")}) + public Mono> deletePolicy( // + @RequestParam(name = "instance", required = true) String id) { + Policy policy = policies.get(id); + if (policy != null && policy.ric().state().equals(Ric.RicState.IDLE)) { + return a1Client.deletePolicy(policy.ric().getConfig().baseUrl(), id) // + .doOnEach(notUsed -> policies.removeId(id)) // + .flatMap(notUsed -> { + return Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT)); + }); + } else { + return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)); + } } @PutMapping(path = "/policy") - public ResponseEntity putPolicy( // - @RequestParam(name = "type", required = true) String type, // + @ApiOperation(value = "Create the policy") + @ApiResponses(value = {@ApiResponse(code = 201, message = "Policy created")}) + public Mono> putPolicy( // + @RequestParam(name = "type", required = true) String typeName, // @RequestParam(name = "instance", required = true) String instanceId, // - @RequestParam(name = "ric", required = true) String ric, // + @RequestParam(name = "ric", required = true) String ricName, // @RequestParam(name = "service", required = true) String service, // @RequestBody String jsonBody) { - System.out.println("*********************** " + jsonBody); - - try { + Ric ric = rics.get(ricName); + PolicyType type = policyTypes.get(typeName); + if (ric != null && type != null && ric.state().equals(Ric.RicState.IDLE)) { Policy policy = ImmutablePolicy.builder() // .id(instanceId) // .json(jsonBody) // - .type(types.getType(type)) // - .ric(appConfig.getRic(ric)) // + .type(type) // + .ric(ric) // .ownerServiceName(service) // + .lastModified(getTimeStampUTC()) // .build(); - policies.put(instanceId, policy); - return new ResponseEntity(HttpStatus.OK); - } catch (ServiceException e) { - System.out.println("*********************** " + e.getMessage()); + return a1Client.putPolicy(policy) // + .doOnNext(notUsed -> policies.put(policy)) // + .flatMap(notUsed -> { + return Mono.just(new ResponseEntity<>(HttpStatus.CREATED)); + }); + } + return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)); + } + + @GetMapping("/policies") + @ApiOperation(value = "Returns the policies") + @ApiResponses(value = {@ApiResponse(code = 200, message = "Policies found")}) + public ResponseEntity getPolicies( // + @RequestParam(name = "type", required = false) String type, // + @RequestParam(name = "ric", required = false) String ric, // + @RequestParam(name = "service", required = false) String service) // + { + synchronized (policies) { + Collection 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 = policies.getForRic(ric); + result = filter(result, type, null, service); + } else { + result = policies.getAll(); + } - return new ResponseEntity(e.getMessage(), HttpStatus.NOT_FOUND); + return new ResponseEntity(policiesToJson(result), HttpStatus.OK); } + } + private boolean include(String filter, String value) { + return filter == null || value.equals(filter); } - @PutMapping(path = "/policyX") - public ResponseEntity putPolicyX( // - // @RequestParam(name = "type", required = false) String type, // - // @RequestParam(name = "instance", required = true) String instance, // - // @RequestParam(name = "ric", required = true) String ric, // - @RequestBody String jsonBody) { - System.out.println("*********************** " + jsonBody); - // System.out.println("policy" + type + instance); - return new ResponseEntity(HttpStatus.OK); + private Collection filter(Collection collection, String type, String ric, String service) { + if (type == null && ric == null && service == null) { + return collection; + } + Vector filtered = new Vector<>(); + for (Policy p : collection) { + if (include(type, p.type().name()) && include(ric, p.ric().name()) + && include(service, p.ownerServiceName())) { + filtered.add(p); + } + } + return filtered; + } + + private String policiesToJson(Collection policies) { + Vector v = new Vector<>(policies.size()); + for (Policy p : policies) { + PolicyInfo policyInfo = ImmutablePolicyInfo.builder() // + .json(p.json()) // + .id(p.id()) // + .ric(p.ric().name()) // + .type(p.type().name()) // + .service(p.ownerServiceName()) // + .lastModified(p.lastModified()) // + .build(); + v.add(policyInfo); + } + return gson.toJson(v); + } + + private String toPolicyTypeSchemasJson(Collection types) { + StringBuilder result = new StringBuilder(); + result.append("["); + boolean first = true; + for (PolicyType t : types) { + if (!first) { + result.append(","); + } + first = false; + result.append(t.schema()); + } + result.append("]"); + return result.toString(); + } + + private String toPolicyTypeIdsJson(Collection types) { + Vector v = new Vector<>(types.size()); + for (PolicyType t : types) { + v.add(t.name()); + } + return gson.toJson(v); + } + + private String getTimeStampUTC() { + return java.time.Instant.now().toString(); } }