Reflect changes on Airflow sensor/operator and essence
Fix spontaneous failures of test cases due to delayed client disconnection
Rename event '*.notify_*' to '*.report_*'
Add a new function to report status of workflow runs
Bump up version

Change-Id: I4fe25ec504751c6ea7a196c56ee4d157bab35abd
diff --git a/src/controllers/eventrouter.js b/src/controllers/eventrouter.js
index 975db2e..4919669 100644
--- a/src/controllers/eventrouter.js
+++ b/src/controllers/eventrouter.js
@@ -179,6 +179,18 @@
         return true;
     };
 
+    const setWorkflowRunState = (workflowRunId, state) => {
+        if(!(workflowRunId in workflowRuns)) {
+            logger.log('warn', `cannot find a workflow run with the id - ${workflowRunId}`);
+            return false;
+        }
+
+        if(state in ['success', 'failed', 'end']) {
+            removeWorkflowRun(workflowRunId);
+        }
+        return true;
+    };
+
     const kickstart = (workflowId, workflowRunId) => {
         if(!(workflowId in workflows)) {
             logger.log('warn', `cannot find a workflow with the id - ${workflowId}`);
@@ -191,6 +203,7 @@
         }
 
         ws_manager.kickstartWorkflow(workflowId, workflowRunId);
+        return true;
     };
 
     const removeWorkflow = (workflowId) => {
@@ -608,5 +621,6 @@
         clearWorkflowRuns: clearWorkflowRuns,
         updateWorkflowRunStatus: updateWorkflowRunStatus,
         setWorkflowRunKickstarted: setWorkflowRunKickstarted,
+        setWorkflowRunState: setWorkflowRunState
     };
 })();
diff --git a/src/controllers/ws_manager.js b/src/controllers/ws_manager.js
index 3d81bac..752ee88 100644
--- a/src/controllers/ws_manager.js
+++ b/src/controllers/ws_manager.js
@@ -31,9 +31,11 @@
         WORKFLOW_CHECK: 'cord.workflow.ctlsvc.workflow.check',
         WORKFLOW_REMOVE: 'cord.workflow.ctlsvc.workflow.remove',
         WORKFLOW_REMOVE_RUN: 'cord.workflow.ctlsvc.workflow.run.remove',
-        WORKFLOW_NOTIFY_NEW_RUN: 'cord.workflow.ctlsvc.workflow.notify_new_run',
+        WORKFLOW_REPORT_NEW_RUN: 'cord.workflow.ctlsvc.workflow.report_new_run',
+        WORKFLOW_REPORT_RUN_STATE: 'cord.workflow.ctlsvc.workflow.report_run_state',
         // controller -> manager
-        WORKFLOW_KICKSTART: 'cord.workflow.ctlsvc.workflow.kickstart'
+        WORKFLOW_KICKSTART: 'cord.workflow.ctlsvc.workflow.kickstart',
+        WORKFLOW_CHECK_STATE: 'cord.workflow.ctlsvc.workflow.check.state'
     };
 
     // WebSocket interface for workflow registration
@@ -233,7 +235,7 @@
         let result = eventrouter.removeWorkflow(workflowId);
         cb(null, result);
         return;
-    }
+    };
 
     // WebSocket interface for workflow run removal
     // Message format:
@@ -278,19 +280,19 @@
         let result = eventrouter.removeWorkflowRun(workflowRunId);
         cb(null, result);
         return;
-    }
+    };
 
-    // WebSocket interface for notifying a new workflow run
+    // WebSocket interface for reporting a new workflow run
     // Message format:
     // {
-    //     topic: 'cord.workflow.ctlsvc.workflow.notify_new_run',
+    //     topic: 'cord.workflow.ctlsvc.workflow.report_new_run',
     //     message: {
     //         req_id: <req_id> // optional
     //         workflow_id: <workflow_id>,
     //         workflow_run_id: <workflow_run_id>
     //     }
     // }
