Add description to parameters in the remaining controllers
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / PolicyController.java
index 3240cdd..7cc042c 100644 (file)
@@ -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<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
+    public ResponseEntity<String> 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<PolicyType> 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<String> getPolicySchema(@RequestParam(name = "id", required = true) String id) {
+    public ResponseEntity<String> 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<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
+    public ResponseEntity<String> 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<PolicyType> 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<String> 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<ResponseEntity<Object>> 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<ResponseEntity<Object>> 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,6 +291,15 @@ public class PolicyController {
         return Mono.just("OK");
     }
 
+    private Mono<Object> 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<Object> assertRicStateIdle(Ric ric) {
         if (ric.getState() == Ric.RicState.AVAILABLE) {
             return Mono.just("OK");
@@ -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<String> 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<String> 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<ResponseEntity<String>> 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);
         }