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=7cc042c40b30c40c2a24ca6557e6f33d613a0d21;hb=e8d3abedd8dcc355575057a48aa86fa807095ed8;hp=09916057c6ab1805be0f97f0a00ac557e55b99f9;hpb=25f05019da2af154f169c147dcf12895acaea159;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 09916057..7cc042c4 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 @@ -25,9 +25,11 @@ import com.google.gson.GsonBuilder; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -46,6 +48,8 @@ import org.oransc.policyagent.repository.Ric; import org.oransc.policyagent.repository.Rics; import org.oransc.policyagent.repository.Service; import org.oransc.policyagent.repository.Services; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -84,6 +88,7 @@ public class PolicyController { @Autowired private Services services; + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static Gson gson = new GsonBuilder() // .serializeNulls() // .create(); // @@ -94,7 +99,9 @@ public class PolicyController { 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) { + public ResponseEntity getPolicySchemas( // + @ApiParam(name = "ric", required = false, value = "The name of the Near-RT RIC to get the definitions for.") // + @RequestParam(name = "ric", required = false) String ricName) { if (ricName == null) { Collection types = this.policyTypes.getAll(); return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); @@ -114,7 +121,9 @@ public class PolicyController { 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) { + public ResponseEntity getPolicySchema( // + @ApiParam(name = "id", required = true, value = "The ID of the policy type to get the definition for.") // + @RequestParam(name = "id", required = true) String id) { try { PolicyType type = policyTypes.getType(id); return new ResponseEntity<>(type.schema(), HttpStatus.OK); @@ -133,7 +142,9 @@ public class PolicyController { 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) { + public ResponseEntity getPolicyTypes( // + @ApiParam(name = "ric", required = false, value = "The name of the Near-RT RIC to get types for.") // + @RequestParam(name = "ric", required = false) String ricName) { if (ricName == null) { Collection types = this.policyTypes.getAll(); return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); @@ -155,6 +166,7 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found")} // ) public ResponseEntity getPolicy( // + @ApiParam(name = "id", required = true, value = "The ID of the policy instance.") // @RequestParam(name = "id", required = true) String id) { try { Policy p = policies.getPolicy(id); @@ -172,6 +184,7 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found", response = String.class), @ApiResponse(code = 423, message = "RIC is not operational", response = String.class)}) public Mono> deletePolicy( // + @ApiParam(name = "id", required = true, value = "The ID of the policy instance.") // @RequestParam(name = "id", required = true) String id) { try { Policy policy = policies.getPolicy(id); @@ -201,10 +214,19 @@ public class PolicyController { @ApiResponse(code = 404, message = "RIC or policy type is not found", response = String.class) // }) public Mono> putPolicy( // + @ApiParam(name = "type", required = false, value = "The name of the policy type.") // @RequestParam(name = "type", required = false, defaultValue = "") String typeName, // + @ApiParam(name = "id", required = true, value = "The ID of the policy instance.") // @RequestParam(name = "id", required = true) String instanceId, // + @ApiParam(name = "ric", required = true, value = "The name of the Near-RT RIC where the policy will be " + // + "created.") // @RequestParam(name = "ric", required = true) String ricName, // + @ApiParam(name = "service", required = true, value = "The name of the service creating the policy.") // @RequestParam(name = "service", required = true) String service, // + @ApiParam(name = "transient", required = false, value = "If the policy is transient or not (boolean " + // + "defaulted to false). A policy is transient if it will be forgotten when the service needs to " + // + "reconnect to the Near-RT RIC.") // + @RequestParam(name = "transient", required = false, defaultValue = "false") boolean isTransient, // @RequestBody Object jsonBody) { String jsonString = gson.toJson(jsonBody); @@ -221,18 +243,20 @@ public class PolicyController { .ric(ric) // .ownerServiceName(service) // .lastModified(getTimeStampUtc()) // + .isTransient(isTransient) // .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 -> assertRicStateIdle(ric)) // + .flatMap(notUsed -> checkSupportedType(ric, type)) // + .flatMap(notUsed -> 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()) // + .doOnError(trowable -> ric.getLock().unlockBlocking()) // .flatMap(notUsed -> Mono.just(new ResponseEntity<>(isCreate ? HttpStatus.CREATED : HttpStatus.OK))) // .onErrorResume(this::handleException); } @@ -267,8 +291,17 @@ public class PolicyController { return Mono.just("OK"); } + private Mono checkSupportedType(Ric ric, PolicyType type) { + if (!ric.isSupportingType(type.name())) { + RejectionException e = new RejectionException( + "Type: " + type.name() + " not supported by RIC: " + ric.name(), HttpStatus.NOT_FOUND); + return Mono.error(e); + } + return Mono.just("OK"); + } + private Mono assertRicStateIdle(Ric ric) { - if (ric.getState() == Ric.RicState.IDLE) { + if (ric.getState() == Ric.RicState.AVAILABLE) { return Mono.just("OK"); } else { RejectionException e = new RejectionException( @@ -284,8 +317,11 @@ public class PolicyController { @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( // + @ApiParam(name = "type", required = false, value = "The name of the policy type to get policies for.") // @RequestParam(name = "type", required = false) String type, // + @ApiParam(name = "ric", required = false, value = "The name of the Near-RT RIC to get policies for.") // @RequestParam(name = "ric", required = false) String ric, // + @ApiParam(name = "service", required = false, value = "The name of the service to get policies for.") // @RequestParam(name = "service", required = false) String service) // { if ((type != null && this.policyTypes.get(type) == null)) { @@ -305,8 +341,11 @@ public class PolicyController { value = {@ApiResponse(code = 200, message = "Policy ids", response = String.class, responseContainer = "List"), @ApiResponse(code = 404, message = "RIC or type not found", response = String.class)}) public ResponseEntity getPolicyIds( // + @ApiParam(name = "type", required = false, value = "The name of the policy type to get policies for.") // @RequestParam(name = "type", required = false) String type, // + @ApiParam(name = "ric", required = false, value = "The name of the Near-RT RIC to get policies for.") // @RequestParam(name = "ric", required = false) String ric, // + @ApiParam(name = "service", required = false, value = "The name of the service to get policies for.") // @RequestParam(name = "service", required = false) String service) // { if ((type != null && this.policyTypes.get(type) == null)) { @@ -328,7 +367,9 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found", response = String.class)} // ) public Mono> getPolicyStatus( // - @RequestParam(name = "id", required = true) String id) { + @ApiParam(name = "id", required = true, value = "The ID of the policy.") @RequestParam( + name = "id", // + required = true) String id) { try { Policy policy = policies.getPolicy(id); @@ -389,7 +430,7 @@ public class PolicyController { policyInfo.service = p.ownerServiceName(); policyInfo.lastModified = p.lastModified(); if (!policyInfo.validate()) { - throw new NullPointerException("BUG, all fields must be set"); + logger.error("BUG, all fields must be set"); } v.add(policyInfo); }