Passed classloader property to entitymanager.
[policy/drools-applications.git] / controlloop / common / eventmanager / src / main / java / org / onap / policy / controlloop / eventmanager / ControlLoopOperationManager.java
index ec99c1a..cebdc1e 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.onap.policy.controlloop.eventmanager;
 
-import java.io.InputStream;
 import java.io.Serializable;
 import java.sql.Timestamp;
 import java.time.Instant;
@@ -31,6 +30,8 @@ import java.util.Properties;
 import javax.persistence.EntityManager;
 import javax.persistence.Persistence;
 
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.onap.policy.aai.util.AAIException;
 import org.onap.policy.appc.Response;
 import org.onap.policy.appc.ResponseCode;
 import org.onap.policy.appclcm.LCMResponseWrapper;
@@ -40,10 +41,12 @@ import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 import org.onap.policy.controlloop.actor.appc.APPCActorServiceProvider;
 import org.onap.policy.controlloop.actor.appclcm.AppcLcmActorServiceProvider;
+import org.onap.policy.controlloop.actor.so.SOActorServiceProvider;
 import org.onap.policy.controlloop.actor.vfc.VFCActorServiceProvider;
 import org.onap.policy.controlloop.policy.Policy;
 import org.onap.policy.controlloop.policy.PolicyResult;
-import org.onap.policy.controlloop.actor.so.SOActorServiceProvider;
+import org.onap.policy.drools.system.PolicyEngine;
+import org.onap.policy.guard.Util;
 import org.onap.policy.so.SOResponse;
 import org.onap.policy.vfc.VFCResponse;
 import org.slf4j.Logger;
