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=e29a4e9c2e41358187ddc482865bf4a40cce1249;hb=a76d95e9292f99dfb5cd5782ef6d7bb2ec293fd7;hp=97f9f4c76a88a6ef8702a22e254a382370e1db78;hpb=bca519150103ccd308dd492e07744bc494a4a4ec;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 97f9f4c7..e29a4e9c 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 @@ -20,12 +20,17 @@ package org.oransc.policyagent.controllers; -import java.util.Collection; -import java.util.Vector; - 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.configuration.ApplicationConfig; import org.oransc.policyagent.exceptions.ServiceException; import org.oransc.policyagent.repository.ImmutablePolicy; @@ -45,11 +50,6 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; - @RestController @Api(value = "Policy Management API") public class PolicyController { @@ -68,17 +68,46 @@ public class PolicyController { this.rics = rics; } + @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) { + 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 all the 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) { if (ricName == null) { Collection types = this.policyTypes.getAll(); - return new ResponseEntity(policyTypesToJson(types), HttpStatus.OK); + return new ResponseEntity(toPolicyTypeIdsJson(types), HttpStatus.OK); } else { try { Collection types = rics.getRic(ricName).getSupportedPolicyTypes(); - return new ResponseEntity(policyTypesToJson(types), HttpStatus.OK); + return new ResponseEntity(toPolicyTypeIdsJson(types), HttpStatus.OK); } catch (ServiceException e) { return new ResponseEntity(e.toString(), HttpStatus.NOT_FOUND); } @@ -112,7 +141,7 @@ public class PolicyController { @GetMapping("/policies") @ApiOperation(value = "Returns the policies") - @ApiResponses(value = {@ApiResponse(code = 200, message = "Polcies found")}) + @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, // @@ -170,13 +199,25 @@ public class PolicyController { return gson.toJson(v); } - private String policyTypesToJson(Collection types) { - Vector v = new Vector<>(types.size()); + private String toPolicyTypeSchemasJson(Collection types) { + StringBuilder result = new StringBuilder(); + result.append("["); + boolean first = true; for (PolicyType t : types) { - PolicyTypeInfo policyInfo = ImmutablePolicyTypeInfo.builder() // - .name(t.name()) // - .build(); - v.add(policyInfo); + 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); }