2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.controlloop.eventmanager;
 
  23 import static org.onap.policy.controlloop.ControlLoopTargetType.PNF;
 
  24 import static org.onap.policy.controlloop.ControlLoopTargetType.VM;
 
  25 import static org.onap.policy.controlloop.ControlLoopTargetType.VNF;
 
  27 import java.io.Serializable;
 
  28 import java.util.Collections;
 
  29 import java.util.HashMap;
 
  30 import java.util.LinkedList;
 
  33 import java.util.UUID;
 
  34 import java.util.concurrent.CompletableFuture;
 
  35 import java.util.concurrent.Executor;
 
  36 import java.util.concurrent.ExecutorService;
 
  37 import java.util.concurrent.ForkJoinPool;
 
  38 import java.util.concurrent.TimeUnit;
 
  39 import java.util.concurrent.atomic.AtomicLong;
 
  40 import java.util.concurrent.atomic.AtomicReference;
 
  41 import java.util.function.Consumer;
 
  42 import java.util.stream.Collectors;
 
  43 import java.util.stream.Stream;
 
  45 import lombok.ToString;
 
  46 import org.apache.commons.lang3.StringUtils;
 
  47 import org.drools.core.WorkingMemory;
 
  48 import org.kie.api.runtime.rule.FactHandle;
 
  49 import org.onap.policy.controlloop.ControlLoopEventStatus;
 
  50 import org.onap.policy.controlloop.ControlLoopException;
 
  51 import org.onap.policy.controlloop.ControlLoopNotificationType;
 
  52 import org.onap.policy.controlloop.ControlLoopOperation;
 
  53 import org.onap.policy.controlloop.ControlLoopResponse;
 
  54 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  55 import org.onap.policy.controlloop.VirtualControlLoopNotification;
 
  56 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
 
  57 import org.onap.policy.controlloop.actorserviceprovider.OperationFinalResult;
 
  58 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  59 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  60 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
 
  61 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
 
  62 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
 
  63 import org.onap.policy.drools.core.lock.LockCallback;
 
  64 import org.onap.policy.drools.domain.models.operational.Operation;
 
  65 import org.onap.policy.drools.system.PolicyEngineConstants;
 
  66 import org.slf4j.Logger;
 
  67 import org.slf4j.LoggerFactory;
 
  70  * Manager for a single control loop event. Once this has been created, the event can be
 
  71  * retracted from working memory. Once this has been created, {@link #start()} should be
 
  72  * invoked, and then {@link #nextStep()} should be invoked continually until
 
  73  * {@link #isActive()} returns {@code false}, indicating that all steps have completed.
 
  75 @ToString(onlyExplicitlyIncluded = true)
 
  76 public class ControlLoopEventManager2 implements ManagerContext, Serializable {
 
  77     private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager2.class);
 
  78     private static final long serialVersionUID = -1216568161322872641L;
 
  80     private static final String EVENT_MANAGER_SERVICE_CONFIG = "event-manager";
 
  81     public static final String PROV_STATUS_ACTIVE = "ACTIVE";
 
  82     private static final String VM_NAME = "VM_NAME";
 
  83     private static final String VNF_NAME = "VNF_NAME";
 
  84     public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
 
  85     public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
 
  86     public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
 
  87     public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
 
  88     public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
 
  89     public static final String PNF_IS_IN_MAINT = "pnf.in-maint";
 
  90     public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
 
  91     public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
 
  92     public static final String PNF_ID = "pnf.pnf-id";
 
  93     public static final String PNF_NAME = "pnf.pnf-name";
 
  95     private static final Set<String> VALID_TARGETS = Stream
 
  96                     .of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
 
  97                     .map(String::toLowerCase).collect(Collectors.toSet());
 
  99     private static final Set<String> TRUE_VALUES = Set.of("true", "t", "yes", "y");
 
 102      * Counts the number of these objects that have been created.  This is used by junit
 
 105     private static final AtomicLong createCount = new AtomicLong(0);
 
 107     public enum NewEventStatus {
 
 108         FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
 
 112      * {@code True} if this object was created by this JVM instance, {@code false}
 
 113      * otherwise. This will be {@code false} if this object is reconstituted from a
 
 114      * persistent store or by transfer from another server.
 
 116     private transient boolean createdByThisJvmInstance;
 
 120     public final String closedLoopControlName;
 
 123     private final UUID requestId;
 
 125     private final ControlLoopEventContext context;
 
 127     private int numOnsets = 1;
 
 129     private int numAbatements = 0;
 
 130     private VirtualControlLoopEvent abatement = null;
 
 133      * Time, in milliseconds, when the control loop will time out.
 
 136     private final long endTimeMs;
 
 138     // fields extracted from the ControlLoopParams
 
 140     private final String policyName;
 
 141     private final String policyScope;
 
 142     private final String policyVersion;
 
 144     private final LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
 
 147      * Maps a target entity to its lock.
 
 149     private final transient Map<String, LockData> target2lock = new HashMap<>();
 
 151     private final ControlLoopProcessor processor;
 
 152     private final AtomicReference<ControlLoopOperationManager2> currentOperation = new AtomicReference<>();
 
 154     private OperationFinalResult finalResult = null;
 
 157     private VirtualControlLoopNotification notification;
 
 159     private ControlLoopResponse controlLoopResponse;
 
 162     private boolean updated = false;
 
 164     private final transient WorkingMemory workMem;
 
 165     private transient FactHandle factHandle;
 
 169      * Constructs the object.
 
 171      * @param params control loop parameters
 
 172      * @param event event to be managed by this object
 
 173      * @param workMem working memory to update if this changes
 
 174      * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
 
 177     public ControlLoopEventManager2(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
 
 178                     throws ControlLoopException {
 
 180         createCount.incrementAndGet();
 
 182         checkEventSyntax(event);
 
 184         if (isClosedLoopDisabled(event)) {
 
 185             throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
 
 188         if (isProvStatusInactive(event)) {
 
 189             throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
 
 192         this.createdByThisJvmInstance = true;
 
 193         this.closedLoopControlName = params.getClosedLoopControlName();
 
 194         this.requestId = event.getRequestId();
 
 195         this.context = new ControlLoopEventContext(event);
 
 196         this.policyName = params.getPolicyName();
 
 197         this.policyScope = params.getPolicyScope();
 
 198         this.policyVersion = params.getPolicyVersion();
 
 199         this.processor = new ControlLoopProcessor(params.getToscaPolicy());
 
 200         this.workMem = workMem;
 
 201         this.endTimeMs = System.currentTimeMillis() + detmControlLoopTimeoutMs();
 
 205      * Gets the number of managers objects that have been created.
 
 206      * @return the number of managers objects that have been created
 
 208     public static long getCreateCount() {
 
 209         return createCount.get();
 
 213      * Starts the manager.
 
 215      * @throws ControlLoopException if the processor cannot get a policy
 
 217     public void start() throws ControlLoopException {
 
 219             throw new IllegalStateException("manager is no longer active");
 
 222         if ((factHandle = workMem.getFactHandle(this)) == null) {
 
 223             throw new IllegalStateException("manager is not in working memory");
 
 226         if (currentOperation.get() != null) {
 
 227             throw new IllegalStateException("manager already started");
 
 234      * Starts an operation for the current processor policy.
 
 236      * @throws ControlLoopException if the processor cannot get a policy
 
 238     private synchronized void startOperation() throws ControlLoopException {
 
 240         if ((finalResult = processor.checkIsCurrentPolicyFinal()) == null) {
 
 241             // not final - start the next operation
 
 242             currentOperation.set(makeOperationManager(context, processor.getCurrentPolicy()));
 
 243             currentOperation.get().start(endTimeMs - System.currentTimeMillis());
 
 247         logger.info("final={} oper state={} for {}", finalResult, currentOperation.get().getState(), requestId);
 
 249         controlLoopResponse = null;
 
 250         notification = makeNotification();
 
 251         notification.setHistory(controlLoopHistory);
 
 253         switch (finalResult) {
 
 254             case FINAL_FAILURE_EXCEPTION:
 
 255                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
 
 256                 notification.setMessage("Exception in processing closed loop");
 
 259                 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
 
 262                 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
 
 266                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
 
 272      * Starts the next step, whatever that may be.
 
 274     public synchronized void nextStep() {
 
 282             if (!currentOperation.get().nextStep()) {
 
 283                 // current operation is done - try the next
 
 284                 controlLoopHistory.addAll(currentOperation.get().getHistory());
 
 285                 processor.nextPolicyForResult(currentOperation.get().getOperationResult());
 
 289         } catch (ControlLoopException | RuntimeException e) {
 
 290             // processor problem - this is fatal
 
 291             logger.warn("{}: cannot start next step for {}", closedLoopControlName, requestId, e);
 
 292             finalResult = OperationFinalResult.FINAL_FAILURE_EXCEPTION;
 
 293             controlLoopResponse = null;
 
 294             notification = makeNotification();
 
 295             notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
 
 296             notification.setMessage("Policy processing aborted due to policy error");
 
 297             notification.setHistory(controlLoopHistory);
 
 302      * Determines if the manager is still active.
 
 304      * @return {@code true} if the manager is still active, {@code false} otherwise
 
 306     public synchronized boolean isActive() {
 
 307         return (createdByThisJvmInstance && finalResult == null);
 
 311      * Updates working memory if this changes.
 
 313      * @param operation operation manager that was updated
 
 316     public synchronized void updated(ControlLoopOperationManager2 operation) {
 
 317         if (!isActive() || operation != currentOperation.get()) {
 
 318             // no longer working on the given operation
 
 322         controlLoopResponse = operation.getControlLoopResponse();
 
 323         notification = makeNotification();
 
 325         VirtualControlLoopEvent event = context.getEvent();
 
 327         switch (operation.getState()) {
 
 329                 notification.setNotification(ControlLoopNotificationType.REJECTED);
 
 330                 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is already locked");
 
 333                 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
 
 334                 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is no longer locked");
 
 337                 notification.setNotification(ControlLoopNotificationType.OPERATION);
 
 338                 notification.setMessage(
 
 339                                 "Sending guard query for " + operation.getActor() + " " + operation.getOperation());
 
 341             case GUARD_PERMITTED:
 
 342                 notification.setNotification(ControlLoopNotificationType.OPERATION);
 
 343                 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
 
 347                 notification.setNotification(ControlLoopNotificationType.OPERATION);
 
 348                 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
 
 351             case OPERATION_STARTED:
 
 352                 notification.setNotification(ControlLoopNotificationType.OPERATION);
 
 353                 notification.setMessage(operation.getOperationMessage());
 
 354                 notification.setHistory(Collections.emptyList());
 
 356             case OPERATION_SUCCESS:
 
 357                 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
 
 360             case CONTROL_LOOP_TIMEOUT:
 
 361                 logger.warn("{}: control loop timed out for {}", closedLoopControlName, requestId);
 
 362                 controlLoopHistory.addAll(currentOperation.get().getHistory());
 
 363                 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
 
 364                 notification.setMessage("Control Loop timed out");
 
 365                 notification.setHistory(controlLoopHistory);
 
 366                 finalResult = OperationFinalResult.FINAL_FAILURE;
 
 369             case OPERATION_FAILURE:
 
 371                 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
 
 376         workMem.update(factHandle, this);
 
 380      * Cancels the current operation and frees all locks.
 
 382     public synchronized void destroy() {
 
 383         ControlLoopOperationManager2 oper = currentOperation.get();
 
 388         getBlockingExecutor().execute(this::freeAllLocks);
 
 394     private void freeAllLocks() {
 
 395         target2lock.values().forEach(LockData::free);
 
 399      * Makes a notification message for the current operation.
 
 401      * @return a new notification
 
 403     public synchronized VirtualControlLoopNotification makeNotification() {
 
 404         VirtualControlLoopNotification notif = new VirtualControlLoopNotification(context.getEvent());
 
 405         notif.setNotification(ControlLoopNotificationType.OPERATION);
 
 406         notif.setFrom("policy");
 
 407         notif.setPolicyScope(policyScope);
 
 408         notif.setPolicyVersion(policyVersion);
 
 410         if (finalResult == null) {
 
 411             ControlLoopOperationManager2 oper = currentOperation.get();
 
 413                 notif.setMessage(oper.getOperationHistory());
 
 414                 notif.setHistory(oper.getHistory());
 
 422      * An event onset/abatement.
 
 424      * @param event the event
 
 427     public synchronized NewEventStatus onNewEvent(VirtualControlLoopEvent event) {
 
 429             checkEventSyntax(event);
 
 431             if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
 
 432                 if (event.equals(context.getEvent())) {
 
 433                     return NewEventStatus.FIRST_ONSET;
 
 437                 return NewEventStatus.SUBSEQUENT_ONSET;
 
 440                 if (abatement == null) {
 
 443                     return NewEventStatus.FIRST_ABATEMENT;
 
 446                     return NewEventStatus.SUBSEQUENT_ABATEMENT;
 
 449         } catch (ControlLoopException e) {
 
 450             logger.error("{}: onNewEvent threw an exception", this, e);
 
 451             return NewEventStatus.SYNTAX_ERROR;
 
 456      * Determines the overall control loop timeout.
 
 458      * @return the policy timeout, in milliseconds, if specified, a default timeout
 
 461     private long detmControlLoopTimeoutMs() {
 
 462         // validation checks preclude null or 0 timeout values in the policy
 
 463         Integer timeout = processor.getPolicy().getProperties().getTimeout();
 
 464         return TimeUnit.MILLISECONDS.convert(timeout, TimeUnit.SECONDS);
 
 468      * Check an event syntax.
 
 470      * @param event the event syntax
 
 471      * @throws ControlLoopException if an error occurs
 
 473     protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
 
 474         validateStatus(event);
 
 475         if (StringUtils.isBlank(event.getClosedLoopControlName())) {
 
 476             throw new ControlLoopException("No control loop name");
 
 478         if (event.getRequestId() == null) {
 
 479             throw new ControlLoopException("No request ID");
 
 481         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
 
 484         if (StringUtils.isBlank(event.getTarget())) {
 
 485             throw new ControlLoopException("No target field");
 
 486         } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
 
 487             throw new ControlLoopException("target field invalid");
 
 489         validateAaiData(event);
 
 492     private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
 
 493         if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
 
 494                         && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
 
 495             throw new ControlLoopException("Invalid value in closedLoopEventStatus");
 
 499     private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
 
 500         Map<String, String> eventAai = event.getAai();
 
 501         if (eventAai == null) {
 
 502             throw new ControlLoopException("AAI is null");
 
 504         if (event.getTargetType() == null) {
 
 505             throw new ControlLoopException("The Target type is null");
 
 507         switch (event.getTargetType()) {
 
 510                 validateAaiVmVnfData(eventAai);
 
 513                 validateAaiPnfData(eventAai);
 
 516                 throw new ControlLoopException("The target type is not supported");
 
 520     private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
 
 521         if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
 
 522                         && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
 
 523             throw new ControlLoopException(
 
 524                             "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
 
 528     private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
 
 529         if (eventAai.get(PNF_NAME) == null) {
 
 530             throw new ControlLoopException("AAI PNF object key pnf-name is missing");
 
 535      * Is closed loop disabled for an event.
 
 537      * @param event the event
 
 538      * @return <code>true</code> if the control loop is disabled, <code>false</code>
 
 541     private static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
 
 542         Map<String, String> aai = event.getAai();
 
 543         return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
 
 544                         || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
 
 545                         || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
 
 549      * Does provisioning status, for an event, have a value other than ACTIVE.
 
 551      * @param event the event
 
 552      * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
 
 553      *         {@code false} otherwise
 
 555     private static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
 
 556         Map<String, String> aai = event.getAai();
 
 557         return !(PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
 
 558                         && PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
 
 562      * Determines the boolean value represented by the given AAI field value.
 
 564      * @param aaiValue value to be examined
 
 565      * @return the boolean value represented by the field value, or {@code false} if the
 
 566      *         value is {@code null}
 
 568     private static boolean isAaiTrue(String aaiValue) {
 
 569         return (aaiValue != null && TRUE_VALUES.contains(aaiValue.toLowerCase()));
 
 573      * Requests a lock. This requests the lock for the time that remains before the
 
 574      * timeout expires. This avoids having to extend the lock.
 
 576      * @param targetEntity entity to be locked
 
 577      * @param lockUnavailableCallback function to be invoked if the lock is
 
 579      * @return a future that can be used to await the lock
 
 582     public synchronized CompletableFuture<OperationOutcome> requestLock(String targetEntity,
 
 583                     Consumer<OperationOutcome> lockUnavailableCallback) {
 
 585         long remainingMs = endTimeMs - System.currentTimeMillis();
 
 586         int remainingSec = 15 + Math.max(0, (int) TimeUnit.SECONDS.convert(remainingMs, TimeUnit.MILLISECONDS));
 
 588         LockData data = target2lock.computeIfAbsent(targetEntity, key -> {
 
 589             LockData data2 = new LockData(key, requestId);
 
 590             makeLock(targetEntity, requestId.toString(), remainingSec, data2);
 
 594         data.addUnavailableCallback(lockUnavailableCallback);
 
 596         return data.getFuture();
 
 600      * Initializes various components, on demand.
 
 602     private static class LazyInitData {
 
 603         private static final OperationHistoryDataManager DATA_MANAGER;
 
 604         private static final ActorService ACTOR_SERVICE;
 
 607             EventManagerServices services = new EventManagerServices(EVENT_MANAGER_SERVICE_CONFIG);
 
 608             ACTOR_SERVICE = services.getActorService();
 
 609             DATA_MANAGER = services.getDataManager();
 
 613     // the following methods may be overridden by junit tests
 
 615     protected ControlLoopOperationManager2 makeOperationManager(ControlLoopEventContext ctx, Operation operation) {
 
 616         return new ControlLoopOperationManager2(this, ctx, operation, getExecutor());
 
 619     protected Executor getExecutor() {
 
 620         return ForkJoinPool.commonPool();
 
 623     protected ExecutorService getBlockingExecutor() {
 
 624         return PolicyEngineConstants.getManager().getExecutorService();
 
 627     protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
 
 628         PolicyEngineConstants.getManager().createLock(targetEntity, requestId, holdSec, callback, false);
 
 632     public ActorService getActorService() {
 
 633         return LazyInitData.ACTOR_SERVICE;
 
 637     public OperationHistoryDataManager getDataManager() {
 
 638         return LazyInitData.DATA_MANAGER;