Updates of the NBI
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / PolicyController.java
index 890b2a4..e29a4e9 100644 (file)
  * 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.Collection;
 import java.util.Vector;
 
@@ -37,7 +40,6 @@ 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.oransc.policyagent.repository.Services;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -52,47 +54,75 @@ import org.springframework.web.bind.annotation.RestController;
 @Api(value = "Policy Management API")
 public class PolicyController {
 
-    private final ApplicationConfig appConfig;
     private final Rics rics;
     private final PolicyTypes policyTypes;
     private final Policies policies;
-    private final Services services;
     private static Gson gson = new GsonBuilder() //
         .serializeNulls() //
         .create(); //
 
     @Autowired
-    PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
-        this.appConfig = config;
+    PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics) {
         this.policyTypes = types;
         this.policies = policies;
         this.rics = rics;
-        this.services = services;
     }
 
-    @GetMapping("/policy_types")
-    @ApiOperation(value = "Returns all the policy types")
-    @ApiResponses(
-        value = {
-            @ApiResponse(code = 200, message = "Policy Types found")
-        })
-    public ResponseEntity<String> getPolicyTypes() {
+    @GetMapping("/policy_schemas")
+    @ApiOperation(value = "Returns policy type schema definitions")
+    @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")})
+    public ResponseEntity<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
+        if (ricName == null) {
+            Collection<PolicyType> types = this.policyTypes.getAll();
+            return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
+        } else {
+            try {
+                Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
+                return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
+            } catch (ServiceException e) {
+                return new ResponseEntity<String>(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<String> getPolicySchema(@RequestParam(name = "id", required = true) String id) {
+        try {
+            PolicyType type = policyTypes.getType(id);
+            return new ResponseEntity<String>(type.schema(), HttpStatus.OK);
+        } catch (ServiceException e) {
+            return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
+        }
+    }
 
-        Collection<PolicyType> types = this.policyTypes.getAll();
-        return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
+    @GetMapping("/policy_types")
+    @ApiOperation(value = "Returns policy types")
+    @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")})
+    public ResponseEntity<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
+        if (ricName == null) {
+            Collection<PolicyType> types = this.policyTypes.getAll();
+            return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
+        } else {
+            try {
+                Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
+                return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
+            } catch (ServiceException e) {
+                return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
+            }
+        }
     }
 
     @GetMapping("/policy")
     @ApiOperation(value = "Returns the policy")
     @ApiResponses(
-        value = {
-            @ApiResponse(code = 200, message = "Policy found"),
-            @ApiResponse(code = 204, message = "Policy is not found")
-        })
+        value = {@ApiResponse(code = 200, message = "Policy found"),
+            @ApiResponse(code = 204, message = "Policy is not found")})
     public ResponseEntity<String> getPolicy( //
         @RequestParam(name = "instance", required = true) String instance) {
         try {
-            Policy p = policies.get(instance);
+            Policy p = policies.getPolicy(instance);
             return new ResponseEntity<String>(p.json(), HttpStatus.OK);
         } catch (ServiceException e) {
             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
@@ -101,23 +131,17 @@ public class PolicyController {
 
     @DeleteMapping("/policy")
     @ApiOperation(value = "Deletes the policy")
-    @ApiResponses(
-        value = {
-            @ApiResponse(code = 204, message = "Policy deleted")
-        })
+    @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted")})
     public ResponseEntity<Void> deletePolicy( //
         @RequestParam(name = "instance", required = true) String instance) {
 
-        Policy p = policies.removeId(instance);
+        policies.removeId(instance);
         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
     }
 
     @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<String> getPolicies( //
         @RequestParam(name = "type", required = false) String type, //
         @RequestParam(name = "ric", required = false) String ric, //
@@ -175,14 +199,25 @@ public class PolicyController {
         return gson.toJson(v);
     }
 
-    private String policyTypesToJson(Collection<PolicyType> types) {
-        Vector<PolicyTypeInfo> v = new Vector<>(types.size());
+    private String toPolicyTypeSchemasJson(Collection<PolicyType> types) {
+        StringBuilder result = new StringBuilder();
+        result.append("[");
+        boolean first = true;
         for (PolicyType t : types) {
-            PolicyTypeInfo policyInfo = ImmutablePolicyTypeInfo.builder() //
-                .schema(t.jsonSchema()) //
-                .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<PolicyType> types) {
+        Vector<String> v = new Vector<>(types.size());
+        for (PolicyType t : types) {
+            v.add(t.name());
         }
         return gson.toJson(v);
     }
@@ -193,10 +228,7 @@ public class PolicyController {
 
     @PutMapping(path = "/policy")
     @ApiOperation(value = "Create the policy")
-    @ApiResponses(
-        value = {
-            @ApiResponse(code = 201, message = "Policy created")
-        })
+    @ApiResponses(value = {@ApiResponse(code = 201, message = "Policy created")})
     public ResponseEntity<String> putPolicy( //
         @RequestParam(name = "type", required = true) String type, //
         @RequestParam(name = "instance", required = true) String instanceId, //