SO catalogDB updates
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / AbstractServiceTaskProcessor.groovy
index 0692e50..ec3dac5 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -20,8 +22,6 @@
 
 package org.onap.so.bpmn.common.scripts;
 
-import groovy.json.JsonSlurper
-
 import org.camunda.bpm.engine.delegate.BpmnError
 import org.camunda.bpm.engine.delegate.DelegateExecution
 import org.camunda.bpm.engine.variable.VariableMap
@@ -30,98 +30,20 @@ import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats
 import org.camunda.bpm.engine.variable.impl.value.ObjectValueImpl
 import org.onap.so.bpmn.common.workflow.context.WorkflowCallbackResponse
 import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder
-import org.onap.so.bpmn.core.WorkflowException
 import org.onap.so.bpmn.core.UrnPropertiesReader
+import org.onap.so.bpmn.core.WorkflowException
+import org.onap.so.client.HttpClientFactory
+import org.onap.aaiclient.client.aai.AAIResourcesClient
 import org.springframework.web.util.UriUtils
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
 
-public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcessor {
-       public MsoUtils utils = new MsoUtils()
-
-
-       /**
-        * Logs a message at the ERROR level.
-        * @param message the message
-        */
-       public void logError(String message) {
-               log('ERROR', message, null, "true")
-       }
-
-       /**
-        * Logs a message at the ERROR level.
-        * @param message the message
-        * @param cause the cause (stracktrace will be included in the output)
-        */
-       public void logError(String message, Throwable cause) {
-               log('ERROR', message, cause, "true")
-       }
-
-       /**
-        * Logs a message at the WARN level.
-        * @param message the message
-        */
-       public void logWarn(String message) {
-               log('WARN', message, null, "true")
-       }
-
-       /**
-        * Logs a message at the WARN level.
-        * @param message the message
-        * @param cause the cause (stracktrace will be included in the output)
-        */
-       public void logWarn(String message, Throwable cause) {
-               log('WARN', message, cause, "true")
-       }
-
-       /**
-        * Logs a message at the DEBUG level.
-        * @param message the message
-        * @param isDebugLogEnabled a flag indicating if DEBUG level is enabled
-        */
-       public void logDebug(String message, String isDebugLogEnabled) {
-               log('DEBUG', message, null, isDebugLogEnabled)
-       }
-
-       /**
-        * Logs a message at the DEBUG level.
-        * @param message the message
-        * @param cause the cause (stracktrace will be included in the output)
-        * @param isDebugLogEnabled a flag indicating if DEBUG level is enabled
-        */
-       public void logDebug(String message, Throwable cause, String isDebugLogEnabled) {
-               log('DEBUG', message, cause, isDebugLogEnabled)
-       }
+import groovy.json.JsonSlurper
 
-       /**
-        * Logs a message at the specified level.
-        * @param level the level (DEBUG, INFO, WARN, ERROR)
-        * @param message the message
-        * @param isLevelEnabled a flag indicating if the level is enabled
-        *        (used only at the DEBUG level)
-        */
-       public void log(String level, String message, String isLevelEnabled) {
-               log(level, message,  null, isLevelEnabled)
-       }
+public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcessor {
+       private static final Logger logger = LoggerFactory.getLogger( MsoUtils.class);
 
-       /**
-        * Logs a message at the specified level.
-        * @param level the level (DEBUG, INFO, WARN, ERROR)
-        * @param message the message
-        * @param cause the cause (stracktrace will be included in the output)
-        * @param isLevelEnabled a flag indicating if the level is enabled
-        *        (used only at the DEBUG level)
-        */
-       public void log(String level, String message, Throwable cause, String isLevelEnabled) {
-               if (cause == null) {
-                       utils.log(level, message, isLevelEnabled);
-               } else {
-                       StringWriter stringWriter = new StringWriter();
-                       PrintWriter printWriter = new PrintWriter(stringWriter);
-                       printWriter.println(message);
-                       cause.printStackTrace(printWriter);
-                       utils.log(level, stringWriter.toString(), isLevelEnabled);
-                       printWriter.close();
-               }
-       }
+       public MsoUtils utils = new MsoUtils()
 
        /**
         * Logs a WorkflowException at the ERROR level with the specified message.
@@ -131,12 +53,12 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                def workflowException = execution.getVariable("WorkflowException")
 
                if (workflowException == null) {
-                       logError(message);
+                       logger.error(message);
                } else {
-                       logError(message + ": " + workflowException)
+                       logger.error('{}: {}', message, workflowException)
                }
        }
-
+       
        /**
         * Saves the WorkflowException in the execution to the specified variable,
         * clearing the WorkflowException variable so the workflow can continue
@@ -172,7 +94,7 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                        ', requredVariables=' + requiredVariables +
                        ')'
                def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
-               logDebug('Entered ' + method, isDebugLogEnabled)
+               logger.debug('Entered ' + method)
 
                String processKey = getProcessKey(execution)
                def prefix = execution.getVariable("prefix")
@@ -235,14 +157,13 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                                serviceInstanceId = (String) execution.getVariable("mso-service-instance-id")
                        }
 
-                       utils.logContext(requestId, serviceInstanceId)
-                       logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
-                       logDebug('Exited ' + method, isDebugLogEnabled)
+                       logger.debug('Incoming message: ' + System.lineSeparator() + request)
+                       logger.debug('Exited ' + method)
                        return request
                } catch (BpmnError e) {
                        throw e
                } catch (Exception e) {
-                       logError('Caught exception in ' + method, e)
+                       logger.error('Caught exception in {}: {}', method, e.getMessage(), e)
                        exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
                }
        }
@@ -258,18 +179,11 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                                'execution=' + execution.getId() +
                                ')'
                def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
-               logDebug('Entered ' + method, isDebugLogEnabled)
+               logger.debug('Entered ' + method)
 
                String processKey = getProcessKey(execution);
                def prefix = execution.getVariable("prefix")
 
-               def requestId =getVariable(execution, "mso-request-id")
-               def serviceInstanceId = getVariable(execution, "mso-service-instance-id")
-               if(requestId!=null && serviceInstanceId!=null){
-                       utils.logContext(requestId, serviceInstanceId)
-               }
-
-
                def request = getVariable(execution, prefix + 'Request')
 
                if (request == null) {
@@ -285,8 +199,8 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                def parsed = jsonSlurper.parseText(request)
 
 
-               logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
-               logDebug('Exited ' + method, isDebugLogEnabled)
+               logger.debug('Incoming message: ' + System.lineSeparator() + request)
+               logger.debug('Exited ' + method)
                return parsed
 
        }
@@ -316,10 +230,10 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                        }
 
                        if (String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true")) {
-                                       logDebug("Sync response has already been sent for " + processKey, isDebugLogEnabled)
+                                       logger.debug("Sync response has already been sent for " + processKey)
                        }else{
 
-                               logDebug("Building " + processKey + " response ", isDebugLogEnabled)
+                               logger.debug("Building " + processKey + " response ")
 
                                int intResponseCode;
 
@@ -344,11 +258,10 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                                execution.setVariable(processKey + "Status", status);
                                execution.setVariable("WorkflowResponse", response)
 
-                               logDebug("Sending response for " + processKey
+                               logger.debug("Sending response for " + processKey
                                        + " ResponseCode=" + intResponseCode
                                        + " Status=" + status
-                                       + " Response=\n" + response,
-                                       isDebugLogEnabled)
+                                       + " Response=\n" + response)
 
                                // TODO: ensure that this flow was invoked asynchronously?
 
@@ -369,7 +282,7 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                        }
 
                } catch (Exception ex) {
-                       logError("Unable to send workflow response to client ....", ex)
+                       logger.error("Unable to send workflow response to client ....", ex)
                }
        }
 
@@ -386,7 +299,7 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
        /**
         * Returns the process definition key (i.e. the process name) of the
         * current process.
-        * 
+        *
         * @param execution the execution
         */
        public String getProcessKey(DelegateExecution execution) {
@@ -439,7 +352,7 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                def element = utils.getNodeXml(xml, elementName, false)
                if (element.trim().isEmpty()) {
                        def msg = 'Required element \'' + elementName + '\' is missing or empty'
-                       logError(msg)
+                       logger.error(msg)
                        exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
                } else {
                        return element
@@ -461,7 +374,7 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                def elementText = utils.getNodeText(xml, elementName)
                if ((elementText == null) || (elementText.isEmpty())) {
                        def msg = 'Required element \'' + elementName + '\' is missing or empty'
-                       logError(msg)
+                       logger.error(msg)
                        exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
                } else {
                        return elementText
@@ -518,7 +431,7 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
        *@param name
        *@return
        **/
-       public String getVariable(DelegateExecution execution, String name) {
+       public static String getVariable(DelegateExecution execution, String name) {
                def myObj = execution.getVariable(name)
                if(myObj instanceof VariableMap){
                        VariableMap serializedObjectMap = (VariableMap) myObj
@@ -576,9 +489,9 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                String prefix = execution.getVariable('prefix')
                def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
 
-               logDebug('Entered SetSuccessIndicator Method', isDebugLogEnabled)
+               logger.debug('Entered SetSuccessIndicator Method')
                execution.setVariable(prefix+'SuccessIndicator', isSuccess)
-               logDebug('Outgoing SuccessIndicator is: ' + execution.getVariable(prefix+'SuccessIndicator') + '', isDebugLogEnabled)
+               logger.debug('Outgoing SuccessIndicator is: ' + execution.getVariable(prefix+'SuccessIndicator') + '')
        }
 
        /**
@@ -586,14 +499,14 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
         *
         */
        public void sendSyncError(DelegateExecution execution) {
-               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
+               def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
                String requestId = execution.getVariable("mso-request-id")
-               logDebug('sendSyncError, requestId: ' + requestId, isDebugEnabled)
+               logger.debug('sendSyncError, requestId: ' + requestId)
                WorkflowException workflowExceptionObj = execution.getVariable("WorkflowException")
                if (workflowExceptionObj != null) {
                        String errorMessage = workflowExceptionObj.getErrorMessage()
                        def errorCode = workflowExceptionObj.getErrorCode()
-                       logDebug('sendSyncError, requestId: '  + requestId + ' | errorMessage: ' + errorMessage + ' | errorCode: ' + errorCode, isDebugEnabled)
+                       logger.debug('sendSyncError, requestId: '  + requestId + ' | errorMessage: ' + errorMessage + ' | errorCode: ' + errorCode)
                        sendWorkflowResponse(execution, errorCode, errorMessage)
                }
        }
@@ -609,27 +522,26 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                        DelegateExecution execution = (DelegateExecution) args[0]
 
                        def classAndMethod = getClass().getSimpleName() + '.' + methodName + '(execution=' + execution.getId() + ')'
-                       def isDebugEnabled =  execution.getVariable('isDebugLogEnabled')
+                       def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
 
-                       logDebug('Entered ' + classAndMethod, isDebugEnabled)
-                       logDebug('Received parameters: ' + args, isDebugEnabled)
+                       logger.debug('Entered ' + classAndMethod)
+                       logger.debug('Received parameters: ' + args)
 
                        try{
                                def methodToCall = this.metaClass.getMetaMethod(methodName, args)
-                               logDebug('Method to call: ' + methodToCall, isDebugEnabled)
+                               logger.debug('Method to call: ' + methodToCall)
                                methodToCall?.invoke(this, args)
                        }
                        catch(BpmnError bpmnError) {
-                               logDebug('Rethrowing BpmnError ' + bpmnError.getMessage(), isDebugEnabled)
+                               logger.debug('Rethrowing BpmnError ' + bpmnError.getMessage())
                                throw bpmnError
                        }
                        catch(Exception e) {
-                               e.printStackTrace()
-                               logDebug('Unexpected error encountered - ' + e.getMessage(), isDebugEnabled)
+                               logger.debug('Unexpected error encountered - {}', e.getMessage(), e)
                                (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, e.getMessage())
                        }
                        finally {
-                               logDebug('Exited ' + classAndMethod, isDebugEnabled)
+                               logger.debug('Exited ' + classAndMethod)
                        }
                }
        }
@@ -717,24 +629,24 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                        '/' + UriUtils.encodePathSegment(messageType, 'UTF-8') +
                        '/' + UriUtils.encodePathSegment(correlator, 'UTF-8')
        }
-       
+
        public void setRollbackEnabled(DelegateExecution execution, isDebugLogEnabled) {
-               
+
                // Rollback settings
                def prefix = execution.getVariable('prefix')
                def disableRollback = execution.getVariable("disableRollback")
                def defaultRollback = UrnPropertiesReader.getVariable("mso.rollback", execution).toBoolean()
-               
-               logDebug('disableRollback: ' + disableRollback, isDebugLogEnabled)
-               logDebug('defaultRollback: ' + defaultRollback, isDebugLogEnabled)
-               
+
+               logger.debug('disableRollback: ' + disableRollback)
+               logger.debug('defaultRollback: ' + defaultRollback)
+
                def rollbackEnabled
-               
+
                if(disableRollback == null || disableRollback == '' ) {
                        // get from default urn settings for mso_rollback
                        disableRollback = !defaultRollback
                        rollbackEnabled = defaultRollback
-                       logDebug('disableRollback is null or empty!', isDebugLogEnabled)
+                       logger.debug('disableRollback is null or empty!')
                }
                else {
                        if(disableRollback == true) {
@@ -747,11 +659,11 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                                rollbackEnabled = defaultRollback
                        }
                }
-               
+
                execution.setVariable(prefix+"backoutOnFailure", rollbackEnabled)
-               logDebug('rollbackEnabled (aka backoutOnFailure): ' + rollbackEnabled, isDebugLogEnabled)
+               logger.debug('rollbackEnabled (aka backoutOnFailure): ' + rollbackEnabled)
        }
-       
+
        public void setBasicDBAuthHeader(DelegateExecution execution, isDebugLogEnabled) {
                try {
                        String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
@@ -759,8 +671,15 @@ public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcess
                        execution.setVariable("BasicAuthHeaderValueDB",encodedString)
                } catch (IOException ex) {
                        String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
-                       utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)
+                       logger.debug(dataErrorMessage)
                        (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
                }
        }
-}
\ No newline at end of file
+    public AAIResourcesClient getAAIClient(){
+        return  new AAIResourcesClient();
+    }
+
+    HttpClientFactory getHttpClientFactory(){
+        return new HttpClientFactory()
+    }
+}