Fix Generic VNF isClosedLoopDisabled
[policy/drools-applications.git] / controlloop / common / eventmanager / src / main / java / org / onap / policy / controlloop / eventmanager / ControlLoopEventManager.java
index 7aaac9e..4adcff0 100644 (file)
@@ -31,6 +31,7 @@ import java.util.UUID;
 import org.onap.policy.aai.AAIGETVnfResponse;
 import org.onap.policy.aai.AAIGETVserverResponse;
 import org.onap.policy.aai.AAIManager;
+import org.onap.policy.aai.util.AAIException;
 import org.onap.policy.controlloop.ControlLoopEventStatus;
 import org.onap.policy.controlloop.ControlLoopException;
 import org.onap.policy.controlloop.ControlLoopNotificationType;
@@ -72,14 +73,11 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
        private LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
        private ControlLoopOperationManager currentOperation = null;
        private transient TargetLock targetLock = null;
-       private static AAIGETVnfResponse vnfResponse = null;
-       private static AAIGETVserverResponse vserverResponse = null;
+       private AAIGETVnfResponse vnfResponse = null;
+       private AAIGETVserverResponse vserverResponse = null;
        private static String aaiHostURL; 
        private static String aaiUser; 
        private static String aaiPassword;
-       private static String aaiGetQueryByVserver; 
-       private static String aaiGetQueryByVnfID; 
-       private static String aaiGetQueryByVnfName; 
        
        private static Collection<String> requiredAAIKeys = new ArrayList<>();
        static {
@@ -151,6 +149,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                        // Syntax check the event
                        //
                        checkEventSyntax(event);
+                       
                        //
                        // At this point we are good to go with this event
                        //
@@ -268,8 +267,9 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                }
        
                switch (result) {
-               case FINAL_FAILURE:
                case FINAL_FAILURE_EXCEPTION:
+                       notification.message = "Exception in processing closed loop";
+               case FINAL_FAILURE:
                case FINAL_FAILURE_RETRIES:
                case FINAL_FAILURE_TIMEOUT:
                case FINAL_FAILURE_GUARD:
@@ -291,7 +291,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                return notification;
        }
                
