Add Error Handling in A1 Client 23/2023/1
authorRehanRaza <muhammad.rehan.raza@est.tech>
Fri, 13 Dec 2019 16:05:11 +0000 (17:05 +0100)
committerRehanRaza <muhammad.rehan.raza@est.tech>
Fri, 13 Dec 2019 16:07:59 +0000 (17:07 +0100)
Change-Id: I7e24241b84f123ecffdcfe880e43935f5d8e933b
Issue-ID: NONRTRIC-80
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/clients/A1Client.java
policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java
policy-agent/src/main/java/org/oransc/policyagent/exceptions/AsyncRestClientException.java [new file with mode: 0644]

index 39fbc37..db048fb 100644 (file)
@@ -22,12 +22,11 @@ package org.oransc.policyagent.clients;
 import java.lang.invoke.MethodHandles;
 import java.util.ArrayList;
 import java.util.List;
-
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
@@ -62,6 +61,11 @@ public class A1Client {
     public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
         logger.debug("putPolicy nearRtRicUrl = {}, policyId = {}, policyString = {}", nearRtRicUrl, policyId,
             policyString);
+        try {
+            new JSONObject(policyString);
+        } catch (JSONException ex) { // invalid json
+            return Mono.error(ex);
+        }
         AsyncRestClient client = new AsyncRestClient(getBaseUrl(nearRtRicUrl));
         Mono<String> response = client.put("/policies/" + policyId, policyString);
         return response.flatMap(this::createPolicyMono);
@@ -74,32 +78,44 @@ public class A1Client {
     }
 
     private Flux<String> createPolicyTypesFlux(String policyTypesString) {
-        List<String> policyTypesList = new ArrayList<>();
-        JSONArray policyTypesArray = new JSONArray(policyTypesString);
-        for (int i = 0; i < policyTypesArray.length(); i++) {
-            policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
+        try {
+            List<String> policyTypesList = new ArrayList<>();
+            JSONArray policyTypesArray = new JSONArray(policyTypesString);
+            for (int i = 0; i < policyTypesArray.length(); i++) {
+                policyTypesList.add(policyTypesArray.getJSONObject(i).toString());
+            }
+            logger.debug("A1 client: policyTypes = {}", policyTypesList);
+            return Flux.fromIterable(policyTypesList);
+        } catch (JSONException ex) { // invalid json
+            return Flux.error(ex);
         }
-        logger.debug("A1 client: policyTypes = {}", policyTypesList);
-        return Flux.fromIterable(policyTypesList);
     }
 
     private Flux<String> createPoliciesFlux(String policiesString, String policyTypeId) {
-        List<String> policiesList = new ArrayList<>();
-        JSONArray policiesArray = new JSONArray(policiesString);
-        for (int i = 0; i < policiesArray.length(); i++) {
-            JSONObject policyObject = policiesArray.getJSONObject(i);
-            if (policyObject.get("policyTypeId").equals(policyTypeId)) {
-                policiesList.add(policyObject.toString());
+        try {
+            List<String> policiesList = new ArrayList<>();
+            JSONArray policiesArray = new JSONArray(policiesString);
+            for (int i = 0; i < policiesArray.length(); i++) {
+                JSONObject policyObject = policiesArray.getJSONObject(i);
+                if (policyObject.get("policyTypeId").equals(policyTypeId)) {
+                    policiesList.add(policyObject.toString());
+                }
             }
+            logger.debug("A1 client: policies = {}", policiesList);
+            return Flux.fromIterable(policiesList);
+        } catch (JSONException ex) { // invalid json
+            return Flux.error(ex);
         }
-        logger.debug("A1 client: policies = {}", policiesList);
-        return Flux.fromIterable(policiesList);
     }
 
     private Mono<String> createPolicyMono(String policyString) {
-        // remove white-spaces
-        policyString = policyString.replaceAll("\\s+", "");
-        logger.debug("A1 client: policy = {}", policyString);
-        return Mono.just(policyString);
+        try {
+            JSONObject policyObject = new JSONObject(policyString);
+            String policy = policyObject.toString();
+            logger.debug("A1 client: policy = {}", policy);
+            return Mono.just(policy);
+        } catch (JSONException ex) { // invalid json
+            return Mono.error(ex);
+        }
     }
 }
index c69afb2..2e6df94 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.oransc.policyagent.clients;
 
+import org.oransc.policyagent.exceptions.AsyncRestClientException;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Mono;
@@ -36,6 +38,8 @@ public class AsyncRestClient {
             .contentType(MediaType.APPLICATION_JSON) //
             .syncBody(body) //
             .retrieve() //
+            .onStatus(HttpStatus::isError,
+                response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
             .bodyToMono(String.class);
     }
 
@@ -43,6 +47,8 @@ public class AsyncRestClient {
         return client.get() //
             .uri(uri) //
             .retrieve() //
+            .onStatus(HttpStatus::isError,
+                response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
             .bodyToMono(String.class);
     }
 
@@ -50,6 +56,8 @@ public class AsyncRestClient {
         return client.delete() //
             .uri(uri) //
             .retrieve() //
+            .onStatus(HttpStatus::isError,
+                response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
             .bodyToMono(Void.class);
     }
 }
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/exceptions/AsyncRestClientException.java b/policy-agent/src/main/java/org/oransc/policyagent/exceptions/AsyncRestClientException.java
new file mode 100644 (file)
index 0000000..b5a6df3
--- /dev/null
@@ -0,0 +1,30 @@
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 Nordix Foundation
+ * %%
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.oransc.policyagent.exceptions;
+
+public class AsyncRestClientException extends Exception {
+
+    private static final long serialVersionUID = 1L;
+
+    public AsyncRestClientException(String message) {
+        super(message);
+    }
+}