Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / RESTAPI.java
index f26ac19..1b3796e 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. 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.
@@ -16,8 +16,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.restcore;
 
@@ -25,6 +23,14 @@ import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.Callable;
+import java.util.concurrent.TimeoutException;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -77,7 +83,6 @@ public class RESTAPI {
         * Gets the from app id.
         *
         * @param headers the headers
-        * @param logline the logline
         * @return the from app id
         * @throws AAIException the AAI exception
         */
@@ -105,7 +110,6 @@ public class RESTAPI {
         * Gets the trans id.
         *
         * @param headers the headers
-        * @param logline the logline
         * @return the trans id
         * @throws AAIException the AAI exception
         */
@@ -194,6 +198,7 @@ public class RESTAPI {
                templateVars.add(info.getPath().toString());
                templateVars.addAll(e.getTemplateVars());
 
+               ErrorLogHelper.logException(e);
                return Response
                                .status(e.getErrorObject().getHTTPResponseCode())
                                .entity(ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers.getAcceptableMediaTypes(), e, templateVars))
@@ -206,7 +211,6 @@ public class RESTAPI {
         * @param obj the obj
         * @param loader the loader
         * @param uri the uri
-        * @param validateRequired the validate required
         * @throws AAIException the AAI exception
         * @throws UnsupportedEncodingException the unsupported encoding exception
         */
@@ -255,12 +259,15 @@ public class RESTAPI {
                        throw new AAIException("AAI_4009", "X-FromAppId is not set");
                }
 
+               DBConnectionType type = DBConnectionType.REALTIME;
                boolean isRealTimeClient = AAIConfig.get("aai.realtime.clients", "").contains(fromAppId);
                if (isRealTimeClient || realTime != null) {
-                       return DBConnectionType.REALTIME;
+                       type = DBConnectionType.REALTIME;
                } else {
-                       return DBConnectionType.CACHED;
+                       type = DBConnectionType.CACHED;
                }
+
+               return type;
        }
        
        /**
@@ -276,4 +283,116 @@ public class RESTAPI {
                
        }
 
+       /**
+        * Returns the app specific timeout in milliseconds, -1 overrides the timeout for an app
+        *
+        * @param sot
+        * @param appTimeouts
+        * @param defaultTimeout
+        * @return integer timeout in  or -1 to bypass
+        * @throws AAIException
+        */
+
+       public int getTimeoutLimit(String sot, String appTimeouts, String defaultTimeout) throws AAIException{
+               String[] ignoreAppIds = (appTimeouts).split("\\|");
+               int appLimit = Integer.parseInt(defaultTimeout);
+               final Map<String, Integer> m = new HashMap<String, Integer>();
+               if(ignoreAppIds != null) {
+                       for (int i = 0; i < ignoreAppIds.length; i++) {
+                               String[] vals = ignoreAppIds[i].split(",");
+                               m.put(vals[0], Integer.parseInt(vals[1]));
+                       }
+                       if (m.get(sot) != null) {
+                               appLimit = m.get(sot);
+                       }
+               }
+               return appLimit;
+       }
+
+       /**
+        * Returns whether time out is enabled
+        * @param sot
+        * @param isEnabled
+        * @param appTimeouts
+        * @param defaultTimeout
+        * @return boolean of whether the timeout is enabled
+        * @throws AAIException
+        */
+       public boolean isTimeoutEnabled(String sot, String isEnabled, String appTimeouts, String defaultTimeout) throws AAIException{
+               Boolean isTimeoutEnabled = Boolean.parseBoolean(isEnabled);
+               int ata = -1;
+               if(isTimeoutEnabled) {
+                       ata = getTimeoutLimit(sot, appTimeouts, defaultTimeout);
+               }
+               return isTimeoutEnabled && (ata > -1);
+       }
+
+       /**
+        * Executes the process thread and watches the future for the timeout
+        * @param handler
+        * @param sourceOfTruth
+        * @param appTimeoutLimit
+        * @param defaultTimeoutLimit
+        * @param method
+        * @param headers
+        * @param info
+        * @return the response
+        */
+
+       public Response executeProcess(Future<Response> handler, String sourceOfTruth, String appTimeoutLimit, String defaultTimeoutLimit, HttpMethod method, HttpHeaders headers, UriInfo info){
+               Response response = null;
+               int timeoutLimit = 0;
+               try {
+                       timeoutLimit = getTimeoutLimit(sourceOfTruth, appTimeoutLimit, defaultTimeoutLimit);
+                       response = handler.get(timeoutLimit, TimeUnit.MILLISECONDS);
+               } catch (TimeoutException e) {
+                       AAIException ex = new AAIException("AAI_7406", String.format("Timeout limit of %s seconds reached.", timeoutLimit/1000));
+                       response = consumerExceptionResponseGenerator(headers, info, method, ex);
+                       handler.cancel(true);
+               } catch (Exception e) {
+                       AAIException ex = new AAIException("AAI_4000", e);
+                       response = consumerExceptionResponseGenerator(headers, info, method, ex);
+               }
+               return response;
+       }
+
+       /**
+        * runner sets up the timer logic and invokes it
+        * @param toe
+        * @param tba
+        * @param tdl
+        * @param headers
+        * @param info
+        * @param httpMethod
+        * @param c
+        * @return the response
+        */
+       public Response runner(String toe, String tba, String tdl, HttpHeaders headers, UriInfo info, HttpMethod httpMethod, Callable c){
+               Response response = null;
+               Future<Response> handler = null;
+               ExecutorService executor = null;
+               try {
+                       String timeoutEnabled = AAIConfig.get(toe);
+                       String timeoutByApp = AAIConfig.get(tba);
+                       String timeoutDefaultLimit = AAIConfig.get(tdl);
+                       String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
+                       if (isTimeoutEnabled(sourceOfTruth, timeoutEnabled, timeoutByApp, timeoutDefaultLimit)) {
+                               executor = Executors.newSingleThreadExecutor();
+                               handler = executor.submit(c);
+                               response = executeProcess(handler, sourceOfTruth, timeoutByApp, timeoutDefaultLimit, httpMethod, headers, info);
+                       } else {
+                               response = (Response) c.call();
+                       }
+               }catch(Exception e){
+                       AAIException ex = new AAIException("AAI_4000", e);
+                       response = consumerExceptionResponseGenerator(headers, info, httpMethod, ex);
+               }finally{
+                       if(executor != null && handler != null){
+                               executor.shutdownNow();
+                       }
+               }
+               return response;
+       }
+
 }
+