2.3.0 policy-handler - periodically catch_up

- periodically catchup - interval is configurable
     = max_skips defines the number of times the catch_up
        message that is identical to previous one can be skipped
- do not catchup more often than the interval
    even between the manual catchup and auto catchup
- do not send the same catchup message twice in a row
   to the deployment-handler but not exceed a hard limit
   on catchup max_skips
- catchup if the deployment-handler instance is changed

Change-Id: I9a3fcc941e8a9e553abb3952dd882c37e0f5fdfe
Signed-off-by: Alex Shatov <alexs@att.com>
Issue-ID: DCAEGEN2-389
diff --git a/policyhandler/deploy_handler.py b/policyhandler/deploy_handler.py
index 0950dfd..493b7d5 100644
--- a/policyhandler/deploy_handler.py
+++ b/policyhandler/deploy_handler.py
@@ -41,6 +41,7 @@
     _url_path = None
     _target_entity = None
     _custom_kwargs = None
+    _server_instance_uuid = None
 
     @staticmethod
     def _lazy_init(audit):
@@ -72,7 +73,11 @@
 
     @staticmethod
     def policy_update(audit, message):
-        """post policy_updated message to deploy-handler"""
+        """
+        post policy_updated message to deploy-handler
+
+        returns condition whether it needs to catch_up
+        """
         if not message:
             return
 
@@ -116,8 +121,24 @@
         sub_aud.set_http_status_code(res.status_code)
         audit.set_http_status_code(res.status_code)
 
-        sub_aud.metrics("response {0} from {1}: text={2}{3}" \
-            .format(res.status_code, log_action, res.text, log_data))
+        log_line = "response {0} from {1}: text={2}{3}" \
+            .format(res.status_code, log_action, res.text, log_data)
+        sub_aud.metrics(log_line)
+        DeployHandler._logger.info(log_line)
 
-        if res.status_code == requests.codes.ok:
-            return res.json()
+        if res.status_code != requests.codes.ok:
+            return
+
+        result = res.json() or {}
+        prev_server_instance_uuid = DeployHandler._server_instance_uuid
+        DeployHandler._server_instance_uuid = result.get("server_instance_uuid")
+
+        need_to_catch_up = (prev_server_instance_uuid
+            and prev_server_instance_uuid != DeployHandler._server_instance_uuid)
+        if need_to_catch_up:
+            log_line = "need_to_catch_up: {1} != {0}" \
+                .format(prev_server_instance_uuid, DeployHandler._server_instance_uuid)
+            sub_aud.info(log_line)
+            DeployHandler._logger.info(log_line)
+
+        return need_to_catch_up