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=b2d0fab846063eace1cfe7176c9277aa492cb41f;hb=3b5213d7ef998e3f76d58a767230013be0a7927f;hp=ebe833b990eede90463ac074b791e0481811bc4a;hpb=3bdae60a11a5f154500b4e7c5de4090326af1f98;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 ebe833b9..b2d0fab8 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 @@ -19,48 +19,136 @@ */ package org.oransc.policyagent.controllers; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; - -import java.net.http.HttpHeaders; - +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.util.Collection; +import java.util.Vector; +import org.oransc.policyagent.Beans; +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.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.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; +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 public class PolicyController { - // http://localhost:8080/policy?type=type3&instance=xxx + private final ApplicationConfig appConfig; + private final Rics rics; + private final PolicyTypes types; + private final Policies policies; + private static Gson gson = new GsonBuilder() // + .serializeNulls() // + .create(); // + + @Autowired + PolicyController(Beans beans) { + this.appConfig = beans.getApplicationConfig(); + this.rics = beans.getRics(); + this.types = beans.getPolicyTypes(); + this.policies = beans.getPolicies(); + } + @GetMapping("/policy") - public String getPolicy(@RequestParam(name = "type", required = false, defaultValue = "type1") String typeName, - @RequestParam(name = "instance", required = false, defaultValue = "new") String instanceId) { - System.out.println("**** getPolicy " + typeName); + public ResponseEntity getPolicy( // + @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) { + try { + Policy p = policies.get(instance); + return new ResponseEntity(p.json(), HttpStatus.OK); + + } catch (ServiceException e) { + return new ResponseEntity(e.getMessage(), HttpStatus.NO_CONTENT); + } + } - return "policy" + typeName + instanceId; + @GetMapping("/policies") + public ResponseEntity getPolicies( // + @RequestParam(name = "type", required = false) String type, // + @RequestParam(name = "ric", required = false) String ric, // + @RequestParam(name = "service", required = false) String service) // + { + 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(toJson(result), HttpStatus.OK); + } + + private boolean include(String filter, String value) { + return filter == null || value.equals(filter); } - public String getHello() { - return "Howdy"; + 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; } - @GetMapping("/status") - @ApiOperation(value = "Returns status and statistics of DATAFILE service") - @ApiResponses( - value = { // - @ApiResponse(code = 200, message = "DATAFILE service is living"), - @ApiResponse(code = 401, message = "You are not authorized to view the resource"), - @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"), - @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") // - }) - public Mono> getStatus(@RequestHeader HttpHeaders headers) { - Mono> response = Mono.just(new ResponseEntity<>("hunky dory", HttpStatus.OK)); - return response; + private String toJson(Collection policies) { + Vector v = new Vector<>(policies.size()); + for (Policy p : policies) { + PolicyInfo policyInfo = ImmutablePolicyInfo.builder() // + .json(p.json()) // + .name(p.id()) // + .ric(p.ric().name()) // + .type(p.type().name()).build(); + v.add(policyInfo); + } + return gson.toJson(v); + } + + @PutMapping(path = "/policy") + public ResponseEntity putPolicy( // + @RequestParam(name = "type", required = true) String type, // + @RequestParam(name = "instance", required = true) String instanceId, // + @RequestParam(name = "ric", required = true) String ric, // + @RequestParam(name = "service", required = true) String service, // + @RequestBody String jsonBody) { + + try { + Ric ricObj = rics.getRic(ric); + Policy policy = ImmutablePolicy.builder() // + .id(instanceId) // + .json(jsonBody) // + .type(types.getType(type)) // + .ric(ricObj) // + .ownerServiceName(service) // + .build(); + policies.put(policy); + return new ResponseEntity(HttpStatus.OK); + } catch (ServiceException e) { + return new ResponseEntity(e.getMessage(), HttpStatus.NOT_FOUND); + } } }