Enhancement of AAI-traversal healthcheck
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / util / EchoResponse.java
index d0ba708..2e42e4c 100644 (file)
@@ -8,7 +8,7 @@
  * 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
+ *    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,
@@ -36,85 +36,96 @@ import javax.ws.rs.core.Response.Status;
 import org.onap.aai.exceptions.AAIException;
 import org.onap.aai.logging.ErrorLogHelper;
 import org.onap.aai.restcore.RESTAPI;
+import org.springframework.stereotype.Component;
 
 /**
  * The Class EchoResponse.
  */
 @Path("/util")
+@Component
 public class EchoResponse extends RESTAPI {
 
-    protected static String authPolicyFunctionName = "util";
-
-    public static final String ECHO_PATH = "/echo";
-
-    /**
-     * Simple health-check API that echos back the X-FromAppId and X-TransactionId to clients.
-     * If there is a query string, a transaction gets logged into hbase, proving the application is
-     * connected to the data store.
-     * If there is no query string, no transacction logging is done to hbase.
-     *
-     * @param headers the headers
-     * @param req the req
-     * @param myAction if exists will cause transaction to be logged to hbase
-     * @return the response
-     */
-    @GET
-    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
-    @Path(ECHO_PATH)
-    public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req,
-        @QueryParam("action") String myAction) {
-        AAIException ex = null;
-        Response response;
-        String fromAppId;
-        String transId;
-
-        try {
-            fromAppId = getFromAppId(headers);
-            transId = getTransId(headers);
-        } catch (AAIException e) {
-            ArrayList<String> templateVars = new ArrayList<>();
-            templateVars.add("PUT uebProvider");
-            templateVars.add("addTopic");
-            return Response
-                .status(e.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper
-                    .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars))
-                .build();
-        }
-
-        try {
-
-            HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<>();
-
-            ArrayList<String> templateVars = new ArrayList<>();
-            templateVars.add(fromAppId);
-            templateVars.add(transId);
-
-            exceptionList.put(new AAIException("AAI_0002", "OK"), templateVars);
-
-            response = Response
-                .status(Status.OK).entity(ErrorLogHelper
-                    .getRESTAPIInfoResponse(headers.getAcceptableMediaTypes(), exceptionList))
-                .build();
-
-        } catch (Exception e) {
-            ex = new AAIException("AAI_4000", e);
-            ArrayList<String> templateVars = new ArrayList<>();
-            templateVars.add(Action.GET.name());
-            templateVars.add(fromAppId + " " + transId);
-
-            response = Response
-                .status(Status.INTERNAL_SERVER_ERROR).entity(ErrorLogHelper
-                    .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars))
-                .build();
-
-        } finally {
-            if (ex != null) {
-                ErrorLogHelper.logException(ex);
-            }
-
-        }
-
-        return response;
-    }
-
+       protected static String authPolicyFunctionName = "util";
+
+       public static final String ECHO_PATH = "/echo";
+
+       private AaiGraphChecker aaiGraphChecker;
+
+       public EchoResponse(AaiGraphChecker aaiGraphChecker) {
+               this.aaiGraphChecker = aaiGraphChecker;
+       }
+
+       /**
+        * Simple health-check API that echos back the X-FromAppId and X-TransactionId
+        * to clients.
+        * If there is a query string, the healthcheck will also check for database connectivity.
+        *
+        * @param headers  the headers
+        * @param req      the request
+        * @param myAction if exists will cause database connectivity check
+        * @return the response
+        */
+       @GET
+       @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+       @Path(ECHO_PATH)
+       public Response echoResult(@Context HttpHeaders headers, @Context HttpServletRequest req,
+                       @QueryParam("action") String myAction) {
+
+               String fromAppId;
+               String transId;
+               ArrayList<String> templateVars = new ArrayList<>();
+
+               try {
+                       fromAppId = getFromAppId(headers);
+                       transId = getTransId(headers);
+               } catch (AAIException aaiException) {
+                       templateVars.add("PUT uebProvider");
+                       templateVars.add("addTopic");
+                       ErrorLogHelper.logException(aaiException);
+                       return generateFailureResponse(headers, templateVars, aaiException);
+               }
+
+               templateVars.add(fromAppId);
+               templateVars.add(transId);
+               if (myAction != null) {
+                       try {
+                               if (!aaiGraphChecker.isAaiGraphDbAvailable()) {
+                                       throw new AAIException("AAI_5105", "Error establishing a database connection");
+                               }
+                               return generateSuccessResponse(headers, templateVars);
+                       } catch (AAIException aaiException) {
+                               ErrorLogHelper.logException(aaiException);
+                               return generateFailureResponse(headers, templateVars, aaiException);
+                       } catch (Exception e) {
+                               AAIException aaiException = new AAIException("AAI_4000", e);
+                               ErrorLogHelper.logException(aaiException);
+                               return generateFailureResponse(headers, templateVars, aaiException);
+                       }
+               }
+               return generateSuccessResponse(headers, templateVars);
+       }
+
+       private Response generateSuccessResponse(HttpHeaders headers, ArrayList<String> templateVariables) {
+               HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<>();
+               exceptionList.put(new AAIException("AAI_0002", "OK"), templateVariables);
+               try {
+                       return Response.status(Status.OK)
+                                       .entity(
+                                                       ErrorLogHelper.getRESTAPIInfoResponse(headers.getAcceptableMediaTypes(), exceptionList))
+                                       .build();
+               } catch (Exception e) {
+                       AAIException aaiException = new AAIException("AAI_4000", e);
+                       ErrorLogHelper.logException(aaiException);
+                       return generateFailureResponse(headers, templateVariables, aaiException);
+               }
+       }
+
+       private Response generateFailureResponse(HttpHeaders headers, ArrayList<String> templateVariables,
+                       AAIException aaiException) {
+               return Response.status(aaiException.getErrorObject().getHTTPResponseCode())
+                               .entity(
+                                               ErrorLogHelper.getRESTAPIErrorResponseWithLogging(
+                                                               headers.getAcceptableMediaTypes(), aaiException, templateVariables))
+                               .build();
+       }
 }