@@ -82,6 +85,7 @@ public class ControlLoopOperationManager implements Serializable {
        private LinkedList<Operation> operationHistory = new LinkedList<Operation>();
        private PolicyResult policyResult = null;
        private ControlLoopEventManager eventManager = null;
+       private String targetEntity;
 
        public ControlLoopEventManager getEventManager() {
                return eventManager;
@@ -91,6 +95,9 @@ public class ControlLoopOperationManager implements Serializable {
                this.eventManager = eventManager;
        }
 
+       public String getTargetEntity() {
+           return this.targetEntity;
+       }
 
        //
        // Internal class used for tracking
@@ -121,21 +128,72 @@ public class ControlLoopOperationManager implements Serializable {
                this.guardApprovalStatus = guardApprovalStatus;
        }
 
-
-       public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em) throws ControlLoopException {
+       public String getTarget(Policy policy) throws ControlLoopException, AAIException {
+        if (policy.getTarget() != null) {
+            if (policy.getTarget().getType() != null) {
+                switch(policy.getTarget().getType()) {
+                case PNF:
+                    break;
+                case VM:
+                case VNF:
+                    VirtualControlLoopEvent virtualOnset = (VirtualControlLoopEvent) this.onset;
+                    if (this.onset.target.equalsIgnoreCase("vserver.vserver-name")) {
+                        return virtualOnset.AAI.get("vserver.vserver-name");
+                    }
+                    else if (this.onset.target.equalsIgnoreCase("generic-vnf.vnf-id")) {
+                        return virtualOnset.AAI.get("generic-vnf.vnf-id");
+                    }
+                    else if (this.onset.target.equalsIgnoreCase("generic-vnf.vnf-name")) {
+                        /*
+                         * If the vnf-name was retrieved from the onset then the vnf-id
+                         * must be obtained from the event manager's A&AI GET query
+                         */
+                        String vnfId = this.eventManager.getVnfResponse().vnfID;
+                        if (vnfId == null) {
+                            throw new AAIException("No vnf-id found");
+                        }
+                        return vnfId;
+                    }
+                    break;
+                default:
+                    throw new ControlLoopException("The target type is not supported");
+                }
+            }
+            else {
+                throw new ControlLoopException("The target type is null");
+            }
+        }
+        else {
+            throw new ControlLoopException("The target is null");
+        }
+        throw new ControlLoopException("Target does not match target type");
+    }
+       
+       public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em) throws ControlLoopException, AAIException {
                this.onset = onset;
                this.policy = policy;
                this.guardApprovalStatus = "NONE";
                this.eventManager = em;
-
+               this.targetEntity = getTarget(policy);
+               
                //
                // Let's make a sanity check
                //
                switch (policy.getActor()) {
                case "APPC":
+                   if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
+                       /*
+                 * The target vnf-id may not be the same as the source vnf-id
+                 * specified in the yaml, the target vnf-id is retrieved by
+                 * a named query to A&AI.
+                 */
+                       String targetVnf = AppcLcmActorServiceProvider.vnfNamedQuery(
+                                   policy.getTarget().getResourceID(), this.targetEntity);
+                       this.targetEntity = targetVnf;
+                   }
                        break;
                case "SO":
-                       break;
+                   break;
                case "VFC":
                        break;
                default:
@@ -143,7 +201,7 @@ public class ControlLoopOperationManager implements Serializable {
                }
        }
 
-       public Object startOperation(/*VirtualControlLoopEvent*/ControlLoopEvent onset) {
+       public Object startOperation(/*VirtualControlLoopEvent*/ControlLoopEvent onset) throws AAIException {
                //
                // They shouldn't call us if we currently running something
                //
@@ -190,7 +248,7 @@ public class ControlLoopOperationManager implements Serializable {
                this.policyResult = null;
                Operation operation = new Operation();
                operation.attempt = ++this.attempts;
-               operation.operation.actor = this.policy.getActor().toString();
+               operation.operation.actor = this.policy.getActor();
                operation.operation.operation = this.policy.getRecipe();
                operation.operation.target = this.policy.getTarget().toString();
                operation.operation.subRequestId = Integer.toString(operation.attempt);
@@ -204,17 +262,20 @@ public class ControlLoopOperationManager implements Serializable {
                     * request is constructed. Otherwise an LCMRequest
                     * is constructed.
                     */
+                       this.currentOperation = operation;
                    if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
 
-                   this.operationRequest = APPCActorServiceProvider.constructRequest((VirtualControlLoopEvent)onset, operation.operation, this.policy);
+                   this.operationRequest = APPCActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, 
+                                           operation.operation, this.policy, this.targetEntity);
                    }
                    else {
-                       this.operationRequest = AppcLcmActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, operation.operation, this.policy);
+                       this.operationRequest = AppcLcmActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, 
+                                               operation.operation, this.policy, this.targetEntity);
                    }
                        //
                        // Save the operation
                        //
-                       this.currentOperation = operation;
+                       
                        return operationRequest;
                case "SO":
                        SOActorServiceProvider SOAsp = new SOActorServiceProvider();
@@ -223,11 +284,18 @@ public class ControlLoopOperationManager implements Serializable {
                        // Save the operation
                        this.currentOperation = operation;
 
+                       if (this.operationRequest == null) {
+                               this.policyResult = PolicyResult.FAILURE;
+                       }
+
                        return operationRequest;
                case "VFC":
-                        this.operationRequest = VFCActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, operation.operation, this.policy);
-                        this.currentOperation = operation;
-                        return operationRequest;
+            this.operationRequest = VFCActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, operation.operation, this.policy, this.eventManager.getVnfResponse());
+            this.currentOperation = operation;
+            if (this.operationRequest == null) {
+                this.policyResult = PolicyResult.FAILURE;
+            }
+            return operationRequest;
 
                }
                return null;
@@ -346,13 +414,14 @@ public class ControlLoopOperationManager implements Serializable {
                    return null;
                } else if (response instanceof SOResponse) {
                        SOResponse msoResponse = (SOResponse) response;
+
                        switch (msoResponse.httpResponseCode) {
                        case 200:
                        case 202:
                                //
                                // Consider it as success
                                //
-                               this.completeOperation(new Integer(1), msoResponse.httpResponseCode + " Success", PolicyResult.SUCCESS);
+                               this.completeOperation(this.attempts, msoResponse.httpResponseCode + " Success", PolicyResult.SUCCESS);
                                if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
                                        return null;
                                }
@@ -361,7 +430,7 @@ public class ControlLoopOperationManager implements Serializable {
                                //
                                // Consider it as failure
                                //
-                               this.completeOperation(new Integer(1), msoResponse.httpResponseCode + " Failed", PolicyResult.FAILURE);
+                               this.completeOperation(this.attempts, msoResponse.httpResponseCode + " Failed", PolicyResult.FAILURE);
                                if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
                                        return null;
                                }
@@ -370,11 +439,12 @@ public class ControlLoopOperationManager implements Serializable {
 
                } else if (response instanceof VFCResponse) {
                        VFCResponse vfcResponse = (VFCResponse) response;
+
                        if (vfcResponse.responseDescriptor.getStatus().equalsIgnoreCase("finished")) {
                                //
                                // Consider it as success
                                //
-                               this.completeOperation(new Integer(1), " Success", PolicyResult.SUCCESS);
+                               this.completeOperation(this.attempts, " Success", PolicyResult.SUCCESS);
                                if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
                                        return null;
                                }
@@ -383,10 +453,12 @@ public class ControlLoopOperationManager implements Serializable {
                                //
                                // Consider it as failure
                                //
-                               this.completeOperation(new Integer(1), " Failed", PolicyResult.FAILURE);
+                               this.completeOperation(this.attempts, " Failed", PolicyResult.FAILURE);
                                if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
                                        return null;
                                }
+                               // increment operation attempts for retries
+                               this.attempts += 1;
                                return PolicyResult.FAILURE;
                        }
                }
