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=1af607f46c9101c962e4d40068792a0d627224d0;hb=6a8a0d5350a77b6d1e4a8f95c0fe8fbfeef77339;hp=a507a2b14a60cf000395ec94a7060f6793dd2c67;hpb=412e0a2f5d16a227374ebd417e7545a514df1143;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 a507a2b1..1af607f4 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,96 +17,355 @@ * 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.Vector; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import org.oransc.policyagent.clients.A1ClientFactory; import org.oransc.policyagent.configuration.ApplicationConfig; -import org.oransc.policyagent.configuration.RicConfig; +import org.oransc.policyagent.exceptions.ServiceException; +import org.oransc.policyagent.repository.ImmutablePolicy; +import org.oransc.policyagent.repository.Lock.LockType; +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(tags = "A1 Policy Management") public class PolicyController { - private final ApplicationConfig appConfig; + private final Rics rics; + private final PolicyTypes policyTypes; + private final Policies policies; + private final A1ClientFactory a1ClientFactory; + private static Gson gson = new GsonBuilder() // .serializeNulls() // .create(); // @Autowired - PolicyController(ApplicationConfig config) { - this.appConfig = config; + PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, + A1ClientFactory a1ClientFactory) { + this.policyTypes = types; + this.policies = policies; + this.rics = rics; + this.a1ClientFactory = a1ClientFactory; } - // http://localhost:8080/policy?type=type3&instance=xxx - @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); + @GetMapping("/policy_schemas") + @ApiOperation(value = "Returns policy type schema definitions") + @ApiResponses( + value = { + @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(); + 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); + } + } + } + } - return "policy" + typeName + instanceId; + @GetMapping("/policy_schema") + @ApiOperation(value = "Returns one policy type schema definition") + @ApiResponses( + value = { // + @ApiResponse(code = 200, message = "Policy schema", response = Object.class), + @ApiResponse(code = 404, message = "RIC is not found", response = String.class)}) + 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); + } } - public String getHello() { - return "Howdy"; + @GetMapping("/policy_types") + @ApiOperation(value = "Query policy type names") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "Policy type names", + response = String.class, + 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(); + 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); + } + } + } } - @GetMapping("/status") - @ApiOperation(value = "Returns status and statistics of the service") + @GetMapping("/policy") + @ApiOperation(value = "Returns a policy configuration") // @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() { - Mono> response = Mono.just(new ResponseEntity<>("hunky dory", HttpStatus.OK)); - return response; - } - - // http://localhost:8080/rics?managedElementId=kista_1 - @GetMapping("/rics") - @ApiOperation(value = "Returns defined NearRT RIC:s") - public ResponseEntity getRics( - @RequestParam(name = "managedElementId", required = false, defaultValue = "") String managedElementId) { - Vector result = new Vector(); - Vector config = getRicConfigs(managedElementId); - - for (RicConfig ricConfig : config) { - RicInfo ric = ImmutableRicInfo.builder() // - .managedElementIds(ricConfig.managedElementIds()) // - .name(ricConfig.name()) // + @ApiResponse(code = 200, message = "Policy found", response = Object.class), // + @ApiResponse(code = 404, message = "Policy is not found")} // + ) + public ResponseEntity getPolicy( // + @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.NOT_FOUND); + } + } + + @DeleteMapping("/policy") + @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 locked", response = String.class)}) + public Mono> deletePolicy( // + @RequestParam(name = "instance", required = true) String id) { + Policy policy = policies.get(id); + if (policy != null && policy.ric().getState() == Ric.RicState.IDLE) { + Ric ric = policy.ric(); + return ric.getLock().lock(LockType.SHARED) // // + .flatMap(lock -> 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))); + } else if (policy != null) { + return Mono.just(new ResponseEntity<>("Busy, recovering", HttpStatus.LOCKED)); + } else { + 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"), // + @ApiResponse(code = 200, message = "Policy updated"), // + @ApiResponse(code = 423, message = "RIC is locked", response = String.class), // + @ApiResponse(code = 404, message = "RIC or policy type is not found", response = String.class), // + @ApiResponse(code = 405, message = "Change is not allowed", response = String.class)}) + public Mono> putPolicy( // + @RequestParam(name = "type", required = true) String typeName, // + @RequestParam(name = "instance", required = true) String instanceId, // + @RequestParam(name = "ric", required = true) String ricName, // + @RequestParam(name = "service", required = true) String service, // + @RequestBody Object jsonBody) { + + String jsonString = gson.toJson(jsonBody); + Ric ric = rics.get(ricName); + PolicyType type = policyTypes.get(typeName); + 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(); - result.add(ric); + + 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(t -> Mono.just(new ResponseEntity<>(t.getMessage(), HttpStatus.METHOD_NOT_ALLOWED))); } - return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK); + return ric == null || type == null ? Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)) + : Mono.just(new ResponseEntity<>(HttpStatus.LOCKED)); // Recovering } - private Vector getRicConfigs(String managedElementId) { - if (managedElementId.equals("")) { - return this.appConfig.getRicConfigs(); + private Mono 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())) { + return Mono.error(new Exception("Policy cannot change RIC, policyId: " + current.id() + // + ", RIC name: " + current.ric().name() + // + ", new name: " + policy.ric().name())); + } + return Mono.just("OK"); + } + + @GetMapping("/policies") + @ApiOperation(value = "Query policies") + @ApiResponses( + value = { + @ApiResponse(code = 200, message = "Policies", response = PolicyInfo.class, responseContainer = "List"), + @ApiResponse(code = 404, message = "RIC or type not found", response = String.class)}) + public ResponseEntity getPolicies( // + @RequestParam(name = "type", required = false) String type, // + @RequestParam(name = "ric", required = false) String ric, // + @RequestParam(name = "service", required = false) String service) // + { + if ((type != null && this.policyTypes.get(type) == null)) { + return new ResponseEntity<>("Policy type not found", HttpStatus.NOT_FOUND); } + if ((ric != null && this.rics.get(ric) == null)) { + return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND); + } + 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 = filter(policies.getForRic(ric), type, null, service); + } else { + result = policies.getAll(); + } + + String policiesJson; + try { + policiesJson = policiesToJson(result); + } catch (ServiceException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + return new ResponseEntity<>(policiesJson, HttpStatus.OK); + } + } + + @GetMapping("/policy_status") + @ApiOperation(value = "Returns a policy status") // + @ApiResponses( + value = { // + @ApiResponse(code = 200, message = "Policy status", response = Object.class), // + @ApiResponse(code = 404, message = "Policy is not found", response = String.class)} // + ) + public Mono> getPolicyStatus( // + @RequestParam(name = "instance", required = true) String instance) { + try { + Policy policy = policies.getPolicy(instance); + + return a1ClientFactory.createA1Client(policy.ric()) // + .flatMap(client -> client.getPolicyStatus(policy)) // + .flatMap(status -> Mono.just(new ResponseEntity<>(status, HttpStatus.OK))); + } catch (ServiceException e) { + return Mono.just(new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND)); + } + } + + private boolean include(String filter, String value) { + return filter == null || value.equals(filter); + } + + private Collection filter(Collection collection, String type, String ric, String service) { + if (type == null && ric == null && service == null) { + return collection; + } + List filtered = new ArrayList<>(); + 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) throws ServiceException { + List v = new ArrayList<>(policies.size()); + for (Policy p : policies) { + PolicyInfo policyInfo = new PolicyInfo(); + policyInfo.id = p.id(); + policyInfo.json = fromJson(p.json()); + policyInfo.ric = p.ric().name(); + policyInfo.type = p.type().name(); + policyInfo.service = p.ownerServiceName(); + policyInfo.lastModified = p.lastModified(); + if (!policyInfo.validate()) { + throw new ServiceException("BUG, all fields must be set"); + } + v.add(policyInfo); + } + return gson.toJson(v); + } + + private Object fromJson(String jsonStr) { + return gson.fromJson(jsonStr, Object.class); + } + + 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) { + List v = new ArrayList<>(types.size()); + for (PolicyType t : types) { + v.add(t.name()); + } + return gson.toJson(v); + } - Vector result = new Vector(1); - appConfig.getRicConfig(managedElementId).ifPresent((config) -> { - result.add(config); - }); - return result; + private String getTimeStampUtc() { + return java.time.Instant.now().toString(); } }