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.AtomicReference;
40 import java.util.function.Consumer;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
44 import lombok.ToString;
45 import org.apache.commons.lang3.StringUtils;
46 import org.drools.core.WorkingMemory;
47 import org.kie.api.runtime.rule.FactHandle;
48 import org.onap.policy.controlloop.ControlLoopEventStatus;
49 import org.onap.policy.controlloop.ControlLoopException;
50 import org.onap.policy.controlloop.ControlLoopNotificationType;
51 import org.onap.policy.controlloop.ControlLoopOperation;
52 import org.onap.policy.controlloop.VirtualControlLoopEvent;
53 import org.onap.policy.controlloop.VirtualControlLoopNotification;
54 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
56 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
57 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
58 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
59 import org.onap.policy.controlloop.policy.FinalResult;
60 import org.onap.policy.controlloop.policy.Policy;
61 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
62 import org.onap.policy.drools.core.lock.LockCallback;
63 import org.onap.policy.drools.system.PolicyEngineConstants;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
68 * Manager for a single control loop event. Once this has been created, the event can be
69 * retracted from working memory. Once this has been created, {@link #start()} should be
70 * invoked, and then {@link #nextStep()} should be invoked continually until
71 * {@link #isActive()} returns {@code false}, indicating that all steps have completed.
73 @ToString(onlyExplicitlyIncluded = true)
74 public class ControlLoopEventManager2 implements ManagerContext, Serializable {
75 private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManager2.class);
76 private static final long serialVersionUID = -1216568161322872641L;
78 private static final String EVENT_MANAGER_SERVICE_CONFIG = "config/event-manager.properties";
79 public static final String PROV_STATUS_ACTIVE = "ACTIVE";
80 private static final String VM_NAME = "VM_NAME";
81 private static final String VNF_NAME = "VNF_NAME";
82 public static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
83 public static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
84 public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
85 public static final String GENERIC_VNF_IS_CLOSED_LOOP_DISABLED = "generic-vnf.is-closed-loop-disabled";
86 public static final String VSERVER_IS_CLOSED_LOOP_DISABLED = "vserver.is-closed-loop-disabled";
87 public static final String PNF_IS_IN_MAINT = "pnf.in-maint";
88 public static final String GENERIC_VNF_PROV_STATUS = "generic-vnf.prov-status";
89 public static final String VSERVER_PROV_STATUS = "vserver.prov-status";
90 public static final String PNF_ID = "pnf.pnf-id";
91 public static final String PNF_NAME = "pnf.pnf-name";
93 private static final Set<String> VALID_TARGETS = Stream
94 .of(VM_NAME, VNF_NAME, VSERVER_VSERVER_NAME, GENERIC_VNF_VNF_ID, GENERIC_VNF_VNF_NAME, PNF_NAME)
95 .map(String::toLowerCase).collect(Collectors.toSet());
97 private static final Set<String> TRUE_VALUES = Set.of("true", "t", "yes", "y");
99 public enum NewEventStatus {
100 FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
104 * {@code True} if this object was created by this JVM instance, {@code false}
105 * otherwise. This will be {@code false} if this object is reconstituted from a
106 * persistent store or by transfer from another server.
108 private transient boolean createdByThisJvmInstance;
112 public final String closedLoopControlName;
115 private final UUID requestId;
116 private final ControlLoopEventContext context;
118 private int numOnsets = 1;
120 private int numAbatements = 0;
121 private VirtualControlLoopEvent abatement = null;
124 * Time, in milliseconds, when the control loop will time out.
127 private final long endTimeMs;
129 // fields extracted from the ControlLoopParams
131 private final String policyName;
132 private final String policyScope;
133 private final String policyVersion;
135 private final LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
138 * Maps a target entity to its lock.
140 private final transient Map<String, LockData> target2lock = new HashMap<>();
142 private final ControlLoopProcessor processor;
143 private final AtomicReference<ControlLoopOperationManager2> currentOperation = new AtomicReference<>();
145 private FinalResult finalResult = null;
148 private VirtualControlLoopNotification notification;
151 private boolean updated = false;
153 private final transient WorkingMemory workMem;
154 private transient FactHandle factHandle;
158 * Constructs the object.
160 * @param params control loop parameters
161 * @param event event to be managed by this object
162 * @param workMem working memory to update if this changes
163 * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
166 public ControlLoopEventManager2(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
167 throws ControlLoopException {
169 checkEventSyntax(event);
171 if (isClosedLoopDisabled(event)) {
172 throw new IllegalStateException("is-closed-loop-disabled is set to true on VServer or VNF");
175 if (isProvStatusInactive(event)) {
176 throw new IllegalStateException("prov-status is not ACTIVE on VServer or VNF");
179 this.createdByThisJvmInstance = true;
180 this.closedLoopControlName = params.getClosedLoopControlName();
181 this.requestId = event.getRequestId();
182 this.context = new ControlLoopEventContext(event);
183 this.policyName = params.getPolicyName();
184 this.policyScope = params.getPolicyScope();
185 this.policyVersion = params.getPolicyVersion();
186 this.processor = new ControlLoopProcessor(params.getToscaPolicy());
187 this.workMem = workMem;
188 this.endTimeMs = System.currentTimeMillis() + detmControlLoopTimeoutMs();
192 * Starts the manager.
194 * @throws ControlLoopException if the processor cannot get a policy
196 public void start() throws ControlLoopException {
198 throw new IllegalStateException("manager is no longer active");
201 if ((factHandle = workMem.getFactHandle(this)) == null) {
202 throw new IllegalStateException("manager is not in working memory");
205 if (currentOperation.get() != null) {
206 throw new IllegalStateException("manager already started");
213 * Starts an operation for the current processor policy.
215 * @throws ControlLoopException if the processor cannot get a policy
217 private synchronized void startOperation() throws ControlLoopException {
219 if ((finalResult = processor.checkIsCurrentPolicyFinal()) == null) {
220 // not final - start the next operation
221 currentOperation.set(makeOperationManager(context, processor.getCurrentPolicy()));
222 currentOperation.get().start(endTimeMs - System.currentTimeMillis());
226 logger.info("final={} oper state={} for {}", finalResult, currentOperation.get().getState(), requestId);
228 notification = makeNotification();
229 notification.setHistory(controlLoopHistory);
231 switch (finalResult) {
232 case FINAL_FAILURE_EXCEPTION:
233 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
234 notification.setMessage("Exception in processing closed loop");
237 notification.setNotification(ControlLoopNotificationType.FINAL_SUCCESS);
240 notification.setNotification(ControlLoopNotificationType.FINAL_OPENLOOP);
244 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
250 * Starts the next step, whatever that may be.
252 public void nextStep() {
260 if (!currentOperation.get().nextStep()) {
261 // current operation is done - try the next
262 controlLoopHistory.addAll(currentOperation.get().getHistory());
263 processor.nextPolicyForResult(currentOperation.get().getOperationResult());
267 } catch (ControlLoopException | RuntimeException e) {
268 // processor problem - this is fatal
269 logger.warn("{}: cannot start next step for {}", closedLoopControlName, requestId, e);
270 finalResult = FinalResult.FINAL_FAILURE_EXCEPTION;
271 notification = makeNotification();
272 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
273 notification.setMessage("Policy processing aborted due to policy error");
274 notification.setHistory(controlLoopHistory);
279 * Determines if the manager is still active.
281 * @return {@code true} if the manager is still active, {@code false} otherwise
283 public boolean isActive() {
284 return (createdByThisJvmInstance && finalResult == null);
288 * Updates working memory if this changes.
290 * @param operation operation manager that was updated
293 public synchronized void updated(ControlLoopOperationManager2 operation) {
294 if (!isActive() || operation != currentOperation.get()) {
295 // no longer working on the given operation
299 notification = makeNotification();
301 VirtualControlLoopEvent event = context.getEvent();
303 switch (operation.getState()) {
305 notification.setNotification(ControlLoopNotificationType.REJECTED);
306 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is already locked");
309 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
310 notification.setMessage("The target " + event.getAai().get(event.getTarget()) + " is no longer locked");
313 notification.setNotification(ControlLoopNotificationType.OPERATION);
314 notification.setMessage(
315 "Sending guard query for " + operation.getActor() + " " + operation.getOperation());
317 case GUARD_PERMITTED:
318 notification.setNotification(ControlLoopNotificationType.OPERATION);
319 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
323 notification.setNotification(ControlLoopNotificationType.OPERATION);
324 notification.setMessage("Guard result for " + operation.getActor() + " " + operation.getOperation()
327 case OPERATION_STARTED:
328 notification.setNotification(ControlLoopNotificationType.OPERATION);
329 notification.setMessage(operation.getOperationMessage());
330 notification.setHistory(Collections.emptyList());
332 case OPERATION_SUCCESS:
333 notification.setNotification(ControlLoopNotificationType.OPERATION_SUCCESS);
336 case CONTROL_LOOP_TIMEOUT:
337 logger.warn("{}: control loop timed out for {}", closedLoopControlName, requestId);
338 controlLoopHistory.addAll(currentOperation.get().getHistory());
339 notification.setNotification(ControlLoopNotificationType.FINAL_FAILURE);
340 notification.setMessage("Control Loop timed out");
341 notification.setHistory(controlLoopHistory);
342 finalResult = FinalResult.FINAL_FAILURE;
345 case OPERATION_FAILURE:
347 notification.setNotification(ControlLoopNotificationType.OPERATION_FAILURE);
352 workMem.update(factHandle, this);
356 * Cancels the current operation and frees all locks.
358 public void destroy() {
359 ControlLoopOperationManager2 oper = currentOperation.get();
364 getBlockingExecutor().execute(this::freeAllLocks);
370 private void freeAllLocks() {
371 target2lock.values().forEach(LockData::free);
375 * Makes a notification message for the current operation.
377 * @return a new notification
379 public VirtualControlLoopNotification makeNotification() {
380 VirtualControlLoopNotification notif = new VirtualControlLoopNotification(context.getEvent());
381 notif.setNotification(ControlLoopNotificationType.OPERATION);
382 notif.setFrom("policy");
383 notif.setPolicyScope(policyScope);
384 notif.setPolicyVersion(policyVersion);
386 if (finalResult == null) {
387 ControlLoopOperationManager2 oper = currentOperation.get();
389 notif.setMessage(oper.getOperationHistory());
390 notif.setHistory(oper.getHistory());
398 * An event onset/abatement.
400 * @param event the event
403 public NewEventStatus onNewEvent(VirtualControlLoopEvent event) {
405 checkEventSyntax(event);
407 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
408 if (event.equals(context.getEvent())) {
409 return NewEventStatus.FIRST_ONSET;
413 return NewEventStatus.SUBSEQUENT_ONSET;
416 if (abatement == null) {
419 return NewEventStatus.FIRST_ABATEMENT;
422 return NewEventStatus.SUBSEQUENT_ABATEMENT;
425 } catch (ControlLoopException e) {
426 logger.error("{}: onNewEvent threw an exception", this, e);
427 return NewEventStatus.SYNTAX_ERROR;
432 * Determines the overall control loop timeout.
434 * @return the policy timeout, in milliseconds, if specified, a default timeout
437 private long detmControlLoopTimeoutMs() {
438 // validation checks preclude null or 0 timeout values in the policy
439 Integer timeout = processor.getControlLoop().getTimeout();
440 return TimeUnit.MILLISECONDS.convert(timeout, TimeUnit.SECONDS);
444 * Check an event syntax.
446 * @param event the event syntax
447 * @throws ControlLoopException if an error occurs
449 public void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
450 validateStatus(event);
451 if (StringUtils.isBlank(event.getClosedLoopControlName())) {
452 throw new ControlLoopException("No control loop name");
454 if (event.getRequestId() == null) {
455 throw new ControlLoopException("No request ID");
457 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
460 if (StringUtils.isBlank(event.getTarget())) {
461 throw new ControlLoopException("No target field");
462 } else if (!VALID_TARGETS.contains(event.getTarget().toLowerCase())) {
463 throw new ControlLoopException("target field invalid");
465 validateAaiData(event);
468 private void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
469 if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
470 && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
471 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
475 private void validateAaiData(VirtualControlLoopEvent event) throws ControlLoopException {
476 Map<String, String> eventAai = event.getAai();
477 if (eventAai == null) {
478 throw new ControlLoopException("AAI is null");
480 if (event.getTargetType() == null) {
481 throw new ControlLoopException("The Target type is null");
483 switch (event.getTargetType()) {
486 validateAaiVmVnfData(eventAai);
489 validateAaiPnfData(eventAai);
492 throw new ControlLoopException("The target type is not supported");
496 private void validateAaiVmVnfData(Map<String, String> eventAai) throws ControlLoopException {
497 if (eventAai.get(GENERIC_VNF_VNF_ID) == null && eventAai.get(VSERVER_VSERVER_NAME) == null
498 && eventAai.get(GENERIC_VNF_VNF_NAME) == null) {
499 throw new ControlLoopException(
500 "generic-vnf.vnf-id or generic-vnf.vnf-name or vserver.vserver-name information missing");
504 private void validateAaiPnfData(Map<String, String> eventAai) throws ControlLoopException {
505 if (eventAai.get(PNF_NAME) == null) {
506 throw new ControlLoopException("AAI PNF object key pnf-name is missing");
511 * Is closed loop disabled for an event.
513 * @param event the event
514 * @return <code>true</code> if the control loop is disabled, <code>false</code>
517 public static boolean isClosedLoopDisabled(VirtualControlLoopEvent event) {
518 Map<String, String> aai = event.getAai();
519 return (isAaiTrue(aai.get(VSERVER_IS_CLOSED_LOOP_DISABLED))
520 || isAaiTrue(aai.get(GENERIC_VNF_IS_CLOSED_LOOP_DISABLED))
521 || isAaiTrue(aai.get(PNF_IS_IN_MAINT)));
525 * Does provisioning status, for an event, have a value other than ACTIVE.
527 * @param event the event
528 * @return {@code true} if the provisioning status is neither ACTIVE nor {@code null},
529 * {@code false} otherwise
531 protected static boolean isProvStatusInactive(VirtualControlLoopEvent event) {
532 Map<String, String> aai = event.getAai();
533 return !(PROV_STATUS_ACTIVE.equals(aai.getOrDefault(VSERVER_PROV_STATUS, PROV_STATUS_ACTIVE))
534 && PROV_STATUS_ACTIVE.equals(aai.getOrDefault(GENERIC_VNF_PROV_STATUS, PROV_STATUS_ACTIVE)));
538 * Determines the boolean value represented by the given AAI field value.
540 * @param aaiValue value to be examined
541 * @return the boolean value represented by the field value, or {@code false} if the
542 * value is {@code null}
544 protected static boolean isAaiTrue(String aaiValue) {
545 return (aaiValue != null && TRUE_VALUES.contains(aaiValue.toLowerCase()));
549 * Requests a lock. This requests the lock for the time that remains before the
550 * timeout expires. This avoids having to extend the lock.
552 * @param targetEntity entity to be locked
553 * @param lockUnavailableCallback function to be invoked if the lock is
555 * @return a future that can be used to await the lock
558 public synchronized CompletableFuture<OperationOutcome> requestLock(String targetEntity,
559 Consumer<OperationOutcome> lockUnavailableCallback) {
561 long remainingMs = endTimeMs - System.currentTimeMillis();
562 int remainingSec = 15 + Math.max(0, (int) TimeUnit.SECONDS.convert(remainingMs, TimeUnit.MILLISECONDS));
564 LockData data = target2lock.computeIfAbsent(targetEntity, key -> {
565 LockData data2 = new LockData(key, requestId);
566 makeLock(targetEntity, requestId.toString(), remainingSec, data2);
570 data.addUnavailableCallback(lockUnavailableCallback);
572 return data.getFuture();
576 * Initializes various components, on demand.
578 private static class LazyInitData {
579 private static final OperationHistoryDataManager DATA_MANAGER;
580 private static final ActorService ACTOR_SERVICE;
583 EventManagerServices services = new EventManagerServices(EVENT_MANAGER_SERVICE_CONFIG);
584 ACTOR_SERVICE = services.getActorService();
585 DATA_MANAGER = services.getDataManager();
589 // the following methods may be overridden by junit tests
591 protected ControlLoopOperationManager2 makeOperationManager(ControlLoopEventContext ctx, Policy policy) {
592 return new ControlLoopOperationManager2(this, ctx, policy, getExecutor());
595 protected Executor getExecutor() {
596 return ForkJoinPool.commonPool();
599 protected ExecutorService getBlockingExecutor() {
600 return PolicyEngineConstants.getManager().getExecutorService();
603 protected void makeLock(String targetEntity, String requestId, int holdSec, LockCallback callback) {
604 PolicyEngineConstants.getManager().createLock(targetEntity, requestId, holdSec, callback, false);
608 public ActorService getActorService() {
609 return LazyInitData.ACTOR_SERVICE;
613 public OperationHistoryDataManager getDataManager() {
614 return LazyInitData.DATA_MANAGER;