Update to support PMS_v2
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / policyagentapi / PolicyAgentApiImpl.java
index 20e72bb..f8b7c3e 100644 (file)
@@ -33,24 +33,20 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
-import javax.net.ssl.SSLException;
-
 import org.immutables.gson.Gson;
 import org.immutables.value.Value;
 import org.oransc.portal.nonrtric.controlpanel.model.ImmutablePolicyInfo;
 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInfo;
-import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances;
 import org.oransc.portal.nonrtric.controlpanel.model.PolicyType;
 import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes;
 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
+import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
 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;
 import org.springframework.stereotype.Component;
-import org.springframework.web.client.HttpClientErrorException;
-import org.springframework.web.client.HttpServerErrorException;
 
 @Component("PolicyAgentApi")
 public class PolicyAgentApiImpl implements PolicyAgentApi {
@@ -76,90 +72,119 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
     @Override
     public ResponseEntity<String> getAllPolicyTypes() {
         try {
-            final String url = "/policy_schemas";
+            final String url = "/v2/policy-types";
             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
             if (!rsp.getStatusCode().is2xxSuccessful()) {
                 return rsp;
             }
 
             PolicyTypes result = new PolicyTypes();
+            JsonArray policyTypeIds = JsonParser.parseString(rsp.getBody()).getAsJsonObject() //
+                .get("policytype_ids") //
+                .getAsJsonArray(); //
+
+            for (JsonElement policyTypeId : policyTypeIds) {
 
-            JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray();
-            for (JsonElement schema : schemas) {
-                JsonObject schemaObj = schema.getAsJsonObject();
-                if (schemaObj.get("title") != null) {
-                    String title = schemaObj.get("title").getAsString();
-                    String schemaAsStr = schemaObj.toString();
-                    PolicyType pt = new PolicyType(title, schemaAsStr);
-                    result.add(pt);
-                } else {
-                    logger.warn("Ignoring schema: {}", schemaObj);
-                }
+                String typeId = policyTypeId.getAsString();
+
+                JsonObject schemaObj = getIndividualPolicySchema(typeId);
+                PolicyType pt = new PolicyType(typeId, schemaObj.toString());
+                result.add(pt);
             }
             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
         } catch (Exception e) {
-            return handleException(e);
+            return ErrorResponseHandler.handleException(e);
+        }
+    }
+
+    public JsonObject getIndividualPolicySchema(String id) {
+        try {
+            final String url = "/v2/policy-types/" + id;
+            ResponseEntity<String> rsp = webClient.getForEntity(url).block();
+            if (!rsp.getStatusCode().is2xxSuccessful()) {
+                return null;
+            }
+
+            JsonObject policy_schema = JsonParser.parseString(rsp.getBody()).getAsJsonObject() //
+                .get("policy_schema") //
+                .getAsJsonObject(); //
+
+            return policy_schema;
+        } catch (Exception e) {
+            return null;
         }
     }
 
     @Override
     public ResponseEntity<String> getPolicyInstancesForType(String type) {
         try {
-            String url = "/policies?type=" + type;
+            String url = "/v2/policies?policytype_id=" + type;
             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
             if (!rsp.getStatusCode().is2xxSuccessful()) {
                 return rsp;
             }
+            JsonArray policyInstances = JsonParser.parseString(rsp.getBody()).getAsJsonObject() //
+                .get("policy_ids") //
+                .getAsJsonArray(); //
 
             Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
-            List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
-            PolicyInstances result = new PolicyInstances();
-            for (PolicyInfo p : rspParsed) {
-                result.add(p);
-            }
-            return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
+            List<PolicyInfo> rspParsed = gson.fromJson(policyInstances, listType);
+
+            return new ResponseEntity<>(gson.toJson(rspParsed), rsp.getStatusCode());
         } catch (Exception e) {
-            return handleException(e);
+            return ErrorResponseHandler.handleException(e);
         }
     }
 
     @Override
     public ResponseEntity<Object> getPolicyInstance(String id) {
         try {
-            String url = "/policy?id=" + id;
+            String url = "/v2/policies/" + id;
             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
             JsonObject obj = JsonParser.parseString(rsp.getBody()).getAsJsonObject();
             String str = obj.toString();
             return new ResponseEntity<>(str, rsp.getStatusCode());
         } catch (Exception e) {
-            ResponseEntity<String> rsp = handleException(e);
+            ResponseEntity<String> rsp = ErrorResponseHandler.handleException(e);
             return new ResponseEntity<>(rsp.getBody(), rsp.getStatusCode());
         }
     }
 
+    private String getTimeStampUTC() {
+        return java.time.Instant.now().toString();
+    }
+
     @Override
     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
         String ric) {
-        String url =
-            "/policy?type=" + policyTypeIdString + "&id=" + policyInstanceId + "&ric=" + ric + "&service=controlpanel";
+        String url = "/v2/policies/";
+
+        PolicyInfo i = ImmutablePolicyInfo.builder() //
+            .id(policyInstanceId) //
+            .type(policyTypeIdString) //
+            .ric(ric) //
+            .json(json) //
+            .service("controlpanel") //
+            .lastModified(getTimeStampUTC()) //
+            .build(); //
 
         try {
-            String jsonStr = json.toString();
+            String jsonStr = gson.toJson(i, PolicyInfo.class);
             webClient.putForEntity(url, jsonStr).block();
             return new ResponseEntity<>(HttpStatus.OK);
         } catch (Exception e) {
-            return handleException(e);
+            return ErrorResponseHandler.handleException(e);
         }
     }
 
     @Override
     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
-        String url = "/policy?id=" + policyInstanceId;
+        String url = "/v2/policies/" + policyInstanceId;
         try {
             webClient.deleteForEntity(url).block();
             return new ResponseEntity<>(HttpStatus.OK);
         } catch (Exception e) {
-            return handleException(e);
+            return ErrorResponseHandler.handleException(e);
         }
     }
 
@@ -176,11 +201,18 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
     @Override
     public ResponseEntity<String> getRicsSupportingType(String typeName) {
         try {
-            String url = "/rics?policyType=" + typeName;
+            String url = "/v2/rics?policytype_id=" + typeName;
             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
+            if (!rsp.getStatusCode().is2xxSuccessful()) {
+                return rsp;
+            }
+
+            JsonArray rics = JsonParser.parseString(rsp.getBody()).getAsJsonObject() //
+                .get("rics") //
+                .getAsJsonArray(); //
 
             Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
-            List<RicInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
+            List<RicInfo> rspParsed = gson.fromJson(rics, listType);
             Collection<String> result = new ArrayList<>(rspParsed.size());
             for (RicInfo ric : rspParsed) {
                 result.add(ric.ricName());
@@ -188,22 +220,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             String json = gson.toJson(result);
             return new ResponseEntity<>(json, HttpStatus.OK);
         } catch (Exception e) {
-            return handleException(e);
-        }
-    }
-
-    private ResponseEntity<String> handleException(Exception throwable) {
-        if (throwable instanceof HttpClientErrorException) {
-            HttpClientErrorException e = (HttpClientErrorException) throwable;
-            return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
-        } else if (throwable instanceof HttpServerErrorException) {
-            HttpServerErrorException e = (HttpServerErrorException) throwable;
-            return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
-        } else if (throwable instanceof SSLException) {
-            SSLException e = (SSLException) throwable;
-            return new ResponseEntity<>("Could not create WebClient " + e.getMessage(),
-                HttpStatus.INTERNAL_SERVER_ERROR);
+            return ErrorResponseHandler.handleException(e);
         }
-        return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
     }
 }