diff options
Diffstat (limited to 'controlloop/common/eventmanager/src/main')
3 files changed, 117 insertions, 73 deletions
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java index 09f69fb0b..3c264ab24 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,6 +23,7 @@ package org.onap.policy.controlloop.eventmanager; import java.io.Serializable; import java.sql.Timestamp; import java.time.Instant; +import java.util.AbstractMap; import java.util.LinkedList; import javax.persistence.EntityManager; @@ -30,31 +31,32 @@ import javax.persistence.Persistence; import org.onap.policy.appc.Response; import org.onap.policy.appc.ResponseCode; +import org.onap.policy.appclcm.LCMResponseWrapper; import org.onap.policy.controlloop.ControlLoopEvent; +import org.onap.policy.controlloop.ControlLoopException; import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.ControlLoopException; -import org.onap.policy.controlloop.policy.Policy; -import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.controlloop.actor.appc.APPCActorServiceProvider; import org.onap.policy.controlloop.actor.vfc.VFCActorServiceProvider; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.PolicyResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import org.onap.policy.controlloop.actor.appclcm.AppcLcmActorServiceProvider; public class ControlLoopOperationManager implements Serializable { - + /** - * + * */ private static final long serialVersionUID = -3773199283624595410L; private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManager.class); @Override public String toString() { - return "ControlLoopOperationManager [onset=" + (onset != null ? onset.requestID : "null") + ", policy=" + return "ControlLoopOperationManager [onset=" + (onset != null ? onset.requestID : "null") + ", policy=" + (policy != null ? policy.getId() : "null") + ", attempts=" + attempts - + ", policyResult=" + policyResult + + ", policyResult=" + policyResult + ", currentOperation=" + currentOperation + ", operationHistory=" + operationHistory + "]"; } @@ -92,17 +94,17 @@ public class ControlLoopOperationManager implements Serializable { public ControlLoopOperation operation = new ControlLoopOperation(); public PolicyResult policyResult = null; public int attempt = 0; - + @Override public String toString() { return "Operation [attempt=" + attempt + ", policyResult=" + policyResult + ", operation=" + operation + "]"; } } - + private String guardApprovalStatus = "NONE";//"NONE", "PERMIT", "DENY" private Object operationRequest; - + public Object getOperationRequest() { return operationRequest; } @@ -113,14 +115,14 @@ public class ControlLoopOperationManager implements Serializable { public void setGuardApprovalStatus(String guardApprovalStatus) { this.guardApprovalStatus = guardApprovalStatus; } - - - public ControlLoopOperationManager(/*ATTControlLoopEvent*/ControlLoopEvent onset, Policy policy, ControlLoopEventManager em) throws ControlLoopException { + + + public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em) throws ControlLoopException { this.onset = onset; this.policy = policy; this.guardApprovalStatus = "NONE"; this.eventManager = em; - + // // Let's make a sanity check // @@ -139,7 +141,7 @@ public class ControlLoopOperationManager implements Serializable { throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor."); } } - + public Object startOperation(/*VirtualControlLoopEvent*/ControlLoopEvent onset) { // // They shouldn't call us if we currently running something @@ -196,16 +198,22 @@ public class ControlLoopOperationManager implements Serializable { // switch (policy.getActor()) { case "APPC": - //Request request = APPCActorServiceProvider.constructRequest(onset, operation.operation, this.policy); - this.operationRequest = APPCActorServiceProvider.constructRequest((VirtualControlLoopEvent)onset, operation.operation, this.policy); + /* + * If the recipe is ModifyConfig, a legacy APPC + * request is constructed. Otherwise an LCMRequest + * is constructed. + */ + if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) { + + this.operationRequest = APPCActorServiceProvider.constructRequest((VirtualControlLoopEvent)onset, operation.operation, this.policy); + } + else { + this.operationRequest = AppcLcmActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, operation.operation, this.policy); + } // // Save the operation // this.currentOperation = operation; - //System.out.print("************* BEFORE STORING....."); - //this.storeOperationInDataBase("startOperation"); - //System.out.print("************* AFTER STORING....."); - // return operationRequest; case "MSO": // @@ -221,7 +229,7 @@ public class ControlLoopOperationManager implements Serializable { } return null; } - + public PolicyResult onResponse(Object response) { // // Which response is it? @@ -306,10 +314,37 @@ public class ControlLoopOperationManager implements Serializable { } return PolicyResult.FAILURE; } - } + } + else if (response instanceof LCMResponseWrapper) { + + LCMResponseWrapper dmaapResponse = (LCMResponseWrapper) response; + + /* + * Parse out the operation attempt using the subrequestid + */ + Integer operationAttempt = AppcLcmActorServiceProvider.parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId()); + if (operationAttempt == null) { + this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).", PolicyResult.FAILURE_EXCEPTION); + } + + /* + * Process the APPCLCM response to see what PolicyResult + * should be returned + */ + AbstractMap.SimpleEntry<PolicyResult, String> result = AppcLcmActorServiceProvider.processResponse(dmaapResponse); + + if (result.getKey() != null) { + this.completeOperation(operationAttempt, result.getValue(), result.getKey()); + if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) { + return null; + } + return result.getKey(); + } + return null; + } return null; } - + public Integer getOperationTimeout() { // // Sanity check @@ -321,7 +356,7 @@ public class ControlLoopOperationManager implements Serializable { logger.debug("getOperationTimeout returning {}", this.policy.getTimeout()); return this.policy.getTimeout(); } - + public String getOperationTimeoutString(int defaultTimeout) { Integer to = this.getOperationTimeout(); if (to == null || to == 0) { @@ -329,11 +364,11 @@ public class ControlLoopOperationManager implements Serializable { } return to.toString() + "s"; } - + public PolicyResult getOperationResult() { return this.policyResult; } - + public String getOperationMessage() { if (this.currentOperation != null && this.currentOperation.operation != null) { return this.currentOperation.operation.toMessage(); @@ -343,7 +378,7 @@ public class ControlLoopOperationManager implements Serializable { } return null; } - + public String getOperationMessage(String guardResult) { if (this.currentOperation != null && this.currentOperation.operation != null) { return this.currentOperation.operation.toMessage()+ ", Guard result: " + guardResult; @@ -353,7 +388,7 @@ public class ControlLoopOperationManager implements Serializable { } return null; } - + public String getOperationHistory() { if (this.currentOperation != null && this.currentOperation.operation != null) { return this.currentOperation.operation.toHistory(); @@ -363,23 +398,23 @@ public class ControlLoopOperationManager implements Serializable { } return null; } - + public LinkedList<ControlLoopOperation> getHistory() { LinkedList<ControlLoopOperation> history = new LinkedList<ControlLoopOperation>(); for (Operation op : this.operationHistory) { history.add(new ControlLoopOperation(op.operation)); - + } return history; } - + public void setOperationHasTimedOut() { // // // this.completeOperation(this.attempts, "Operation timed out", PolicyResult.FAILURE_TIMEOUT); } - + public void setOperationHasGuardDeny() { // // @@ -434,11 +469,11 @@ public class ControlLoopOperationManager implements Serializable { // return true; } - + public boolean isOperationRunning() { return (this.currentOperation != null); } - + private boolean isRetriesMaxedOut() { if (policy.getRetry() == null || policy.getRetry() == 0) { // @@ -449,19 +484,23 @@ public class ControlLoopOperationManager implements Serializable { } return (this.attempts > policy.getRetry()); } - + private void storeOperationInDataBase(){ - + + String OpsHistPU = System.getProperty("OperationsHistoryPU"); + if(OpsHistPU == null || !OpsHistPU.equals("TestOperationsHistoryPU")){ + OpsHistPU = "OperationsHistoryPU"; + } EntityManager em; try{ - em = Persistence.createEntityManagerFactory("OperationsHistoryPU").createEntityManager();//emf.createEntityManager(); + em = Persistence.createEntityManagerFactory(OpsHistPU).createEntityManager(); }catch(Exception e){ logger.error("storeOperationInDataBase threw: ", e); - return; + return; } - - OperationsHistoryDbEntry newEntry = new OperationsHistoryDbEntry(); - + + OperationsHistoryDbEntry newEntry = new OperationsHistoryDbEntry(); + newEntry.closedLoopName = this.onset.closedLoopControlName; newEntry.requestId = this.onset.requestID.toString(); newEntry.actor = this.currentOperation.operation.actor; @@ -472,17 +511,17 @@ public class ControlLoopOperationManager implements Serializable { newEntry.endtime = new Timestamp(this.currentOperation.operation.end.toEpochMilli()); newEntry.message = this.currentOperation.operation.message; newEntry.outcome = this.currentOperation.operation.outcome; - + em.getTransaction().begin(); em.persist(newEntry); em.getTransaction().commit(); - + em.close(); } - - + + private void completeOperation(Integer attempt, String message, PolicyResult result) { if (attempt == null) { logger.debug("attempt cannot be null (i.e. subRequestID)"); @@ -521,7 +560,7 @@ public class ControlLoopOperationManager implements Serializable { } } logger.debug("Could not find associated operation"); - + } - + } diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java index bb5cec8da..aed72815c 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/drools/impl/PolicyEngineJUnitImpl.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Queue; import org.onap.policy.appc.Request; +import org.onap.policy.appclcm.LCMRequestWrapper; import org.onap.policy.controlloop.ControlLoopNotification; import org.onap.policy.controlloop.util.Serialization; import org.slf4j.Logger; @@ -49,6 +50,10 @@ public class PolicyEngineJUnitImpl implements PolicyEngine { Request request = (Request) obj; logger.debug("Request: {} subrequst {}", request.Action, request.CommonHeader.SubRequestID); } + else if (obj instanceof LCMRequestWrapper) { + LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj; + logger.debug("Request: {} subrequest {}", dmaapRequest.getBody().getAction(), dmaapRequest.getBody().getCommonHeader().getSubRequestId()); + } // // Does the bus exist? // @@ -57,7 +62,7 @@ public class PolicyEngineJUnitImpl implements PolicyEngine { // // Create the bus // - busMap.put(busType, new HashMap<String, Queue<Object>>()); + busMap.put(busType, new HashMap<>()); } // // Get the bus @@ -71,7 +76,7 @@ public class PolicyEngineJUnitImpl implements PolicyEngine { // // Create the topic // - topicMap.put(topic, new LinkedList<Object>()); + topicMap.put(topic, new LinkedList<>()); } // // Get the topic queue diff --git a/controlloop/common/eventmanager/src/main/resources/META-INF/persistence.xml b/controlloop/common/eventmanager/src/main/resources/META-INF/persistence.xml index f007c4951..27c64b60a 100644 --- a/controlloop/common/eventmanager/src/main/resources/META-INF/persistence.xml +++ b/controlloop/common/eventmanager/src/main/resources/META-INF/persistence.xml @@ -1,22 +1,22 @@ <?xml version="1.0" encoding="UTF-8" ?> -<persistence xmlns="http://java.sun.com/xml/ns/persistence" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/persistence - http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> - - <persistence-unit name="OperationsHistoryPU" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - <!-- <jar-file>packedEntity.jar</jar-file>--> - <class>org.onap.policy.controlloop.eventmanager.OperationsHistoryDbEntry</class> - <properties> - <property name="eclipselink.ddl-generation" value="create-tables"/> - <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" /> - <!-- <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:7779/policy"/>--> - <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy"/> - <property name="javax.persistence.jdbc.user" value="root"/> - <property name="javax.persistence.jdbc.password" value="aaaa"/> - <property name="eclipselink.logging.level" value="INFO" /> - </properties> - </persistence-unit> - +<persistence version="2.1" + xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + + <persistence-unit name="OperationsHistoryPU" + transaction-type="RESOURCE_LOCAL"> + <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> + <!-- <jar-file>packedEntity.jar</jar-file> --> + <class>org.onap.policy.controlloop.eventmanager.OperationsHistoryDbEntry</class> + <properties> + <property name="eclipselink.ddl-generation" value="create-tables" /> + <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" /> + <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy" /> + <property name="javax.persistence.jdbc.user" value="root" /> + <property name="javax.persistence.jdbc.password" value="aaaa" /> + <property name="eclipselink.logging.level" value="WARNING" /> + </properties> + </persistence-unit> + + </persistence> |