aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-05-03 15:02:17 -0400
committerJim Hahn <jrh3@att.com>2021-05-04 18:17:10 -0400
commit7cc5fd31420bf71b41853a66c0a0b66bd9495ef3 (patch)
tree2ce679e75ece92137646c81ccc1306f7ca29948d /controlloop
parentcbdf437729ca4d010147acfb208ecd90ef65777c (diff)
Refactor common code from UsecasesEventManager
Created ClEventManagerWithSteps, as a subclass of ControlLoopEventManager. It contains the "Steps" to be performed and also deals with outcomes. Created ClEventManagerWithEvent, as a subclass of that. It contains a VirtualControlLoopEvent object, and uses that to populate data used by the superclass. Updates per review comments: - made loadPolicy() protected - refactored another level to manage operation outcomes Issue-ID: POLICY-3262 Change-Id: Ibf5dd114746ae26e04fe37d562273fc81dd8cfbe Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop')
-rw-r--r--controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManager.java481
-rw-r--r--controlloop/common/controller-usecases/src/main/resources/usecases.drl8
-rw-r--r--controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java410
-rw-r--r--controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEvent.java200
-rw-r--r--controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcome.java229
-rw-r--r--controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithSteps.java341
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEventTest.java380
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcomeTest.java400
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithStepsTest.java482
9 files changed, 2067 insertions, 864 deletions
diff --git a/controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManager.java b/controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManager.java
index d586240d9..4db243408 100644
--- a/controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManager.java
+++ b/controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManager.java
@@ -36,37 +36,21 @@ import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.
import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_PROV_STATUS;
import static org.onap.policy.drools.apps.controller.usecases.UsecasesConstants.VSERVER_VSERVER_NAME;
-import java.util.ArrayDeque;
import java.util.Deque;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import lombok.Getter;
-import lombok.NonNull;
-import lombok.Setter;
-import lombok.ToString;
-import org.apache.commons.lang3.StringUtils;
import org.drools.core.WorkingMemory;
-import org.kie.api.runtime.rule.FactHandle;
-import org.onap.policy.controlloop.ControlLoopEventStatus;
import org.onap.policy.controlloop.ControlLoopException;
-import org.onap.policy.controlloop.ControlLoopNotificationType;
-import org.onap.policy.controlloop.ControlLoopOperation;
import org.onap.policy.controlloop.ControlLoopResponse;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
-import org.onap.policy.controlloop.VirtualControlLoopNotification;
-import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
-import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
-import org.onap.policy.controlloop.actorserviceprovider.TargetType;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.controlloop.eventmanager.ActorConstants;
-import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
+import org.onap.policy.controlloop.eventmanager.ClEventManagerWithEvent;
import org.onap.policy.controlloop.eventmanager.StepContext;
import org.onap.policy.drools.apps.controller.usecases.step.AaiCqStep2;
import org.onap.policy.drools.apps.controller.usecases.step.AaiGetPnfStep2;
@@ -75,14 +59,7 @@ import org.onap.policy.drools.apps.controller.usecases.step.GetTargetEntityStep2
import org.onap.policy.drools.apps.controller.usecases.step.GuardStep2;
import org.onap.policy.drools.apps.controller.usecases.step.LockStep2;
import org.onap.policy.drools.apps.controller.usecases.step.Step2;
-import org.onap.policy.drools.domain.models.operational.ActorOperation;
-import org.onap.policy.drools.domain.models.operational.Operation;
-import org.onap.policy.drools.domain.models.operational.OperationalTarget;
-import org.onap.policy.drools.system.PolicyEngine;
-import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.sdnr.PciMessage;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Manager for a single control loop event. Once this has been created, the event can be
@@ -90,19 +67,11 @@ import org.slf4j.LoggerFactory;
* involves at least one step. As a step is processed, additional preprocessor steps may
* be pushed onto the queue (e.g., locks, A&AI queries, guards).
*/
-@ToString(onlyExplicitlyIncluded = true)
-public class UsecasesEventManager extends ControlLoopEventManager implements StepContext {
+public class UsecasesEventManager extends ClEventManagerWithEvent<Step2> implements StepContext {
- private static final Logger logger = LoggerFactory.getLogger(UsecasesEventManager.class);
private static final long serialVersionUID = -1216568161322872641L;
/**
- * Maximum number of steps, for a single policy, allowed in the queue at a time. This
- * prevents an infinite loop occurring with calls to {@link #loadPreprocessorSteps()}.
- */
- public static final int MAX_STEPS = 30;
-
- /**
* If there's a failure from one of these actors, then the TOSCA processing should be
* aborted.
*/
@@ -132,94 +101,6 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
OperationProperties.AAI_VNF_MODEL, OperationProperties.AAI_SERVICE,
OperationProperties.AAI_RESOURCE_VNF, UsecasesConstants.AAI_DEFAULT_GENERIC_VNF);
- public enum State {
- LOAD_POLICY, POLICY_LOADED, AWAITING_OUTCOME, DONE
- }
-
- public enum NewEventStatus {
- FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
- }
-
- @Getter
- private final VirtualControlLoopEvent event;
-
- /**
- * Request ID, as a String.
- */
- private final String requestIdStr;
-
- @Getter
- @Setter
- private State state;
-
- /**
- * {@code True} if the event has been accepted (i.e., an "ACTIVE" notification has
- * been delivered), {@code false} otherwise.
- */
- @Getter
- @Setter
- private boolean accepted;
-
- /**
- * Queue of steps waiting to be performed.
- */
- @Getter
- private final transient Deque<Step2> steps = new ArrayDeque<>(6);
-
- /**
- * Number of attempts, so far, for the current step.
- */
- @Getter
- private int attempts;
-
- /**
- * Policy currently being processed.
- */
- private Operation policy;
-
- /**
- * Result of the last policy operation. This is just a place where the rules can store
- * the value for passing to {@link #loadNextPolicy()}.
- */
- @Getter
- @Setter
- private OperationResult result = OperationResult.SUCCESS;
-
- @ToString.Include
- private int numOnsets = 1;
- @ToString.Include
- private int numAbatements = 0;
-
- private VirtualControlLoopEvent abatement = null;
-
- /**
- * Full history of operations that have been processed by the rules. This includes the
- * items in {@link #partialHistory}.
- */
- @Getter
- private final transient Deque<OperationOutcome2> fullHistory = new LinkedList<>();
-
- /**
- * History of operations that have been processed by the rules for the current policy.
- * When a step is started, its "start" outcome is added. However, once it completes,
- * its "start" outcome is removed and the "completed" outcome is added.
- */
- @Getter
- private final transient Deque<OperationOutcome2> partialHistory = new LinkedList<>();
-
- @Getter
- private OperationFinalResult finalResult = null;
-
- /**
- * Message to be placed into the final notification. Typically used when something
- * causes processing to abort.
- */
- @Getter
- private String finalMessage = null;
-
- private final transient WorkingMemory workMem;
- private transient FactHandle factHandle;
-
/**
* Constructs the object.
@@ -233,9 +114,7 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
public UsecasesEventManager(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
throws ControlLoopException {
- super(params, event.getRequestId());
-
- checkEventSyntax(event);
+ super(params, event, workMem);
if (isClosedLoopDisabled(event)) {
throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
@@ -244,127 +123,24 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
if (isProvStatusInactive(event)) {
throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
}
-
- this.event = event;
- this.workMem = workMem;
- this.requestIdStr = getRequestId().toString();
- }
-
- @Override
- public void destroy() {
- for (Step2 step : steps) {
- step.cancel();
- }
-
- super.destroy();
- }
-
- /**
- * Starts the manager and loads the first policy.
- *
- * @throws ControlLoopException if the processor cannot get a policy
- */
- public void start() throws ControlLoopException {
- if (!isActive()) {
- throw new IllegalStateException("manager is no longer active");
- }
-
- if ((factHandle = workMem.getFactHandle(this)) == null) {
- throw new IllegalStateException("manager is not in working memory");
- }
-
- if (!steps.isEmpty()) {
- throw new IllegalStateException("manager already started");
- }
-
- loadPolicy();
}
- /**
- * Indicates that processing has been aborted.
- *
- * @param finalState final state
- * @param finalResult final result
- * @param finalMessage final message
- */
- public void abort(@NonNull State finalState, OperationFinalResult finalResult, String finalMessage) {
- this.state = finalState;
- this.finalResult = finalResult;
- this.finalMessage = finalMessage;
- }
-
- /**
- * Loads the next policy.
- *
- * @param lastResult result from the last policy
- *
- * @throws ControlLoopException if the processor cannot get a policy
+ /*
+ * This is needed to satisfy drools.
*/
- public void loadNextPolicy(@NonNull OperationResult lastResult) throws ControlLoopException {
- getProcessor().nextPolicyForResult(lastResult);
- loadPolicy();
- }
-
- /**
- * Loads the current policy.
- *
- * @throws ControlLoopException if the processor cannot get a policy
- */
- private void loadPolicy() throws ControlLoopException {
- partialHistory.clear();
-
- if ((finalResult = getProcessor().checkIsCurrentPolicyFinal()) != null) {
- // final policy - nothing more to do
- return;
- }
-
- policy = getProcessor().getCurrentPolicy();
-
- ActorOperation actor = policy.getActorOperation();
-
- OperationalTarget target = actor.getTarget();
- String targetType = (target != null ? target.getTargetType() : null);
- Map<String, String> entityIds = (target != null ? target.getEntityIds() : null);
-
- // convert policy payload from Map<String,String> to Map<String,Object>
- Map<String, Object> payload = new LinkedHashMap<>();
- if (actor.getPayload() != null) {
- payload.putAll(actor.getPayload());
- }
-
- // @formatter:off
- ControlLoopOperationParams params = ControlLoopOperationParams.builder()
- .actorService(getActorService())
- .actor(actor.getActor())
- .operation(actor.getOperation())
- .requestId(event.getRequestId())
- .executor(getExecutor())
- .retry(policy.getRetries())
- .timeoutSec(policy.getTimeout())
- .targetType(TargetType.toTargetType(targetType))
- .targetEntityIds(entityIds)
- .payload(payload)
- .startCallback(this::onStart)
- .completeCallback(this::onComplete)
- .build();
- // @formatter:on
-
- // load the policy's operation
- steps.add(new Step2(this, params, event));
+ @Override
+ public Deque<Step2> getSteps() {
+ return super.getSteps();
}
/**
* Loads the preprocessor steps needed by the step that's at the front of the queue.
*/
public void loadPreprocessorSteps() {
- if (steps.size() >= MAX_STEPS) {
- throw new IllegalStateException("too many steps");
- }
-
- final Step2 step = steps.peek();
+ super.loadPreprocessorSteps();
- // initialize the step so we can query its properties
- step.init();
+ final Deque<Step2> steps = getSteps();
+ final Step2 step = getSteps().peek();
// determine if any A&AI queries are needed
boolean needCq = false;
@@ -422,124 +198,13 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
}
/**
- * Executes the first step in the queue.
- *
- * @return {@code true} if the step was started, {@code false} if it is no longer
- * needed (or if the queue is empty)
- */
- public boolean executeStep() {
- attempts = 0;
-
- Step2 step = steps.peek();
- if (step == null) {
- return false;
- }
-
- return step.start(getEndTimeMs() - System.currentTimeMillis());
- }
-
- /**
- * Discards the current step, if any.
- */
- public void nextStep() {
- steps.poll();
- }
-
- /**
- * Increments the number of attempts.
- */
- public void bumpAttempts() {
- ++attempts;
- }
-
- /**
* Determines if the TOSCA should be aborted due to the given outcome.
*
* @param outcome outcome to examine
* @return {@code true} if the TOSCA should be aborted, {@code false} otherwise
*/
public boolean isAbort(OperationOutcome outcome) {
- return (outcome.getResult() != OperationResult.SUCCESS && ABORT_ACTORS.contains(outcome.getActor()));
- }
-
- /**
- * Adds the outcome to the history.
- *
- * @param outcome outcome to add
- */
- public void addToHistory(OperationOutcome outcome) {
- OperationOutcome2 last = partialHistory.peekLast();
-
- if (last != null && last.getOutcome().getEnd() == null
- && last.getOutcome().isFor(outcome.getActor(), outcome.getOperation())) {
- // last item was a "start" - remove it
- partialHistory.removeLast();
-
- if (fullHistory.peekLast() == last) {
- fullHistory.removeLast();
- }
- }
-
- OperationOutcome2 outcome2 = new OperationOutcome2(outcome);
- partialHistory.add(outcome2);
- fullHistory.add(outcome2);
- }
-
- /**
- * Makes a notification message for the current operation.
- *
- * @return a new notification
- */
- public VirtualControlLoopNotification makeNotification() {
- VirtualControlLoopNotification notif = new VirtualControlLoopNotification(event);
- notif.setNotification(ControlLoopNotificationType.OPERATION);
- notif.setFrom("policy");
- notif.setPolicyVersion(getPolicyVersion());
-
- if (finalResult != null) {
- return notif;
- }
-
- OperationOutcome2 last = partialHistory.peekLast();
- if (last == null) {
- return notif;
- }
-
- notif.setMessage(last.getClOperation().toHistory());
- notif.setHistory(partialHistory.stream().map(OperationOutcome2::getClOperation).collect(Collectors.toList()));
-
- return notif;
- }
-
- /**
- * Delivers a notification to a topic.
- *
- * @param sinkName name of the topic sink
- * @param notification notification to be published, or {@code null} if nothing is to
- * be published
- * @param notificationType type of notification, used when logging error messages
- * @param ruleName name of the rule doing the publishing
- */
- public <T> void deliver(String sinkName, T notification, String notificationType, String ruleName) {
- try {
- if (notification != null) {
- getPolicyEngineManager().deliver(sinkName, notification);
- }
-
- } catch (RuntimeException e) {
- logger.warn("{}: {}.{}: manager={} exception publishing {}", getClosedLoopControlName(), getPolicyName(),
- ruleName, this, notificationType, e);
- }
- }
-
- /**
- * Get the last operation, as a message.
- *
- * @return the last operation, as a message
- */
- public String getOperationMessage() {
- OperationOutcome2 last = fullHistory.peekLast();
- return (last == null ? null : last.getClOperation().toMessage());
+ return (super.isAbort(outcome) && ABORT_ACTORS.contains(outcome.getActor()));
}
/**
@@ -548,10 +213,7 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
* @param outcome operation outcome to store
*/
public void storeInDataBase(OperationOutcome2 outcome) {
- String targetEntity = getProperty(OperationProperties.AAI_TARGET_ENTITY);
-
- getDataManager().store(requestIdStr, event.getClosedLoopControlName(), event, targetEntity,
- outcome.getClOperation());
+ storeInDataBase(outcome, getProperty(OperationProperties.AAI_TARGET_ENTITY));
}
/**
@@ -561,14 +223,7 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
* @return a new control loop response, or {@code null} if none is required
*/
public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
- ControlLoopResponse clRsp = new ControlLoopResponse();
- clRsp.setFrom(outcome.getActor());
- clRsp.setTarget("DCAE");
- clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
- clRsp.setPolicyName(event.getPolicyName());
- clRsp.setPolicyVersion(event.getPolicyVersion());
- clRsp.setRequestId(event.getRequestId());
- clRsp.setVersion(event.getVersion());
+ ControlLoopResponse clRsp = super.makeControlLoopResponse(outcome);
Object obj = outcome.getResponse();
if (!(obj instanceof PciMessage)) {
@@ -584,68 +239,22 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
}
/**
- * An event onset/abatement.
- *
- * @param newEvent the event
- * @return the status
- */
- public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
- try {
- checkEventSyntax(newEvent);
-
- if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
- if (newEvent.equals(event)) {
- return NewEventStatus.FIRST_ONSET;
- }
-
- numOnsets++;
- return NewEventStatus.SUBSEQUENT_ONSET;
-
- } else {
- if (abatement == null) {
- abatement = newEvent;
- numAbatements++;
- return NewEventStatus.FIRST_ABATEMENT;
- } else {
- numAbatements++;
- return NewEventStatus.SUBSEQUENT_ABATEMENT;
- }
- }
- } catch (ControlLoopException e) {
- logger.error("{}: onNewEvent threw an exception", this, e);
- return NewEventStatus.SYNTAX_ERROR;
- }
- }
-
- /**
* Check an event syntax.
*
* @param event the event syntax
* @throws ControlLoopException if an error occurs
*/
protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
- validateStatus(event);
- if (StringUtils.isBlank(event.getClosedLoopControlName())) {
- throw new ControlLoopException("No control loop name");
- }
- if (event.getRequestId() == null) {
- throw new ControlLoopException("No request ID");
- }
- if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
- return;
- }
- if (StringUtils.isBlank(event.getTarget())) {
- throw new ControlLoopException("No target field");
- } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
- throw new ControlLoopException("target field invalid");
- }
+ super.checkEventSyntax(event);
validateAaiData(event);
}
- private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
- if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
- && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
- throw new ControlLoopException("Invalid value in closedLoopEventStatus");
+ @Override
+ protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
+ super.validateTarget(event);
+
+ if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
+ throw new ControlLoopException("target field invalid");
}
}
@@ -724,51 +333,7 @@ public class UsecasesEventManager extends ControlLoopEventManager implements Ste
}
@Override
- public void onStart(OperationOutcome outcome) {
- super.onStart(outcome);
- workMem.update(factHandle, this);
- }
-
- @Override
- public void onComplete(OperationOutcome outcome) {
- super.onComplete(outcome);
- workMem.update(factHandle, this);
- }
-
- @Getter
- @ToString
- public class OperationOutcome2 {
- private final int attempt;
- private final OperationOutcome outcome;
- private final ControlLoopOperation clOperation;
-
- /**
- * Constructs the object.
- *
- * @param outcome outcome of the operation
- */
- public OperationOutcome2(OperationOutcome outcome) {
- this.outcome = outcome;
- this.attempt = attempts;
-
- clOperation = outcome.toControlLoopOperation();
-
- // TODO encode()?
- OperationalTarget target = policy.getActorOperation().getTarget();
- String targetStr = (target != null ? target.toString() : null);
- clOperation.setTarget(targetStr);
-
- if (outcome.getEnd() == null) {
- clOperation.setOutcome("Started");
- } else if (clOperation.getOutcome() == null) {
- clOperation.setOutcome("");
- }
- }
- }
-
- // these following methods may be overridden by junit tests
-
- protected PolicyEngine getPolicyEngineManager() {
- return PolicyEngineConstants.getManager();
+ protected void loadPolicyStep(ControlLoopOperationParams params) {
+ getSteps().add(new Step2(this, params, getEvent()));
}
}
diff --git a/controlloop/common/controller-usecases/src/main/resources/usecases.drl b/controlloop/common/controller-usecases/src/main/resources/usecases.drl
index 12c9849f9..49ab78d71 100644
--- a/controlloop/common/controller-usecases/src/main/resources/usecases.drl
+++ b/controlloop/common/controller-usecases/src/main/resources/usecases.drl
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 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.
@@ -42,9 +42,9 @@ import org.onap.policy.controlloop.eventmanager.ActorConstants;
import org.onap.policy.controlloop.eventmanager.Step;
import org.onap.policy.controlloop.utils.ControlLoopUtils;
import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager;
-import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager.State;
-import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager.NewEventStatus;
-import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager.OperationOutcome2;
+import org.onap.policy.controlloop.eventmanager.ClEventManagerWithSteps.State;
+import org.onap.policy.controlloop.eventmanager.ClEventManagerWithOutcome.OperationOutcome2;
+import org.onap.policy.controlloop.eventmanager.ClEventManagerWithEvent.NewEventStatus;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.slf4j.LoggerFactory;
diff --git a/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java b/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
index 7160f372f..318d6b7cc 100644
--- a/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
+++ b/controlloop/common/controller-usecases/src/test/java/org/onap/policy/drools/apps/controller/usecases/UsecasesEventManagerTest.java
@@ -22,17 +22,13 @@ package org.onap.policy.drools.apps.controller.usecases;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -45,7 +41,6 @@ import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
-import java.util.concurrent.ForkJoinPool;
import org.drools.core.WorkingMemory;
import org.junit.Before;
import org.junit.Test;
@@ -56,17 +51,14 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
-import org.onap.policy.common.utils.io.Serializer;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.controlloop.ControlLoopEventStatus;
import org.onap.policy.controlloop.ControlLoopException;
import org.onap.policy.controlloop.ControlLoopResponse;
import org.onap.policy.controlloop.ControlLoopTargetType;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
-import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.controlloop.actorserviceprovider.ActorService;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
-import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
@@ -77,7 +69,6 @@ import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.controlloop.eventmanager.ActorConstants;
import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
-import org.onap.policy.drools.apps.controller.usecases.UsecasesEventManager.NewEventStatus;
import org.onap.policy.drools.apps.controller.usecases.step.AaiCqStep2;
import org.onap.policy.drools.apps.controller.usecases.step.AaiGetPnfStep2;
import org.onap.policy.drools.apps.controller.usecases.step.AaiGetTenantStep2;
@@ -104,13 +95,10 @@ public class UsecasesEventManagerTest {
private static final String SIMPLE_ACTOR = "First";
private static final String SIMPLE_OPERATION = "OperationA";
private static final String MY_TARGET = "my-target";
- private static final String EVENT_MGR_MULTI_YAML =
- "../eventmanager/src/test/resources/eventManager/event-mgr-multi.yaml";
private static final String EVENT_MGR_SIMPLE_YAML =
"../eventmanager/src/test/resources/eventManager/event-mgr-simple.yaml";
private static final Coder yamlCoder = new StandardYamlCoder();
private static final String OUTCOME_MSG = "my outcome message";
- private static final String MY_SINK = "my-topic-sink";
@Mock
private PolicyEngine engineMgr;
@@ -221,163 +209,6 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testIsActive() throws Exception {
- mgr = new UsecasesEventManager(params, event, workMem);
- assertTrue(mgr.isActive());
-
- // deserialized manager should be inactive
- UsecasesEventManager mgr2 = Serializer.roundTrip(mgr);
- assertFalse(mgr2.isActive());
- }
-
- @Test
- public void testDestroy_testGetSteps() {
- // add some steps to the queue
- mgr.getSteps().add(stepa);
- mgr.getSteps().add(stepb);
-
- mgr.destroy();
-
- verify(stepa).cancel();
- verify(stepb).cancel();
-
- // if superclass destroy() was invoked, then freeLock() should have been submitted
- // to the executor
- verify(executor).execute(any());
- }
-
- @Test
- public void testOnStart() throws ControlLoopException {
- OperationOutcome outcome = makeOutcome();
-
- mgr.start();
- mgr.onStart(outcome);
-
- assertSame(outcome, mgr.getOutcomes().poll());
- assertThat(mgr.getOutcomes()).isEmpty();
-
- verify(workMem).update(factHandle, mgr);
- }
-
- @Test
- public void testOnComplete() throws ControlLoopException {
- OperationOutcome outcome = makeCompletedOutcome();
-
- mgr.start();
- mgr.onComplete(outcome);
-
- assertSame(outcome, mgr.getOutcomes().poll());
- assertThat(mgr.getOutcomes()).isEmpty();
-
- verify(workMem).update(factHandle, mgr);
- }
-
- @Test
- public void testToString() {
- assertNotNull(mgr.toString());
- }
-
- @Test
- public void testStart() throws ControlLoopException {
- // start it
- mgr.start();
-
- // cannot re-start
- assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
- .hasMessage("manager already started");
- }
-
- /**
- * Tests start() when the manager is not in working memory.
- */
- @Test
- public void testStartNotInWorkingMemory() throws ControlLoopException {
- when(workMem.getFactHandle(any())).thenReturn(null);
-
- assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
- .hasMessage("manager is not in working memory");
- }
-
- /**
- * Tests start() when the manager is not active.
- */
- @Test
- public void testStartInactive() throws Exception {
- // make an inactive manager by deserializing it
- mgr = Serializer.roundTrip(new UsecasesEventManager(params, event, workMem));
-
- // cannot re-start
- assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
- .hasMessage("manager is no longer active");
- }
-
- @Test
- public void testAbort() {
- mgr.abort(UsecasesEventManager.State.DONE, OperationFinalResult.FINAL_FAILURE_GUARD, "some message");
-
- assertEquals(UsecasesEventManager.State.DONE, mgr.getState());
- assertEquals(OperationFinalResult.FINAL_FAILURE_GUARD, mgr.getFinalResult());
- assertEquals("some message", mgr.getFinalMessage());
-
- // try null state
- assertThatThrownBy(() -> mgr.abort(null, OperationFinalResult.FINAL_FAILURE_GUARD, ""))
- .isInstanceOf(NullPointerException.class).hasMessageContaining("finalState");
- }
-
- @Test
- public void testLoadNextPolicy_testGetFullHistory_testGetPartialHistory() throws Exception {
- loadPolicy(EVENT_MGR_MULTI_YAML);
- mgr = new MyManager(params, event, workMem);
-
- // start and load step for first policy
- mgr.start();
- assertEquals("OperationA", mgr.getSteps().poll().getOperationName());
- assertNull(mgr.getFinalResult());
-
- // add an outcome
- OperationOutcome outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- // indicate success and load next policy
- mgr.loadNextPolicy(OperationResult.SUCCESS);
- assertEquals("OperationB", mgr.getSteps().poll().getOperationName());
- assertNull(mgr.getFinalResult());
-
- // loadPolicy() should clear the partial history, but not the full history
- assertThat(mgr.getPartialHistory()).isEmpty();
- assertThat(mgr.getFullHistory()).hasSize(1);
-
- // indicate failure - should go to final failure
- mgr.loadNextPolicy(OperationResult.FAILURE);
- assertEquals(OperationFinalResult.FINAL_FAILURE, mgr.getFinalResult());
- }
-
- @Test
- public void testLoadPolicy() throws ControlLoopException {
- // start() will invoke loadPolicy()
- mgr.start();
-
- assertNull(mgr.getFinalResult());
-
- Step2 step = mgr.getSteps().peek();
- assertNotNull(step);
- assertEquals("First", step.getActorName());
- assertEquals("OperationA", step.getOperationName());
-
- ControlLoopOperationParams params2 = step.getParams();
- assertSame(actors, params2.getActorService());
- assertSame(REQ_ID, params2.getRequestId());
- assertSame(ForkJoinPool.commonPool(), params2.getExecutor());
- assertNotNull(params2.getTargetType());
- assertNotNull(params2.getTargetEntityIds());
- assertEquals(Integer.valueOf(300), params2.getTimeoutSec());
- assertEquals(Integer.valueOf(0), params2.getRetry());
- assertThat(params2.getPayload()).isEmpty();
- assertNotNull(params2.getStartCallback());
- assertNotNull(params2.getCompleteCallback());
- }
-
- @Test
public void testLoadPreprocessorSteps() {
stepa = new Step2(mgr, ControlLoopOperationParams.builder().build(), event) {
@Override
@@ -409,39 +240,6 @@ public class UsecasesEventManagerTest {
}
/**
- * Tests loadPreprocessorSteps() when there are too many steps in the queue.
- */
- @Test
- public void testLoadPreprocessorStepsTooManySteps() {
- loadStepsWithProperties(OperationProperties.AAI_PNF);
-
- Deque<Step2> steps = mgr.getSteps();
- stepa = steps.getFirst();
- steps.clear();
-
- // load up a bunch of steps
- for (int nsteps = 0; nsteps < UsecasesEventManager.MAX_STEPS; ++nsteps) {
- steps.add(stepa);
- }
-
- // should fail
- assertThatIllegalStateException().isThrownBy(() -> mgr.loadPreprocessorSteps()).withMessage("too many steps");
-
- // add another step, should still fail
- steps.add(stepa);
- assertThatIllegalStateException().isThrownBy(() -> mgr.loadPreprocessorSteps()).withMessage("too many steps");
-
- // remove two steps - should now succeed
- steps.remove();
- steps.remove();
-
- int nsteps = steps.size();
-
- mgr.loadPreprocessorSteps();
- assertEquals(nsteps + 1, steps.size());
- }
-
- /**
* Tests loadPreprocessorSteps() when no additional steps are needed.
*/
@Test
@@ -563,44 +361,6 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testExecuteStep() {
- mgr.bumpAttempts();
-
- // no steps to execute
- assertFalse(mgr.executeStep());
- assertEquals(0, mgr.getAttempts());
-
- // add a step to the queue
- mgr.getSteps().add(stepa);
-
- // step returns false
- when(stepa.start(anyLong())).thenReturn(false);
- assertFalse(mgr.executeStep());
-
- // step returns true
- when(stepa.start(anyLong())).thenReturn(true);
- assertTrue(mgr.executeStep());
- }
-
- @Test
- public void testNextStep() {
- mgr.getSteps().add(stepa);
-
- mgr.nextStep();
-
- assertThat(mgr.getSteps()).isEmpty();
- }
-
- @Test
- public void testBumpAttempts() {
- assertEquals(0, mgr.getAttempts());
-
- mgr.bumpAttempts();
- mgr.bumpAttempts();
- assertEquals(2, mgr.getAttempts());
- }
-
- @Test
public void testIsAbort() {
OperationOutcome outcome = makeCompletedOutcome();
outcome.setResult(OperationResult.FAILURE);
@@ -619,137 +379,6 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testAddToHistory() throws ControlLoopException {
- mgr.start();
-
- // add a "start" outcome
- OperationOutcome outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getPartialHistory()).hasSize(1);
- assertThat(mgr.getFullHistory()).hasSize(1);
-
- // add a "completion" outcome - should replace the start
- outcome = makeCompletedOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getPartialHistory()).hasSize(1);
- assertThat(mgr.getFullHistory()).hasSize(1);
- assertSame(outcome, mgr.getPartialHistory().peek().getOutcome());
- assertSame(outcome, mgr.getFullHistory().peek().getOutcome());
-
- // add another start
- outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getPartialHistory()).hasSize(2);
- assertThat(mgr.getFullHistory()).hasSize(2);
- assertSame(outcome, mgr.getPartialHistory().peekLast().getOutcome());
- assertSame(outcome, mgr.getFullHistory().peekLast().getOutcome());
-
- // remove the last item from the full history and then add a "completion"
- mgr.getFullHistory().removeLast();
- outcome = makeCompletedOutcome();
- mgr.addToHistory(outcome);
- assertThat(mgr.getPartialHistory()).hasSize(2);
- assertThat(mgr.getFullHistory()).hasSize(2);
-
- // add another "start"
- outcome = makeOutcome();
- mgr.addToHistory(outcome);
- assertThat(mgr.getPartialHistory()).hasSize(3);
- assertThat(mgr.getFullHistory()).hasSize(3);
-
- // add a "completion" for a different actor - should NOT replace the start
- outcome = makeCompletedOutcome();
- outcome.setActor("different-actor");
- mgr.addToHistory(outcome);
- assertThat(mgr.getPartialHistory()).hasSize(4);
- assertThat(mgr.getFullHistory()).hasSize(4);
- assertSame(outcome, mgr.getPartialHistory().peekLast().getOutcome());
- assertSame(outcome, mgr.getFullHistory().peekLast().getOutcome());
- }
-
- @Test
- public void testMakeNotification() throws Exception {
- loadPolicy(EVENT_MGR_MULTI_YAML);
- mgr = new MyManager(params, event, workMem);
-
- // before started
- assertNotNull(mgr.makeNotification());
-
- mgr.start();
-
- mgr.addToHistory(makeCompletedOutcome());
- mgr.addToHistory(makeCompletedOutcome());
- mgr.addToHistory(makeCompletedOutcome());
-
- // check notification while running
- VirtualControlLoopNotification notif = mgr.makeNotification();
- assertThat(notif.getMessage()).contains(SIMPLE_ACTOR);
- assertThat(notif.getHistory()).hasSize(3);
-
- // indicate success and load the next policy - should clear the partial history
- mgr.loadNextPolicy(OperationResult.SUCCESS);
-
- // check notification
- notif = mgr.makeNotification();
- assertNull(notif.getMessage());
- assertThat(notif.getHistory()).isEmpty();
-
- // add outcomes and check again
- mgr.addToHistory(makeCompletedOutcome());
- mgr.addToHistory(makeCompletedOutcome());
-
- notif = mgr.makeNotification();
- assertNotNull(notif.getMessage());
-
- // should only have history for last two outcomes
- assertThat(notif.getHistory()).hasSize(2);
-
- // indicate failure - should go to final state
- mgr.loadNextPolicy(OperationResult.FAILURE);
-
- // check notification
- notif = mgr.makeNotification();
- assertNull(notif.getMessage());
-
- // should be no history
- assertThat(notif.getHistory()).isEmpty();
-
- // null case
- assertThatThrownBy(() -> mgr.loadNextPolicy(null)).isInstanceOf(NullPointerException.class)
- .hasMessageContaining("lastResult");
- }
-
- @Test
- public void testDeliver() {
- mgr.deliver(MY_SINK, null, "null notification", "null rule");
- verify(engineMgr, never()).deliver(any(), any());
-
- mgr.deliver(MY_SINK, "publishA", "A notification", "A rule");
- verify(engineMgr).deliver(MY_SINK, "publishA");
-
- // cause deliver() to throw an exception
- when(engineMgr.deliver(any(), any())).thenThrow(new IllegalStateException("expected exception"));
- assertThatCode(() -> mgr.deliver(MY_SINK, "publishB", "B notification", "B rule")).doesNotThrowAnyException();
- }
-
- @Test
- public void testGetOperationMessage() throws ControlLoopException {
- // no history yet
- assertNull(mgr.getOperationMessage());
-
- // add an outcome
- mgr.start();
- OperationOutcome outcome = makeOutcome();
- mgr.addToHistory(outcome);
-
- assertThat(mgr.getOperationMessage()).contains("actor=" + SIMPLE_ACTOR)
- .contains("operation=" + SIMPLE_OPERATION);
- }
-
- @Test
public void testStoreInDataBase() throws ControlLoopException {
mgr.start();
OperationOutcome outcome = makeOutcome();
@@ -806,27 +435,11 @@ public class UsecasesEventManagerTest {
}
@Test
- public void testOnNewEvent() {
- VirtualControlLoopEvent event2 = new VirtualControlLoopEvent(event);
- assertEquals(NewEventStatus.FIRST_ONSET, mgr.onNewEvent(event2));
-
- event2.setPayload("other payload");
- assertEquals(NewEventStatus.SUBSEQUENT_ONSET, mgr.onNewEvent(event2));
- assertEquals(NewEventStatus.SUBSEQUENT_ONSET, mgr.onNewEvent(event2));
- assertEquals(NewEventStatus.FIRST_ONSET, mgr.onNewEvent(event));
-
- event2.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
- assertEquals(NewEventStatus.FIRST_ABATEMENT, mgr.onNewEvent(event2));
-
- assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, mgr.onNewEvent(event2));
- assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, mgr.onNewEvent(event2));
-
- event2.setClosedLoopEventStatus(null);
- assertEquals(NewEventStatus.SYNTAX_ERROR, mgr.onNewEvent(event2));
- }
-
- @Test
public void testCheckEventSyntax() {
+ /*
+ * only need to check one success and one failure from the super class method
+ */
+
// initially, it's valid
assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
@@ -838,27 +451,19 @@ public class UsecasesEventManagerTest {
assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
.hasMessage("No target field");
- // abated supersedes previous errors - so it shouldn't throw an exception
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
- assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
-
event.setRequestId(null);
assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
.hasMessage("No request ID");
-
- event.setClosedLoopControlName(null);
- assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
- .hasMessage("No control loop name");
}
@Test
public void testValidateStatus() {
+ /*
+ * only need to check one success and one failure from the super class method
+ */
event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
- assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
-
event.setClosedLoopEventStatus(null);
assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
.hasMessage("Invalid value in closedLoopEventStatus");
@@ -985,6 +590,7 @@ public class UsecasesEventManagerTest {
}
+
private Map<String, String> addAai(Map<String, String> original, String key, String value) {
Map<String, String> map = new TreeMap<>(original);
map.put(key, value);
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEvent.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEvent.java
new file mode 100644
index 000000000..db7ec1d93
--- /dev/null
+++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEvent.java
@@ -0,0 +1,200 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 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.
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.eventmanager;
+
+import java.util.HashMap;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
+import org.drools.core.WorkingMemory;
+import org.onap.policy.controlloop.ControlLoopEventStatus;
+import org.onap.policy.controlloop.ControlLoopException;
+import org.onap.policy.controlloop.ControlLoopResponse;
+import org.onap.policy.controlloop.VirtualControlLoopEvent;
+import org.onap.policy.controlloop.VirtualControlLoopNotification;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Manager for a single control loop event. Once this has been created, the event can be
+ * retracted from working memory.
+ */
+public abstract class ClEventManagerWithEvent<T extends Step> extends ClEventManagerWithOutcome<T>
+ implements StepContext {
+
+ private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithEvent.class);
+ private static final long serialVersionUID = -1216568161322872641L;
+
+ public enum NewEventStatus {
+ FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
+ }
+
+ @Getter
+ private final VirtualControlLoopEvent event;
+
+ @Getter
+ @Setter(AccessLevel.PROTECTED)
+ private VirtualControlLoopEvent abatement = null;
+
+
+ /**
+ * Constructs the object.
+ *
+ * @param params control loop parameters
+ * @param event event to be managed by this object
+ * @param workMem working memory to update if this changes
+ * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
+ * be created
+ */
+ public ClEventManagerWithEvent(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
+ throws ControlLoopException {
+
+ super(params, event.getRequestId(), workMem);
+
+ checkEventSyntax(event);
+
+ this.event = event;
+ }
+
+ @Override
+ protected void populateNotification(VirtualControlLoopNotification notif) {
+ super.populateNotification(notif);
+
+ notif.setClosedLoopControlName(event.getClosedLoopControlName());
+ notif.setRequestId(event.getRequestId());
+ notif.setClosedLoopEventClient(event.getClosedLoopEventClient());
+ notif.setTargetType(event.getTargetType());
+ notif.setTarget(event.getTarget());
+
+ if (event.getAai() != null) {
+ notif.setAai(new HashMap<>(event.getAai()));
+ }
+ notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
+ notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
+ }
+
+ /**
+ * Stores an operation outcome in the DB.
+ *
+ * @param outcome operation outcome to store
+ * @param targetEntity target entity
+ */
+ protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
+ getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
+ outcome.getClOperation());
+ }
+
+ @Override
+ public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
+ ControlLoopResponse clRsp = super.makeControlLoopResponse(outcome);
+ clRsp.setTarget("DCAE");
+
+ clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
+ clRsp.setPolicyName(event.getPolicyName());
+ clRsp.setPolicyVersion(event.getPolicyVersion());
+ clRsp.setRequestId(event.getRequestId());
+ clRsp.setVersion(event.getVersion());
+
+ return clRsp;
+ }
+
+ /**
+ * An event onset/abatement.
+ *
+ * @param newEvent the event
+ * @return the status
+ */
+ public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
+ try {
+ checkEventSyntax(newEvent);
+
+ if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
+ if (newEvent.equals(event)) {
+ return NewEventStatus.FIRST_ONSET;
+ }
+
+ bumpOffsets();
+ return NewEventStatus.SUBSEQUENT_ONSET;
+
+ } else {
+ if (abatement == null) {
+ abatement = newEvent;
+ bumpAbatements();
+ return NewEventStatus.FIRST_ABATEMENT;
+ } else {
+ bumpAbatements();
+ return NewEventStatus.SUBSEQUENT_ABATEMENT;
+ }
+ }
+ } catch (ControlLoopException e) {
+ logger.error("{}: onNewEvent threw an exception", this, e);
+ return NewEventStatus.SYNTAX_ERROR;
+ }
+ }
+
+ /**
+ * Check an event syntax.
+ *
+ * @param event the event syntax
+ * @throws ControlLoopException if an error occurs
+ */
+ protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
+ validateStatus(event);
+ if (StringUtils.isBlank(event.getClosedLoopControlName())) {
+ throw new ControlLoopException("No control loop name");
+ }
+ if (event.getRequestId() == null) {
+ throw new ControlLoopException("No request ID");
+ }
+ if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
+ return;
+ }
+ validateTarget(event);
+ }
+
+ /**
+ * Verifies that the event status is valid.
+ *
+ * @param event event to check
+ * @throws ControlLoopException if the status is invalid
+ */
+ protected void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
+ if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
+ && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
+ throw new ControlLoopException("Invalid value in closedLoopEventStatus");
+ }
+ }
+
+ /**
+ * Verifies that the event target is valid.
+ *
+ * @param event event to check
+ * @throws ControlLoopException if the status is invalid
+ */
+ protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
+ if (StringUtils.isBlank(event.getTarget())) {
+ throw new ControlLoopException("No target field");
+ }
+ }
+}
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcome.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcome.java
new file mode 100644
index 000000000..a94598ef7
--- /dev/null
+++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcome.java
@@ -0,0 +1,229 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 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.
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.eventmanager;
+
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import lombok.Getter;
+import lombok.ToString;
+import org.drools.core.WorkingMemory;
+import org.onap.policy.controlloop.ControlLoopException;
+import org.onap.policy.controlloop.ControlLoopNotificationType;
+import org.onap.policy.controlloop.ControlLoopOperation;
+import org.onap.policy.controlloop.ControlLoopResponse;
+import org.onap.policy.controlloop.VirtualControlLoopNotification;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.onap.policy.drools.domain.models.operational.OperationalTarget;
+
+/**
+ * Manager for a single control loop event, with operation outcomes.
+ */
+public abstract class ClEventManagerWithOutcome<T extends Step> extends ClEventManagerWithSteps<T>
+ implements StepContext {
+
+ private static final long serialVersionUID = -1216568161322872641L;
+
+ /**
+ * Number of attempts, so far, for the current step.
+ */
+ @Getter
+ private int attempts;
+
+ /**
+ * Full history of operations that have been processed by the rules. This includes the
+ * items in {@link #partialHistory}.
+ */
+ @Getter
+ private final transient Deque<OperationOutcome2> fullHistory = new LinkedList<>();
+
+ /**
+ * History of operations that have been processed by the rules for the current policy.
+ * When a step is started, its "start" outcome is added. However, once it completes,
+ * its "start" outcome is removed and the "completed" outcome is added.
+ */
+ @Getter
+ private final transient Deque<OperationOutcome2> partialHistory = new LinkedList<>();
+
+
+ /**
+ * Constructs the object.
+ *
+ * @param params control loop parameters
+ * @param requestId event request ID
+ * @param workMem working memory to update if this changes
+ * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
+ * be created
+ */
+ public ClEventManagerWithOutcome(ControlLoopParams params, UUID requestId, WorkingMemory workMem)
+ throws ControlLoopException {
+
+ super(params, requestId, workMem);
+ }
+
+ @Override
+ protected void loadPolicy() throws ControlLoopException {
+ partialHistory.clear();
+ super.loadPolicy();
+ }
+
+ @Override
+ public boolean executeStep() {
+ attempts = 0;
+ return super.executeStep();
+ }
+
+ /**
+ * Increments the number of attempts.
+ */
+ public void bumpAttempts() {
+ ++attempts;
+ }
+
+ /**
+ * Determines if the TOSCA should be aborted due to the given outcome.
+ *
+ * @param outcome outcome to examine
+ * @return {@code true} if the TOSCA should be aborted, {@code false} otherwise
+ */
+ public boolean isAbort(OperationOutcome outcome) {
+ return (outcome.getResult() != OperationResult.SUCCESS);
+ }
+
+ /**
+ * Adds the outcome to the history.
+ *
+ * @param outcome outcome to add
+ */
+ public void addToHistory(OperationOutcome outcome) {
+ OperationOutcome2 last = partialHistory.peekLast();
+
+ if (last != null && last.getOutcome().getEnd() == null
+ && last.getOutcome().isFor(outcome.getActor(), outcome.getOperation())) {
+ // last item was a "start" - remove it
+ partialHistory.removeLast();
+
+ if (fullHistory.peekLast() == last) {
+ fullHistory.removeLast();
+ }
+ }
+
+ OperationOutcome2 outcome2 = makeOperationOutcome2(outcome);
+ partialHistory.add(outcome2);
+ fullHistory.add(outcome2);
+ }
+
+ /**
+ * Makes a notification message for the current operation.
+ *
+ * @return a new notification
+ */
+ public VirtualControlLoopNotification makeNotification() {
+ VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
+ populateNotification(notif);
+
+ if (getFinalResult() != null) {
+ return notif;
+ }
+
+ OperationOutcome2 last = partialHistory.peekLast();
+ if (last == null) {
+ return notif;
+ }
+
+ notif.setMessage(last.getClOperation().toHistory());
+ notif.setHistory(partialHistory.stream().map(OperationOutcome2::getClOperation).collect(Collectors.toList()));
+
+ return notif;
+ }
+
+ /**
+ * Populates a notification structure.
+ *
+ * @param notif the notification to populate
+ */
+ protected void populateNotification(VirtualControlLoopNotification notif) {
+ notif.setNotification(ControlLoopNotificationType.OPERATION);
+ notif.setFrom("policy");
+ notif.setPolicyVersion(getPolicyVersion());
+ }
+
+ /**
+ * Get the last operation, as a message.
+ *
+ * @return the last operation, as a message
+ */
+ public String getOperationMessage() {
+ OperationOutcome2 last = fullHistory.peekLast();
+ return (last == null ? null : last.getClOperation().toMessage());
+ }
+
+ /**
+ * Makes a control loop response.
+ *
+ * @param outcome operation outcome
+ * @return a new control loop response, or {@code null} if none is required
+ */
+ public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
+ ControlLoopResponse clRsp = new ControlLoopResponse();
+ clRsp.setFrom(outcome.getActor());
+
+ return clRsp;
+ }
+
+ @Getter
+ @ToString
+ public class OperationOutcome2 {
+ private final int attempt;
+ private final OperationOutcome outcome;
+ private final ControlLoopOperation clOperation;
+
+ /**
+ * Constructs the object.
+ *
+ * @param outcome outcome of the operation
+ */
+ public OperationOutcome2(OperationOutcome outcome) {
+ this.outcome = outcome;
+ this.attempt = attempts;
+
+ clOperation = outcome.toControlLoopOperation();
+
+ // TODO encode()?
+ OperationalTarget target = getPolicy().getActorOperation().getTarget();
+ String targetStr = (target != null ? target.toString() : null);
+ clOperation.setTarget(targetStr);
+
+ if (outcome.getEnd() == null) {
+ clOperation.setOutcome("Started");
+ } else if (clOperation.getOutcome() == null) {
+ clOperation.setOutcome("");
+ }
+ }
+ }
+
+ protected OperationOutcome2 makeOperationOutcome2(OperationOutcome outcome) {
+ return new OperationOutcome2(outcome);
+ }
+}
diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithSteps.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithSteps.java
new file mode 100644
index 000000000..6f6cd0fda
--- /dev/null
+++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithSteps.java
@@ -0,0 +1,341 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 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.
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.eventmanager;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.UUID;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.NonNull;
+import lombok.Setter;
+import lombok.ToString;
+import org.drools.core.WorkingMemory;
+import org.kie.api.runtime.rule.FactHandle;
+import org.onap.policy.controlloop.ControlLoopException;
+import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
+import org.onap.policy.controlloop.actorserviceprovider.TargetType;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.onap.policy.drools.domain.models.operational.ActorOperation;
+import org.onap.policy.drools.domain.models.operational.Operation;
+import org.onap.policy.drools.domain.models.operational.OperationalTarget;
+import org.onap.policy.drools.system.PolicyEngine;
+import org.onap.policy.drools.system.PolicyEngineConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Manager for a single control loop event. Processing progresses through each policy,
+ * which involves at least one step. As a step is processed, additional preprocessor steps
+ * may be pushed onto the queue (e.g., locks, A&AI queries, guards).
+ */
+@ToString(onlyExplicitlyIncluded = true)
+public abstract class ClEventManagerWithSteps<T extends Step> extends ControlLoopEventManager implements StepContext {
+
+ private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithSteps.class);
+ private static final long serialVersionUID = -1216568161322872641L;
+
+ /**
+ * Maximum number of steps, for a single policy, allowed in the queue at a time. This
+ * prevents an infinite loop occurring with calls to {@link #loadPreprocessorSteps()}.
+ */
+ public static final int MAX_STEPS = 30;
+
+ public enum State {
+ LOAD_POLICY, POLICY_LOADED, AWAITING_OUTCOME, DONE
+ }
+
+ /**
+ * Request ID, as a String.
+ */
+ @Getter
+ private final String requestIdStr;
+
+ @Getter
+ @Setter
+ private State state;
+
+ /**
+ * {@code True} if the event has been accepted (i.e., an "ACTIVE" notification has
+ * been delivered), {@code false} otherwise.
+ */
+ @Getter
+ @Setter
+ private boolean accepted;
+
+ /**
+ * Queue of steps waiting to be performed.
+ */
+ @Getter
+ private final transient Deque<T> steps = new ArrayDeque<>(6);
+
+ /**
+ * Policy currently being processed.
+ */
+ @Getter(AccessLevel.PROTECTED)
+ private Operation policy;
+
+ /**
+ * Result of the last policy operation. This is just a place where the rules can store
+ * the value for passing to {@link #loadNextPolicy()}.
+ */
+ @Getter
+ @Setter
+ private OperationResult result = OperationResult.SUCCESS;
+
+ @Getter
+ @ToString.Include
+ private int numOnsets = 1;
+ @Getter
+ @ToString.Include
+ private int numAbatements = 0;
+
+ @Getter
+ private OperationFinalResult finalResult = null;
+
+ /**
+ * Message to be placed into the final notification. Typically used when something
+ * causes processing to abort.
+ */
+ @Getter
+ private String finalMessage = null;
+
+ private final transient WorkingMemory workMem;
+ private transient FactHandle factHandle;
+
+
+ /**
+ * Constructs the object.
+ *
+ * @param params control loop parameters
+ * @param requestId event request ID
+ * @param workMem working memory to update if this changes
+ * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
+ * be created
+ */
+ public ClEventManagerWithSteps(ControlLoopParams params, UUID requestId, WorkingMemory workMem)
+ throws ControlLoopException {
+
+ super(params, requestId);
+
+ if (requestId == null) {
+ throw new ControlLoopException("No request ID");
+ }
+
+ this.workMem = workMem;
+ this.requestIdStr = getRequestId().toString();
+ }
+
+ @Override
+ public void destroy() {
+ for (T step : getSteps()) {
+ step.cancel();
+ }
+
+ super.destroy();
+ }
+
+ /**
+ * Starts the manager and loads the first policy.
+ *
+ * @throws ControlLoopException if the processor cannot get a policy
+ */
+ public void start() throws ControlLoopException {
+ if (!isActive()) {
+ throw new IllegalStateException("manager is no longer active");
+ }
+
+ if ((factHandle = workMem.getFactHandle(this)) == null) {
+ throw new IllegalStateException("manager is not in working memory");
+ }
+
+ if (!getSteps().isEmpty()) {
+ throw new IllegalStateException("manager already started");
+ }
+
+ loadPolicy();
+ }
+
+ /**
+ * Indicates that processing has been aborted.
+ *
+ * @param finalState final state
+ * @param finalResult final result
+ * @param finalMessage final message
+ */
+ public void abort(@NonNull State finalState, OperationFinalResult finalResult, String finalMessage) {
+ this.state = finalState;
+ this.finalResult = finalResult;
+ this.finalMessage = finalMessage;
+ }
+
+ /**
+ * Loads the next policy.
+ *
+ * @param lastResult result from the last policy
+ *
+ * @throws ControlLoopException if the processor cannot get a policy
+ */
+ public void loadNextPolicy(@NonNull OperationResult lastResult) throws ControlLoopException {
+ getProcessor().nextPolicyForResult(lastResult);
+ loadPolicy();
+ }
+
+ /**
+ * Loads the current policy.
+ *
+ * @throws ControlLoopException if the processor cannot get a policy
+ */
+ protected void loadPolicy() throws ControlLoopException {
+ if ((finalResult = getProcessor().checkIsCurrentPolicyFinal()) != null) {
+ // final policy - nothing more to do
+ return;
+ }
+
+ policy = getProcessor().getCurrentPolicy();
+
+ ActorOperation actor = policy.getActorOperation();
+
+ OperationalTarget target = actor.getTarget();
+ String targetType = (target != null ? target.getTargetType() : null);
+ Map<String, String> entityIds = (target != null ? target.getEntityIds() : null);
+
+ // convert policy payload from Map<String,String> to Map<String,Object>
+ Map<String, Object> payload = new LinkedHashMap<>();
+ if (actor.getPayload() != null) {
+ payload.putAll(actor.getPayload());
+ }
+
+ // @formatter:off
+ ControlLoopOperationParams params = ControlLoopOperationParams.builder()
+ .actorService(getActorService())
+ .actor(actor.getActor())
+ .operation(actor.getOperation())
+ .requestId(getRequestId())
+ .executor(getExecutor())
+ .retry(policy.getRetries())
+ .timeoutSec(policy.getTimeout())
+ .targetType(TargetType.toTargetType(targetType))
+ .targetEntityIds(entityIds)
+ .payload(payload)
+ .startCallback(this::onStart)
+ .completeCallback(this::onComplete)
+ .build();
+ // @formatter:on
+
+ // load the policy's operation
+ loadPolicyStep(params);
+ }
+
+ /**
+ * Makes the step associated with the given parameters.
+ *
+ * @param params operation's parameters
+ * @return a new step
+ */
+ protected abstract void loadPolicyStep(ControlLoopOperationParams params);
+
+ /**
+ * Loads the preprocessor steps needed by the step that's at the front of the queue.
+ */
+ public void loadPreprocessorSteps() {
+ if (getSteps().size() >= MAX_STEPS) {
+ throw new IllegalStateException("too many steps");
+ }
+
+ // initialize the step so we can query its properties
+ getSteps().peek().init();
+ }
+
+ /**
+ * Executes the first step in the queue.
+ *
+ * @return {@code true} if the step was started, {@code false} if it is no longer
+ * needed (or if the queue is empty)
+ */
+ public boolean executeStep() {
+ T step = getSteps().peek();
+ if (step == null) {
+ return false;
+ }
+
+ return step.start(getEndTimeMs() - System.currentTimeMillis());
+ }
+
+ /**
+ * Discards the current step, if any.
+ */
+ public void nextStep() {
+ getSteps().poll();
+ }
+
+ /**
+ * Delivers a notification to a topic.
+ *
+ * @param sinkName name of the topic sink
+ * @param notification notification to be published, or {@code null} if nothing is to
+ * be published
+ * @param notificationType type of notification, used when logging error messages
+ * @param ruleName name of the rule doing the publishing
+ */
+ public <N> void deliver(String sinkName, N notification, String notificationType, String ruleName) {
+ try {
+ if (notification != null) {
+ getPolicyEngineManager().deliver(sinkName, notification);
+ }
+
+ } catch (RuntimeException e) {
+ logger.warn("{}: {}.{}: manager={} exception publishing {}", getClosedLoopControlName(), getPolicyName(),
+ ruleName, this, notificationType, e);
+ }
+ }
+
+ protected int bumpOffsets() {
+ return numOnsets++;
+ }
+
+ protected int bumpAbatements() {
+ return numAbatements++;
+ }
+
+ @Override
+ public void onStart(OperationOutcome outcome) {
+ super.onStart(outcome);
+ workMem.update(factHandle, this);
+ }
+
+ @Override
+ public void onComplete(OperationOutcome outcome) {
+ super.onComplete(outcome);
+ workMem.update(factHandle, this);
+ }
+
+ // these following methods may be overridden by junit tests
+
+ protected PolicyEngine getPolicyEngineManager() {
+ return PolicyEngineConstants.getManager();
+ }
+}
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEventTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEventTest.java
new file mode 100644
index 000000000..1a4c1b5b2
--- /dev/null
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithEventTest.java
@@ -0,0 +1,380 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 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.
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.eventmanager;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicReference;
+import org.drools.core.WorkingMemory;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.kie.api.runtime.rule.FactHandle;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.controlloop.ControlLoopEventStatus;
+import org.onap.policy.controlloop.ControlLoopException;
+import org.onap.policy.controlloop.ControlLoopResponse;
+import org.onap.policy.controlloop.ControlLoopTargetType;
+import org.onap.policy.controlloop.VirtualControlLoopEvent;
+import org.onap.policy.controlloop.VirtualControlLoopNotification;
+import org.onap.policy.controlloop.actorserviceprovider.ActorService;
+import org.onap.policy.controlloop.actorserviceprovider.Operation;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
+import org.onap.policy.controlloop.actorserviceprovider.Operator;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.onap.policy.controlloop.eventmanager.ClEventManagerWithEvent.NewEventStatus;
+import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
+import org.onap.policy.drools.core.lock.LockCallback;
+import org.onap.policy.drools.core.lock.LockImpl;
+import org.onap.policy.drools.core.lock.LockState;
+import org.onap.policy.drools.system.PolicyEngine;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ClEventManagerWithEventTest {
+ private static final UUID REQ_ID = UUID.randomUUID();
+ private static final String CL_NAME = "my-closed-loop-name";
+ private static final String POLICY_NAME = "my-policy-name";
+ private static final String POLICY_SCOPE = "my-scope";
+ private static final String POLICY_VERSION = "1.2.3";
+ private static final String SIMPLE_ACTOR = "First";
+ private static final String SIMPLE_OPERATION = "OperationA";
+ private static final String TARGET_PROP = "my-target-property";
+ private static final String MY_TARGET = "my-target";
+ private static final String EVENT_MGR_MULTI_YAML =
+ "../eventmanager/src/test/resources/eventManager/event-mgr-multi.yaml";
+ private static final String EVENT_MGR_SIMPLE_YAML =
+ "../eventmanager/src/test/resources/eventManager/event-mgr-simple.yaml";
+ private static final Coder yamlCoder = new StandardYamlCoder();
+ private static final String OUTCOME_MSG = "my outcome message";
+
+ @Mock
+ private PolicyEngine engineMgr;
+ @Mock
+ private WorkingMemory workMem;
+ @Mock
+ private FactHandle factHandle;
+ @Mock
+ private Operator policyOperator;
+ @Mock
+ private Operation policyOperation;
+ @Mock
+ private Actor policyActor;
+ @Mock
+ private ActorService actors;
+ @Mock
+ private OperationHistoryDataManager dataMgr;
+ @Mock
+ private ExecutorService executor;
+ @Mock
+ private MyStep stepa;
+ @Mock
+ private MyStep stepb;
+
+ private List<LockImpl> locks;
+ private ToscaPolicy tosca;
+ private ControlLoopParams params;
+ private VirtualControlLoopEvent event;
+ private ClEventManagerWithEvent<MyStep> mgr;
+
+ /**
+ * Sets up.
+ */
+ @Before
+ public void setUp() throws ControlLoopException, CoderException {
+ when(workMem.getFactHandle(any())).thenReturn(factHandle);
+
+ event = new VirtualControlLoopEvent();
+ event.setRequestId(REQ_ID);
+ event.setTarget(TARGET_PROP);
+ event.setAai(new TreeMap<>(Map.of(TARGET_PROP, MY_TARGET)));
+ event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
+ event.setClosedLoopControlName(CL_NAME);
+ event.setTargetType(ControlLoopTargetType.VNF);
+
+ params = new ControlLoopParams();
+ params.setClosedLoopControlName(CL_NAME);
+ params.setPolicyName(POLICY_NAME);
+ params.setPolicyScope(POLICY_SCOPE);
+ params.setPolicyVersion(POLICY_VERSION);
+
+ loadPolicy(EVENT_MGR_SIMPLE_YAML);
+
+ locks = new ArrayList<>();
+
+ mgr = new MyManager(params, event, workMem);
+ }
+
+ @Test
+ public void testConstructor() {
+ assertEquals(POLICY_NAME, mgr.getPolicyName());
+ assertSame(event, mgr.getEvent());
+
+ // valid
+ assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
+
+ // invalid
+ event.setTarget("");
+ assertThatThrownBy(() -> new MyManager(params, event, workMem)).isInstanceOf(ControlLoopException.class);
+ }
+
+ @Test
+ public void testPopulateNotification() throws Exception {
+ loadPolicy(EVENT_MGR_MULTI_YAML);
+ mgr = new MyManager(params, event, workMem);
+
+ // before started
+ assertNotNull(mgr.makeNotification());
+
+ mgr.start();
+
+ mgr.addToHistory(makeCompletedOutcome());
+ mgr.addToHistory(makeCompletedOutcome());
+ mgr.addToHistory(makeCompletedOutcome());
+
+ // check notification while running
+ VirtualControlLoopNotification notif = mgr.makeNotification();
+ assertThat(notif.getMessage()).contains(SIMPLE_ACTOR);
+ assertThat(notif.getHistory()).hasSize(3);
+ assertThat(notif.getAai()).isEqualTo(event.getAai());
+ assertThat(notif.getClosedLoopAlarmEnd()).isEqualTo(event.getClosedLoopAlarmEnd());
+ assertThat(notif.getClosedLoopAlarmStart()).isEqualTo(event.getClosedLoopAlarmStart());
+ assertThat(notif.getClosedLoopControlName()).isEqualTo(event.getClosedLoopControlName());
+ assertThat(notif.getClosedLoopEventClient()).isEqualTo(event.getClosedLoopEventClient());
+ assertThat(notif.getFrom()).isEqualTo("policy");
+ assertThat(notif.getTarget()).isEqualTo(event.getTarget());
+ assertThat(notif.getTargetType()).isEqualTo(event.getTargetType());
+
+ // indicate success and load the next policy - should clear the partial history
+ mgr.loadNextPolicy(OperationResult.SUCCESS);
+
+ // check notification
+ notif = mgr.makeNotification();
+ assertNull(notif.getMessage());
+ assertThat(notif.getHistory()).isEmpty();
+
+ // add outcomes and check again
+ mgr.addToHistory(makeCompletedOutcome());
+ mgr.addToHistory(makeCompletedOutcome());
+
+ notif = mgr.makeNotification();
+ assertNotNull(notif.getMessage());
+
+ // should only have history for last two outcomes
+ assertThat(notif.getHistory()).hasSize(2);
+
+ // indicate failure - should go to final state
+ mgr.loadNextPolicy(OperationResult.FAILURE);
+
+ // check notification
+ notif = mgr.makeNotification();
+ assertNull(notif.getMessage());
+
+ // should be no history
+ assertThat(notif.getHistory()).isEmpty();
+
+ // null case
+ assertThatThrownBy(() -> mgr.loadNextPolicy(null)).isInstanceOf(NullPointerException.class)
+ .hasMessageContaining("lastResult");
+ }
+
+ @Test
+ public void testStoreInDataBase() throws ControlLoopException {
+ mgr.start();
+ OperationOutcome outcome = makeOutcome();
+ mgr.addToHistory(outcome);
+
+ mgr.storeInDataBase(mgr.getPartialHistory().peekLast(), MY_TARGET);
+
+ verify(dataMgr).store(REQ_ID.toString(), event.getClosedLoopControlName(), event, MY_TARGET,
+ mgr.getPartialHistory().peekLast().getClOperation());
+ }
+
+ @Test
+ public void testMakeControlLoopResponse() {
+ final OperationOutcome outcome = new OperationOutcome();
+
+ ControlLoopResponse resp = mgr.makeControlLoopResponse(outcome);
+ assertNotNull(resp);
+ assertEquals("DCAE", resp.getTarget());
+ assertEquals(event.getClosedLoopControlName(), resp.getClosedLoopControlName());
+ assertEquals(event.getPolicyName(), resp.getPolicyName());
+ assertEquals(event.getPolicyVersion(), resp.getPolicyVersion());
+ assertEquals(REQ_ID, resp.getRequestId());
+ assertEquals(event.getVersion(), resp.getVersion());
+ }
+
+ @Test
+ public void testOnNewEvent() {
+ VirtualControlLoopEvent event2 = new VirtualControlLoopEvent(event);
+ assertEquals(NewEventStatus.FIRST_ONSET, mgr.onNewEvent(event2));
+
+ event2.setPayload("other payload");
+ assertEquals(NewEventStatus.SUBSEQUENT_ONSET, mgr.onNewEvent(event2));
+ assertEquals(NewEventStatus.SUBSEQUENT_ONSET, mgr.onNewEvent(event2));
+ assertEquals(NewEventStatus.FIRST_ONSET, mgr.onNewEvent(event));
+
+ event2.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
+ assertEquals(NewEventStatus.FIRST_ABATEMENT, mgr.onNewEvent(event2));
+
+ assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, mgr.onNewEvent(event2));
+ assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, mgr.onNewEvent(event2));
+
+ event2.setClosedLoopEventStatus(null);
+ assertEquals(NewEventStatus.SYNTAX_ERROR, mgr.onNewEvent(event2));
+ }
+
+ @Test
+ public void testCheckEventSyntax() {
+ // initially, it's valid
+ assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
+
+ event.setTarget(null);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
+ .hasMessage("No target field");
+
+ // abated supersedes previous errors - so it shouldn't throw an exception
+ event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
+
+ event.setRequestId(null);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
+ .hasMessage("No request ID");
+
+ event.setClosedLoopControlName(null);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
+ .hasMessage("No control loop name");
+ }
+
+ @Test
+ public void testValidateStatus() {
+ event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
+
+ event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).doesNotThrowAnyException();
+
+ event.setClosedLoopEventStatus(null);
+ assertThatCode(() -> mgr.checkEventSyntax(event)).isInstanceOf(ControlLoopException.class)
+ .hasMessage("Invalid value in closedLoopEventStatus");
+ }
+
+ private void loadPolicy(String fileName) throws CoderException {
+ ToscaServiceTemplate template =
+ yamlCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
+ tosca = template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
+
+ params.setToscaPolicy(tosca);
+ }
+
+ private OperationOutcome makeCompletedOutcome() {
+ OperationOutcome outcome = makeOutcome();
+ outcome.setEnd(outcome.getStart());
+
+ return outcome;
+ }
+
+ private OperationOutcome makeOutcome() {
+ OperationOutcome outcome = new OperationOutcome();
+ outcome.setActor(SIMPLE_ACTOR);
+ outcome.setOperation(SIMPLE_OPERATION);
+ outcome.setMessage(OUTCOME_MSG);
+ outcome.setResult(OperationResult.SUCCESS);
+ outcome.setStart(Instant.now());
+ outcome.setTarget(MY_TARGET);
+
+ return outcome;
+ }
+
+
+ private class MyManager extends ClEventManagerWithEvent<MyStep> {
+ private static final long serialVersionUID = 1L;
+
+ public MyManager(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
+ throws ControlLoopException {
+
+ super(params, event, workMem);
+ }
+
+ @Override
+ protected ExecutorService getBlockingExecutor() {
+ return executor;
+ }
+
+ @Override
+ protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
+ LockImpl lock = new LockImpl(LockState.ACTIVE, targetEntity, requestId, holdSec, callback);
+ locks.add(lock);
+ callback.lockAvailable(lock);
+ }
+
+ @Override
+ public ActorService getActorService() {
+ return actors;
+ }
+
+ @Override
+ public OperationHistoryDataManager getDataManager() {
+ return dataMgr;
+ }
+
+ @Override
+ protected PolicyEngine getPolicyEngineManager() {
+ return engineMgr;
+ }
+
+ @Override
+ protected void loadPolicyStep(ControlLoopOperationParams params) {
+ getSteps().add(new MyStep(this, params, getEvent()));
+ }
+ }
+
+ private static class MyStep extends Step {
+ public MyStep(StepContext stepContext, ControlLoopOperationParams params, VirtualControlLoopEvent event) {
+ super(params, new AtomicReference<>());
+ }
+ }
+}
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcomeTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcomeTest.java
new file mode 100644
index 000000000..d64d2bbc5
--- /dev/null
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithOutcomeTest.java
@@ -0,0 +1,400 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 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.
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.eventmanager;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicReference;
+import org.drools.core.WorkingMemory;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.kie.api.runtime.rule.FactHandle;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.controlloop.ControlLoopException;
+import org.onap.policy.controlloop.ControlLoopResponse;
+import org.onap.policy.controlloop.VirtualControlLoopNotification;
+import org.onap.policy.controlloop.actorserviceprovider.ActorService;
+import org.onap.policy.controlloop.actorserviceprovider.Operation;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
+import org.onap.policy.controlloop.actorserviceprovider.Operator;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
+import org.onap.policy.drools.core.lock.LockCallback;
+import org.onap.policy.drools.core.lock.LockImpl;
+import org.onap.policy.drools.core.lock.LockState;
+import org.onap.policy.drools.system.PolicyEngine;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ClEventManagerWithOutcomeTest {
+ private static final UUID REQ_ID = UUID.randomUUID();
+ private static final String CL_NAME = "my-closed-loop-name";
+ private static final String POLICY_NAME = "my-policy-name";
+ private static final String POLICY_SCOPE = "my-scope";
+ private static final String POLICY_VERSION = "1.2.3";
+ private static final String SIMPLE_ACTOR = "First";
+ private static final String SIMPLE_OPERATION = "OperationA";
+ private static final String MY_TARGET = "my-target";
+ private static final String EVENT_MGR_MULTI_YAML =
+ "../eventmanager/src/test/resources/eventManager/event-mgr-multi.yaml";
+ private static final String EVENT_MGR_SIMPLE_YAML =
+ "../eventmanager/src/test/resources/eventManager/event-mgr-simple.yaml";
+ private static final Coder yamlCoder = new StandardYamlCoder();
+ private static final String OUTCOME_MSG = "my outcome message";
+
+ @Mock
+ private PolicyEngine engineMgr;
+ @Mock
+ private WorkingMemory workMem;
+ @Mock
+ private FactHandle factHandle;
+ @Mock
+ private Operator policyOperator;
+ @Mock
+ private Operation policyOperation;
+ @Mock
+ private Actor policyActor;
+ @Mock
+ private ActorService actors;
+ @Mock
+ private OperationHistoryDataManager dataMgr;
+ @Mock
+ private ExecutorService executor;
+ @Mock
+ private MyStep stepa;
+ @Mock
+ private MyStep stepb;
+
+ private List<LockImpl> locks;
+ private ToscaPolicy tosca;
+ private ControlLoopParams params;
+ private ClEventManagerWithOutcome<MyStep> mgr;
+
+ /**
+ * Sets up.
+ */
+ @Before
+ public void setUp() throws ControlLoopException, CoderException {
+ when(workMem.getFactHandle(any())).thenReturn(factHandle);
+
+ params = new ControlLoopParams();
+ params.setClosedLoopControlName(CL_NAME);
+ params.setPolicyName(POLICY_NAME);
+ params.setPolicyScope(POLICY_SCOPE);
+ params.setPolicyVersion(POLICY_VERSION);
+
+ loadPolicy(EVENT_MGR_SIMPLE_YAML);
+
+ locks = new ArrayList<>();
+
+ mgr = new MyManager(params, REQ_ID, workMem);
+ }
+
+ @Test
+ public void testConstructor() {
+ assertEquals(POLICY_NAME, mgr.getPolicyName());
+
+ // invalid
+ assertThatThrownBy(() -> new MyManager(params, null, workMem)).isInstanceOf(ControlLoopException.class);
+ }
+
+ @Test
+ public void testLoadNextPolicy_testGetFullHistory_testGetPartialHistory() throws Exception {
+ loadPolicy(EVENT_MGR_MULTI_YAML);
+ mgr = new MyManager(params, REQ_ID, workMem);
+
+ // start and load step for first policy
+ mgr.start();
+ assertEquals("OperationA", mgr.getSteps().poll().getOperationName());
+ assertNull(mgr.getFinalResult());
+
+ // add an outcome
+ OperationOutcome outcome = makeOutcome();
+ mgr.addToHistory(outcome);
+
+ // indicate success and load next policy
+ mgr.loadNextPolicy(OperationResult.SUCCESS);
+ assertEquals("OperationB", mgr.getSteps().poll().getOperationName());
+ assertNull(mgr.getFinalResult());
+
+ // loadPolicy() should clear the partial history, but not the full history
+ assertThat(mgr.getPartialHistory()).isEmpty();
+ assertThat(mgr.getFullHistory()).hasSize(1);
+ }
+
+ @Test
+ public void testExecuteStep() {
+ mgr.bumpAttempts();
+
+ // no steps to execute
+ assertFalse(mgr.executeStep());
+ assertEquals(0, mgr.getAttempts());
+ }
+
+ @Test
+ public void testBumpAttempts() {
+ assertEquals(0, mgr.getAttempts());
+
+ mgr.bumpAttempts();
+ mgr.bumpAttempts();
+ assertEquals(2, mgr.getAttempts());
+ }
+
+ @Test
+ public void testIsAbort() {
+ OperationOutcome outcome = makeCompletedOutcome();
+
+ outcome.setResult(OperationResult.FAILURE);
+ assertTrue(mgr.isAbort(outcome));
+
+ // no effect for success
+ outcome.setResult(OperationResult.SUCCESS);
+ assertFalse(mgr.isAbort(outcome));
+ }
+
+ @Test
+ public void testAddToHistory() throws ControlLoopException {
+ mgr.start();
+
+ // add a "start" outcome
+ OperationOutcome outcome = makeOutcome();
+ mgr.addToHistory(outcome);
+
+ assertThat(mgr.getPartialHistory()).hasSize(1);
+ assertThat(mgr.getFullHistory()).hasSize(1);
+
+ // add a "completion" outcome - should replace the start
+ outcome = makeCompletedOutcome();
+ mgr.addToHistory(outcome);
+
+ assertThat(mgr.getPartialHistory()).hasSize(1);
+ assertThat(mgr.getFullHistory()).hasSize(1);
+ assertSame(outcome, mgr.getPartialHistory().peek().getOutcome());
+ assertSame(outcome, mgr.getFullHistory().peek().getOutcome());
+
+ // add another start
+ outcome = makeOutcome();
+ mgr.addToHistory(outcome);
+
+ assertThat(mgr.getPartialHistory()).hasSize(2);
+ assertThat(mgr.getFullHistory()).hasSize(2);
+ assertSame(outcome, mgr.getPartialHistory().peekLast().getOutcome());
+ assertSame(outcome, mgr.getFullHistory().peekLast().getOutcome());
+
+ // remove the last item from the full history and then add a "completion"
+ mgr.getFullHistory().removeLast();
+ outcome = makeCompletedOutcome();
+ mgr.addToHistory(outcome);
+ assertThat(mgr.getPartialHistory()).hasSize(2);
+ assertThat(mgr.getFullHistory()).hasSize(2);
+
+ // add another "start"
+ outcome = makeOutcome();
+ mgr.addToHistory(outcome);
+ assertThat(mgr.getPartialHistory()).hasSize(3);
+ assertThat(mgr.getFullHistory()).hasSize(3);
+
+ // add a "completion" for a different actor - should NOT replace the start
+ outcome = makeCompletedOutcome();
+ outcome.setActor("different-actor");
+ mgr.addToHistory(outcome);
+ assertThat(mgr.getPartialHistory()).hasSize(4);
+ assertThat(mgr.getFullHistory()).hasSize(4);
+ assertSame(outcome, mgr.getPartialHistory().peekLast().getOutcome());
+ assertSame(outcome, mgr.getFullHistory().peekLast().getOutcome());
+ }
+
+ @Test
+ public void testMakeNotification() throws Exception {
+ loadPolicy(EVENT_MGR_MULTI_YAML);
+ mgr = new MyManager(params, REQ_ID, workMem);
+
+ // before started
+ assertNotNull(mgr.makeNotification());
+
+ mgr.start();
+
+ mgr.addToHistory(makeCompletedOutcome());
+ mgr.addToHistory(makeCompletedOutcome());
+ mgr.addToHistory(makeCompletedOutcome());
+
+ // check notification while running
+ VirtualControlLoopNotification notif = mgr.makeNotification();
+ assertThat(notif.getMessage()).contains(SIMPLE_ACTOR);
+ assertThat(notif.getHistory()).hasSize(3);
+
+ // indicate success and load the next policy - should clear the partial history
+ mgr.loadNextPolicy(OperationResult.SUCCESS);
+
+ // check notification
+ notif = mgr.makeNotification();
+ assertNull(notif.getMessage());
+ assertThat(notif.getHistory()).isEmpty();
+
+ // add outcomes and check again
+ mgr.addToHistory(makeCompletedOutcome());
+ mgr.addToHistory(makeCompletedOutcome());
+
+ notif = mgr.makeNotification();
+ assertNotNull(notif.getMessage());
+
+ // should only have history for last two outcomes
+ assertThat(notif.getHistory()).hasSize(2);
+
+ // indicate failure - should go to final state
+ mgr.loadNextPolicy(OperationResult.FAILURE);
+
+ // check notification
+ notif = mgr.makeNotification();
+ assertNull(notif.getMessage());
+
+ // should be no history
+ assertThat(notif.getHistory()).isEmpty();
+
+ // null case
+ assertThatThrownBy(() -> mgr.loadNextPolicy(null)).isInstanceOf(NullPointerException.class)
+ .hasMessageContaining("lastResult");
+ }
+
+ @Test
+ public void testGetOperationMessage() throws ControlLoopException {
+ // no history yet
+ assertNull(mgr.getOperationMessage());
+
+ // add an outcome
+ mgr.start();
+ OperationOutcome outcome = makeOutcome();
+ mgr.addToHistory(outcome);
+
+ assertThat(mgr.getOperationMessage()).contains("actor=" + SIMPLE_ACTOR)
+ .contains("operation=" + SIMPLE_OPERATION);
+ }
+
+ @Test
+ public void testMakeControlLoopResponse() {
+ final OperationOutcome outcome = new OperationOutcome();
+ outcome.setActor(SIMPLE_ACTOR);
+
+ ControlLoopResponse resp = mgr.makeControlLoopResponse(outcome);
+ assertNotNull(resp);
+ assertEquals(SIMPLE_ACTOR, resp.getFrom());
+ }
+
+ private void loadPolicy(String fileName) throws CoderException {
+ ToscaServiceTemplate template =
+ yamlCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
+ tosca = template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
+
+ params.setToscaPolicy(tosca);
+ }
+
+ private OperationOutcome makeCompletedOutcome() {
+ OperationOutcome outcome = makeOutcome();
+ outcome.setEnd(outcome.getStart());
+
+ return outcome;
+ }
+
+ private OperationOutcome makeOutcome() {
+ OperationOutcome outcome = new OperationOutcome();
+ outcome.setActor(SIMPLE_ACTOR);
+ outcome.setOperation(SIMPLE_OPERATION);
+ outcome.setMessage(OUTCOME_MSG);
+ outcome.setResult(OperationResult.SUCCESS);
+ outcome.setStart(Instant.now());
+ outcome.setTarget(MY_TARGET);
+
+ return outcome;
+ }
+
+
+ private class MyManager extends ClEventManagerWithOutcome<MyStep> {
+ private static final long serialVersionUID = 1L;
+
+ public MyManager(ControlLoopParams params, UUID requestId, WorkingMemory workMem) throws ControlLoopException {
+
+ super(params, requestId, workMem);
+ }
+
+ @Override
+ protected ExecutorService getBlockingExecutor() {
+ return executor;
+ }
+
+ @Override
+ protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
+ LockImpl lock = new LockImpl(LockState.ACTIVE, targetEntity, requestId, holdSec, callback);
+ locks.add(lock);
+ callback.lockAvailable(lock);
+ }
+
+ @Override
+ public ActorService getActorService() {
+ return actors;
+ }
+
+ @Override
+ public OperationHistoryDataManager getDataManager() {
+ return dataMgr;
+ }
+
+ @Override
+ protected PolicyEngine getPolicyEngineManager() {
+ return engineMgr;
+ }
+
+ @Override
+ protected void loadPolicyStep(ControlLoopOperationParams params) {
+ getSteps().add(new MyStep(this, params));
+ }
+ }
+
+
+ private static class MyStep extends Step {
+ public MyStep(StepContext stepContext, ControlLoopOperationParams params) {
+ super(params, new AtomicReference<>());
+ }
+ }
+}
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithStepsTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithStepsTest.java
new file mode 100644
index 000000000..06dc838e2
--- /dev/null
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ClEventManagerWithStepsTest.java
@@ -0,0 +1,482 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 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.
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.controlloop.eventmanager;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.atomic.AtomicReference;
+import org.drools.core.WorkingMemory;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.kie.api.runtime.rule.FactHandle;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.io.Serializer;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.controlloop.ControlLoopException;
+import org.onap.policy.controlloop.actorserviceprovider.ActorService;
+import org.onap.policy.controlloop.actorserviceprovider.Operation;
+import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
+import org.onap.policy.controlloop.actorserviceprovider.Operator;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
+import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
+import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
+import org.onap.policy.drools.core.lock.LockCallback;
+import org.onap.policy.drools.core.lock.LockImpl;
+import org.onap.policy.drools.core.lock.LockState;
+import org.onap.policy.drools.system.PolicyEngine;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ClEventManagerWithStepsTest {
+ private static final UUID REQ_ID = UUID.randomUUID();
+ private static final String CL_NAME = "my-closed-loop-name";
+ private static final String POLICY_NAME = "my-policy-name";
+ private static final String POLICY_SCOPE = "my-scope";
+ private static final String POLICY_VERSION = "1.2.3";
+ private static final String SIMPLE_ACTOR = "First";
+ private static final String SIMPLE_OPERATION = "OperationA";
+ private static final String MY_TARGET = "my-target";
+ private static final String EVENT_MGR_MULTI_YAML =
+ "../eventmanager/src/test/resources/eventManager/event-mgr-multi.yaml";
+ private static final String EVENT_MGR_SIMPLE_YAML =
+ "../eventmanager/src/test/resources/eventManager/event-mgr-simple.yaml";
+ private static final Coder yamlCoder = new StandardYamlCoder();
+ private static final String OUTCOME_MSG = "my outcome message";
+ private static final String MY_SINK = "my-topic-sink";
+
+ @Mock
+ private PolicyEngine engineMgr;
+ @Mock
+ private WorkingMemory workMem;
+ @Mock
+ private FactHandle factHandle;
+ @Mock
+ private Operator policyOperator;
+ @Mock
+ private Operation policyOperation;
+ @Mock
+ private Actor policyActor;
+ @Mock
+ private ActorService actors;
+ @Mock
+ private OperationHistoryDataManager dataMgr;
+ @Mock
+ private ExecutorService executor;
+ @Mock
+ private MyStep stepa;
+ @Mock
+ private MyStep stepb;
+
+ private List<LockImpl> locks;
+ private ToscaPolicy tosca;
+ private ControlLoopParams params;
+ private ClEventManagerWithSteps<MyStep> mgr;
+
+ /**
+ * Sets up.
+ */
+ @Before
+ public void setUp() throws ControlLoopException, CoderException {
+ when(workMem.getFactHandle(any())).thenReturn(factHandle);
+
+ params = new ControlLoopParams();
+ params.setClosedLoopControlName(CL_NAME);
+ params.setPolicyName(POLICY_NAME);
+ params.setPolicyScope(POLICY_SCOPE);
+ params.setPolicyVersion(POLICY_VERSION);
+
+ loadPolicy(EVENT_MGR_SIMPLE_YAML);
+
+ locks = new ArrayList<>();
+
+ mgr = new MyManager(params, REQ_ID, workMem);
+ }
+
+ @Test
+ public void testConstructor() {
+ assertEquals(POLICY_NAME, mgr.getPolicyName());
+
+ // invalid
+ assertThatThrownBy(() -> new MyManager(params, null, workMem)).isInstanceOf(ControlLoopException.class);
+ }
+
+ @Test
+ public void testDestroy_testGetSteps() {
+ // add some steps to the queue
+ mgr.getSteps().add(stepa);
+ mgr.getSteps().add(stepb);
+
+ mgr.destroy();
+
+ verify(stepa).cancel();
+ verify(stepb).cancel();
+
+ // if superclass destroy() was invoked, then freeLock() should have been submitted
+ // to the executor
+ verify(executor).execute(any());
+ }
+
+ @Test
+ public void testOnStart() throws ControlLoopException {
+ OperationOutcome outcome = makeOutcome();
+
+ mgr.start();
+ mgr.onStart(outcome);
+
+ assertSame(outcome, mgr.getOutcomes().poll());
+ assertThat(mgr.getOutcomes()).isEmpty();
+
+ verify(workMem).update(factHandle, mgr);
+ }
+
+ @Test
+ public void testOnComplete() throws ControlLoopException {
+ OperationOutcome outcome = makeCompletedOutcome();
+
+ mgr.start();
+ mgr.onComplete(outcome);
+
+ assertSame(outcome, mgr.getOutcomes().poll());
+ assertThat(mgr.getOutcomes()).isEmpty();
+
+ verify(workMem).update(factHandle, mgr);
+ }
+
+ @Test
+ public void testToString() {
+ assertNotNull(mgr.toString());
+ }
+
+ @Test
+ public void testStart() throws ControlLoopException {
+ // start it
+ mgr.start();
+
+ // cannot re-start
+ assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
+ .hasMessage("manager already started");
+ }
+
+ /**
+ * Tests start() when the manager is not in working memory.
+ */
+ @Test
+ public void testStartNotInWorkingMemory() throws ControlLoopException {
+ when(workMem.getFactHandle(any())).thenReturn(null);
+
+ assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
+ .hasMessage("manager is not in working memory");
+ }
+
+ /**
+ * Tests start() when the manager is not active.
+ */
+ @Test
+ public void testStartInactive() throws Exception {
+ // make an inactive manager by deserializing it
+ RealManager mgr2 = Serializer.roundTrip(new RealManager(params, REQ_ID, workMem));
+ mgr = mgr2;
+
+ // cannot re-start
+ assertThatCode(() -> mgr.start()).isInstanceOf(IllegalStateException.class)
+ .hasMessage("manager is no longer active");
+ }
+
+ @Test
+ public void testAbort() {
+ mgr.abort(ClEventManagerWithSteps.State.DONE, OperationFinalResult.FINAL_FAILURE_GUARD, "some message");
+
+ assertEquals(ClEventManagerWithSteps.State.DONE, mgr.getState());
+ assertEquals(OperationFinalResult.FINAL_FAILURE_GUARD, mgr.getFinalResult());
+ assertEquals("some message", mgr.getFinalMessage());
+
+ // try null state
+ assertThatThrownBy(() -> mgr.abort(null, OperationFinalResult.FINAL_FAILURE_GUARD, ""))
+ .isInstanceOf(NullPointerException.class).hasMessageContaining("finalState");
+ }
+
+ @Test
+ public void testLoadNextPolicy() throws Exception {
+ loadPolicy(EVENT_MGR_MULTI_YAML);
+ mgr = new MyManager(params, REQ_ID, workMem);
+
+ // start and load step for first policy
+ mgr.start();
+ assertEquals("OperationA", mgr.getSteps().poll().getOperationName());
+ assertNull(mgr.getFinalResult());
+
+ // indicate success and load next policy
+ mgr.loadNextPolicy(OperationResult.SUCCESS);
+ assertEquals("OperationB", mgr.getSteps().poll().getOperationName());
+ assertNull(mgr.getFinalResult());
+
+ // indicate failure - should go to final failure
+ mgr.loadNextPolicy(OperationResult.FAILURE);
+ assertEquals(OperationFinalResult.FINAL_FAILURE, mgr.getFinalResult());
+ }
+
+ @Test
+ public void testLoadPolicy() throws ControlLoopException {
+ // start() will invoke loadPolicy()
+ mgr.start();
+
+ assertNull(mgr.getFinalResult());
+
+ MyStep step = mgr.getSteps().peek();
+ assertNotNull(step);
+ assertEquals("First", step.getActorName());
+ assertEquals("OperationA", step.getOperationName());
+
+ ControlLoopOperationParams params2 = step.getParams();
+ assertSame(actors, params2.getActorService());
+ assertSame(REQ_ID, params2.getRequestId());
+ assertSame(ForkJoinPool.commonPool(), params2.getExecutor());
+ assertNotNull(params2.getTargetType());
+ assertNotNull(params2.getTargetEntityIds());
+ assertEquals(Integer.valueOf(300), params2.getTimeoutSec());
+ assertEquals(Integer.valueOf(0), params2.getRetry());
+ assertThat(params2.getPayload()).isEmpty();
+ assertNotNull(params2.getStartCallback());
+ assertNotNull(params2.getCompleteCallback());
+ }
+
+ @Test
+ public void testLoadPreprocessorSteps() {
+ stepa = new MyStep(mgr, ControlLoopOperationParams.builder().build()) {
+ @Override
+ protected Operation buildOperation() {
+ return policyOperation;
+ }
+ };
+
+ Deque<MyStep> steps = mgr.getSteps();
+ steps.add(stepa);
+ steps.add(stepb);
+
+ mgr.loadPreprocessorSteps();
+
+ // no additional steps should have been loaded
+ assertThat(steps).hasSize(2);
+
+ assertSame(stepa, steps.poll());
+ assertSame(stepb, steps.poll());
+ assertThat(steps).isEmpty();
+
+ assertNotNull(stepa.getOperation());
+ assertNull(stepb.getOperation());
+ }
+
+ /**
+ * Tests loadPreprocessorSteps() when there are too many steps in the queue.
+ */
+ @Test
+ public void testLoadPreprocessorStepsTooManySteps() {
+ stepa = new MyStep(mgr, ControlLoopOperationParams.builder().build()) {
+ @Override
+ protected Operation buildOperation() {
+ return policyOperation;
+ }
+ };
+
+ Deque<MyStep> steps = mgr.getSteps();
+
+ // load up a bunch of steps
+ for (int nsteps = 0; nsteps < ClEventManagerWithSteps.MAX_STEPS; ++nsteps) {
+ steps.add(stepa);
+ }
+
+ // should fail
+ assertThatIllegalStateException().isThrownBy(() -> mgr.loadPreprocessorSteps()).withMessage("too many steps");
+
+ // add another step, should still fail
+ steps.add(stepa);
+ assertThatIllegalStateException().isThrownBy(() -> mgr.loadPreprocessorSteps()).withMessage("too many steps");
+
+ // remove two steps - should now succeed
+ steps.remove();
+ steps.remove();
+
+ int nsteps = steps.size();
+
+ mgr.loadPreprocessorSteps();
+ assertEquals(nsteps, steps.size());
+ }
+
+ @Test
+ public void testExecuteStep() {
+ // no steps to execute
+ assertFalse(mgr.executeStep());
+
+ // add a step to the queue
+ mgr.getSteps().add(stepa);
+
+ // step returns false
+ when(stepa.start(anyLong())).thenReturn(false);
+ assertFalse(mgr.executeStep());
+
+ // step returns true
+ when(stepa.start(anyLong())).thenReturn(true);
+ assertTrue(mgr.executeStep());
+ }
+
+ @Test
+ public void testNextStep() {
+ mgr.getSteps().add(stepa);
+
+ mgr.nextStep();
+
+ assertThat(mgr.getSteps()).isEmpty();
+ }
+
+ @Test
+ public void testDeliver() {
+ mgr.deliver(MY_SINK, null, "null notification", "null rule");
+ verify(engineMgr, never()).deliver(any(), any());
+
+ mgr.deliver(MY_SINK, "publishA", "A notification", "A rule");
+ verify(engineMgr).deliver(MY_SINK, "publishA");
+
+ // cause deliver() to throw an exception
+ when(engineMgr.deliver(any(), any())).thenThrow(new IllegalStateException("expected exception"));
+ assertThatCode(() -> mgr.deliver(MY_SINK, "publishB", "B notification", "B rule")).doesNotThrowAnyException();
+ }
+
+ private void loadPolicy(String fileName) throws CoderException {
+ ToscaServiceTemplate template =
+ yamlCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
+ tosca = template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
+
+ params.setToscaPolicy(tosca);
+ }
+
+ private OperationOutcome makeCompletedOutcome() {
+ OperationOutcome outcome = makeOutcome();
+ outcome.setEnd(outcome.getStart());
+
+ return outcome;
+ }
+
+ private OperationOutcome makeOutcome() {
+ OperationOutcome outcome = new OperationOutcome();
+ outcome.setActor(SIMPLE_ACTOR);
+ outcome.setOperation(SIMPLE_OPERATION);
+ outcome.setMessage(OUTCOME_MSG);
+ outcome.setResult(OperationResult.SUCCESS);
+ outcome.setStart(Instant.now());
+ outcome.setTarget(MY_TARGET);
+
+ return outcome;
+ }
+
+
+ private class MyManager extends ClEventManagerWithSteps<MyStep> {
+ private static final long serialVersionUID = 1L;
+
+ public MyManager(ControlLoopParams params, UUID requestId, WorkingMemory workMem) throws ControlLoopException {
+
+ super(params, requestId, workMem);
+ }
+
+ @Override
+ protected ExecutorService getBlockingExecutor() {
+ return executor;
+ }
+
+ @Override
+ protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
+ LockImpl lock = new LockImpl(LockState.ACTIVE, targetEntity, requestId, holdSec, callback);
+ locks.add(lock);
+ callback.lockAvailable(lock);
+ }
+
+ @Override
+ public ActorService getActorService() {
+ return actors;
+ }
+
+ @Override
+ public OperationHistoryDataManager getDataManager() {
+ return dataMgr;
+ }
+
+ @Override
+ protected PolicyEngine getPolicyEngineManager() {
+ return engineMgr;
+ }
+
+ @Override
+ protected void loadPolicyStep(ControlLoopOperationParams params) {
+ getSteps().add(new MyStep(this, params));
+ }
+ }
+
+
+ private static class RealManager extends ClEventManagerWithSteps<MyStep> {
+ private static final long serialVersionUID = 1L;
+
+ public RealManager(ControlLoopParams params, UUID requestId, WorkingMemory workMem)
+ throws ControlLoopException {
+
+ super(params, requestId, workMem);
+ }
+
+ @Override
+ protected void loadPolicyStep(ControlLoopOperationParams params) {
+ getSteps().add(new MyStep(this, params));
+ }
+ }
+
+ private static class MyStep extends Step {
+ public MyStep(StepContext stepContext, ControlLoopOperationParams params) {
+ super(params, new AtomicReference<>());
+ }
+ }
+}