-       public ControlLoopOperationManager      processControlLoop() throws ControlLoopException {
+       public ControlLoopOperationManager      processControlLoop() throws ControlLoopException, AAIException {
                //
                // Check if they activated us
                //
@@ -399,7 +399,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                        //
                        LockResult<GuardResult, TargetLock> lockResult = PolicyGuard.lockTarget(
                                                                                                                                                this.currentOperation.policy.getTarget().getType(), 
-                                                                                                                                               this.getTargetInstance(this.currentOperation.policy),
+                                                                                                                                               this.currentOperation.getTargetEntity(),
                                                                                                                                                this.onset.requestID,
                                                                                                                                                this);
                        //
@@ -436,14 +436,19 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                ;
        }
                
-       public NEW_EVENT_STATUS onNewEvent(VirtualControlLoopEvent event) {
+       public NEW_EVENT_STATUS onNewEvent(VirtualControlLoopEvent event) throws AAIException {
                try {
-                       ControlLoopEventManager.checkEventSyntax(event);
+                       this.checkEventSyntax(event);
                        if (event.closedLoopEventStatus == ControlLoopEventStatus.ONSET) {
                                //
                                // Check if this is our original ONSET
                                //
                                if (event.equals(this.onset)) {
+                                   //
+                                   // Query A&AI if needed
+                                   //
+                                   queryAai(event);
+                                   
                                        //
                                        // DO NOT retract it
                                        //
@@ -521,7 +526,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                return vserverResponse; 
        }
        
-       public static void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
+       public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
                if (event.closedLoopEventStatus == null || 
                                (event.closedLoopEventStatus != ControlLoopEventStatus.ONSET &&
                                event.closedLoopEventStatus != ControlLoopEventStatus.ABATED)) {
@@ -533,57 +538,71 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                if (event.requestID == null) {
                        throw new ControlLoopException("No request ID");
                }
-               if (event.AAI == null) {
-                       throw new ControlLoopException("AAI is null");
+               if (event.closedLoopEventStatus == ControlLoopEventStatus.ABATED) {
+                       return;
                }
-               if (event.AAI.get("generic-vnf.vnf-id") == null && event.AAI.get("vserver.vserver-name") == null &&
-                       event.AAI.get("generic-vnf.vnf-name") == null) {
-                       throw new ControlLoopException("generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
+               if (event.target == null || event.target.length() < 1) {
+                       throw new ControlLoopException("No target field");
+               } else if (! "VM_NAME".equalsIgnoreCase(event.target) &&
+                               ! "VNF_NAME".equalsIgnoreCase(event.target) &&
+                               ! "vserver.vserver-name".equalsIgnoreCase(event.target) &&
+                               ! "generic-vnf.vnf-id".equalsIgnoreCase(event.target) &&
+                               ! "generic-vnf.vnf-name".equalsIgnoreCase(event.target) ) {
+                       throw new ControlLoopException("target field invalid - expecting VM_NAME or VNF_NAME");
                }
-               if (event.AAI.get("vserver.is-closed-loop-disabled") == null) {
+               if (event.AAI == null) {
+            throw new ControlLoopException("AAI is null");
+        }
+        if (event.AAI.get("generic-vnf.vnf-id") == null && event.AAI.get("vserver.vserver-name") == null &&
+                event.AAI.get("generic-vnf.vnf-name") == null) {
+            throw new ControlLoopException("generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
+        }
+       }
+       
+       public void queryAai(VirtualControlLoopEvent event) throws AAIException {
+               if (event.AAI.get("vserver.is-closed-loop-disabled") == null && 
+                       event.AAI.get("generic-vnf.is-closed-loop-disabled") == null) {
                        try {
                                if (event.AAI.get("generic-vnf.vnf-id") != null) {
-                              vnfResponse = getAAIVnfInfo(event); 
-                              if (vnfResponse == null) {
-                                  throw new ControlLoopException("AAI Response is null (query by vnf-id)");
-                              }
-                              if (isClosedLoopDisabled(vnfResponse) == true) {
-                                          throw new ControlLoopException("is-closed-loop-disabled is set to true");    
-                              }
+                                       vnfResponse = getAAIVnfInfo(event); 
+                                       if (vnfResponse == null) {
+                                               throw new AAIException("AAI Response is null (query by vnf-id)");
+                                       }
+                                       if (vnfResponse.requestError != null) {
+                                               throw new AAIException("AAI Responded with a request error (query by vnf-id)");
+                                       }
+                                       if (isClosedLoopDisabled(vnfResponse) == true) {
+                                               throw new AAIException("is-closed-loop-disabled is set to true");       
+                                       }
                                } else if (event.AAI.get("generic-vnf.vnf-name") != null) {
-                                   vnfResponse = getAAIVnfInfo(event); 
-                                   if (vnfResponse == null) {
-                                       throw new ControlLoopException("AAI Response is null (query by vnf-name)");
-                                   }
-                                   if (isClosedLoopDisabled(vnfResponse) == true) {
-                                               throw new ControlLoopException("is-closed-loop-disabled is set to true");       
-                                   }
+                                       vnfResponse = getAAIVnfInfo(event); 
+                                       if (vnfResponse == null) {
+                                               throw new AAIException("AAI Response is null (query by vnf-name)");
+                                       }
+                                       if (vnfResponse.requestError != null) {
+                                               throw new AAIException("AAI Responded with a request error (query by vnf-name)");
+                                       }
+                                       if (isClosedLoopDisabled(vnfResponse) == true) {
+                                               throw new AAIException("is-closed-loop-disabled is set to true");       
+                                       }
                                } else if (event.AAI.get("vserver.vserver-name") != null) {
-                                   vserverResponse = getAAIVserverInfo(event); 
-                                   if (vserverResponse == null) {
-                                      throw new ControlLoopException("AAI Response is null (query by vserver-name)");
-                                   }
-                                   if (isClosedLoopDisabled(vserverResponse) == true) {
-                                               throw new ControlLoopException("is-closed-loop-disabled is set to true");       
-                                   }
+                                       vserverResponse = getAAIVserverInfo(event); 
+                                       if (vserverResponse == null) {
+                                               throw new AAIException("AAI Response is null (query by vserver-name)");
+                                       }
+                                       if (vserverResponse.requestError != null) {
+                                               throw new AAIException("AAI responded with a request error (query by vserver-name)");
+                                       }
+                                       if (isClosedLoopDisabled(vserverResponse) == true) {
+                                               throw new AAIException("is-closed-loop-disabled is set to true");       
+                                       }
                                }
                        } catch (Exception e) {
                                logger.error("Exception from getAAIInfo: ", e);
-                               throw new ControlLoopException("Exception from getAAIInfo: " + e.toString());
+                               throw new AAIException("Exception from getAAIInfo: " + e.toString());
                        }
                } else if (isClosedLoopDisabled(event)) {
-                       throw new ControlLoopException("is-closed-loop-disabled is set to true");
-               }
-               if (event.target == null || event.target.length() < 1) {
-                       throw new ControlLoopException("No target field");
-               } else {
-                       if (! event.target.equalsIgnoreCase("VM_NAME") &&
-                               ! event.target.equalsIgnoreCase("VNF_NAME") &&
-                               ! event.target.equalsIgnoreCase("vserver.vserver-name") &&
-                               ! event.target.equalsIgnoreCase("generic-vnf.vnf-id") &&
-                               ! event.target.equalsIgnoreCase("generic-vnf.vnf-name") ) {
-                               throw new ControlLoopException("target field invalid - expecting VM_NAME or VNF_NAME");
-                       }
+                       throw new AAIException("is-closed-loop-disabled is set to true");
                }
        }
        
@@ -617,7 +636,13 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                    "yes".equalsIgnoreCase(event.AAI.get("vserver.is-closed-loop-disabled")) || 
                    "Y".equalsIgnoreCase(event.AAI.get("vserver.is-closed-loop-disabled"))) { 
                        return true; 
-               }               
+               }
+               else if ("true".equalsIgnoreCase(event.AAI.get("generic-vnf.is-closed-loop-disabled")) || 
+                   "T".equalsIgnoreCase(event.AAI.get("generic-vnf.is-closed-loop-disabled")) || 
+                   "yes".equalsIgnoreCase(event.AAI.get("generic-vnf.is-closed-loop-disabled")) || 
+                   "Y".equalsIgnoreCase(event.AAI.get("generic-vnf.is-closed-loop-disabled"))) { 
+                   return true; 
+           }
                return false;
        }
        
@@ -686,32 +711,6 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                return false;
        }
 
-       public String getTargetInstance(Policy policy) {
-               if (policy.getTarget() != null) {
-                       if (policy.getTarget().getType() != null) {
-                               switch(policy.getTarget().getType()) {
-                               case PNF:
-                                       break;
-                               case VM:
-                               case VNF:
-                                       if (this.onset.target.equalsIgnoreCase("vserver.vserver-name")) {
-                                               return this.onset.AAI.get("vserver.vserver-name");
-                                       }
-                                       else if (this.onset.target.equalsIgnoreCase("generic-vnf.vnf-id")) {
-                                           return this.onset.AAI.get("generic-vnf.vnf-id");
-                                       }
-                                       else if (this.onset.target.equalsIgnoreCase("generic-vnf.vnf-name")) {
-                                           return this.onset.AAI.get("generic-vnf.vnf-name");
-                                       }
-                                       break;
-                               default:
-                                       break;
-                               }
-                       }
-               }
-               return null;
-       }
-
        @Override
        public String toString() {
                return "ControlLoopEventManager [closedLoopControlName=" + closedLoopControlName + ", requestID=" + requestID