-    const notifyNewWorkflowRun = (topic, message, cb) => {
+    const reportNewWorkflowRun = (topic, message, cb) => {
         const eventrouter = require('./eventrouter.js');
 
         let errorMessage;
@@ -325,6 +327,63 @@
         let result = eventrouter.setWorkflowRunKickstarted(workflowRunId);
         cb(null, result);
         return;
+    };
+
+    // WebSocket interface for reporting workflow run state
+    // Message format:
+    // {
+    //     topic: 'cord.workflow.ctlsvc.workflow.report_run_state',
+    //     message: {
+    //         req_id: <req_id> // optional
+    //         workflow_id: <workflow_id>,
+    //         workflow_run_id: <workflow_run_id>,
+    //         state: one of ['success', 'running', 'failed', 'unknown']
+    //     }
+    // }
+    const reportWorkflowRunState = (topic, message, cb) => {
+        const eventrouter = require('./eventrouter.js');
+
+        let errorMessage;
+        if(!message) {
+            // error
+            errorMessage = `Message body for topic ${topic} is null or empty`;
+            logger.log('warn', `Return error - ${errorMessage}`);
+            cb(errorMessage, false);
+            return;
+        }
+
+        if(!('workflow_id' in message)) {
+            // error
+            errorMessage = `field 'workflow_id' does not exist in message body - ${JSON.stringify(message)}`;
+            logger.log('warn', `Return error - ${errorMessage}`);
+            cb(errorMessage, false);
+            return;
+        }
+
+        if(!('workflow_run_id' in message)) {
+            // error
+            errorMessage = `field 'workflow_run_id' does not exist in message body - ${JSON.stringify(message)}`;
+            logger.log('warn', `Return error - ${errorMessage}`);
+            cb(errorMessage, false);
+            return;
+        }
+
+        if(!('state' in message)) {
+            // error
+            errorMessage = `field 'state' does not exist in message body - ${JSON.stringify(message)}`;
+            logger.log('warn', `Return error - ${errorMessage}`);
+            cb(errorMessage, false);
+            return;
+        }
+
+        let workflowRunId = message.workflow_run_id;
+        let state = message.state;
+
+        // there must be a workflow matching
+        // set workflow state
+        let result = eventrouter.setWorkflowRunState(workflowRunId, state);
+        cb(null, result);
+        return;
     }
 
     const getRouter = () => {
@@ -357,9 +416,13 @@
                 topic: serviceEvents.WORKFLOW_REMOVE_RUN,
                 handler: removeWorkflowRun
             },
-            notifyNewWorkflowRun: {
-                topic: serviceEvents.WORKFLOW_NOTIFY_NEW_RUN,
-                handler: notifyNewWorkflowRun
+            reportNewWorkflowRun: {
+                topic: serviceEvents.WORKFLOW_REPORT_NEW_RUN,
+                handler: reportNewWorkflowRun
+            },
+            reportWorkflowRunState: {
+                topic: serviceEvents.WORKFLOW_REPORT_RUN_STATE,
+                handler: reportWorkflowRunState
             }
         };
     };
@@ -381,9 +444,26 @@
         return;
     };
 
+    const checkWorkflowState = (workflowId, workflowRunId) => {
+        const eventrouter = require('./eventrouter.js');
+
+        let clients = eventrouter.getWorkflowManagerClients();
+        _.forOwn(clients, (client, _clientId) => {
+            let socket = client.getSocket();
+            if(socket) {
+                socket.emit(serviceEvents.WORKFLOW_CHECK_STATE, {
+                    workflow_id: workflowId,
+                    workflow_run_id: workflowRunId
+                });
+            }
+        });
+        return;
+    };
+
     module.exports = {
         serviceEvents: serviceEvents,
         getRouter: getRouter,
-        kickstartWorkflow: kickstartWorkflow
+        kickstartWorkflow: kickstartWorkflow,
+        checkWorkflowState: checkWorkflowState
     };
 })();
diff --git a/src/types/workflowtask.js b/src/types/workflowtask.js
index 8e18700..9216a92 100644
--- a/src/types/workflowtask.js
+++ b/src/types/workflowtask.js
@@ -21,7 +21,6 @@
     const logger = require('../config/logger.js');
 
     const CORD_SENSORS = [
-        'XOSEventSensor', 'XOSModelSensor',
         'CORDEventSensor', 'CORDModelSensor'
     ];
 
@@ -133,7 +132,7 @@
                 return false;
             }
 
-            // general Airflow operators other than XOS operators don't have these fields.
+            // general Airflow operators other than XOS/CORD operators don't have these fields.
             //
             // if(!this.topic) {
             //     logger.log('error', 'topic is not given');
diff --git a/src/workflows/hello_workflow.json b/src/workflows/hello_workflow.json
index 9de71bc..4d86250 100644
--- a/src/workflows/hello_workflow.json
+++ b/src/workflows/hello_workflow.json
@@ -9,14 +9,13 @@
         },
         "tasks": {
             "onu_event_handler": {
-                "class": "XOSEventSensor",
+                "class": "CORDEventSensor",
+                "controller_conn_id": "local_cord_controller",
                 "dag_id": "hello_workflow",
                 "dag": "dag_hello",
                 "key_field": "serialNumber",
                 "local_variable": "onu_event_handler",
                 "poke_interval": 5,
-                "provide_context": true,
-                "python_callable": "ONU_event",
                 "task_id": "onu_event_handler",
                 "topic": "onu.events"
             }