/*-
* ============LICENSE_START=======================================================
- * controlloop event manager
+ * ONAP
* ================================================================================
- * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.controlloop.eventmanager;
-import static org.onap.policy.controlloop.ControlLoopTargetType.PNF;
-import static org.onap.policy.controlloop.ControlLoopTargetType.VM;
-import static org.onap.policy.controlloop.ControlLoopTargetType.VNF;
-
import java.io.Serializable;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
+import java.time.Instant;
+import java.util.Deque;
+import java.util.HashMap;
import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Set;
import java.util.UUID;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-import org.apache.commons.lang3.StringUtils;
-import org.onap.policy.aai.AaiCqResponse;
-import org.onap.policy.aai.AaiManager;
-import org.onap.policy.aai.util.AaiException;
-import org.onap.policy.controlloop.ControlLoopEventStatus;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.ToString;
import org.onap.policy.controlloop.ControlLoopException;
-import org.onap.policy.controlloop.ControlLoopNotificationType;
import org.onap.policy.controlloop.ControlLoopOperation;
-import org.onap.policy.controlloop.VirtualControlLoopEvent;
-import org.onap.policy.controlloop.VirtualControlLoopNotification;
-import org.onap.policy.controlloop.policy.FinalResult;
-import org.onap.policy.controlloop.policy.Policy;
+import org.onap.policy.controlloop.actorserviceprovider.ActorService;
+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.controlloop.ophistory.OperationHistoryDataManager;
+import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
import org.onap.policy.controlloop.processor.ControlLoopProcessor;
-import org.onap.policy.drools.core.lock.Lock;
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.PolicyEngineConstants;
-import org.onap.policy.drools.utils.Pair;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.rest.RestManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class ControlLoopEventManager implements Serializable {
- public static final String PROV_STATUS_ACTIVE = "ACTIVE";
- private static final String VM_NAME = "VM_NAME";
- private static final String VNF_NAME = "VNF_NAME";
- public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
- public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
- public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
- public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
- public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
- private static final String PNF_IS_IN_MAINT = "pnf.in-maint";
- public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
- public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
- public static final String PNF_ID = "pnf.pnf-id";
- public static final String PNF_NAME = "pnf.pnf-name";
-
- public static final String AAI_URL = "aai.url";
- public static final String AAI_USERNAME_PROPERTY = "aai.username";
- public static final String AAI_PASS_PROPERTY = "aai.password";
+/**
+ * Manager for a single event. Once this has been created, the event can be retracted from
+ * working memory. Invoke {@link #isActive()} to determine if the manager is active (i.e.,
+ * hasn't been replicated from another server). When the manager is no longer needed,
+ * {@link #destroy()} should be invoked.
+ */
+@ToString(onlyExplicitlyIncluded = true)
+public class ControlLoopEventManager implements StepContext, Serializable {
+ private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager.class);
+ private static final long serialVersionUID = -1216568161322872641L;
/**
- * Additional time, in seconds, to add to a "lock" request. This ensures that the lock won't expire right before an
- * operation completes.
+ * Data manager used when the policy engine's guard.disabled property is "true".
*/
- private static final int ADDITIONAL_LOCK_SEC = 60;
+ private static final OperationHistoryDataManager STUB_DATA_MANAGER = new OperationHistoryDataManagerStub();
- private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager.class);
+ public static final String GUARD_DISABLED_PROPERTY = "guard.disabled";
- private static final long serialVersionUID = -1216568161322872641L;
+ /**
+ * Counts the number of these objects that have been created. This is used by junit
+ * tests.
+ */
+ private static final AtomicLong createCount = new AtomicLong(0);
- private static final Set<String> VALID_TARGETS;
+ /**
+ * {@code True} if this object was created by this JVM instance, {@code false}
+ * otherwise. This will be {@code false} if this object is reconstituted from a
+ * persistent store or by transfer from another server.
+ */
+ private final transient boolean createdByThisJvmInstance;
- static {
- VALID_TARGETS = Collections.unmodifiableSet(new HashSet<>(
- Stream.of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
- .map(String::toLowerCase).collect(Collectors.toList())));
- }
+ private final transient EventManagerServices services;
+ @Getter
+ @ToString.Include
public final String closedLoopControlName;
+ @Getter
+ @ToString.Include
private final UUID requestId;
- private String controlLoopResult;
- private ControlLoopProcessor processor = null;
- private VirtualControlLoopEvent onset;
- private Integer numOnsets = 0;
- private Integer numAbatements = 0;
- private VirtualControlLoopEvent abatement;
- private FinalResult controlLoopTimedOut = null;
-
- private boolean isActivated = false;
- private LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
- private ControlLoopOperationManager currentOperation = null;
- private ControlLoopOperationManager lastOperationManager = null;
- private transient Lock targetLock = null;
- private boolean useTargetLock = true;
-
/**
- * Constructs the object.
- *
- * @param closedLoopControlName name of the control loop
- * @param requestId ID of the request with which this manager is associated
+ * Time, in milliseconds, when the control loop will time out.
*/
- public ControlLoopEventManager(String closedLoopControlName, UUID requestId) {
- this.closedLoopControlName = closedLoopControlName;
- this.requestId = requestId;
- }
-
- public String getClosedLoopControlName() {
- return closedLoopControlName;
- }
-
- public String getControlLoopResult() {
- return controlLoopResult;
- }
-
- public void setControlLoopResult(String controlLoopResult) {
- this.controlLoopResult = controlLoopResult;
- }
-
- public Integer getNumOnsets() {
- return numOnsets;
- }
-
- public void setNumOnsets(Integer numOnsets) {
- this.numOnsets = numOnsets;
- }
-
- public Integer getNumAbatements() {
- return numAbatements;
- }
-
- public void setNumAbatements(Integer numAbatements) {
- this.numAbatements = numAbatements;
- }
-
- public boolean isActivated() {
- return isActivated;
- }
-
- public void setActivated(boolean isActivated) {
- this.isActivated = isActivated;
- }
-
- public boolean useTargetLock() {
- return useTargetLock();
- }
-
- public void setUseTargetLock(boolean useTargetLock) {
- this.useTargetLock = useTargetLock;
- }
-
- public VirtualControlLoopEvent getOnsetEvent() {
- return this.onset;
- }
-
- public VirtualControlLoopEvent getAbatementEvent() {
- return this.abatement;
- }
+ @Getter
+ private final long endTimeMs;
- public ControlLoopProcessor getProcessor() {
- return this.processor;
- }
-
- public UUID getRequestId() {
- return requestId;
- }
-
-
- private VirtualControlLoopNotification rejectNotification(VirtualControlLoopEvent event, String message) {
- VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
- notification.setNotification(ControlLoopNotificationType.REJECTED);
- notification.setMessage(message);
- return notification;
- }
+ // fields extracted from the ControlLoopParams
+ @Getter
+ private final String policyName;
+ @Getter
+ private final String policyVersion;
/**
- * Preactivations check for an event.
- *
- * @param event the event
- * @return the VirtualControlLoopNotification
+ * Maps a target entity to its lock.
*/
- private VirtualControlLoopNotification preActivationChecks(VirtualControlLoopEvent event) {
- try {
- //
- // This method should ONLY be called ONCE
- //
- if (this.isActivated) {
- return rejectNotification(event, "ControlLoopEventManager has already been activated.");
- }
+ private final transient Map<String, LockData> target2lock = new HashMap<>();
- //
- // Syntax check the event
- //
- checkEventSyntax(event);
- } catch (ControlLoopException e) {
- logger.warn("{}: invalid event syntax: ", this, e);
- return rejectNotification(event, e.getMessage());
-
- }
-
- return new VirtualControlLoopNotification(event);
- }
+ @Getter(AccessLevel.PROTECTED)
+ private final ControlLoopProcessor processor;
/**
- * Activate a control loop event.
- *
- * @param event the event
- * @return the VirtualControlLoopNotification
+ * Set of properties used while processing the event.
*/
- public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
- VirtualControlLoopNotification notification = preActivationChecks(event);
- if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
- return notification;
- }
-
- return postActivate(event, notification);
- }
+ private final Map<String, Serializable> properties = new ConcurrentHashMap<>();
/**
- * Activate a control loop event.
- *
- * @param yamlSpecification the yaml specification
- * @param event the event
- * @return the VirtualControlLoopNotification
+ * Unprocessed outcomes from the operations. Outcomes are added to this each time the
+ * "start" or "complete" callback is invoked, typically by an operation running in a
+ * background thread, thus it must be thread safe.
*/
- public VirtualControlLoopNotification activate(String yamlSpecification, VirtualControlLoopEvent event) {
- VirtualControlLoopNotification notification = preActivationChecks(event);
- if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
- return notification;
- }
-
- if (yamlSpecification == null || yamlSpecification.length() < 1) {
- return rejectNotification(event, "yaml specification is null or 0 length");
- }
-
- String decodedYaml = null;
- try {
- decodedYaml = URLDecoder.decode(yamlSpecification, "UTF-8");
- if (decodedYaml != null && decodedYaml.length() > 0) {
- yamlSpecification = decodedYaml;
- }
- } catch (UnsupportedEncodingException e) {
- logger.warn("{}: YAML decode in activate by YAML specification and event threw: ", this, e);
- return rejectNotification(event, e.getMessage());
- }
-
- try {
- //
- // Parse the YAML specification
- //
- this.processor = new ControlLoopProcessor(yamlSpecification);
- } catch (ControlLoopException e) {
- logger.error("{}: activate by YAML specification and event threw: ", this, e);
- return rejectNotification(event, e.getMessage());
- }
+ @Getter
+ private final transient Deque<OperationOutcome> outcomes = new ConcurrentLinkedDeque<>();
- return postActivate(event, notification);
- }
/**
- * Activate a control loop event.
+ * Constructs the object.
*
- * @param toscaPolicy the tosca policy
- * @param event the event
- * @return the VirtualControlLoopNotification
+ * @param services services the manager should use when processing the event
+ * @param params control loop parameters
+ * @param requestId event request ID
+ * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
+ * be created
*/
- public VirtualControlLoopNotification activate(ToscaPolicy toscaPolicy, VirtualControlLoopEvent event) {
- VirtualControlLoopNotification notification = preActivationChecks(event);
- if (notification.getNotification() == ControlLoopNotificationType.REJECTED) {
- return notification;
- }
+ public ControlLoopEventManager(EventManagerServices services, ControlLoopParams params, UUID requestId)
+ throws ControlLoopException {
- try {
- this.processor = new ControlLoopProcessor(toscaPolicy);
- } catch (ControlLoopException e) {
- logger.error("{}: activate from Tosca Policy threw: ", this, e);
- return rejectNotification(event, e.getMessage());
- }
+ createCount.incrementAndGet();
- return postActivate(event, notification);
- }
-
- private VirtualControlLoopNotification postActivate(
- VirtualControlLoopEvent event, VirtualControlLoopNotification notification) {
- //
- // At this point we are good to go with this event
- //
- this.onset = event;
- this.numOnsets = 1;
-
- //
- // Set ourselves as active
- //
- this.isActivated = true;
-
- notification.setNotification(ControlLoopNotificationType.ACTIVE);
- return notification;
+ this.createdByThisJvmInstance = true;
+ this.services = services;
+ this.closedLoopControlName = params.getClosedLoopControlName();
+ this.requestId = requestId;
+ this.policyName = params.getPolicyName();
+ this.policyVersion = params.getPolicyVersion();
+ this.processor = new ControlLoopProcessor(params.getToscaPolicy());
+ this.endTimeMs = System.currentTimeMillis() + detmControlLoopTimeoutMs();
}
/**
- * Check if the control loop is final.
+ * Gets the number of manager objects that have been created.
*
- * @return a VirtualControlLoopNotification if the control loop is final, otherwise <code>null</code> is returned
- * @throws ControlLoopException if an error occurs
+ * @return the number of manager objects that have been created
*/
- public VirtualControlLoopNotification isControlLoopFinal() throws ControlLoopException {
- validateFinalControlLoop();
- //
- // Ok, start creating the notification
- //
- VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
- //
- // Check if the overall control loop has timed out
- //
- if (this.isControlLoopTimedOut()) {
- //
- // Yes we have timed out
- //
- notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
- notification.setMessage("Control Loop timed out");
- notification.getHistory().addAll(this.controlLoopHistory);
- return notification;
- }
- //
- // Check if the current policy is Final
- //
- FinalResult result = this.processor.checkIsCurrentPolicyFinal();
- if (result == null) {
- //
- // we are not at a final result
- //
- return null;
- }
-
- switch (result) {
- case FINAL_FAILURE_EXCEPTION:
- notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
- notification.setMessage("Exception in processing closed loop");
- break;
- case FINAL_FAILURE:
- case FINAL_FAILURE_RETRIES:
- case FINAL_FAILURE_TIMEOUT:
- case FINAL_FAILURE_GUARD:
- notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
- break;
- case FINAL_OPENLOOP:
- notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
- break;
- case FINAL_SUCCESS:
- notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
- break;
- default:
- return null;
- }
- //
- // Be sure to add all the history
- //
- notification.getHistory().addAll(this.controlLoopHistory);
- return notification;
- }
-
- private void validateFinalControlLoop() throws ControlLoopException {
- //
- // Check if they activated us
- //
- if (!this.isActivated) {
- throw new ControlLoopException("ControlLoopEventManager MUST be activated first.");
- }
- //
- // Make sure we are expecting this call.
- //
- if (this.onset == null) {
- throw new ControlLoopException("No onset event for ControlLoopEventManager.");
- }
+ public static long getCreateCount() {
+ return createCount.get();
}
/**
- * Process the control loop.
+ * Determines if the manager is still active.
*
- * @return a ControlLoopOperationManager
- * @throws ControlLoopException if an error occurs
+ * @return {@code true} if the manager is still active, {@code false} otherwise
*/
- public ControlLoopOperationManager processControlLoop() throws ControlLoopException {
- validateFinalControlLoop();
- //
- // Is there a current operation?
- //
- if (this.currentOperation != null) {
- //
- // Throw an exception, or simply return the current operation?
- //
- throw new ControlLoopException("Already working an Operation, do not call this method.");
- }
- //
- // Ensure we are not FINAL
- //
- VirtualControlLoopNotification notification = this.isControlLoopFinal();
- if (notification != null) {
- //
- // This is weird, we require them to call the isControlLoopFinal() method first
- //
- // We should really abstract this and avoid throwing an exception, because it really
- // isn't an exception.
- //
- throw new ControlLoopException("Control Loop is in FINAL state, do not call this method.");
- }
- //
- // Not final so get the policy that needs to be worked on.
- //
- Policy policy = this.processor.getCurrentPolicy();
- if (policy == null) {
- throw new ControlLoopException("ControlLoopEventManager: processor came upon null Policy.");
- }
- //
- // And setup an operation
- //
- this.lastOperationManager = this.currentOperation;
- this.currentOperation = new ControlLoopOperationManager(this.onset, policy, this);
- //
- // Return it
- //
- return this.currentOperation;
+ public boolean isActive() {
+ return createdByThisJvmInstance;
}
/**
- * Finish an operation.
- *
- * @param operation the operation
+ * Cancels the current operation and frees all locks.
*/
- public void finishOperation(ControlLoopOperationManager operation) throws ControlLoopException {
- //
- // Verify we have a current operation
- //
- if (this.currentOperation != null) {
- //
- // Validate they are finishing the current operation
- // PLD - this is simply comparing the policy. Do we want to equals the whole object?
- //
- if (this.currentOperation.policy.equals(operation.policy)) {
- logger.debug("Finishing {} result is {}", this.currentOperation.policy.getRecipe(),
- this.currentOperation.getOperationResult());
- //
- // Save history
- //
- this.controlLoopHistory.addAll(this.currentOperation.getHistory());
- //
- // Move to the next Policy
- //
- this.processor.nextPolicyForResult(this.currentOperation.getOperationResult());
- //
- // Just null this out
- //
- this.lastOperationManager = this.currentOperation;
- this.currentOperation = null;
-
- //
- // Don't release the lock - it may be re-used by the next operation
- //
-
- return;
- }
- logger.debug("Cannot finish current operation {} does not match given operation {}",
- this.currentOperation.policy, operation.policy);
- return;
+ public void destroy() {
+ if (isActive()) {
+ getBlockingExecutor().execute(this::freeAllLocks);
}
- throw new ControlLoopException("No operation to finish.");
}
/**
- * Obtain a lock for the current operation.
- *
- * @param callback call-back to be invoked when the lock state changes
- * @return a pair containing the old lock and the new lock, either of which may be null
- * @throws ControlLoopException if an error occurs
+ * Frees all locks.
*/
- public synchronized Pair<Lock, Lock> lockCurrentOperation(LockCallback callback) throws ControlLoopException {
- //
- // Sanity check
- //
- if (this.currentOperation == null) {
- throw new ControlLoopException("Do not have a current operation.");
- }
-
- //
- // Release the old lock if it's for a different resource.
- //
- Lock oldLock = null;
- if (this.targetLock != null
- && !this.targetLock.getResourceId().equals(this.currentOperation.getTargetEntity())) {
- logger.debug("{}: different resource - releasing old lock", getClosedLoopControlName());
- oldLock = this.targetLock;
- this.targetLock = null;
- }
-
- // keep the lock a little longer than the operation, including retries
- int optimeout = Math.max(1, this.currentOperation.getOperationTimeout());
- int nattempts = 1 + Math.max(0, this.currentOperation.getMaxRetries());
- int holdSec = optimeout * nattempts + ADDITIONAL_LOCK_SEC;
-
- //
- // Have we acquired it already?
- //
- if (this.targetLock != null) {
- // we have the lock - just extend it
- this.targetLock.extend(holdSec, callback);
- return new Pair<>(oldLock, null);
-
- } else if (this.useTargetLock) {
- this.targetLock = createRealLock(this.currentOperation.getTargetEntity(), this.onset.getRequestId(),
- holdSec, callback);
- return new Pair<>(oldLock, this.targetLock);
-
- } else {
- // Not using target locks - create a lock w/o actually locking.
- logger.debug("{}: not using target locking; using pseudo locks", getClosedLoopControlName());
- this.targetLock = createPseudoLock(this.currentOperation.getTargetEntity(), this.onset.getRequestId(),
- holdSec, callback);
-
- // Note: no need to invoke callback, as the lock is already ACTIVE
-
- return new Pair<>(oldLock, this.targetLock);
- }
+ private void freeAllLocks() {
+ target2lock.values().forEach(LockData::free);
}
/**
- * Releases the lock for the current operation, deleting it from working memory.
+ * Determines the overall control loop timeout.
*
- * @return the lock, if the operation was locked, {@code null} otherwise
+ * @return the policy timeout, in milliseconds, if specified, a default timeout
+ * otherwise
*/
- public synchronized Lock unlockCurrentOperation() {
- if (this.targetLock == null) {
- return null;
- }
+ private long detmControlLoopTimeoutMs() {
+ // validation checks preclude null or 0 timeout values in the policy
+ int timeout = processor.getPolicy().getProperties().getTimeout();
+ return TimeUnit.MILLISECONDS.convert(timeout, TimeUnit.SECONDS);
+ }
- Lock lock = this.targetLock;
- this.targetLock = null;
+ @Override
+ public synchronized CompletableFuture<OperationOutcome> requestLock(String targetEntity) {
- lock.free();
+ long remainingMs = endTimeMs - System.currentTimeMillis();
+ int remainingSec = 15 + Math.max(0, (int) TimeUnit.SECONDS.convert(remainingMs, TimeUnit.MILLISECONDS));
- return lock;
- }
+ LockData data = target2lock.computeIfAbsent(targetEntity, key -> {
+ var data2 = new LockData(key, requestId);
+ makeLock(targetEntity, requestId.toString(), remainingSec, data2);
- public enum NewEventStatus {
- FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR;
- }
+ data2.addUnavailableCallback(this::onComplete);
- /**
- * An event onset/abatement.
- *
- * @param event the event
- * @return the status
- * @throws AaiException if an error occurs retrieving information from A&AI
- */
- public NewEventStatus onNewEvent(VirtualControlLoopEvent event) throws AaiException {
- try {
- this.checkEventSyntax(event);
- if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
- //
- // Check if this is our original ONSET
- //
- if (event.equals(this.onset)) {
- //
- // DO NOT retract it
- //
- return NewEventStatus.FIRST_ONSET;
- }
- //
- // Log that we got an onset
- //
- this.numOnsets++;
- return NewEventStatus.SUBSEQUENT_ONSET;
- } else if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
- //
- // Have we already got an abatement?
- //
- if (this.abatement == null) {
- //
- // Save this
- //
- this.abatement = event;
- //
- // Keep track that we received another
- //
- this.numAbatements++;
- //
- //
- //
- return NewEventStatus.FIRST_ABATEMENT;
- } else {
- //
- // Keep track that we received another
- //
- this.numAbatements++;
- //
- //
- //
- return NewEventStatus.SUBSEQUENT_ABATEMENT;
- }
- }
- } catch (ControlLoopException e) {
- logger.error("{}: onNewEvent threw: ", this, e);
- }
- return NewEventStatus.SYNTAX_ERROR;
+ return data2;
+ });
+
+ return data.getFuture();
}
+ @Override
+ public synchronized CompletableFuture<OperationOutcome> releaseLock(String targetEntity) {
+ LockData data = target2lock.remove(targetEntity);
+
+ if (data == null) {
+ // lock did not exist - immediately return a success
+ OperationOutcome outcome = makeUnlockOutcome(targetEntity);
+ outcome.setEnd(outcome.getStart());
+ onComplete(outcome);
- /**
- * Commit the abatement to the history database.
- *
- * @param message the abatement message
- * @param outcome the abatement outcome
- */
- public void commitAbatement(String message, String outcome) {
- if (this.lastOperationManager == null) {
- logger.error("{}: commitAbatement: no operation manager", this);
- return;
- }
- try {
- this.lastOperationManager.commitAbatement(message, outcome);
- } catch (NoSuchElementException e) {
- logger.error("{}: commitAbatement threw an exception ", this, e);
+ return CompletableFuture.completedFuture(outcome);
}
- }
+ /*
+ * previous lock operation may not have completed yet, thus we tack the unlock
+ * operation onto it.
+ *
+ * Note: we must invoke free(), asynchronously (i.e., using whenCompleteAsync()),
+ * as it may block
+ */
- /**
- * Set the control loop time out.
- *
- * @return a VirtualControlLoopNotification
- */
- public VirtualControlLoopNotification setControlLoopTimedOut() {
- this.controlLoopTimedOut = FinalResult.FINAL_FAILURE_TIMEOUT;
- VirtualControlLoopNotification notification = new VirtualControlLoopNotification(this.onset);
- notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
- notification.setMessage("Control Loop timed out");
- notification.getHistory().addAll(this.controlLoopHistory);
- return notification;
- }
+ return data.getFuture().whenCompleteAsync((lockOutcome, thrown) -> {
- public boolean isControlLoopTimedOut() {
- return (this.controlLoopTimedOut == FinalResult.FINAL_FAILURE_TIMEOUT);
- }
+ OperationOutcome outcome = makeUnlockOutcome(targetEntity);
- /**
- * Get the control loop timeout.
- *
- * @param defaultTimeout the default timeout
- * @return the timeout
- */
- public int getControlLoopTimeout(Integer defaultTimeout) {
- if (this.processor != null && this.processor.getControlLoop() != null) {
- Integer timeout = this.processor.getControlLoop().getTimeout();
- if (timeout != null && timeout > 0) {
- return timeout;
+ try {
+ data.free();
+
+ } catch (RuntimeException e) {
+ logger.warn("failed to unlock {}", targetEntity, e);
+ outcome.setResult(OperationResult.FAILURE_EXCEPTION);
+ outcome.setMessage(ControlLoopOperation.FAILED_MSG + ": " + e.getMessage());
}
- }
- if (defaultTimeout != null) {
- return defaultTimeout;
- }
- return 0;
- }
- /**
- * Check an event syntax.
- *
- * @param event the event syntax
- * @throws ControlLoopException if an error occurs
- */
- public 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 - expecting VM_NAME or VNF_NAME");
- }
- validateAaiData(event);
- }
+ outcome.setEnd(Instant.now());
+ onComplete(outcome);
- private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
- if (event.getClosedLoopEventStatus() == null
- || (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
- && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED)) {
- throw new ControlLoopException("Invalid value in closedLoopEventStatus");
- }
+ }, getBlockingExecutor());
}
- private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
- Map<String, String> eventAai = event.getAai();
- if (eventAai == null) {
- throw new ControlLoopException("AAI is null");
- }
- switch (event.getTargetType()) {
- case VM:
- case VNF:
- validateAaiVmVnfData(eventAai);
- return;
- case PNF:
- validateAaiPnfData(eventAai);
- return;
- default:
- throw new ControlLoopException("The target type is not supported");
- }
+ private OperationOutcome makeUnlockOutcome(String targetEntity) {
+ var outcome = new OperationOutcome();
+ outcome.setActor(ActorConstants.LOCK_ACTOR);
+ outcome.setOperation(ActorConstants.UNLOCK_OPERATION);
+ outcome.setTarget(targetEntity);
+ outcome.setResult(OperationResult.SUCCESS);
+ outcome.setMessage(ControlLoopOperation.SUCCESS_MSG);
+ outcome.setFinalOutcome(true);
+ outcome.setStart(Instant.now());
+ return outcome;
}
- private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
- if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
- && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
- throw new ControlLoopException(
- "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
- }
+ public void onStart(OperationOutcome outcome) {
+ outcomes.add(outcome);
}
- private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
- if (eventAai.get(PNF_NAME) == null) {
- throw new ControlLoopException("AAI PNF object key pnf-name is missing");
- }
+ public void onComplete(OperationOutcome outcome) {
+ outcomes.add(outcome);
}
/**
- * Is closed loop disabled for an event.
+ * Determines if the context contains a property.
*
- * @param event the event
- * @return <code>true</code> if the control loop is disabled, <code>false</code> otherwise
+ * @param name name of the property of interest
+ * @return {@code true} if the context contains the property, {@code false} otherwise
*/
- public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
- Map<String, String> aai = event.getAai();
- return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
- || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
- || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
+ @Override
+ public boolean contains(String name) {
+ return properties.containsKey(name);
}
/**
- * Does provisioning status, for an event, have a value other than ACTIVE.
+ * Gets a property, casting it to the desired type.
*
- * @param event the event
- * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null}, {@code false} otherwise
+ * @param <T> desired type
+ * @param name name of the property whose value is to be retrieved
+ * @return the property's value, or {@code null} if it does not yet have a value
*/
- protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
- Map<String, String> aai = event.getAai();
- return (!PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
- || !PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T getProperty(String name) {
+ return (T) properties.get(name);
}
/**
- * Determines the boolean value represented by the given AAI field value.
+ * Sets a property's value.
*
- * @param aaiValue value to be examined
- * @return the boolean value represented by the field value, or {@code false} if the value is {@code null}
+ * @param name property name
+ * @param value new property value
*/
- protected static boolean isAaiTrue(String aaiValue) {
- return ("true".equalsIgnoreCase(aaiValue) || "T".equalsIgnoreCase(aaiValue) || "yes".equalsIgnoreCase(aaiValue)
- || "Y".equalsIgnoreCase(aaiValue));
- }
-
@Override
- public String toString() {
- return "ControlLoopEventManager [closedLoopControlName=" + closedLoopControlName + ", requestId=" + requestId
- + ", processor=" + processor + ", onset=" + (onset != null ? onset.getRequestId() : "null")
- + ", numOnsets=" + numOnsets + ", numAbatements=" + numAbatements + ", isActivated=" + isActivated
- + ", currentOperation=" + currentOperation + ", targetLock=" + targetLock + "]";
+ public void setProperty(String name, Serializable value) {
+ logger.info("set property {}={} manager={}", name, value, this);
+ properties.put(name, value);
}
/**
- * This function calls Aai Custom Query and responds with the AaiCqResponse.
+ * Removes a property.
*
- * @param event input event
- * @return AaiCqResponse Response from Aai for custom query. Can not be null.
- * @throws AaiException if error occurs
+ * @param name property name
*/
- public AaiCqResponse getCqResponse(VirtualControlLoopEvent event) throws AaiException {
-
- Map<String, String> aai = event.getAai();
-
- if (aai.containsKey(VSERVER_IS_CLOSED_LOOP_DISABLED) || aai.containsKey(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED)) {
-
- if (isClosedLoopDisabled(event)) {
- throw new AaiException("is-closed-loop-disabled is set to true on VServer or VNF");
- }
-
- if (isProvStatusInactive(event)) {
- throw new AaiException("prov-status is not ACTIVE on VServer or VNF");
- }
- }
-
- if (!aai.containsKey(VSERVER_VSERVER_NAME)) {
- throw new AaiException("Vserver name is missing");
- }
-
- UUID reqId = event.getRequestId();
- AaiCqResponse response = null;
- String vserverId = event.getAai().get(VSERVER_VSERVER_NAME);
-
- String aaiHostUrl = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_URL);
- String aaiUser = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_USERNAME_PROPERTY);
- String aaiPassword = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_PASS_PROPERTY);
-
- response = new AaiManager(new RestManager()).getCustomQueryResponse(aaiHostUrl, aaiUser, aaiPassword, reqId,
- vserverId);
-
- if (response == null) {
- throw new AaiException("Target vnf-id could not be found");
- }
-
- return response;
-
+ @Override
+ public void removeProperty(String name) {
+ properties.remove(name);
}
- /**
- * Get the specified pnf data from aai.
- * @param event the event containing pnf id.
- * @return pnf key value data.
- * @throws AaiException if an aai error occurs.
- */
- public Map<String, String> getPnf(VirtualControlLoopEvent event) throws AaiException {
- Map<String, String> aai = event.getAai();
-
- if (!aai.containsKey(PNF_NAME)) {
- throw new AaiException("Missing unique identifier for PNF AAI object in the event.");
- }
-
- UUID reqId = event.getRequestId();
- String pnfName = event.getAai().get(PNF_NAME);
- String aaiHostUrl = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_URL);
- String aaiUser = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_USERNAME_PROPERTY);
- String aaiPassword = PolicyEngineConstants.getManager().getEnvironmentProperty(AAI_PASS_PROPERTY);
+ // the following methods may be overridden by junit tests
- Map<String, String> pnfParams =
- new AaiManager(new RestManager()).getPnf(aaiHostUrl, aaiUser, aaiPassword, reqId, pnfName);
+ public Executor getExecutor() {
+ return ForkJoinPool.commonPool();
+ }
- if (pnfParams == null) {
- throw new AaiException("Aai response is undefined");
- }
- return pnfParams;
+ protected ExecutorService getBlockingExecutor() {
+ return PolicyEngineConstants.getManager().getExecutorService();
}
+ protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
+ PolicyEngineConstants.getManager().createLock(targetEntity, requestId, holdSec, callback, false);
+ }
- // the following methods may be overridden by junit tests
+ public ActorService getActorService() {
+ return services.getActorService();
+ }
- protected Lock createRealLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
- return PolicyEngineConstants.getManager().createLock(targetEntity, requestId.toString(), holdSec, callback,
- false);
+ public OperationHistoryDataManager getDataManager() {
+ boolean guardDisabled = "true".equalsIgnoreCase(getEnvironmentProperty(GUARD_DISABLED_PROPERTY));
+ return (guardDisabled ? STUB_DATA_MANAGER : services.getDataManager());
}
- // note: the "callback" is required, because it will be invoked when lock.extend() is
- // invoked
- protected Lock createPseudoLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
- return new LockImpl(LockState.ACTIVE, targetEntity, requestId.toString(), holdSec, callback);
+ protected String getEnvironmentProperty(String propName) {
+ return PolicyEngineConstants.getManager().getEnvironmentProperty(propName);
}
}