@@ -421,7 +493,8 @@ public class ControlLoopOperationManager implements Serializable {
                if (this.currentOperation != null && this.currentOperation.operation != null) {
                        return this.currentOperation.operation.toMessage();
                }
-               if (this.operationHistory != null && this.operationHistory.size() > 0) {
+
+               if (!this.operationHistory.isEmpty()) {
                        return this.operationHistory.getLast().operation.toMessage();
                }
                return null;
@@ -431,7 +504,8 @@ public class ControlLoopOperationManager implements Serializable {
                if (this.currentOperation != null && this.currentOperation.operation != null) {
                        return this.currentOperation.operation.toMessage()+ ", Guard result: " + guardResult;
                }
-               if (this.operationHistory != null && this.operationHistory.size() > 0) {
+          
+               if (!this.operationHistory.isEmpty()) {
                        return this.operationHistory.getLast().operation.toMessage() + ", Guard result: " + guardResult;
                }
                return null;
@@ -441,7 +515,8 @@ public class ControlLoopOperationManager implements Serializable {
                if (this.currentOperation != null && this.currentOperation.operation != null) {
                        return this.currentOperation.operation.toHistory();
                }
-               if (this.operationHistory != null && this.operationHistory.size() > 0) {
+          
+               if (!this.operationHistory.isEmpty()) {
                        return this.operationHistory.getLast().operation.toHistory();
                }
                return null;
@@ -469,6 +544,10 @@ public class ControlLoopOperationManager implements Serializable {
                //
                this.completeOperation(this.attempts, "Operation denied by Guard", PolicyResult.FAILURE_GUARD);
        }
+       
+       public void setOperationHasException(String message) {
+               this.completeOperation(this.attempts, message, PolicyResult.FAILURE_EXCEPTION);
+       }
 
        public boolean  isOperationComplete() {
                //
@@ -534,15 +613,25 @@ public class ControlLoopOperationManager implements Serializable {
        }
 
        private void    storeOperationInDataBase(){
+               // Only store in DB if enabled
+               boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
+               if( !guardEnabled ){
+                       return;
+               }
+
 
                // DB Properties
                Properties props = new Properties();
-               try (InputStream is = org.onap.policy.guard.PIPEngineGetHistory.class.getResourceAsStream(org.onap.policy.guard.PIPEngineGetHistory.OPS_HIST_PROPS_LOC)){
-                       props.load(is);
-               } catch (Exception ex) {
-                       logger.error("getCountFromDB threw: ", ex);
-                       return;
+               if(PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL) != null &&
+                               PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER) != null &&
+                               PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS) != null){
+                       props.put(Util.ECLIPSE_LINK_KEY_URL, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL));
+                       props.put(Util.ECLIPSE_LINK_KEY_USER, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER));
+                       props.put(Util.ECLIPSE_LINK_KEY_PASS, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS));
+                       props.put(PersistenceUnitProperties.CLASSLOADER, ControlLoopOperationManager.class.getClassLoader());
                }
+               
+               
                String OpsHistPU = System.getProperty("OperationsHistoryPU");
                if(OpsHistPU == null || !OpsHistPU.equals("TestOperationsHistoryPU")){
                        OpsHistPU = "OperationsHistoryPU";
@@ -564,7 +653,7 @@ public class ControlLoopOperationManager implements Serializable {
                newEntry.requestId = this.onset.requestID.toString();
                newEntry.actor = this.currentOperation.operation.actor;
                newEntry.operation = this.currentOperation.operation.operation;
-               newEntry.target = this.eventManager.getTargetInstance(this.policy);
+               newEntry.target = this.targetEntity;
                newEntry.starttime = Timestamp.from(this.currentOperation.operation.start);
                newEntry.subrequestId = this.currentOperation.operation.subRequestId;
                newEntry.endtime = new Timestamp(this.currentOperation.operation.end.toEpochMilli());