activate refactoring in ControlLoopEventManager 05/101405/2
authorjhh <jorge.hernandez-herrero@att.com>
Fri, 7 Feb 2020 19:10:03 +0000 (13:10 -0600)
committerjhh <jorge.hernandez-herrero@att.com>
Fri, 7 Feb 2020 19:17:05 +0000 (13:17 -0600)
This is to support backwards compatible implementation
of Tosca Compliant Operational Policies.

Issue-ID: POLICY-2360
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I0e8b96b19375c58e4b62e0dcd1da5c508091ea2f
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java

index 868ab00..f297a8f 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * controlloop event manager
  * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 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.
@@ -186,42 +186,66 @@ public class ControlLoopEventManager implements Serializable {
         return requestId;
     }
 
+
+    private VirtualControlLoopNotification rejectNotification(VirtualControlLoopEvent event, String message) {
+        VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
+        notification.setNotification(ControlLoopNotificationType.REJECTED);
+        notification.setMessage(message);
+        return notification;
+    }
+
     /**
-     * Activate a control loop event.
+     * Preactivations check for an event.
      *
      * @param event the event
      * @return the VirtualControlLoopNotification
      */
-    public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
-        VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
+    private VirtualControlLoopNotification preActivationChecks(VirtualControlLoopEvent event) {
         try {
             //
             // This method should ONLY be called ONCE
             //
             if (this.isActivated) {
-                throw new ControlLoopException("ControlLoopEventManager has already been activated.");
+                return rejectNotification(event, "ControlLoopEventManager has already been activated.");
             }
+
             //
             // Syntax check the event
             //
             checkEventSyntax(event);
-
-            //
-            // At this point we are good to go with this event
-            //
-            this.onset = event;
-            this.numOnsets = 1;
-            //
-            notification.setNotification(ControlLoopNotificationType.ACTIVE);
-            //
-            // Set ourselves as active
-            //
-            this.isActivated = true;
         } catch (ControlLoopException e) {
-            logger.error("{}: activate by event threw: ", this, e);
-            notification.setNotification(ControlLoopNotificationType.REJECTED);
-            notification.setMessage(e.getMessage());
+            logger.warn("{}: invalid event syntax: ", this, e);
+            return rejectNotification(event, e.getMessage());
+
+        }
+
+        return new VirtualControlLoopNotification(event);
+    }
+
+    /**
+     * Activate a control loop event.
+     *
+     * @param event the event
+     * @return the VirtualControlLoopNotification
+     */
+    public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
+        VirtualControlLoopNotification notification = preActivationChecks(event);
+        if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
+            return notification;
         }
+
+        //
+        // At this point we are good to go with this event
+        //
+        this.onset = event;
+        this.numOnsets = 1;
+        //
+        //
+        // Set ourselves as active
+        //
+        this.isActivated = true;
+
+        notification.setNotification(ControlLoopNotificationType.ACTIVE);
         return notification;
     }
 
@@ -233,32 +257,15 @@ public class ControlLoopEventManager implements Serializable {
      * @return the VirtualControlLoopNotification
      */
     public VirtualControlLoopNotification activate(String yamlSpecification, VirtualControlLoopEvent event) {
-        VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
-        try {
-            //
-            // This method should ONLY be called ONCE
-            //
-            if (this.isActivated) {
-                throw new ControlLoopException("ControlLoopEventManager has already been activated.");
-            }
-            //
-            // Syntax check the event
-            //
-            checkEventSyntax(event);
-
-            //
-            // Check the YAML
-            //
-            if (yamlSpecification == null || yamlSpecification.length() < 1) {
-                throw new ControlLoopException("yaml specification is null or 0 length");
-            }
-        } catch (ControlLoopException e) {
-            logger.error("{}: activate by YAML specification and event threw: ", this, e);
-            notification.setNotification(ControlLoopNotificationType.REJECTED);
-            notification.setMessage(e.getMessage());
+        VirtualControlLoopNotification notification = preActivationChecks(event);
+        if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
             return notification;
         }
 
+        if (yamlSpecification == null || yamlSpecification.length() < 1) {
+            return rejectNotification(event, "yaml specification is null or 0 length");
+        }
+
         String decodedYaml = null;
         try {
             decodedYaml = URLDecoder.decode(yamlSpecification, "UTF-8");
@@ -266,10 +273,8 @@ public class ControlLoopEventManager implements Serializable {
                 yamlSpecification = decodedYaml;
             }
         } catch (UnsupportedEncodingException e) {
-            logger.error("{}: YAML decode in activate by YAML specification and event threw: ", this, e);
-            notification.setNotification(ControlLoopNotificationType.REJECTED);
-            notification.setMessage(e.getMessage());
-            return notification;
+            logger.warn("{}: YAML decode in activate by YAML specification and event threw: ", this, e);
+            return rejectNotification(event, e.getMessage());
         }
 
         try {
@@ -277,24 +282,26 @@ public class ControlLoopEventManager implements Serializable {
             // Parse the YAML specification
             //
             this.processor = new ControlLoopProcessor(yamlSpecification);
-            //
-            // At this point we are good to go with this event
-            //
-            this.onset = event;
-            this.numOnsets = 1;
-            //
-            //
-            //
-            notification.setNotification(ControlLoopNotificationType.ACTIVE);
-            //
-            // Set ourselves as active
-            //
-            this.isActivated = true;
         } catch (ControlLoopException e) {
             logger.error("{}: activate by YAML specification and event threw: ", this, e);
-            notification.setNotification(ControlLoopNotificationType.REJECTED);
-            notification.setMessage(e.getMessage());
+            return rejectNotification(event, e.getMessage());
         }
+
+        //
+        // At this point we are good to go with this event
+        //
+        this.onset = event;
+        this.numOnsets = 1;
+
+        //
+        // Set ourselves as active
+        //
+        this.isActivated = true;
+
+        //
+        //
+        //
+        notification.setNotification(ControlLoopNotificationType.ACTIVE);
         return notification;
     }