Fix swagger and improve exception handling 96/127096/5
authora.sreekumar <ajith.sreekumar@bell.ca>
Mon, 14 Feb 2022 11:11:11 +0000 (11:11 +0000)
committera.sreekumar <ajith.sreekumar@bell.ca>
Tue, 1 Mar 2022 11:05:24 +0000 (11:05 +0000)
1) Swagger was getting generated in a different format
due to a serialization isue. It is fixed.
2) Improved exception handling by taking care of any exceptions
that could occur in and around database operations.
3) AAF enabling/disabling was done using spring profiles.
This is changed to using parameters, as it is more easier to configure
in an OOM helm chart deployment

Change-Id: If1bee01379ba5c4efac29822662896d8aa883fc8
Issue-ID: POLICY-3975
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
main/src/main/java/org/onap/policy/pap/main/PolicyPapApplication.java
main/src/main/java/org/onap/policy/pap/main/config/PapAafConfig.java
main/src/main/java/org/onap/policy/pap/main/exception/ControllerExceptionHandler.java [moved from main/src/main/java/org/onap/policy/pap/main/PapExceptionHandler.java with 96% similarity]
main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java [new file with mode: 0644]
main/src/main/resources/application.yaml
main/src/test/java/org/onap/policy/pap/main/rest/CommonPapRestServer.java
main/src/test/resources/config/application.yaml
packages/policy-pap-tarball/src/main/resources/etc/papParameters.yaml

index ede4012..edb6ff6 100644 (file)
@@ -22,12 +22,15 @@ package org.onap.policy.pap.main;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSerializer;
 import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
 import org.springframework.context.annotation.Bean;
+import springfox.documentation.spring.web.json.Json;
 
 @SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
 @EntityScan(
@@ -39,8 +42,17 @@ public class PolicyPapApplication {
         SpringApplication.run(PolicyPapApplication.class, args);
     }
 
+    /**
+     * Configure gson with the required adapters to be used in the application.
+     *
+     * @return the gson bean
+     */
     @Bean
     public Gson gson() {
-        return GsonMessageBodyHandler.configBuilder(new GsonBuilder()).create();
+        GsonBuilder gsonBuilder = GsonMessageBodyHandler.configBuilder(new GsonBuilder());
+        JsonSerializer<Json> serializer =
+            (json, type, jsonSerializationContext) -> JsonParser.parseString(json.value());
+        gsonBuilder.registerTypeAdapter(Json.class, serializer);
+        return gsonBuilder.create();
     }
 }
index d73a37d..4bbf29f 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Bell Canada. All rights reserved.
+ *  Copyright (C) 2021-2022 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,12 +22,12 @@ package org.onap.policy.pap.main.config;
 
 import javax.servlet.Filter;
 import org.onap.policy.pap.main.rest.PapAafFilter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Profile;
 
 @Configuration
-@Profile(value = {"aaf-auth"})
+@ConditionalOnProperty(value = "pap.aaf", havingValue = "true")
 public class PapAafConfig {
 
     /**
@@ -18,7 +18,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.policy.pap.main;
+package org.onap.policy.pap.main.exception;
 
 import java.util.UUID;
 import org.onap.policy.models.base.PfModelException;
@@ -34,9 +34,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.context.request.WebRequest;
 
 @RestControllerAdvice
-public class PapExceptionHandler {
+public class ControllerExceptionHandler {
 
-    private static final Logger logger = LoggerFactory.getLogger(PapExceptionHandler.class);
+    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);
 
     /**
      * Handle PfModelException.
diff --git a/main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java b/main/src/main/java/org/onap/policy/pap/main/exception/ServiceExceptionHandler.java
new file mode 100644 (file)
index 0000000..8088796
--- /dev/null
@@ -0,0 +1,64 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Bell Canada. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.exception;
+
+import javax.ws.rs.core.Response;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.AfterThrowing;
+import org.aspectj.lang.annotation.Aspect;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.TransactionException;
+
+@Aspect
+@Component
+public class ServiceExceptionHandler {
+
+    /**
+     * Handle any exceptions that are not already handled.
+     * For e.g., runtime exceptions that could happen during SQL query execution related to data integrity etc.
+     *
+     * @param joinPoint the point of execution
+     * @param exception the exception
+     */
+    @AfterThrowing(pointcut = "execution(* org.onap.policy.pap.main.service.*.*(..))", throwing = "exception")
+    public void handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
+        if (exception instanceof PfModelRuntimeException) {
+            throw (PfModelRuntimeException) exception;
+        } else {
+            throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), exception);
+        }
+    }
+
+    /**
+     * Handle DB Transaction related exceptions.
+     * All service classes in org.onap.policy.pap.main.service are transactional now,
+     * Autowiring these service classes can cause TransactionException.
+     * For e.g., JDBC connection failure occurs and failed to open transaction at service level
+     *
+     * @param joinPoint the point of execution
+     * @param exception the exception
+     */
+    @AfterThrowing(pointcut = "execution(* org.onap.policy.pap.main.*.*.*(..))", throwing = "exception")
+    public void handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
+        throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), exception);
+    }
+}
index 2b6d8fc..ebe94b8 100644 (file)
@@ -26,6 +26,7 @@ server:
 
 pap:
   name: PapGroup
+  aaf: false
   pdpParameters:
     heartBeatMs: 120000
     updateParameters:
index c2d9f03..fd4bb74 100644 (file)
@@ -51,11 +51,11 @@ import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.common.utils.security.SelfSignedKeyStore;
 import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.PolicyPapApplication;
 import org.onap.policy.pap.main.parameters.CommonTestData;
 import org.onap.policy.pap.main.startstop.PapActivator;
 import org.powermock.reflect.Whitebox;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.web.server.LocalServerPort;
 import org.springframework.test.annotation.DirtiesContext;
@@ -91,7 +91,6 @@ public abstract class CommonPapRestServer {
     @LocalServerPort
     private int port;
 
-    @Autowired
     private PapActivator papActivator;
 
     /**
@@ -126,6 +125,7 @@ public abstract class CommonPapRestServer {
     @Before
     public void setUp() throws Exception {
         httpsPrefix = "https://localhost:" + port + "/";
+        papActivator = Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class);
         activatorWasAlive = papActivator.isAlive();
     }
 
index b6a497a..2fb9514 100644 (file)
@@ -24,6 +24,7 @@ server:
 
 pap:
   name: "PapGroup"
+  aaf: false
   pdpParameters:
     updateParameters:
       maxRetryCount: 1